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
13 
14 //LArSoft Includes
19 
20 //C++ Includes
21 #include <tuple>
22 
23 namespace ShowerRecoTools {
24 
26 
27  public:
29 
30  //Physics Function. Calculate the shower Energy.
31  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
33  reco::shower::ShowerElementHolder& ShowerElementHolder) override;
34 
35  private:
36  double CalculateEnergy(const detinfo::DetectorClocksData& clockData,
37  const detinfo::DetectorPropertiesData& detProp,
38  const std::vector<art::Ptr<recob::Hit>>& hits,
39  const geo::PlaneID::PlaneID_t plane) const;
40 
42  int fVerbose;
43 
46 
47  //Services
50 
51  // Declare stuff
53  };
54 
56  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
57  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
58  , fVerbose(pset.get<int>("Verbose"))
59  , fShowerEnergyOutputLabel(pset.get<std::string>("ShowerEnergyOutputLabel"))
60  , fShowerBestPlaneOutputLabel(pset.get<std::string>("ShowerBestPlaneOutputLabel"))
61  , fCalorimetryAlg(pset.get<fhicl::ParameterSet>("CalorimetryAlg"))
62  , fRecombinationFactor(pset.get<double>("RecombinationFactor"))
63  {}
64 
65  int
67  art::Event& Event,
68  reco::shower::ShowerElementHolder& ShowerEleHolder)
69  {
70 
71  if (fVerbose)
72  std::cout
73  << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Shower Reco Energy Tool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
74  << std::endl;
75 
76  // Get the assocated pfParicle vertex PFParticles
77  auto const pfpHandle = Event.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
78 
79  //Get the clusters
80  auto const clusHandle = Event.getValidHandle<std::vector<recob::Cluster>>(fPFParticleLabel);
81 
82  const art::FindManyP<recob::Cluster>& fmc =
83  ShowerEleHolder.GetFindManyP<recob::Cluster>(pfpHandle, Event, fPFParticleLabel);
84  // art::FindManyP<recob::Cluster> fmc(pfpHandle, Event, fPFParticleLabel);
85  std::vector<art::Ptr<recob::Cluster>> clusters = fmc.at(pfparticle.key());
86 
87  //Get the hit association
88  const art::FindManyP<recob::Hit>& fmhc =
89  ShowerEleHolder.GetFindManyP<recob::Hit>(clusHandle, Event, fPFParticleLabel);
90  // art::FindManyP<recob::Hit> fmhc(clusHandle, Event, fPFParticleLabel);
91 
92  std::map<geo::PlaneID::PlaneID_t, std::vector<art::Ptr<recob::Hit>>> planeHits;
93 
94  //Loop over the clusters in the plane and get the hits
95  for (auto const& cluster : clusters) {
96 
97  //Get the hits
98  std::vector<art::Ptr<recob::Hit>> hits = fmhc.at(cluster.key());
99 
100  //Get the plane.
101  const geo::PlaneID::PlaneID_t plane(cluster->Plane().Plane);
102 
103  planeHits[plane].insert(planeHits[plane].end(), hits.begin(), hits.end());
104  }
105 
106  // Calculate the energy for each plane && best plane
108  unsigned int bestPlaneNumHits = 0;
109 
110  //Holder for the final product
111  std::vector<double> energyVec(fGeom->Nplanes(), -999.);
112  std::vector<double> energyError(fGeom->Nplanes(), -999.);
113 
114  auto const clockData =
116  auto const detProp =
118 
119  for (auto const& [plane, hits] : planeHits) {
120 
121  unsigned int planeNumHits = hits.size();
122 
123  //Calculate the Energy for
124  double Energy = CalculateEnergy(clockData, detProp, hits, plane);
125  // If the energy is negative, leave it at -999
126  if (Energy > 0) energyVec.at(plane) = Energy;
127 
128  if (planeNumHits > bestPlaneNumHits) {
129  bestPlane = plane;
130  bestPlaneNumHits = planeNumHits;
131  }
132  }
133 
134  ShowerEleHolder.SetElement(energyVec, energyError, fShowerEnergyOutputLabel);
135  // Only set the best plane if it has some hits in it
136  if (bestPlane < fGeom->Nplanes()) {
137  // Need to cast as an int for legacy default of -999
138  // have to define a new variable as we pass-by-reference when filling
139  int bestPlaneVal(bestPlane);
140  ShowerEleHolder.SetElement(bestPlaneVal, fShowerBestPlaneOutputLabel);
141  }
142 
143  return 0;
144  }
145 
146  // function to calculate the reco energy
147  double
149  const detinfo::DetectorPropertiesData& detProp,
150  const std::vector<art::Ptr<recob::Hit>>& hits,
151  const geo::PlaneID::PlaneID_t plane) const
152  {
153 
154  double totalCharge = 0;
155  double totalEnergy = 0;
156  double correctedtotalCharge = 0;
157  double nElectrons = 0;
158 
159  for (auto const& hit : hits) {
160  totalCharge +=
161  hit->Integral() *
163  clockData, detProp, hit->PeakTime()); // obtain charge and correct for lifetime
164  }
165 
166  // correct charge due to recombination
167  correctedtotalCharge = totalCharge / fRecombinationFactor;
168  // calculate # of electrons and the corresponding energy
169  nElectrons = fCalorimetryAlg.ElectronsFromADCArea(correctedtotalCharge, plane);
170  totalEnergy = (nElectrons / util::kGeVToElectrons) * 1000; // energy in MeV
171  return totalEnergy;
172  }
173 }
174 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
std::string string
Definition: nybbler.cc:12
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)
struct vector vector
STL namespace.
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)
Cluster finding and building.
double ElectronsFromADCArea(double area, unsigned short plane) const
constexpr double kGeVToElectrons
23.6eV per ion pair, 1e9 eV/GeV
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
key_type key() const noexcept
Definition: Ptr.h:216
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
static int max(int a, int b)
Detector simulation of raw signals on wires.
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerElementHolder) override
Contains all timing reference information for the detector.
Definition: types.h:32
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
ShowerNumElectronsEnergy(const fhicl::ParameterSet &pset)
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
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.
QTextStream & endl(QTextStream &s)