anatest_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 // Class: anatest
3 // Plugin Type: analyzer (art v2_11_02)
4 // File: anatest_module.cc
5 //
6 // Generated at Tue Mar 3 11:11:11 2020 by Leo Bellantoni
7 //
8 // a plugin for testing reco output - derived from anatree_module.cc
9 //
10 ////////////////////////////////////////////////////////////////////////////////
11 
18 #include "art_root_io/TFileService.h"
20 #include "fhiclcpp/ParameterSet.h"
23 #include "canvas/Persistency/Common/FindOne.h"
24 #include "canvas/Persistency/Common/FindOneP.h"
25 #include "canvas/Persistency/Common/FindMany.h"
26 #include "canvas/Persistency/Common/FindManyP.h"
27 
31 #include "MCCheater/BackTracker.h"
43 
44 #include "TTree.h"
45 #include "TDatabasePDG.h"
46 #include "TParticlePDG.h"
47 
48 #include <string>
49 #include <vector>
50 #include <unordered_map>
51 
52 #include <chrono>
53 using namespace std::chrono;
54 
55 
56 
57 namespace gar {
58 
59  class anatest : public art::EDAnalyzer {
60  public:
61  explicit anatest(fhicl::ParameterSet const & p);
62  // The compiler-generated destructor is fine for non-base
63  // classes without bare pointers or other resource use.
64 
65  // Plugins should not be copied or assigned.
66  anatest(anatest const &) = delete;
67  anatest(anatest &&) = delete;
68  anatest & operator = (anatest const &) = delete;
69  anatest & operator = (anatest &&) = delete;
70 
71  virtual void beginJob() override;
72 
73  // Required functions.
74  void analyze(art::Event const & e) override;
75 
76 
77 
78  private:
79 
80  void ClearVectors();
81  void FillVectors(art::Event const & e);
82 
83  // Position of TPC from geometry service; 1 S Boston Ave.
84  double ItsInTulsa[3];
85 
86  // Input data labels
87  std::vector<std::string> fGeneratorLabels;
97 
98  // the analysis output tree
99  TTree *fTree;
100 
101  // global event info
102  Int_t fEvent; ///< number of the event being processed
103  Int_t fRun; ///< number of the run being processed
104  Int_t fSubRun; ///< number of the sub-run being processed
105 
106  // Some random fields for now
107  std::vector<Int_t> fNeutrinoType;
108  std::vector<Int_t> fInteractionType;
109  std::vector<Float_t> fQ2;
110  };
111 }
112 
113 
114 
115 //==============================================================================
116 //==============================================================================
117 //==============================================================================
118 // constructor
119 gar::anatest::anatest(fhicl::ParameterSet const & p) : EDAnalyzer(p) {
120 
121  bool usegenlabels =
122  p.get_if_present<std::vector<std::string> >("GeneratorLabels",fGeneratorLabels);
123  if (!usegenlabels) fGeneratorLabels.clear();
124 
125  fGeantLabel = p.get<std::string>("GEANTLabel","geant");
126  fHitLabel = p.get<std::string>("HitLabel","hit");
127  fTPCClusterLabel = p.get<std::string>("TPCClusterLabel","tpccluster");
128  fTrackLabel = p.get<std::string>("TrackLabel","track");
129  fVertexLabel = p.get<std::string>("VertexLabel","vertex");
130  fRawCaloHitLabel = p.get<std::string>("RawCaloHitLabel","daqecal");
131  fCaloHitLabel = p.get<std::string>("CaloHitLabel","calohit");
132  fClusterLabel = p.get<std::string>("ClusterLabel","calocluster");
133  fECALAssnLabel = p.get<std::string>("ECALAssnLabel","trkecalassn");
134 
135  fTree = nullptr;
136 
137  if (usegenlabels) {
138  for (size_t i=0; i<fGeneratorLabels.size(); ++i) {
139  consumes<std::vector<simb::MCTruth> >(fGeneratorLabels.at(i));
140  }
141  } else {
142  consumesMany<std::vector<simb::MCTruth> >();
143  }
144 
145 
146 
147  consumes<art::Assns<simb::MCTruth, simb::MCParticle> >(fGeantLabel);
148 
149  consumes<std::vector<rec::Hit> >(fHitLabel);
150  consumes<std::vector<rec::TPCCluster> >(fTPCClusterLabel);
151  consumes<std::vector<rec::Track> >(fTrackLabel);
152  consumes<art::Assns<rec::Track, rec::TPCCluster> >(fTPCClusterLabel);
153  consumes<std::vector<rec::Vertex> >(fVertexLabel);
154  consumes<art::Assns<rec::Track, rec::Vertex> >(fVertexLabel);
155 
156  consumes<std::vector<gar::sdp::CaloDeposit> >(fGeantLabel);
157  consumes<std::vector<raw::CaloRawDigit> >(fRawCaloHitLabel);
158  consumes<std::vector<rec::CaloHit> >(fCaloHitLabel);
159  consumes<std::vector<rec::Cluster> >(fClusterLabel);
160  consumes<art::Assns<rec::Cluster, rec::Track>>(fECALAssnLabel);
161 
162  return;
163 } // end constructor
164 
165 
166 
167 //==============================================================================
168 //==============================================================================
169 //==============================================================================
171 
173  ItsInTulsa[0] = euclid->TPCXCent();
174  ItsInTulsa[1] = euclid->TPCYCent();
175  ItsInTulsa[2] = euclid->TPCZCent();
176 
177 
178 
180  fTree = tfs->make<TTree>("MPDtestTree","MPDtestTree");
181 
182 
183 
184  fTree->Branch("Run", &fRun, "Run/I");
185  fTree->Branch("SubRun", &fSubRun, "SubRun/I");
186  fTree->Branch("Event", &fEvent, "Event/I");
187 
188  fTree->Branch("NType", &fNeutrinoType);
189  fTree->Branch("InterT", &fInteractionType);
190  fTree->Branch("MC_Q2", &fQ2);
191 
192  return;
193 } // End of :anatest::beginJob
194 
195 
196 
197 //==============================================================================
198 //==============================================================================
199 //==============================================================================
201 
202  ClearVectors();
203  FillVectors(e);
204  fTree->Fill();
205  return;
206 }
207 
208 
209 
210 //==============================================================================
211 //==============================================================================
212 //==============================================================================
214 
215  // clear out all our vectors
216  fNeutrinoType.clear();
217  fInteractionType.clear();
218  fQ2.clear();
219 
220  return;
221 } // end :anatest::ClearVectors
222 
223 
224 
225 //==============================================================================
226 //==============================================================================
227 //==============================================================================
229 
230 
231 
232  // ============= Get art handles ==========================================
233  // Get handles for MCinfo, also good for MCPTrajectory
234  std::vector< art::Handle<std::vector<simb::MCTruth>> > mcthandlelist;
235  if (fGeneratorLabels.size()<1) {
236  mcthandlelist = e.getMany<std::vector<simb::MCTruth> >(); // get them all (even if there are none)
237  } else {
238  mcthandlelist.resize(fGeneratorLabels.size());
239  for (size_t i=0; i< fGeneratorLabels.size(); ++i) {
240  mcthandlelist.at(i) = e.getHandle<std::vector<simb::MCTruth> >(fGeneratorLabels.at(i));
241  if (!mcthandlelist.at(i))
242  throw cet::exception("anatest") << " No simb::MCTruth branch."
243  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
244  }
245  }
246 
247 
248 
249  auto MCPHandle = e.getHandle<std::vector<simb::MCParticle> >(fGeantLabel);
250  if (!MCPHandle) {
251  throw cet::exception("anatest") << " No simb::MCParticle branch."
252  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
253  }
254 
255 
256 
257  // Get handles for MCCaloInfo
258  auto SimHitHandle = e.getHandle< std::vector<gar::sdp::CaloDeposit> >(fGeantLabel);
259  if (!SimHitHandle) {
260  throw cet::exception("anatest") << " No gar::sdp::CaloDeposit branch."
261  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
262  }
263 
264 
265 
266  // Get handle for TPC hit data
267  auto HitHandle = e.getHandle< std::vector<rec::Hit> >(fHitLabel);
268  if (!HitHandle) {
269  throw cet::exception("anatest") << " No rec::Hit branch."
270  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
271  }
272 
273 
274 
275  // Get handle for TPCClusters
276  auto TPCClusterHandle = e.getHandle< std::vector<rec::TPCCluster> >(fTPCClusterLabel);
277  if (!TPCClusterHandle) {
278  throw cet::exception("anatest") << " No rec::TPCCluster branch."
279  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
280  }
281 
282 
283 
284  // Get handles for Tracks and also Assn's to TPCClusters, TrackIoniz
285  auto TrackHandle = e.getHandle< std::vector<rec::Track> >(fTrackLabel);
286  if (!TrackHandle) {
287  throw cet::exception("anatest") << " No rec::Track branch."
288  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
289  }
290  //art::FindManyP<rec::TPCCluster>* findManyTPCClusters = NULL;
291  //findManyTPCClusters = new art::FindManyP<rec::TPCCluster>(TrackHandle,e,fTrackLabel);
292 
293 
294 
295  // Get handle for Vertices; also Assn's to Tracks
296  auto VertexHandle = e.getHandle< std::vector<rec::Vertex> >(fVertexLabel);
297  if (!VertexHandle) {
298  throw cet::exception("anatest") << " No rec::Vertex branch."
299  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
300  }
301  //art::FindManyP<rec::Track, rec::TrackEnd>* findManyTrackEnd = NULL;
302  //findManyTrackEnd = new art::FindManyP<rec::Track, rec::TrackEnd>(VertexHandle,e,fVertexLabel);
303 
304 
305  // Get handle for CaloDigits
306  auto RawHitHandle = e.getHandle< std::vector<gar::raw::CaloRawDigit> >(fRawCaloHitLabel);
307  if (!RawHitHandle) {
308  throw cet::exception("anatest") << " No :raw::CaloRawDigit branch."
309  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
310  }
311 
312 
313  // Get handle for CaloHits
314  auto RecoHitHandle = e.getHandle< std::vector<rec::CaloHit> >(fCaloHitLabel);
315  if (!RecoHitHandle) {
316  throw cet::exception("anatest") << " No rec::CaloHit branch."
317  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
318  }
319 
320 
321  // Get handle for CaloClusters; also Assn for matching tracks
322  auto RecoClusterHandle = e.getHandle< std::vector<rec::Cluster> >(fClusterLabel);
323  if (!RecoClusterHandle) {
324  throw cet::exception("anatest") << " No rec::Cluster branch."
325  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
326  }
327  //art::FindManyP<rec::Track, rec::TrackEnd>* findManyCALTrackEnd = NULL;
328  //findManyCALTrackEnd = new art::FindManyP<rec::Track, rec::TrackEnd>
329  // (RecoClusterHandle,e,fECALAssnLabel);
330 
331 
332 
333  // ============= Pull art handles =========================================
334  fRun = e.run();
335  fSubRun = e.subRun();
336  fEvent = e.id().event();
337 
338  for (size_t imchl = 0; imchl < mcthandlelist.size(); ++imchl) {
339  for ( auto const& mct : (*mcthandlelist.at(imchl)) ) {
340  if (mct.NeutrinoSet()) {
341  simb::MCNeutrino nuw = mct.GetNeutrino();
342  fNeutrinoType.push_back(nuw.Nu().PdgCode());
343  fInteractionType.push_back(nuw.InteractionType());
344  }
345  }
346  }
347 
348  // Need a non-constant backtracker instance, for now.
349  cheat::BackTrackerCore const* const_bt = gar::providerFrom<cheat::BackTracker>();
350  cheat::BackTrackerCore* bt = const_cast<cheat::BackTrackerCore*>(const_bt);
351 
352  bool const dumpMCP = false;
353  sim::ParticleList* partList = bt->GetParticleList();
354  int nMotherless = 0;
355  for ( auto const& mcp : *MCPHandle ) {
356  if (mcp.Mother() > 0) {
357  simb::MCParticle* TPCeve = bt->FindTPCEve(mcp.TrackId());
358  if ( TPCeve->TrackId() == mcp.TrackId() ) ++nMotherless;
359  }
360  if (dumpMCP) {
361  std::cout << "TrackID: " << mcp.TrackId() << " is PDG: " << mcp.PdgCode() <<
362  " with mother " << mcp.Mother() << " produced by process " << mcp.Process()
363  << std::endl;
364  }
365  }
366  if (dumpMCP) {
367  std::cout << "Number of children: " << partList->size() << std::endl;
368  std::cout << "Number of motherless children: " << nMotherless << std::endl;
369  }
370 
371 
372 
373 
374  std::vector<art::Ptr<rec::Hit>> allhits;
375  art::PtrMaker<rec::Hit> makeHitPtr(e,HitHandle.id());
376  for (size_t iHit=0; iHit<HitHandle->size(); ++iHit) {
377  art::Ptr<rec::Hit> aPtr = makeHitPtr(iHit);
378  allhits.push_back(aPtr);
379  }
380  for (simb::MCParticle mcp : *MCPHandle) {
381  if ( mcp.Mother() > 0 ) continue;
382  if ( abs(mcp.PdgCode()) == 13 ) bt->ParticleToHits(&mcp,allhits);
383  }
384 
385 
386 
387 
388 
389 /* for ( rec::Cluster cluster : *RecoClusterHandle ) {
390  std::vector<std::pair<simb::MCParticle*,float>> whatMatches;
391  whatMatches = bt->ClusterToMCParticles(&cluster);
392  std::cout << "\nCluster No. " << cluster.getIDNumber() << " is made of MCParticles: " << std::endl;
393  for (auto itr = whatMatches.begin(); itr!=whatMatches.end(); ++itr) {
394  std::cout << "G4 track number " << itr->first->TrackId() << "\thas PDG code " <<
395  itr->first->PdgCode() << "\tand its mother is G4 track " << itr->first->Mother()
396  << "\tand energy fraction " << 100*(itr->second) << "%\n";
397  }
398  }
399 
400  std::vector<art::Ptr<rec::Cluster>> clusterCol;
401  art::PtrMaker<rec::Cluster> makeClusterPtr(e,RecoClusterHandle.id());
402  for (size_t iCluster=0; iCluster<RecoClusterHandle->size(); ++iCluster ) {
403  art::Ptr<rec::Cluster> aPtr = makeClusterPtr(iCluster);
404  clusterCol.push_back(aPtr);
405  }
406  for ( simb::MCParticle mcp : *MCPHandle ) {
407  if (mcp.Mother() == 0) {
408  std::cout << "Particle PDG: " << mcp.PdgCode() << " matches to\n";
409  std::vector<art::Ptr<rec::Cluster>> clusterList =
410  bt->MCParticleToClusters(&mcp, clusterCol);
411  for (art::Ptr<rec::Cluster> iCluster : clusterList)
412  std::cout << iCluster->getIDNumber() << std::endl;
413  }
414  } */
415 
416 
417 
418 
419 
420  return;
421 }
422 
423 
424 
def analyze(root, level, gtrees, gbranches, doprint)
Definition: rootstat.py:69
int PdgCode() const
Definition: MCParticle.h:212
std::string fRawCaloHitLabel
Int_t fSubRun
number of the sub-run being processed
Int_t fEvent
number of the event being processed
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
std::string fClusterLabel
std::string fTrackLabel
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
virtual void beginJob() override
std::string fVertexLabel
Particle class.
std::string fECALAssnLabel
int TrackId() const
Definition: MCParticle.h:210
int InteractionType() const
Definition: MCNeutrino.h:150
T abs(T value)
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
bt
Definition: tracks.py:83
void beginJob()
Definition: Breakpoints.cc:14
std::string fHitLabel
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const
Definition: DataViewImpl.h:479
T get(std::string const &key) const
Definition: ParameterSet.h:271
std::string fTPCClusterLabel
anatest(fhicl::ParameterSet const &p)
std::string fCaloHitLabel
p
Definition: test.py:223
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
Definition of basic calo raw digits.
sim::ParticleList * GetParticleList()
void analyze(art::Event const &e) override
RunNumber_t run() const
Definition: DataViewImpl.cc:71
std::vector< Float_t > fQ2
std::vector< Int_t > fInteractionType
std::vector< std::string > fGeneratorLabels
General GArSoft Utilities.
Int_t fRun
number of the run being processed
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
EventNumber_t event() const
Definition: EventID.h:116
std::string fGeantLabel
Event generator information.
Definition: MCNeutrino.h:18
double ItsInTulsa[3]
EventID id() const
Definition: Event.cc:34
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void FillVectors(art::Event const &e)
QTextStream & endl(QTextStream &s)
std::vector< Int_t > fNeutrinoType