CosmicRemovalAna_module.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////
2 // Cosmic Removal Module Ana
3 //
4 // Module Designed to loop over tracks / clusters / hits that
5 // have the cosmic tag association and removeor ignore the
6 // hits and check to see what is left over
7 //
8 // Yale Workshop (Cosmics Removal Group)
9 //
10 /////////////////////////////////////////////////////////////
11 
12 // Framework includes
18 #include "art_root_io/TFileService.h"
19 #include "fhiclcpp/ParameterSet.h"
20 
21 // LArSoft Includes
31 
32 // ROOT Includes
33 #include "TTree.h"
34 
35 #include <string>
36 #include <vector>
37 
38 namespace microboone {
39 
41 
42  public:
43  explicit CosmicRemovalAna(fhicl::ParameterSet const& pset);
44 
45  /// read access to event
46  void analyze(const art::Event& evt);
47  void beginJob();
48 
50 
51  private:
52  unsigned int nCosmicTags;
53 
54  // TTree *tTagTree;
55  TTree* tEventTree;
56 
63  std::vector<std::string> fCosmicTagAssocLabel;
64  std::vector<float> fCosmicScoreThresholds;
65 
66  void InitEventTree(int run_number, int event_number);
67 
68  void FillMCInfo(art::Event const& e,
69  std::vector<recob::Hit> const& hitlist,
70  std::vector<hit_origin_t>& hitOrigins,
71  std::vector<sim::MCHitCollection> const& mchitCollectionVector,
72  std::map<int, const simb::MCTruth*> const& trackIDToTruthMap);
73 
74  void FillTrackInfo(size_t const& hit_iter,
75  hit_origin_t const& origin,
76  float const& charge,
77  std::vector<size_t> const& track_indices_this_hit,
78  std::vector<std::vector<const anab::CosmicTag*>> const& tags_per_cluster,
79  std::vector<bool>& hitsAccounted_per_tag,
80  std::vector<bool>& hitsAllTags);
81 
82  void FillClusterInfo(size_t const& hit_iter,
83  hit_origin_t const& origin,
84  float const& charge,
85  std::vector<size_t> const& cluster_indices_this_hit,
86  std::vector<std::vector<const anab::CosmicTag*>> const& tags_per_cluster,
87  std::vector<bool>& hitsAccounted_per_tag,
88  std::vector<bool>& hitsAllTags);
89 
90  void FillAllTagsInfo(recob::Hit const& hit, hit_origin_t const& origin);
91 
92  std::vector<float> cTaggedCharge_Cosmic;
93  std::vector<float> cTaggedCharge_NonCosmic;
94  std::vector<int> cTaggedHits_Cosmic;
95  std::vector<int> cTaggedHits_NonCosmic;
96  // std::vector<std::string> *cTagAlgorithmNames;
97 
98  /*
99  typedef struct {
100  int eventNumber;
101  int tagType;
102  float x0;
103  float x1;
104  float y0;
105  float y1;
106  float z0;
107  float z1;
108  int nHits;
109  int nGoodHits;
110  int origin;
111  float score;
112  int pdg;
113  float energy;
114  } cTagProperties_t;
115  cTagProperties_t cTagVals;
116  */
117  }; //<---End class
118 }
119 
120 typedef struct {
123 
127 
131 
135 
136  float qTrack;
139 
143 
144  float qCluster;
147 
152 
155 
156 // =====================================================
157 // fhicl::ParameterSet
158 // =====================================================
160  : EDAnalyzer(pset)
161  , fHitsModuleLabel(pset.get<std::string>("HitsModuleLabel"))
162  , fMCModuleLabel(pset.get<std::string>("MCModuleLabel"))
163  , fMCHitsModuleLabel(pset.get<std::string>("MCHitsModuleLabel"))
164  , fClusterModuleLabel(pset.get<std::string>("ClusterModuleLabel"))
165  , fTrackModuleLabel(pset.get<std::string>("TrackModuleLabel"))
166  , fHitCompareCut(pset.get<float>("HitCompareCut"))
167  , fCosmicTagAssocLabel(pset.get<std::vector<std::string>>("CosmicTagAssocLabel"))
168  , fCosmicScoreThresholds(pset.get<std::vector<float>>("CosmicScoreThresholds"))
169 {}
170 
171 // =====================================================
172 // BeginJob
173 // =====================================================
174 
175 void
177 {
178 
179  //static cEventProperties_t cEventVals;
180 
186 
188  tEventTree = (TTree*)tfs->make<TTree>("CosmicEventTree", "CosmicEventTree");
189 
190  tEventTree->Branch(
191  "event",
192  &cEventVals,
193  "runNumber/I:eventNumber/I:nHitsTotal_Unknown/I:nHitsTotal_Cosmic/I:nHitsTotal_NonCosmic/"
194  "I:qTotal_Unknown/F:qTotal_Cosmic/F:qTotal_NonCosmic/F:nHitsTrack/I:nHitsTrack_Cosmic/"
195  "I:nHitsTrack_NonCosmic/I:qTrack/F:qTrack_Cosmic/F:qTrack_NonCosmic/F:nHitsCluster/"
196  "I:nHitsCluster_Cosmic/I:nHitsCluster_NonCosmic/I:qCluster/F:qCluster_Cosmic/"
197  "F:qCluster_NonCosmic/F:TotalTaggedCharge_Cosmic/F:TotalTaggedCharge_NonCosmic/"
198  "F:TotalTaggedHits_Cosmic/I:TotalTaggedHits_NonCosmic/I");
199  tEventTree->Branch("TaggedCharge_Cosmic", &cTaggedCharge_Cosmic);
200  tEventTree->Branch("TaggedCharge_NonCosmic", &cTaggedCharge_NonCosmic);
201  tEventTree->Branch("TaggedHits_Cosmic", &cTaggedHits_Cosmic);
202  tEventTree->Branch("TaggedHits_NonCosmic", &cTaggedHits_NonCosmic);
203 }
204 
205 // =====================================================
206 // Event Loop
207 // =====================================================
208 void
210 {
211 
212  InitEventTree(evt.run(), evt.event());
213 
214  // ##################################################################
215  // ### Grabbing ALL HITS in the event to monitor the backtracking ###
216  // ##################################################################
218  evt.getByLabel(fHitsModuleLabel, hitListHandle);
219  std::vector<recob::Hit> const& hitVector(*hitListHandle);
220 
221  std::vector<hit_origin_t> hitOrigins(hitVector.size());
222 
223  //get mcHitCollection
225  evt.getByLabel(fMCHitsModuleLabel, mchitListHandle);
226  std::vector<sim::MCHitCollection> const& mchitcolVector(*mchitListHandle);
227 
228  //get mcparticles out of the event
230  evt.getByLabel(fMCModuleLabel, mcParticleHandle);
231  std::vector<simb::MCParticle> const& mcParticleVector(*mcParticleHandle);
232 
233  //get associations of mc particles to mc truth
235  evt.getByLabel(fMCModuleLabel, assnMCParticleTruthHandle);
236 
237  std::vector<const simb::MCTruth*> particle_to_truth =
238  util::GetAssociatedVectorOneP(assnMCParticleTruthHandle, mcParticleHandle);
239 
240  //make trackId to MCTruth map
241  std::map<int, const simb::MCTruth*> trackIDToTruthMap;
242  for (size_t p_iter = 0; p_iter < mcParticleVector.size(); p_iter++)
243  trackIDToTruthMap[mcParticleVector[p_iter].TrackId()] = particle_to_truth[p_iter];
244 
245  FillMCInfo(evt, hitVector, hitOrigins, mchitcolVector, trackIDToTruthMap);
246 
247  art::Handle<std::vector<recob::Track>> trackListHandle;
248  evt.getByLabel(fTrackModuleLabel, trackListHandle);
249  std::vector<recob::Track> const& trackVector(*trackListHandle);
250 
251  art::Handle<std::vector<recob::Cluster>> clusterListHandle;
252  evt.getByLabel(fClusterModuleLabel, clusterListHandle);
253  std::vector<recob::Cluster> const& clusterVector(*clusterListHandle);
254 
256  evt.getByLabel(fTrackModuleLabel, assnHitTrackHandle);
257  std::vector<std::vector<size_t>> track_indices_per_hit =
258  util::GetAssociatedVectorManyI(assnHitTrackHandle, hitListHandle);
259 
261  evt.getByLabel(fClusterModuleLabel, assnHitClusterHandle);
262  std::vector<std::vector<size_t>> cluster_indices_per_hit =
263  util::GetAssociatedVectorManyI(assnHitClusterHandle, hitListHandle);
264 
265  std::vector<art::Handle<std::vector<anab::CosmicTag>>> cosmicTagHandlesVector(
266  fCosmicTagAssocLabel.size());
267  std::vector<art::Handle<art::Assns<recob::Track, anab::CosmicTag>>> assnTrackTagHandlesVector(
268  fCosmicTagAssocLabel.size());
269  std::vector<std::vector<const anab::CosmicTag*>> tags_per_track(
270  trackVector.size(), std::vector<const anab::CosmicTag*>(fCosmicTagAssocLabel.size()));
271  std::vector<art::Handle<art::Assns<recob::Cluster, anab::CosmicTag>>> assnClusterTagHandlesVector(
272  fCosmicTagAssocLabel.size());
273  std::vector<std::vector<const anab::CosmicTag*>> tags_per_cluster(
274  clusterVector.size(), std::vector<const anab::CosmicTag*>(fCosmicTagAssocLabel.size()));
275 
276  for (size_t label_i = 0; label_i < fCosmicTagAssocLabel.size(); label_i++) {
277  try {
278  evt.getByLabel(fCosmicTagAssocLabel[label_i], cosmicTagHandlesVector[label_i]);
279  }
280  catch (...) {
281  continue;
282  }
283  try {
284  evt.getByLabel(fCosmicTagAssocLabel[label_i], assnTrackTagHandlesVector[label_i]);
285  for (auto const& pair : *assnTrackTagHandlesVector[label_i])
286  tags_per_track.at(pair.first.key())[label_i] = &(*(pair.second));
287  }
288  catch (...) {
289  }
290  try {
291  evt.getByLabel(fCosmicTagAssocLabel[label_i], assnClusterTagHandlesVector[label_i]);
292  for (auto const& pair : *assnClusterTagHandlesVector[label_i])
293  tags_per_cluster.at(pair.first.key())[label_i] = &(*(pair.second));
294  }
295  catch (...) {
296  }
297  }
298 
299  std::vector<std::vector<bool>> hitsAccounted(
300  hitVector.size(), std::vector<bool>(fCosmicTagAssocLabel.size(), false));
301  std::vector<bool> hitsAllTags(hitVector.size(), false);
302 
303  for (size_t hit_iter = 0; hit_iter < hitVector.size(); hit_iter++) {
304 
305  float charge = hitVector[hit_iter].Integral();
306  hit_origin_t origin = hitOrigins[hit_iter];
307 
308  if (track_indices_per_hit[hit_iter].size() != 0)
309  FillTrackInfo(hit_iter,
310  origin,
311  charge,
312  track_indices_per_hit[hit_iter],
313  tags_per_track,
314  hitsAccounted[hit_iter],
315  hitsAllTags);
316 
317  if (cluster_indices_per_hit[hit_iter].size() != 0)
318  FillClusterInfo(hit_iter,
319  origin,
320  charge,
321  cluster_indices_per_hit[hit_iter],
322  tags_per_cluster,
323  hitsAccounted[hit_iter],
324  hitsAllTags);
325 
326  if (hitsAllTags[hit_iter]) FillAllTagsInfo(hitVector[hit_iter], origin);
327 
328  } //end loop over all the hits
329 
330  tEventTree->Fill();
331 }
332 
333 //-------------------------------------------------------------------------------------------------------------------
334 void
335 microboone::CosmicRemovalAna::InitEventTree(int run_number, int event_number)
336 {
337 
338  cEventVals.runNumber = run_number; //evt.run();
339  cEventVals.eventNumber = event_number; //evt.event();
340 
341  cEventVals.nHitsTotal_Unknown = 0;
342  cEventVals.nHitsTotal_Cosmic = 0;
343  cEventVals.nHitsTotal_NonCosmic = 0;
344 
345  cEventVals.qTotal_Unknown = 0;
346  cEventVals.qTotal_Cosmic = 0;
347  cEventVals.qTotal_NonCosmic = 0;
348 
349  cEventVals.nHitsTrack = 0;
350  cEventVals.nHitsTrack_Cosmic = 0;
351  cEventVals.nHitsTrack_NonCosmic = 0;
352 
353  cEventVals.qTrack = 0;
354  cEventVals.qTrack_Cosmic = 0;
355  cEventVals.qTrack_NonCosmic = 0;
356 
357  cEventVals.nHitsCluster = 0;
358  cEventVals.nHitsCluster_Cosmic = 0;
359  cEventVals.nHitsCluster_NonCosmic = 0;
360 
361  cEventVals.qCluster = 0;
362  cEventVals.qCluster_Cosmic = 0;
363  cEventVals.qCluster_NonCosmic = 0;
364 
365  cEventVals.TotalTaggedCharge_Cosmic = 0.;
366  cEventVals.TotalTaggedCharge_NonCosmic = 0.;
367  cEventVals.TotalTaggedHits_Cosmic = 0;
368  cEventVals.TotalTaggedHits_NonCosmic = 0;
369 
370  for (size_t iter = 0; iter < fCosmicTagAssocLabel.size(); iter++) {
371  cTaggedHits_Cosmic.at(iter) = 0;
372  cTaggedCharge_Cosmic.at(iter) = 0;
373  cTaggedHits_NonCosmic.at(iter) = 0;
374  cTaggedCharge_NonCosmic.at(iter) = 0;
375  }
376 }
377 
378 //-------------------------------------------------------------------------------------------------------------------
379 // take in a list of hits, and determine the origin for those hits (and fill in the tree info)
380 void
382  art::Event const& e,
383  std::vector<recob::Hit> const& hitlist,
384  std::vector<hit_origin_t>& hitOrigins,
385  std::vector<sim::MCHitCollection> const& mchitCollectionVector,
386  std::map<int, const simb::MCTruth*> const& trackIdToTruthMap)
387 {
388  auto const clock_data =
390 
391  for (size_t itr = 0; itr < hitlist.size(); itr++) {
392 
393  recob::Hit const& this_hit = hitlist[itr];
394 
395  std::vector<int> trackIDs;
396  std::vector<double> energy;
397 
398  for (auto const& mchit : mchitCollectionVector[this_hit.Channel()]) {
399  if (std::abs(clock_data.TPCTDC2Tick(mchit.PeakTime()) - this_hit.PeakTime()) <
400  fHitCompareCut) {
401  trackIDs.push_back(mchit.PartTrackId());
402  energy.push_back(mchit.PartEnergy());
403  }
404  }
405 
406  if (trackIDs.size() == 0) {
407  hitOrigins[itr] = hit_origin_Unknown;
408  cEventVals.nHitsTotal_Unknown++;
409  cEventVals.qTotal_Unknown += this_hit.Integral();
410  continue;
411  }
412 
413  float cosmic_energy = 0;
414  float non_cosmic_energy = 0;
415 
416  for (size_t iter = 0; iter < trackIDs.size(); iter++) {
417  auto map_element = trackIdToTruthMap.find(std::abs(trackIDs[iter]));
418  if (map_element == trackIdToTruthMap.end()) continue;
419  int origin = map_element->second->Origin();
420  if (origin == simb::kBeamNeutrino)
421  non_cosmic_energy += energy[iter];
422  else
423  cosmic_energy += energy[iter];
424  }
425 
426  if (non_cosmic_energy > cosmic_energy) {
427  hitOrigins[itr] = hit_origin_NonCosmic;
428  cEventVals.nHitsTotal_NonCosmic++;
429  cEventVals.qTotal_NonCosmic += this_hit.Integral();
430  }
431  else {
432  hitOrigins[itr] = hit_origin_Cosmic;
433  cEventVals.nHitsTotal_Cosmic++;
434  cEventVals.qTotal_Cosmic += this_hit.Integral();
435  }
436  }
437 
438 } //end FillMCInfo
439 
440 //-------------------------------------------------------------------------------------------------------------------
441 void
443  size_t const& hit_iter,
444  hit_origin_t const& origin,
445  float const& charge,
446  std::vector<size_t> const& track_indices_this_hit,
447  std::vector<std::vector<const anab::CosmicTag*>> const& tags_per_track,
448  std::vector<bool>& hitsAccounted_per_tag,
449  std::vector<bool>& hitsAllTags)
450 {
451 
452  cEventVals.nHitsTrack++;
453  cEventVals.qTrack += charge;
454 
455  if (origin == hit_origin_Cosmic) {
456  cEventVals.nHitsTrack_Cosmic++;
457  cEventVals.qTrack_Cosmic += charge;
458  }
459  else if (origin == hit_origin_NonCosmic) {
460  cEventVals.nHitsTrack_NonCosmic++;
461  cEventVals.qTrack_NonCosmic += charge;
462  }
463 
464  for (unsigned int nCT = 0; nCT < fCosmicTagAssocLabel.size();
465  nCT++) { //<---This loops over the vector of cosmicTags in stored in the event
466  if (hitsAccounted_per_tag[nCT]) continue;
467 
468  for (auto const& track_index : track_indices_this_hit) {
469  if (!tags_per_track[track_index][nCT]) continue;
470  const anab::CosmicTag* currentTag(tags_per_track[track_index][nCT]);
471  if (currentTag->CosmicScore() > fCosmicScoreThresholds[nCT]) {
472 
473  hitsAccounted_per_tag[nCT] = true;
474  hitsAllTags[hit_iter] = true;
475  if (origin == hit_origin_Cosmic) {
476  cTaggedHits_Cosmic[nCT]++;
477  cTaggedCharge_Cosmic[nCT] += charge;
478  }
479  else if (origin == hit_origin_NonCosmic) {
480  cTaggedHits_NonCosmic[nCT]++;
481  cTaggedCharge_NonCosmic[nCT] += charge;
482  }
483  }
484  }
485  }
486 
487 } //end FillTrackInfo
488 
489 //-------------------------------------------------------------------------------------------------------------------
490 void
492  size_t const& hit_iter,
493  hit_origin_t const& origin,
494  float const& charge,
495  std::vector<size_t> const& cluster_indices_this_hit,
496  std::vector<std::vector<const anab::CosmicTag*>> const& tags_per_cluster,
497  std::vector<bool>& hitsAccounted_per_tag,
498  std::vector<bool>& hitsAllTags)
499 {
500 
501  cEventVals.nHitsCluster++;
502  cEventVals.qCluster += charge;
503 
504  if (origin == hit_origin_Cosmic) {
505  cEventVals.nHitsCluster_Cosmic++;
506  cEventVals.qCluster_Cosmic += charge;
507  }
508  else if (origin == hit_origin_NonCosmic) {
509  cEventVals.nHitsCluster_NonCosmic++;
510  cEventVals.qCluster_NonCosmic += charge;
511  }
512 
513  for (unsigned int nCT = 0; nCT < fCosmicTagAssocLabel.size();
514  nCT++) { //<---This loops over the vector of cosmicTags in stored in the event
515  if (hitsAccounted_per_tag[nCT]) continue;
516 
517  for (auto const& cluster_index : cluster_indices_this_hit) {
518  if (!tags_per_cluster[cluster_index][nCT]) continue;
519  const anab::CosmicTag* currentTag(tags_per_cluster[cluster_index][nCT]);
520  if (currentTag->CosmicScore() > fCosmicScoreThresholds[nCT]) {
521 
522  hitsAccounted_per_tag[nCT] = true;
523  hitsAllTags[hit_iter] = true;
524  if (origin == hit_origin_Cosmic) {
525  cTaggedHits_Cosmic[nCT]++;
526  cTaggedCharge_Cosmic[nCT] += charge;
527  }
528  else if (origin == hit_origin_NonCosmic) {
529  cTaggedHits_NonCosmic[nCT]++;
530  cTaggedCharge_NonCosmic[nCT] += charge;
531  }
532  }
533  }
534  }
535 
536 } //end FillClusterInfo
537 
538 //-------------------------------------------------------------------------------------------------------------------
539 void
541 {
542  if (origin == hit_origin_Cosmic) {
543  cEventVals.TotalTaggedCharge_Cosmic += hit.Integral();
544  cEventVals.TotalTaggedHits_Cosmic++;
545  }
546  else if (origin == hit_origin_NonCosmic) {
547  cEventVals.TotalTaggedCharge_NonCosmic += hit.Integral();
548  cEventVals.TotalTaggedHits_NonCosmic++;
549  }
550 }
551 
552 namespace microboone {
553 
555 }
void analyze(const art::Event &evt)
read access to event
CosmicRemovalAna(fhicl::ParameterSet const &pset)
EventNumber_t event() const
Definition: DataViewImpl.cc:85
void FillAllTagsInfo(recob::Hit const &hit, hit_origin_t const &origin)
std::string string
Definition: nybbler.cc:12
void FillTrackInfo(size_t const &hit_iter, hit_origin_t const &origin, float const &charge, std::vector< size_t > const &track_indices_this_hit, std::vector< std::vector< const anab::CosmicTag * >> const &tags_per_cluster, std::vector< bool > &hitsAccounted_per_tag, std::vector< bool > &hitsAllTags)
cEventProperties_t cEventVals
struct vector vector
std::vector< const U * > GetAssociatedVectorOneP(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
STL namespace.
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
Definition: Hit.h:224
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Particle class.
float & CosmicScore()
Definition: CosmicTag.h:61
std::vector< float > cTaggedCharge_NonCosmic
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
T abs(T value)
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void FillMCInfo(art::Event const &e, std::vector< recob::Hit > const &hitlist, std::vector< hit_origin_t > &hitOrigins, std::vector< sim::MCHitCollection > const &mchitCollectionVector, std::map< int, const simb::MCTruth * > const &trackIDToTruthMap)
void InitEventTree(int run_number, int event_number)
RunNumber_t run() const
Definition: DataViewImpl.cc:71
std::vector< float > fCosmicScoreThresholds
Detector simulation of raw signals on wires.
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:218
Declaration of signal hit object.
std::vector< std::vector< size_t > > GetAssociatedVectorManyI(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
Provides recob::Track data product.
std::vector< float > cTaggedCharge_Cosmic
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
Definition: Hit.h:230
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:227
Beam neutrinos.
Definition: MCTruth.h:23
void FillClusterInfo(size_t const &hit_iter, hit_origin_t const &origin, float const &charge, std::vector< size_t > const &cluster_indices_this_hit, std::vector< std::vector< const anab::CosmicTag * >> const &tags_per_cluster, std::vector< bool > &hitsAccounted_per_tag, std::vector< bool > &hitsAllTags)
std::vector< std::string > fCosmicTagAssocLabel