ClusterCounter4_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: ClusterCounter4
3 // Module Type: analyzer
4 // File: ClusterCounter4_module.cc
5 //
6 // Same as #3, then find best matching MC truth particle and compute how
7 // clean is the reco object.
8 // Robert Sulej
9 ////////////////////////////////////////////////////////////////////////
10 
17 #include "art_root_io/TFileService.h"
18 #include "canvas/Persistency/Common/FindManyP.h"
20 #include "fhiclcpp/ParameterSet.h"
22 
26 
30 
31 #include "TTree.h"
32 
33 namespace tutorial {
34 
36 public:
37  explicit ClusterCounter4(fhicl::ParameterSet const & p);
38 
39  // Plugins should not be copied or assigned.
40  ClusterCounter4(ClusterCounter4 const &) = delete;
41  ClusterCounter4(ClusterCounter4 &&) = delete;
42  ClusterCounter4 & operator = (ClusterCounter4 const &) = delete;
44 
45  // Required functions.
46  void analyze(art::Event const & e) override;
47 
48  // Selected optional functions.
49  void beginJob() override;
50  void endJob() override;
51 
52 private:
53  float sumAdc(const std::vector< art::Ptr<recob::Hit> > & hits) const;
54 
56  detinfo::DetectorClocksData const& clockData,
57  const std::vector< art::Ptr<recob::Hit> > & hits,
58  float & fraction, bool & foundEmParent) const;
59 
60  size_t fEvNumber;
61 
62  TTree *fEventTree;
63  size_t fNClusters;
64 
65  TTree *fClusterTree;
66  size_t fNHits;
67  float fAdcSum;
68  float fClean;
69 
70  // ******* fcl parameters *******
72  size_t fMinSize;
73 };
74 
76 {
77  fClusterModuleLabel = p.get< std::string >("ClusterModuleLabel");
78  fMinSize = p.get< size_t >("MinSize");
79 }
80 
82 {
83  art::ServiceHandle<art::TFileService> tfs; // TTree's are created in the memory managed by ROOT (you don't delete them)
84 
85  fEventTree = tfs->make<TTree>("EventTree", "event by event info");
86  fEventTree->Branch("event", &fEvNumber, "fEvNumber/I");
87  fEventTree->Branch("nclusters", &fNClusters, "fNClusters/I");
88 
89  fClusterTree = tfs->make<TTree>("ClusterTree", "cluster by cluster info");
90  fClusterTree->Branch("event", &fEvNumber, "fEvNumber/I");
91  fClusterTree->Branch("nhits", &fNHits, "fNHits/I");
92  fClusterTree->Branch("adcsum", &fAdcSum, "fAdcSum/F");
93  fClusterTree->Branch("clean", &fClean, "fClean/F");
94 }
95 
97 {
98  fEvNumber = evt.id().event();
99  mf::LogVerbatim("ClusterCounter4") << "ClusterCounter4 module on event #" << fEvNumber;
100 
101  // use auto to make the line shorter when you remember all art types,
102  // the full type of the handle is: art::ValidHandle< std::vector<recob::Cluster> >
103  auto cluHandle = evt.getValidHandle< std::vector<recob::Cluster> >(fClusterModuleLabel);
104 
105  // this will let us look for all hits associated to selected cluster
106  art::FindManyP< recob::Hit > hitsFromClusters(cluHandle, evt, fClusterModuleLabel);
107 
108  fNClusters = 0;
109 
110  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService>()->DataFor(evt);
111  // and here we go by index:
112  for (size_t i = 0; i < cluHandle->size(); ++i)
113  {
114  fNHits = cluHandle->at(i).NHits();
115  fAdcSum = sumAdc(hitsFromClusters.at(i));
116 
117  bool isEM = false;
118  const simb::MCParticle* p = getTruthParticle(clockData, hitsFromClusters.at(i), fClean, isEM);
119  if (p)
120  {
121  if (isEM) { mf::LogVerbatim("ClusterCounter4") << "matched mother particle PDG: " << p->PdgCode(); }
122  else { mf::LogVerbatim("ClusterCounter4") << "matched particle PDG: " << p->PdgCode(); }
123  }
124  else { mf::LogWarning("ClusterCounter4") << "No matcched particle??"; }
125 
126  fClusterTree->Fill();
127 
128  mf::LogVerbatim("ClusterCounter4")
129  << "NHits() = " << fNHits << ", assn size = " << hitsFromClusters.at(i).size()
130  << " SummedADC() = " << cluHandle->at(i).SummedADC() << ", sum hits adc = " << fAdcSum;
131 
132  if (fNHits >= fMinSize) { ++fNClusters; }
133  }
134  fEventTree->Fill();
135 }
136 
138  const std::vector< art::Ptr<recob::Hit> > & hits,
139  float & fraction, bool & foundEmParent) const
140 {
141  const simb::MCParticle* mcParticle = 0;
142  fraction = 0;
143  foundEmParent = false;
144 
147  std::unordered_map<int, double> trkIDE;
148  for (auto const & h : hits)
149  {
150  for (auto const & ide : bt_serv->HitToTrackIDEs(clockData, h)) // loop over std::vector<sim::TrackIDE>
151  {
152  trkIDE[ide.trackID] += ide.energy; // sum energy contribution by each track ID
153  }
154  }
155 
156  int best_id = 0;
157  double tot_e = 0, max_e = 0;
158  for (auto const & contrib : trkIDE)
159  {
160  tot_e += contrib.second; // sum total energy in these hits
161  if (contrib.second > max_e) // find track ID corresponding to max energy
162  {
163  max_e = contrib.second;
164  best_id = contrib.first;
165  }
166  }
167 
168  if ((max_e > 0) && (tot_e > 0)) // ok, found something reasonable
169  {
170  if (best_id < 0) // NOTE: negative ID means this is EM activity
171  { // caused by track with the same but positive ID
172  best_id = -best_id; // --> we'll find mother MCParticle of these hits
173  foundEmParent = true;
174  }
175  mcParticle = pi_serv->TrackIdToParticle_P(best_id); // MCParticle corresponding to track ID
176  fraction = max_e / tot_e;
177  }
178  else { mf::LogWarning("ClusterCounter4") << "No energy deposits??"; }
179 
180  return mcParticle;
181 }
182 
184 {
185  float sum = 0;
186  for (auto const & h : hits)
187  {
188  sum += h->SummedADC();
189  }
190  return sum;
191 }
192 
194 {
195  mf::LogVerbatim("ClusterCounter4") << "ClusterCounter4 finished job";
196 }
197 
198 } // tutorial namespace
199 
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
int PdgCode() const
Definition: MCParticle.h:212
ClusterCounter4 & operator=(ClusterCounter4 const &)=delete
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
std::string string
Definition: nybbler.cc:12
const simb::MCParticle * TrackIdToParticle_P(int id) const
struct vector vector
void analyze(art::Event const &e) override
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Particle class.
float sumAdc(const std::vector< art::Ptr< recob::Hit > > &hits) const
const simb::MCParticle * getTruthParticle(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit > > &hits, float &fraction, bool &foundEmParent) const
ClusterCounter4(fhicl::ParameterSet const &p)
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
T get(std::string const &key) const
Definition: ParameterSet.h:271
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
Declaration of signal hit object.
Contains all timing reference information for the detector.
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
EventNumber_t event() const
Definition: EventID.h:116
TCEvent evt
Definition: DataStructs.cxx:7
EventID id() const
Definition: Event.cc:34