ShowerLinearEnergy_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerLinearEnergy ###
3 //### Author: Dominic Barker ###
4 //### Date: 13.05.19 ###
5 //### Description: Tool for finding the Energy of the shower. Derived ###
6 //### from the linear energy algorithm, written for ###
7 //### the EMShower_module.cc ###
8 //############################################################################
9 
10 //Framework Includes
12 
13 //LArSoft Includes
16 
17 namespace ShowerRecoTools {
18 
20 
21  public:
23 
24  //Physics Function. Calculate the shower Energy.
25  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
27  reco::shower::ShowerElementHolder& ShowerElementHolder) override;
28 
29  private:
30  double CalculateEnergy(const detinfo::DetectorClocksData& clockData,
31  const detinfo::DetectorPropertiesData& detProp,
32  const std::vector<art::Ptr<recob::Hit>>& hits,
33  const geo::PlaneID::PlaneID_t plane) const;
34 
35  //fcl parameters
36  unsigned int fNumPlanes;
37  std::vector<double> fGradients; //Gradient of the linear fit of total charge to total energy
38  std::vector<double> fIntercepts; //Intercept of the linear fit of total charge to total energy
39 
41  int fVerbose;
42 
45 
46  //Services
48  };
49 
51  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
52  , fGradients(pset.get<std::vector<double>>("Gradients"))
53  , fIntercepts(pset.get<std::vector<double>>("Intercepts"))
54  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
55  , fVerbose(pset.get<int>("Verbose"))
56  , fShowerEnergyOutputLabel(pset.get<std::string>("ShowerEnergyOutputLabel"))
57  , fShowerBestPlaneOutputLabel(pset.get<std::string>("ShowerBestPlaneOutputLabel"))
58  {
60  if (fNumPlanes != fGradients.size() || fNumPlanes != fIntercepts.size()) {
61  throw cet::exception("ShowerLinearEnergy")
62  << "The number of planes does not match the size of the fcl parametes passed: Num Planes: "
63  << fNumPlanes << ", Gradients size: " << fGradients.size()
64  << ", Intercpts size: " << fIntercepts.size();
65  }
66  }
67 
68  int
70  art::Event& Event,
71  reco::shower::ShowerElementHolder& ShowerEleHolder)
72  {
73 
74  // Get the assocated pfParicle vertex PFParticles
75  auto const pfpHandle = Event.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
76 
77  //Get the clusters
78  auto const clusHandle = Event.getValidHandle<std::vector<recob::Cluster>>(fPFParticleLabel);
79 
80  const art::FindManyP<recob::Cluster>& fmc =
81  ShowerEleHolder.GetFindManyP<recob::Cluster>(pfpHandle, Event, fPFParticleLabel);
82  // art::FindManyP<recob::Cluster> fmc(pfpHandle, Event, fPFParticleLabel);
83  std::vector<art::Ptr<recob::Cluster>> clusters = fmc.at(pfparticle.key());
84 
85  //Get the hit association
86  const art::FindManyP<recob::Hit>& fmhc =
87  ShowerEleHolder.GetFindManyP<recob::Hit>(clusHandle, Event, fPFParticleLabel);
88  // art::FindManyP<recob::Hit> fmhc(clusHandle, Event, fPFParticleLabel);
89 
90  std::map<geo::PlaneID::PlaneID_t, std::vector<art::Ptr<recob::Hit>>> planeHits;
91 
92  //Loop over the clusters in the plane and get the hits
93  for (auto const& cluster : clusters) {
94 
95  //Get the hits
96  std::vector<art::Ptr<recob::Hit>> hits = fmhc.at(cluster.key());
97 
98  //Get the plane.
99  const geo::PlaneID::PlaneID_t plane(cluster->Plane().Plane);
100 
101  planeHits[plane].insert(planeHits[plane].end(), hits.begin(), hits.end());
102  }
103 
104  // Calculate the energy for each plane && best plane
106  unsigned int bestPlaneNumHits = 0;
107 
108  //Holder for the final product
109  std::vector<double> energyVec(fNumPlanes, -999.);
110  std::vector<double> energyError(fNumPlanes, -999.);
111 
112  auto const clockData =
114  auto const detProp =
116 
117  for (auto const& [plane, hits] : planeHits) {
118 
119  unsigned int planeNumHits = hits.size();
120 
121  //Calculate the Energy for
122  double Energy = CalculateEnergy(clockData, detProp, hits, plane);
123  // If the energy is negative, leave it at -999
124  if (Energy > 0) energyVec.at(plane) = Energy;
125 
126  if (planeNumHits > bestPlaneNumHits) {
127  bestPlane = plane;
128  bestPlaneNumHits = planeNumHits;
129  }
130  }
131 
132  ShowerEleHolder.SetElement(energyVec, energyError, fShowerEnergyOutputLabel);
133  // Only set the best plane if it has some hits in it
134  if (bestPlane < fGeom->Nplanes()) {
135  // Need to cast as an int for legacy default of -999
136  // have to define a new variable as we pass-by-reference when filling
137  int bestPlaneVal(bestPlane);
138  ShowerEleHolder.SetElement(bestPlaneVal, fShowerBestPlaneOutputLabel);
139  }
140 
141  return 0;
142  }
143 
144  //Function to calculate the energy of a shower in a plane. Using a linear map between charge and Energy.
145  //Exactly the same method as the ShowerEnergyAlg.cxx. Thanks Mike.
146  double
148  const detinfo::DetectorPropertiesData& detProp,
149  const std::vector<art::Ptr<recob::Hit>>& hits,
150  const geo::PlaneID::PlaneID_t plane) const
151  {
152 
153  double totalCharge = 0, totalEnergy = 0;
154 
155  for (auto const& hit : hits) {
156  totalCharge += (hit->Integral() * std::exp((sampling_rate(clockData) * hit->PeakTime()) /
157  (detProp.ElectronLifetime() * 1e3)));
158  }
159 
160  totalEnergy = (totalCharge * fGradients.at(plane)) + fIntercepts.at(plane);
161 
162  return totalEnergy;
163  }
164 }
165 
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
ShowerLinearEnergy(const fhicl::ParameterSet &pset)
std::string string
Definition: nybbler.cc:12
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
unsigned int PlaneID_t
Type for the ID number.
Definition: geo_types.h:473
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)
art::ServiceHandle< geo::Geometry > fGeom
Cluster finding and building.
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
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33