All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShowerNumElectronsEnergy_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerNumElectronsEnergy ###
3 //### Author: Tom Ham ###
4 //### Date: 01/04/2020 ###
5 //### Description: Tool for finding the Energy of the shower by going ###
6 //### from number of hits -> number of electrons -> energy. ###
7 //### Derived from the linear energy algorithm, written for ###
8 //### the EMShower_module.cc ###
9 //############################################################################
10 
11 //Framework Includes
12 #include "art/Utilities/ToolMacros.h"
13 
14 //LArSoft Includes
22 
23 //C++ Includes
24 #include <tuple>
25 
26 namespace ShowerRecoTools {
27 
29 
30  public:
31  ShowerNumElectronsEnergy(const fhicl::ParameterSet& pset);
32 
33  //Physics Function. Calculate the shower Energy.
34  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
35  art::Event& Event,
36  reco::shower::ShowerElementHolder& ShowerElementHolder) override;
37 
38  private:
39  double CalculateEnergy(const detinfo::DetectorClocksData& clockData,
41  const std::vector<art::Ptr<recob::Hit>>& hits,
42  const geo::PlaneID::PlaneID_t plane) const;
43 
44  art::InputTag fPFParticleLabel;
45  int fVerbose;
46 
49 
50  //Services
51  art::ServiceHandle<geo::Geometry> fGeom;
53 
54  // Declare stuff
56  };
57 
58  ShowerNumElectronsEnergy::ShowerNumElectronsEnergy(const fhicl::ParameterSet& pset)
59  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
60  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
61  , fVerbose(pset.get<int>("Verbose"))
62  , fShowerEnergyOutputLabel(pset.get<std::string>("ShowerEnergyOutputLabel"))
63  , fShowerBestPlaneOutputLabel(pset.get<std::string>("ShowerBestPlaneOutputLabel"))
64  , fCalorimetryAlg(pset.get<fhicl::ParameterSet>("CalorimetryAlg"))
65  , fRecombinationFactor(pset.get<double>("RecombinationFactor"))
66  {}
67 
68  int
69  ShowerNumElectronsEnergy::CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
70  art::Event& Event,
71  reco::shower::ShowerElementHolder& ShowerEleHolder)
72  {
73 
74  if (fVerbose)
75  std::cout
76  << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Shower Reco Energy Tool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
77  << std::endl;
78 
79  // Get the assocated pfParicle vertex PFParticles
80  auto const pfpHandle = Event.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
81 
82  //Get the clusters
83  auto const clusHandle = Event.getValidHandle<std::vector<recob::Cluster>>(fPFParticleLabel);
84 
85  const art::FindManyP<recob::Cluster>& fmc =
86  ShowerEleHolder.GetFindManyP<recob::Cluster>(pfpHandle, Event, fPFParticleLabel);
87  // art::FindManyP<recob::Cluster> fmc(pfpHandle, Event, fPFParticleLabel);
88  std::vector<art::Ptr<recob::Cluster>> clusters = fmc.at(pfparticle.key());
89 
90  //Get the hit association
91  const art::FindManyP<recob::Hit>& fmhc =
92  ShowerEleHolder.GetFindManyP<recob::Hit>(clusHandle, Event, fPFParticleLabel);
93  // art::FindManyP<recob::Hit> fmhc(clusHandle, Event, fPFParticleLabel);
94 
95  std::map<geo::PlaneID::PlaneID_t, std::vector<art::Ptr<recob::Hit>>> planeHits;
96 
97  //Loop over the clusters in the plane and get the hits
98  for (auto const& cluster : clusters) {
99 
100  //Get the hits
101  std::vector<art::Ptr<recob::Hit>> hits = fmhc.at(cluster.key());
102 
103  //Get the plane.
104  const geo::PlaneID::PlaneID_t plane(cluster->Plane().Plane);
105 
106  planeHits[plane].insert(planeHits[plane].end(), hits.begin(), hits.end());
107  }
108 
109  // Calculate the energy for each plane && best plane
110  geo::PlaneID::PlaneID_t bestPlane = std::numeric_limits<geo::PlaneID::PlaneID_t>::max();
111  unsigned int bestPlaneNumHits = 0;
112 
113  //Holder for the final product
114  std::vector<double> energyVec(fGeom->Nplanes(), -999.);
115  std::vector<double> energyError(fGeom->Nplanes(), -999.);
116 
117  auto const clockData =
118  art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(Event);
119  auto const detProp =
120  art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(Event, clockData);
121 
122  for (auto const& [plane, hits] : planeHits) {
123 
124  unsigned int planeNumHits = hits.size();
125 
126  //Calculate the Energy for
127  double Energy = CalculateEnergy(clockData, detProp, hits, plane);
128  // If the energy is negative, leave it at -999
129  if (Energy > 0) energyVec.at(plane) = Energy;
130 
131  if (planeNumHits > bestPlaneNumHits) {
132  bestPlane = plane;
133  bestPlaneNumHits = planeNumHits;
134  }
135  }
136 
137  ShowerEleHolder.SetElement(energyVec, energyError, fShowerEnergyOutputLabel);
138  // Only set the best plane if it has some hits in it
139  if (bestPlane < fGeom->Nplanes()) {
140  // Need to cast as an int for legacy default of -999
141  // have to define a new variable as we pass-by-reference when filling
142  int bestPlaneVal(bestPlane);
143  ShowerEleHolder.SetElement(bestPlaneVal, fShowerBestPlaneOutputLabel);
144  }
145 
146  return 0;
147  }
148 
149  // function to calculate the reco energy
150  double
153  const std::vector<art::Ptr<recob::Hit>>& hits,
154  const geo::PlaneID::PlaneID_t plane) const
155  {
156 
157  double totalCharge = 0;
158  double totalEnergy = 0;
159  double correctedtotalCharge = 0;
160  double nElectrons = 0;
161 
162  for (auto const& hit : hits) {
163  totalCharge +=
164  hit->Integral() *
166  clockData, detProp, hit->PeakTime()); // obtain charge and correct for lifetime
167  }
168 
169  // correct charge due to recombination
170  correctedtotalCharge = totalCharge / fRecombinationFactor;
171  // calculate # of electrons and the corresponding energy
172  nElectrons = fCalorimetryAlg.ElectronsFromADCArea(correctedtotalCharge, plane);
173  totalEnergy = (nElectrons / util::kGeVToElectrons) * 1000; // energy in MeV
174  return totalEnergy;
175  }
176 }
177 
178 DEFINE_ART_CLASS_TOOL(ShowerRecoTools::ShowerNumElectronsEnergy)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
process_name cluster
Definition: cheaterreco.fcl:51
unsigned int PlaneID_t
Type for the ID number.
Definition: geo_types.h:473
double CalculateEnergy(const detinfo::DetectorClocksData &clockData, const detinfo::DetectorPropertiesData &detProp, const std::vector< art::Ptr< recob::Hit >> &hits, const geo::PlaneID::PlaneID_t plane) const
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
Set of hits with a 2D structure.
Definition: Cluster.h:71
const art::FindManyP< T1 > & GetFindManyP(const art::ValidHandle< std::vector< T2 > > &handle, const art::Event &evt, const art::InputTag &moduleTag)
process_name hit
Definition: cheaterreco.fcl:51
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
double ElectronsFromADCArea(double area, unsigned short plane) const
constexpr double kGeVToElectrons
23.6eV per ion pair, 1e9 eV/GeV
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
Declaration of cluster object.
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerElementHolder) override
Contains all timing reference information for the detector.
process_name Energy
Definition: lArDet.fcl:66
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
ShowerNumElectronsEnergy(const fhicl::ParameterSet &pset)
double LifetimeCorrection(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, double time, double T0=0) const
Collection of Physical constants used in LArSoft.
BEGIN_PROLOG could also be cout
auto const detProp