All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TCShowerAnalysis_module.cc
Go to the documentation of this file.
1 // -------------------------------------------------
2 // tcshower analysis tree
3 //
4 // Author: Rory Fitzpatrick (roryfitz@umich.edu)
5 // Created: 7/16/18
6 // -------------------------------------------------
7 
8 // Framework includes
9 #include "art/Framework/Core/EDAnalyzer.h"
10 #include "art/Framework/Core/ModuleMacros.h"
11 #include "art/Framework/Principal/Event.h"
12 #include "art/Framework/Principal/Handle.h"
13 #include "art/Framework/Services/Registry/ServiceHandle.h"
14 #include "art_root_io/TFileService.h"
15 #include "canvas/Persistency/Common/FindManyP.h"
16 #include "fhiclcpp/ParameterSet.h"
17 
25 #include "nusimdata/SimulationBase/MCTruth.h"
26 
27 #include "TTree.h"
28 
29 constexpr int kMaxShowers = 1000; // maximum number of showers
30 
31 namespace shower {
32 
33  class TCShowerAnalysis : public art::EDAnalyzer {
34  public:
35  explicit TCShowerAnalysis(fhicl::ParameterSet const& pset);
36 
37  private:
38  void beginJob() override;
39  void analyze(const art::Event& evt) override;
40 
41  void reset();
42 
43  void truthMatcher(detinfo::DetectorClocksData const& clockData,
44  std::vector<art::Ptr<recob::Hit>> all_hits,
45  std::vector<art::Ptr<recob::Hit>> shower_hits,
46  const simb::MCParticle*& MCparticle,
47  double& Efrac,
48  double& Ecomplet);
49 
50  TTree* fTree;
51  int run;
52  int subrun;
53  int event;
57  int nshws; // number of showers
58  int shwid[kMaxShowers]; // recob::Shower::ID()
59  float shwdcosx[kMaxShowers]; // shower direction cosin
62  float shwstartx[kMaxShowers]; // shower start position (cm)
65  double shwdedx[kMaxShowers][2]; // shower dE/dx of the initial track
66  // measured on the 3 plane (MeV/cm)
67  int shwbestplane[kMaxShowers]; // recommended plane for energy and dE/dx
68  // information
69 
72 
73  std::string fHitModuleLabel;
74  std::string fShowerModuleLabel;
75  std::string fGenieGenModuleLabel;
76  std::string fDigitModuleLabel;
77 
79 
80  }; // class TCShowerAnalysis
81 
82 } // shower
83 
84 // -------------------------------------------------
85 
86 shower::TCShowerAnalysis::TCShowerAnalysis(fhicl::ParameterSet const& pset)
87  : EDAnalyzer(pset)
88  , fHitModuleLabel(pset.get<std::string>("HitModuleLabel", "trajcluster"))
89  , fShowerModuleLabel(pset.get<std::string>("ShowerModuleLabel", "tcshower"))
90  , fGenieGenModuleLabel(pset.get<std::string>("GenieGenModuleLabel", "generator"))
91  , fDigitModuleLabel(pset.get<std::string>("DigitModuleLabel", "largeant"))
92  , fCalorimetryAlg(pset.get<fhicl::ParameterSet>("CalorimetryAlg"))
93 {} // TCShowerTemplateMaker
94 
95 // -------------------------------------------------
96 
97 void
99 {
100 
101  art::ServiceHandle<art::TFileService const> tfs;
102 
103  fTree = tfs->make<TTree>("tcshowerana", "tcshowerana");
104  fTree->Branch("run", &run, "run/I");
105  fTree->Branch("subrun", &subrun, "subrun/I");
106  fTree->Branch("event", &event, "event/I");
107 
108  fTree->Branch("nuPDG_truth", &nuPDG_truth, "nuPDG_truth/I");
109  fTree->Branch("ccnc_truth", &ccnc_truth, "ccnc_truth/I");
110  fTree->Branch("mode_truth", &mode_truth, "mode_truth/I");
111 
112  fTree->Branch("nshws", &nshws, "nshws/I");
113  fTree->Branch("shwid", shwid, "shwid[nshws]/I");
114  fTree->Branch("shwdcosx", shwdcosx, "shwdcosx[nshws]/F");
115  fTree->Branch("shwdcosy", shwdcosy, "shwdcosy[nshws]/F");
116  fTree->Branch("shwdcosz", shwdcosz, "shwdcosz[nshws]/F");
117  fTree->Branch("shwstartx", shwstartx, "shwstartx[nshws]/F");
118  fTree->Branch("shwstarty", shwstarty, "shwstarty[nshws]/F");
119  fTree->Branch("shwstartz", shwstartz, "shwstartz[nshws]/F");
120  fTree->Branch("shwdedx", shwdedx, "shwdedx[nshws][2]/D");
121  fTree->Branch("shwbestplane", shwbestplane, "shwbestplane[nshws]/I");
122 
123  fTree->Branch("highestHitsPDG", &highestHitsPDG, "highestHitsPDG/I");
124  fTree->Branch("highestHitsFrac", &highestHitsFrac, "highestHitsFrac/D");
125 
126 } // beginJob
127 
128 // -------------------------------------------------
129 
130 void
132 {
133 
134  run = evt.run();
135  subrun = evt.subRun();
136  event = evt.id().event();
137 
138  art::Handle<std::vector<recob::Hit>> hitListHandle;
139  std::vector<art::Ptr<recob::Hit>> hitlist;
140  if (evt.getByLabel(fHitModuleLabel, hitListHandle)) art::fill_ptr_vector(hitlist, hitListHandle);
141 
142  art::Handle<std::vector<sim::SimChannel>> scListHandle;
143  std::vector<art::Ptr<sim::SimChannel>> simchanlist;
144  if (evt.getByLabel(fDigitModuleLabel, scListHandle))
145  art::fill_ptr_vector(simchanlist, scListHandle);
146 
147  art::Handle<std::vector<recob::Shower>> showerListHandle;
148  std::vector<art::Ptr<recob::Shower>> showerlist;
149  if (evt.getByLabel(fShowerModuleLabel, showerListHandle))
150  art::fill_ptr_vector(showerlist, showerListHandle);
151 
152  art::Handle<std::vector<simb::MCTruth>> mctruthListHandle;
153  std::vector<art::Ptr<simb::MCTruth>> mclist;
154  if (evt.getByLabel(fGenieGenModuleLabel, mctruthListHandle))
155  art::fill_ptr_vector(mclist, mctruthListHandle);
156 
157  art::FindManyP<recob::Hit> shwfm(showerListHandle, evt, fShowerModuleLabel);
158 
159  //shower information
160  if (showerListHandle.isValid()) {
161  nshws = showerlist.size();
162 
163  for (int i = 0; i < std::min(int(showerlist.size()), kMaxShowers); ++i) {
164  shwid[i] = showerlist[i]->ID();
165  shwdcosx[i] = showerlist[i]->Direction().X();
166  shwdcosy[i] = showerlist[i]->Direction().Y();
167  shwdcosz[i] = showerlist[i]->Direction().Z();
168  shwstartx[i] = showerlist[i]->ShowerStart().X();
169  shwstarty[i] = showerlist[i]->ShowerStart().Y();
170  shwstartz[i] = showerlist[i]->ShowerStart().Z();
171  for (size_t j = 0; j < (showerlist[i]->dEdx()).size(); ++j) {
172  shwdedx[i][j] = showerlist[i]->dEdx()[j];
173  }
174  shwbestplane[i] = showerlist[i]->best_plane();
175  }
176 
177  } // shower info
178 
179  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
180 
181  if (mclist.size()) {
182  art::Ptr<simb::MCTruth> mctruth = mclist[0];
183  if (mctruth->NeutrinoSet()) {
184 
185  nuPDG_truth = mctruth->GetNeutrino().Nu().PdgCode();
186  ccnc_truth = mctruth->GetNeutrino().CCNC();
187  mode_truth = mctruth->GetNeutrino().Mode();
188 
189  if (showerlist.size()) { // only looks at the first shower since this is
190  // for tcshower
191  std::vector<art::Ptr<recob::Hit>> showerhits = shwfm.at(0);
192  // get shower truth info
193  const simb::MCParticle* particle;
194  double tmpEfrac = 0.0;
195  double tmpEcomplet = 0;
196  truthMatcher(clockData, hitlist, showerhits, particle, tmpEfrac, tmpEcomplet);
197  if (particle) {
198  std::cout << "shower pdg: " << particle->PdgCode() << " efrac " << tmpEfrac << std::endl;
199 
200  highestHitsPDG = particle->PdgCode();
201  highestHitsFrac = tmpEfrac;
202  }
203  }
204  }
205  }
206 
207  fTree->Fill();
208 
209 } // analyze
210 
211 // -------------------------------------------------
212 
213 void
215 {
216 
217  run = -99999;
218  subrun = -99999;
219  event = -99999;
220 
221  nuPDG_truth = -99999;
222  ccnc_truth = -99999;
223  mode_truth = -99999;
224 
225  nshws = 0;
226  for (int i = 0; i < kMaxShowers; ++i) {
227  shwid[i] = -99999;
228  shwdcosx[i] = -99999;
229  shwdcosy[i] = -99999;
230  shwdcosz[i] = -99999;
231  shwstartx[i] = -99999;
232  shwstarty[i] = -99999;
233  shwstartz[i] = -99999;
234  for (int j = 0; j < 2; ++j) {
235  shwdedx[i][j] = -99999;
236  }
237  shwbestplane[i] = -99999;
238  }
239 
240  highestHitsPDG = -99999;
241  highestHitsFrac = -99999;
242 
243 } // reset
244 
245 // -------------------------------------------------
246 
247 void
249  std::vector<art::Ptr<recob::Hit>> all_hits,
250  std::vector<art::Ptr<recob::Hit>> shower_hits,
251  const simb::MCParticle*& MCparticle,
252  double& Efrac,
253  double& Ecomplet)
254 {
255 
256  MCparticle = 0;
257  Efrac = 1.0;
258  Ecomplet = 0;
259 
260  art::ServiceHandle<cheat::BackTrackerService const> bt_serv;
261  art::ServiceHandle<cheat::ParticleInventoryService const> pi_serv;
262  std::map<int, double> trkID_E;
263  for (size_t j = 0; j < shower_hits.size(); ++j) {
264  art::Ptr<recob::Hit> hit = shower_hits[j];
265  // For know let's use collection plane to look at the shower reconstruction
266  if (hit->View() != 1) continue;
267  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToEveTrackIDEs(clockData, hit);
268  for (size_t k = 0; k < TrackIDs.size(); k++) {
269  if (trkID_E.find(std::abs(TrackIDs[k].trackID)) == trkID_E.end())
270  trkID_E[std::abs(TrackIDs[k].trackID)] = 0;
271  trkID_E[std::abs(TrackIDs[k].trackID)] += TrackIDs[k].energy;
272  }
273  }
274  double max_E = -999.0;
275  double total_E = 0.0;
276  int TrackID = -999;
277  double partial_E = 0.0;
278 
279  if (!trkID_E.size()) return; // Ghost shower???
280  for (std::map<int, double>::iterator ii = trkID_E.begin(); ii != trkID_E.end(); ++ii) {
281  total_E += ii->second;
282  if ((ii->second) > max_E) {
283  partial_E = ii->second;
284  max_E = ii->second;
285  TrackID = ii->first;
286  }
287  }
288  MCparticle = pi_serv->TrackIdToParticle_P(TrackID);
289 
290  Efrac = partial_E / total_E;
291 
292  // completeness
293  double totenergy = 0;
294  for (size_t k = 0; k < all_hits.size(); ++k) {
295  art::Ptr<recob::Hit> hit = all_hits[k];
296  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToEveTrackIDEs(clockData, hit);
297  for (size_t l = 0; l < TrackIDs.size(); ++l) {
298  if (std::abs(TrackIDs[l].trackID) == TrackID) { totenergy += TrackIDs[l].energy; }
299  }
300  }
301  Ecomplet = partial_E / totenergy;
302 
303 } // truthMatcher
304 
305 DEFINE_ART_MODULE(shower::TCShowerAnalysis)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
double shwdedx[kMaxShowers][2]
TCShowerAnalysis(fhicl::ParameterSet const &pset)
std::vector< TrackID > TrackIDs
void truthMatcher(detinfo::DetectorClocksData const &clockData, std::vector< art::Ptr< recob::Hit >> all_hits, std::vector< art::Ptr< recob::Hit >> shower_hits, const simb::MCParticle *&MCparticle, double &Efrac, double &Ecomplet)
Declaration of signal hit object.
std::size_t size(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:561
calo::CalorimetryAlg fCalorimetryAlg
process_name hit
Definition: cheaterreco.fcl:51
process_name shower
Definition: cheaterreco.fcl:51
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
T abs(T value)
constexpr int kMaxShowers
void analyze(const art::Event &evt) override
Contains all timing reference information for the detector.
object containing MC truth information necessary for making RawDigits and doing back tracking ...
art::ServiceHandle< art::TFileService > tfs
TCEvent evt
Definition: DataStructs.cxx:8
pdgs k
Definition: selectors.fcl:22
BEGIN_PROLOG could also be cout