All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
11 #include "art/Utilities/ToolMacros.h"
12 
13 //LArSoft Includes
20 
21 namespace ShowerRecoTools {
22 
24 
25  public:
26  ShowerLinearEnergy(const fhicl::ParameterSet& pset);
27 
28  //Physics Function. Calculate the shower Energy.
29  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
30  art::Event& Event,
31  reco::shower::ShowerElementHolder& ShowerElementHolder) override;
32 
33  private:
34  double CalculateEnergy(const detinfo::DetectorClocksData& clockData,
36  const std::vector<art::Ptr<recob::Hit>>& hits,
37  const geo::PlaneID::PlaneID_t plane) const;
38 
39  //fcl parameters
40  unsigned int fNumPlanes;
41  std::vector<double> fGradients; //Gradient of the linear fit of total charge to total energy
42  std::vector<double> fIntercepts; //Intercept of the linear fit of total charge to total energy
43 
44  art::InputTag fPFParticleLabel;
45  int fVerbose;
46 
49 
50  //Services
51  art::ServiceHandle<geo::Geometry> fGeom;
52  };
53 
54  ShowerLinearEnergy::ShowerLinearEnergy(const fhicl::ParameterSet& pset)
55  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
56  , fGradients(pset.get<std::vector<double>>("Gradients"))
57  , fIntercepts(pset.get<std::vector<double>>("Intercepts"))
58  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
59  , fVerbose(pset.get<int>("Verbose"))
60  , fShowerEnergyOutputLabel(pset.get<std::string>("ShowerEnergyOutputLabel"))
61  , fShowerBestPlaneOutputLabel(pset.get<std::string>("ShowerBestPlaneOutputLabel"))
62  {
63  fNumPlanes = fGeom->Nplanes();
64  if (fNumPlanes != fGradients.size() || fNumPlanes != fIntercepts.size()) {
65  throw cet::exception("ShowerLinearEnergy")
66  << "The number of planes does not match the size of the fcl parametes passed: Num Planes: "
67  << fNumPlanes << ", Gradients size: " << fGradients.size()
68  << ", Intercpts size: " << fIntercepts.size();
69  }
70  }
71 
72  int
73  ShowerLinearEnergy::CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
74  art::Event& Event,
75  reco::shower::ShowerElementHolder& ShowerEleHolder)
76  {
77 
78  // Get the assocated pfParicle vertex PFParticles
79  auto const pfpHandle = Event.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
80 
81  //Get the clusters
82  auto const clusHandle = Event.getValidHandle<std::vector<recob::Cluster>>(fPFParticleLabel);
83 
84  const art::FindManyP<recob::Cluster>& fmc =
85  ShowerEleHolder.GetFindManyP<recob::Cluster>(pfpHandle, Event, fPFParticleLabel);
86  // art::FindManyP<recob::Cluster> fmc(pfpHandle, Event, fPFParticleLabel);
87  std::vector<art::Ptr<recob::Cluster>> clusters = fmc.at(pfparticle.key());
88 
89  //Get the hit association
90  const art::FindManyP<recob::Hit>& fmhc =
91  ShowerEleHolder.GetFindManyP<recob::Hit>(clusHandle, Event, fPFParticleLabel);
92  // art::FindManyP<recob::Hit> fmhc(clusHandle, Event, fPFParticleLabel);
93 
94  std::map<geo::PlaneID::PlaneID_t, std::vector<art::Ptr<recob::Hit>>> planeHits;
95 
96  //Loop over the clusters in the plane and get the hits
97  for (auto const& cluster : clusters) {
98 
99  //Get the hits
100  std::vector<art::Ptr<recob::Hit>> hits = fmhc.at(cluster.key());
101 
102  //Get the plane.
103  const geo::PlaneID::PlaneID_t plane(cluster->Plane().Plane);
104 
105  planeHits[plane].insert(planeHits[plane].end(), hits.begin(), hits.end());
106  }
107 
108  // Calculate the energy for each plane && best plane
109  geo::PlaneID::PlaneID_t bestPlane = std::numeric_limits<geo::PlaneID::PlaneID_t>::max();
110  unsigned int bestPlaneNumHits = 0;
111 
112  //Holder for the final product
113  std::vector<double> energyVec(fNumPlanes, -999.);
114  std::vector<double> energyError(fNumPlanes, -999.);
115 
116  auto const clockData =
117  art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(Event);
118  auto const detProp =
119  art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(Event, clockData);
120 
121  for (auto const& [plane, hits] : planeHits) {
122 
123  unsigned int planeNumHits = hits.size();
124 
125  //Calculate the Energy for
126  double Energy = CalculateEnergy(clockData, detProp, hits, plane);
127  // If the energy is negative, leave it at -999
128  if (Energy > 0) energyVec.at(plane) = Energy;
129 
130  if (planeNumHits > bestPlaneNumHits) {
131  bestPlane = plane;
132  bestPlaneNumHits = planeNumHits;
133  }
134  }
135 
136  ShowerEleHolder.SetElement(energyVec, energyError, fShowerEnergyOutputLabel);
137  // Only set the best plane if it has some hits in it
138  if (bestPlane < fGeom->Nplanes()) {
139  // Need to cast as an int for legacy default of -999
140  // have to define a new variable as we pass-by-reference when filling
141  int bestPlaneVal(bestPlane);
142  ShowerEleHolder.SetElement(bestPlaneVal, fShowerBestPlaneOutputLabel);
143  }
144 
145  return 0;
146  }
147 
148  //Function to calculate the energy of a shower in a plane. Using a linear map between charge and Energy.
149  //Exactly the same method as the ShowerEnergyAlg.cxx. Thanks Mike.
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, totalEnergy = 0;
158 
159  for (auto const& hit : hits) {
160  totalCharge += (hit->Integral() * std::exp((sampling_rate(clockData) * hit->PeakTime()) /
161  (detProp.ElectronLifetime() * 1e3)));
162  }
163 
164  totalEnergy = (totalCharge * fGradients.at(plane)) + fIntercepts.at(plane);
165 
166  return totalEnergy;
167  }
168 }
169 
170 DEFINE_ART_CLASS_TOOL(ShowerRecoTools::ShowerLinearEnergy)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
ShowerLinearEnergy(const fhicl::ParameterSet &pset)
process_name cluster
Definition: cheaterreco.fcl:51
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
Declaration of signal hit object.
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)
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
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
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
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
auto const detProp