MDMAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: MDMAna
3 // Plugin Type: analyzer (art v3_04_00)
4 // File: MDMAna_module.cc
5 //
6 // Generated at Wed Feb 19 22:04:32 2020 by Tingjun Yang using cetskelgen
7 // from cetlib version v3_09_00.
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "fhiclcpp/ParameterSet.h"
19 #include "art_root_io/TFileService.h"
20 
25 
26 #include "TTree.h"
27 
28 #include <vector>
29 
30 namespace pdsp {
31  class MDMAna;
32 }
33 
34 
35 class pdsp::MDMAna : public art::EDAnalyzer {
36 public:
37  explicit MDMAna(fhicl::ParameterSet const& p);
38  // The compiler-generated destructor is fine for non-base
39  // classes without bare pointers or other resource use.
40 
41  // Plugins should not be copied or assigned.
42  MDMAna(MDMAna const&) = delete;
43  MDMAna(MDMAna&&) = delete;
44  MDMAna& operator=(MDMAna const&) = delete;
45  MDMAna& operator=(MDMAna&&) = delete;
46 
47  // Required functions.
48  void analyze(art::Event const& e) override;
49 
50  // Selected optional functions.
51  void beginJob() override;
52 
53 private:
54 
55  // Declare member data here.
56  TTree *mdmtree;
57  int run;
58  int subrun;
59  int event;
60  std::vector<int> trackid;
61  std::vector<double> tracklen;
62  std::vector<int> trackorg;
63  std::vector<int> trackpdg;
64 
65 };
66 
67 
69  : EDAnalyzer{p} // ,
70  // More initializers here.
71 {
72  // Call appropriate consumes<>() for any products to be retrieved by this module.
73 }
74 
76 {
77  run = e.run();
78  subrun = e.subRun();
79  event = e.id().event();
80  trackid.clear();
81  tracklen.clear();
82  trackorg.clear();
83  trackpdg.clear();
84 
85  //Services
88 
89  // Reconstruciton information
90  std::vector < art::Ptr < recob::Track > > trackList;
91  auto trackListHandle = e.getHandle < std::vector < recob::Track > >("pandoraTrack");
92  if (trackListHandle) {
93  art::fill_ptr_vector(trackList, trackListHandle);
94  }
95  else return;
96 
97  //Get hits associated with track
98  art::FindManyP < recob::Hit > hitsFromTrack(trackListHandle, e, "pandoraTrack");
99 
100  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService>()->DataFor(e);
101  for (auto const& track : trackList){
102  int this_trackid = track.key();
103  double this_tracklen = track->Length();
104  int this_trackorg = -1;
105  int this_trackpdg = 0;
106 
107  auto const & allHits = hitsFromTrack.at(track.key());
108 
109  if (!e.isRealData()){
110  // Find true particle for reconstructed track
111  int TrackID = 0;
112  std::map<int,double> trkide;
113  for(size_t h = 0; h < allHits.size(); ++h){
114  art::Ptr<recob::Hit> hit = allHits[h];
115  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToTrackIDEs(clockData, hit);
116  for(size_t e = 0; e < TrackIDs.size(); ++e){
117  trkide[TrackIDs[e].trackID] += TrackIDs[e].energy;
118  }
119  }
120  // Work out which IDE despoited the most charge in the hit if there was more than one.
121  double maxe = -1;
122  double tote = 0;
123  for (std::map<int,double>::iterator ii = trkide.begin(); ii!=trkide.end(); ++ii){
124  tote += ii->second;
125  if ((ii->second)>maxe){
126  maxe = ii->second;
127  TrackID = ii->first;
128  }
129  }
130  // Now have trackID, so get PdG code and T0 etc.
131  if (TrackID){
132  const simb::MCParticle *particle = pi_serv->TrackIdToParticle_P(TrackID);
133  if (particle){
134  this_trackpdg = particle->PdgCode();
135  auto & truth = pi_serv->ParticleToMCTruth_P(particle);
136  this_trackorg = truth->Origin();
137  }
138  }
139  }
140  trackid.push_back(this_trackid);
141  tracklen.push_back(this_tracklen);
142  trackorg.push_back(this_trackorg);
143  trackpdg.push_back(this_trackpdg);
144  }
145  if (!trackid.empty()) mdmtree->Fill();
146 }
147 
149 {
150  art::ServiceHandle<art::TFileService> fileServiceHandle;
151  mdmtree = fileServiceHandle->make<TTree>("mdmtree", "MDM info");
152  mdmtree->Branch("run", &run, "run/I");
153  mdmtree->Branch("subrun", &subrun, "subrun/I");
154  mdmtree->Branch("event", &event, "event/I");
155  mdmtree->Branch("trackid", &trackid);
156  mdmtree->Branch("tracklen", &tracklen);
157  mdmtree->Branch("trackorg", &trackorg);
158  mdmtree->Branch("trackpdg", &trackpdg);
159 
160 }
161 
intermediate_table::iterator iterator
int PdgCode() const
Definition: MCParticle.h:212
TTree * mdmtree
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
std::vector< int > trackpdg
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::vector< TrackID > TrackIDs
const simb::MCParticle * TrackIdToParticle_P(int id) const
simb::Origin_t Origin() const
Definition: MCTruth.h:74
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
MDMAna(fhicl::ParameterSet const &p)
const art::Ptr< simb::MCTruth > & ParticleToMCTruth_P(const simb::MCParticle *p) const
std::vector< double > tracklen
void beginJob() override
bool isRealData() const
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
p
Definition: test.py:223
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
RunNumber_t run() const
Definition: DataViewImpl.cc:71
std::vector< int > trackorg
Detector simulation of raw signals on wires.
MDMAna & operator=(MDMAna const &)=delete
void analyze(art::Event const &e) override
std::vector< int > trackid
Provides recob::Track data product.
EventNumber_t event() const
Definition: EventID.h:116
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
EventID id() const
Definition: Event.cc:34
Event finding and building.