LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
CosmicRemovalAna_module.cc
Go to the documentation of this file.
1 // 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 //
11 
12 #ifndef COSMICSREMOVALANA_H
13 #define COSMICSREMOVALANA_H
14 
15 // Framework includes
23 #include "fhiclcpp/ParameterSet.h"
25 
26 // LArSoft Includes
38 
39 
40 // ROOT Includes
41 #include "TTree.h"
42 
43 #include <vector>
44 #include <string>
45 #include <sstream>
46 #include <fstream>
47 #include <algorithm>
48 #include <functional>
49 #include <iterator>
50 
51 
52 
53 namespace microboone {
54 
56 
57  public:
58 
59  explicit CosmicRemovalAna(fhicl::ParameterSet const& pset);
60 
62  void analyze(const art::Event& evt);
63  void beginJob();
64 
65  enum hit_origin_t {
69  };
70 
71  private:
72 
73  unsigned int nCosmicTags;
74 
75  // TTree *tTagTree;
76  TTree *tEventTree;
77 
78  std::string fHitsModuleLabel;
79  std::string fMCModuleLabel;
80  std::string fMCHitsModuleLabel;
81  std::string fClusterModuleLabel;
82  std::string fTrackModuleLabel;
84  std::vector <std::string> fCosmicTagAssocLabel;
85  std::vector <float> fCosmicScoreThresholds;
86 
87  void InitEventTree(int run_number, int event_number);
88 
89  void FillMCInfo( std::vector<recob::Hit> const& hitlist,
90  std::vector<hit_origin_t> & hitOrigins,
91  std::vector<sim::MCHitCollection> const& mchitCollectionVector,
92  std::map<int,const simb::MCTruth* > const& trackIDToTruthMap);
93 
94  void FillTrackInfo(size_t const& hit_iter,
95  hit_origin_t const& origin,
96  float const& charge,
97  std::vector<size_t> const& track_indices_this_hit,
98  std::vector< std::vector< const anab::CosmicTag* > > const& tags_per_cluster,
99  std::vector<bool> & hitsAccounted_per_tag,
100  std::vector<bool> & hitsAllTags);
101 
102  void FillClusterInfo(size_t const& hit_iter,
103  hit_origin_t const& origin,
104  float const& charge,
105  std::vector<size_t> const& cluster_indices_this_hit,
106  std::vector< std::vector< const anab::CosmicTag* > > const& tags_per_cluster,
107  std::vector<bool> & hitsAccounted_per_tag,
108  std::vector<bool> & hitsAllTags);
109 
110  void FillAllTagsInfo(recob::Hit const& hit,
111  hit_origin_t const& origin);
112 
113  std::vector<float> cTaggedCharge_Cosmic;
114  std::vector<float> cTaggedCharge_NonCosmic;
115  std::vector<int> cTaggedHits_Cosmic;
116  std::vector<int> cTaggedHits_NonCosmic;
117  // std::vector<std::string> *cTagAlgorithmNames;
118 
119  /*
120  typedef struct {
121  int eventNumber;
122  int tagType;
123  float x0;
124  float x1;
125  float y0;
126  float y1;
127  float z0;
128  float z1;
129  int nHits;
130  int nGoodHits;
131  int origin;
132  float score;
133  int pdg;
134  float energy;
135  } cTagProperties_t;
136  cTagProperties_t cTagVals;
137  */
138  };//<---End class
139 }
140 
141  typedef struct {
144 
148 
152 
156 
157  float qTrack;
160 
164 
165  float qCluster;
168 
173 
174 
177 
178 // =====================================================
179 // fhicl::ParameterSet
180 // =====================================================
182  EDAnalyzer(pset),
183  fHitsModuleLabel (pset.get< std::string >("HitsModuleLabel") ),
184  fMCModuleLabel (pset.get< std::string >("MCModuleLabel") ),
185  fMCHitsModuleLabel (pset.get< std::string >("MCHitsModuleLabel") ),
186  fClusterModuleLabel (pset.get< std::string >("ClusterModuleLabel") ),
187  fTrackModuleLabel (pset.get< std::string >("TrackModuleLabel") ),
188  fHitCompareCut (pset.get< float >("HitCompareCut") ),
189  fCosmicTagAssocLabel (pset.get<std::vector< std::string > >("CosmicTagAssocLabel") ),
190  fCosmicScoreThresholds (pset.get<std::vector<float> > ("CosmicScoreThresholds") )
191 {
192 }
193 
194 
195 // =====================================================
196 // BeginJob
197 // =====================================================
198 
200 {
201 
202  //static cEventProperties_t cEventVals;
203 
209 
211  tEventTree = (TTree*)tfs->make<TTree>("CosmicEventTree","CosmicEventTree");
212 
213  tEventTree->Branch("event", &cEventVals, "runNumber/I:eventNumber/I:nHitsTotal_Unknown/I:nHitsTotal_Cosmic/I:nHitsTotal_NonCosmic/I:qTotal_Unknown/F:qTotal_Cosmic/F:qTotal_NonCosmic/F:nHitsTrack/I:nHitsTrack_Cosmic/I:nHitsTrack_NonCosmic/I:qTrack/F:qTrack_Cosmic/F:qTrack_NonCosmic/F:nHitsCluster/I:nHitsCluster_Cosmic/I:nHitsCluster_NonCosmic/I:qCluster/F:qCluster_Cosmic/F:qCluster_NonCosmic/F:TotalTaggedCharge_Cosmic/F:TotalTaggedCharge_NonCosmic/F:TotalTaggedHits_Cosmic/I:TotalTaggedHits_NonCosmic/I");
214  tEventTree->Branch("TaggedCharge_Cosmic",&cTaggedCharge_Cosmic);
215  tEventTree->Branch("TaggedCharge_NonCosmic",&cTaggedCharge_NonCosmic);
216  tEventTree->Branch("TaggedHits_Cosmic",&cTaggedHits_Cosmic);
217  tEventTree->Branch("TaggedHits_NonCosmic",&cTaggedHits_NonCosmic);
218 
219 }
220 
221 
222 // =====================================================
223 // Event Loop
224 // =====================================================
226 {
227 
228  InitEventTree(evt.run(), evt.event());
229 
230  // ##################################################################
231  // ### Grabbing ALL HITS in the event to monitor the backtracking ###
232  // ##################################################################
234  evt.getByLabel("daq",rawdigitHandle);
235 
236  art::Handle< std::vector<recob::Hit> > hitListHandle;
237  evt.getByLabel(fHitsModuleLabel,hitListHandle);
238  std::vector<recob::Hit> const& hitVector(*hitListHandle);
239 
240  std::vector<hit_origin_t> hitOrigins(hitVector.size());
241 
242  //get mcHitCollection
244  evt.getByLabel(fMCHitsModuleLabel,mchitListHandle);
245  std::vector<sim::MCHitCollection> const& mchitcolVector(*mchitListHandle);
246 
247  //get mcparticles out of the event
249  evt.getByLabel(fMCModuleLabel,mcParticleHandle);
250  std::vector<simb::MCParticle> const& mcParticleVector(*mcParticleHandle);
251 
252  //get associations of mc particles to mc truth
254  evt.getByLabel(fMCModuleLabel,assnMCParticleTruthHandle);
255 
256  std::vector< const simb::MCTruth* >
257  particle_to_truth = util::GetAssociatedVectorOneP(assnMCParticleTruthHandle,
258  mcParticleHandle);
259 
260  //make trackId to MCTruth map
261  std::map<int,const simb::MCTruth* > trackIDToTruthMap;
262  for(size_t p_iter=0; p_iter<mcParticleVector.size(); p_iter++)
263  trackIDToTruthMap[ mcParticleVector[p_iter].TrackId() ] = particle_to_truth[p_iter];
264 
265  FillMCInfo(hitVector, hitOrigins, mchitcolVector, trackIDToTruthMap);
266 
267  art::Handle< std::vector<recob::Track> > trackListHandle;
268  evt.getByLabel(fTrackModuleLabel,trackListHandle);
269  std::vector<recob::Track> const& trackVector(*trackListHandle);
270 
271  art::Handle< std::vector<recob::Cluster> > clusterListHandle;
272  evt.getByLabel(fClusterModuleLabel,clusterListHandle);
273  std::vector<recob::Cluster> const& clusterVector(*clusterListHandle);
274 
276  evt.getByLabel(fTrackModuleLabel,assnHitTrackHandle);
277  std::vector< std::vector<size_t> >
278  track_indices_per_hit = util::GetAssociatedVectorManyI(assnHitTrackHandle,
279  hitListHandle);
280 
282  evt.getByLabel(fClusterModuleLabel,assnHitClusterHandle);
283  std::vector< std::vector<size_t> >
284  cluster_indices_per_hit = util::GetAssociatedVectorManyI(assnHitClusterHandle,
285  hitListHandle);
286 
287  std::vector< art::Handle< std::vector<anab::CosmicTag> > > cosmicTagHandlesVector(fCosmicTagAssocLabel.size());
288  std::vector< art::Handle< art::Assns<recob::Track,anab::CosmicTag> > > assnTrackTagHandlesVector(fCosmicTagAssocLabel.size());
289  std::vector< std::vector< const anab::CosmicTag* > > tags_per_track(trackVector.size(), std::vector<const anab::CosmicTag*>(fCosmicTagAssocLabel.size()));
290  std::vector< art::Handle< art::Assns<recob::Cluster,anab::CosmicTag> > > assnClusterTagHandlesVector(fCosmicTagAssocLabel.size());
291  std::vector< std::vector< const anab::CosmicTag* > > tags_per_cluster(clusterVector.size(), std::vector<const anab::CosmicTag*>(fCosmicTagAssocLabel.size()));
292 
293  for(size_t label_i=0; label_i<fCosmicTagAssocLabel.size(); label_i++){
294  try{ evt.getByLabel(fCosmicTagAssocLabel[label_i],cosmicTagHandlesVector[label_i]); }
295  catch(...){ continue; }
296  try{
297  evt.getByLabel(fCosmicTagAssocLabel[label_i],assnTrackTagHandlesVector[label_i]);
298  for(auto const& pair : *assnTrackTagHandlesVector[label_i])
299  tags_per_track.at(pair.first.key())[label_i] = &(*(pair.second));
300  }
301  catch(...){}
302  try{
303  evt.getByLabel(fCosmicTagAssocLabel[label_i],assnClusterTagHandlesVector[label_i]);
304  for(auto const& pair : *assnClusterTagHandlesVector[label_i])
305  tags_per_cluster.at(pair.first.key())[label_i] = &(*(pair.second));
306  }
307  catch(...){}
308  }
309 
310  std::vector< std::vector<bool> > hitsAccounted(hitVector.size(),std::vector<bool>(fCosmicTagAssocLabel.size(),false));
311  std::vector<bool> hitsAllTags(hitVector.size(),false);
312 
313  for(size_t hit_iter=0; hit_iter<hitVector.size(); hit_iter++){
314 
315  float charge = hitVector[hit_iter].Integral();
316  hit_origin_t origin = hitOrigins[hit_iter];
317 
318  if(track_indices_per_hit[hit_iter].size()!=0)
319  FillTrackInfo(hit_iter,
320  origin,
321  charge,
322  track_indices_per_hit[hit_iter],
323  tags_per_track,
324  hitsAccounted[hit_iter],
325  hitsAllTags);
326 
327  if(cluster_indices_per_hit[hit_iter].size()!=0)
328  FillClusterInfo(hit_iter,
329  origin,
330  charge,
331  cluster_indices_per_hit[hit_iter],
332  tags_per_cluster,
333  hitsAccounted[hit_iter],
334  hitsAllTags);
335 
336  if(hitsAllTags[hit_iter]) FillAllTagsInfo(hitVector[hit_iter],origin);
337 
338  }//end loop over all the hits
339 
340  tEventTree->Fill();
341 }
342 
343 //-------------------------------------------------------------------------------------------------------------------
344 void microboone::CosmicRemovalAna::InitEventTree(int run_number, int event_number){
345 
346  cEventVals.runNumber = run_number;//evt.run();
347  cEventVals.eventNumber = event_number;//evt.event();
348 
349  cEventVals.nHitsTotal_Unknown = 0;
350  cEventVals.nHitsTotal_Cosmic = 0;
351  cEventVals.nHitsTotal_NonCosmic = 0;
352 
353  cEventVals.qTotal_Unknown = 0;
354  cEventVals.qTotal_Cosmic = 0;
355  cEventVals.qTotal_NonCosmic = 0;
356 
357  cEventVals.nHitsTrack = 0;
358  cEventVals.nHitsTrack_Cosmic = 0;
359  cEventVals.nHitsTrack_NonCosmic = 0;
360 
361  cEventVals.qTrack = 0;
362  cEventVals.qTrack_Cosmic = 0;
363  cEventVals.qTrack_NonCosmic = 0;
364 
365  cEventVals.nHitsCluster = 0;
366  cEventVals.nHitsCluster_Cosmic = 0;
367  cEventVals.nHitsCluster_NonCosmic = 0;
368 
369  cEventVals.qCluster = 0;
370  cEventVals.qCluster_Cosmic = 0;
371  cEventVals.qCluster_NonCosmic = 0;
372 
373  cEventVals.TotalTaggedCharge_Cosmic = 0.;
374  cEventVals.TotalTaggedCharge_NonCosmic = 0.;
375  cEventVals.TotalTaggedHits_Cosmic = 0;
376  cEventVals.TotalTaggedHits_NonCosmic = 0;
377 
378  for(size_t iter=0; iter<fCosmicTagAssocLabel.size(); iter++){
379  cTaggedHits_Cosmic.at(iter) = 0;
380  cTaggedCharge_Cosmic.at(iter) = 0;
381  cTaggedHits_NonCosmic.at(iter) = 0;
382  cTaggedCharge_NonCosmic.at(iter) = 0;
383  }
384 
385 }
386 
387 //-------------------------------------------------------------------------------------------------------------------
388 // take in a list of hits, and determine the origin for those hits (and fill in the tree info)
389 void microboone::CosmicRemovalAna::FillMCInfo( std::vector<recob::Hit> const& hitlist,
390  std::vector<hit_origin_t> & hitOrigins,
391  std::vector<sim::MCHitCollection> const& mchitCollectionVector,
392  std::map<int,const simb::MCTruth* > const& trackIdToTruthMap){
393 
394  auto const* ts = lar::providerFrom<detinfo::DetectorClocksService>();
395 
396  for(size_t itr=0; itr<hitlist.size(); itr++){
397 
398  recob::Hit const& this_hit = hitlist[itr];
399 
400  std::vector<int> trackIDs;
401  std::vector<double> energy;
402 
403  for( auto const& mchit : mchitCollectionVector[this_hit.Channel()] ){
404  if( std::abs(ts->TPCTDC2Tick(mchit.PeakTime()) - this_hit.PeakTime()) < fHitCompareCut){
405  trackIDs.push_back(mchit.PartTrackId());
406  energy.push_back(mchit.PartEnergy());
407  }
408  }
409 
410  if(trackIDs.size()==0){
411  hitOrigins[itr] = hit_origin_Unknown;
412  cEventVals.nHitsTotal_Unknown++;
413  cEventVals.qTotal_Unknown += this_hit.Integral();
414  continue;
415  }
416 
417  float cosmic_energy=0;
418  float non_cosmic_energy=0;
419 
420  for(size_t iter=0; iter<trackIDs.size(); iter++){
421  auto map_element = trackIdToTruthMap.find(std::abs(trackIDs[iter]));
422  if(map_element==trackIdToTruthMap.end()) continue;
423  int origin = map_element->second->Origin();
424  if(origin == simb::kBeamNeutrino)
425  non_cosmic_energy += energy[iter];
426  else
427  cosmic_energy += energy[iter];
428  }
429 
430  if(non_cosmic_energy > cosmic_energy){
431  hitOrigins[itr] = hit_origin_NonCosmic;
432  cEventVals.nHitsTotal_NonCosmic++;
433  cEventVals.qTotal_NonCosmic += this_hit.Integral();
434  }
435  else{
436  hitOrigins[itr] = hit_origin_Cosmic;
437  cEventVals.nHitsTotal_Cosmic++;
438  cEventVals.qTotal_Cosmic += this_hit.Integral();
439  }
440  }
441 
442 }//end FillMCInfo
443 
444 //-------------------------------------------------------------------------------------------------------------------
446  hit_origin_t const& origin,
447  float const& charge,
448  std::vector<size_t> const& track_indices_this_hit,
449  std::vector< std::vector< const anab::CosmicTag* > > const& tags_per_track,
450  std::vector<bool> & hitsAccounted_per_tag,
451  std::vector<bool> & hitsAllTags){
452 
453  cEventVals.nHitsTrack++;
454  cEventVals.qTrack += charge;
455 
456  if(origin==hit_origin_Cosmic){
457  cEventVals.nHitsTrack_Cosmic++;
458  cEventVals.qTrack_Cosmic += charge;
459  }
460  else if(origin==hit_origin_NonCosmic){
461  cEventVals.nHitsTrack_NonCosmic++;
462  cEventVals.qTrack_NonCosmic += charge;
463  }
464 
465  for(unsigned int nCT = 0; nCT < fCosmicTagAssocLabel.size(); 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 
488 }//end FillTrackInfo
489 
490 //-------------------------------------------------------------------------------------------------------------------
492  hit_origin_t const& origin,
493  float const& charge,
494  std::vector<size_t> const& cluster_indices_this_hit,
495  std::vector< std::vector< const anab::CosmicTag* > > const& tags_per_cluster,
496  std::vector<bool> & hitsAccounted_per_tag,
497  std::vector<bool> & hitsAllTags){
498 
499  cEventVals.nHitsCluster++;
500  cEventVals.qCluster += charge;
501 
502  if(origin==hit_origin_Cosmic){
503  cEventVals.nHitsCluster_Cosmic++;
504  cEventVals.qCluster_Cosmic += charge;
505  }
506  else if(origin==hit_origin_NonCosmic){
507  cEventVals.nHitsCluster_NonCosmic++;
508  cEventVals.qCluster_NonCosmic += charge;
509  }
510 
511  for(unsigned int nCT = 0; nCT < fCosmicTagAssocLabel.size(); nCT++){//<---This loops over the vector of cosmicTags in stored in the event
512  if(hitsAccounted_per_tag[nCT]) continue;
513 
514  for(auto const& cluster_index : cluster_indices_this_hit){
515  if(!tags_per_cluster[cluster_index][nCT]) continue;
516  const anab::CosmicTag* currentTag(tags_per_cluster[cluster_index][nCT]);
517  if( currentTag->CosmicScore() > fCosmicScoreThresholds[nCT] ){
518 
519  hitsAccounted_per_tag[nCT] = true;
520  hitsAllTags[hit_iter] = true;
521  if(origin==hit_origin_Cosmic){
522  cTaggedHits_Cosmic[nCT]++;
523  cTaggedCharge_Cosmic[nCT] += charge;
524  }
525  else if(origin==hit_origin_NonCosmic){
526  cTaggedHits_NonCosmic[nCT]++;
527  cTaggedCharge_NonCosmic[nCT] += charge;
528  }
529  }
530  }
531 
532 
533  }
534 
535 }//end FillClusterInfo
536 
537 
538 //-------------------------------------------------------------------------------------------------------------------
540  hit_origin_t const& origin){
541  if(origin==hit_origin_Cosmic){
542  cEventVals.TotalTaggedCharge_Cosmic += hit.Integral();
543  cEventVals.TotalTaggedHits_Cosmic++;
544  }
545  else if(origin==hit_origin_NonCosmic){
546  cEventVals.TotalTaggedCharge_NonCosmic += hit.Integral();
547  cEventVals.TotalTaggedHits_NonCosmic++;
548  }
549 
550 }
551 
552 namespace microboone{
553 
555 }
556 
557 
558 
559 
560 
561 
562 
563 
564 #endif
void analyze(const art::Event &evt)
read access to event
CosmicRemovalAna(fhicl::ParameterSet const &pset)
void FillMCInfo(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 FillAllTagsInfo(recob::Hit const &hit, hit_origin_t const &origin)
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)
Declaration of signal hit object.
cEventProperties_t cEventVals
std::vector< const U * > GetAssociatedVectorOneP(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
STL namespace.
Definition of basic raw digits.
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
Definition: Hit.h:225
Particle class.
float & CosmicScore()
Definition: CosmicTag.h:65
std::vector< float > cTaggedCharge_NonCosmic
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
Provides recob::Track data product.
double energy
Definition: plottest35.C:25
void InitEventTree(int run_number, int event_number)
EventNumber_t event() const
Definition: Event.h:67
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
Declaration of cluster object.
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:219
T * make(ARGS...args) const
Utility object to perform functions of association.
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::vector< std::vector< size_t > > GetAssociatedVectorManyI(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:49
TCEvent evt
Definition: DataStructs.cxx:5
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)
RunNumber_t run() const
Definition: Event.h:77
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
Definition: Hit.h:231
art framework interface to geometry description
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:230
Beam neutrinos.
Definition: MCTruth.h:21
std::vector< std::string > fCosmicTagAssocLabel