LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
TCShowerAnalysis_module.cc
Go to the documentation of this file.
1 // -------------------------------------------------
2 // tcshower analysis tree
3 //
4 // Author: Rory Fitzpatrick (roryfitz@umich.edu)
5 // Created: 7/16/18
6 // -------------------------------------------------
7 
8 // Framework includes
16 #include "fhiclcpp/ParameterSet.h"
19 
37 
38 #include "TTree.h"
39 #include "TFile.h"
40 #include "TH1.h"
41 #include "TH3.h"
42 #include "TProfile.h"
43 #include "TProfile2D.h"
44 #include <TROOT.h>
45 #include <TStyle.h>
46 
47 const int kMaxShowers = 1000; //maximum number of showers
48 
49 namespace shower {
50 
52 
53  public:
54 
55  explicit TCShowerAnalysis(fhicl::ParameterSet const& pset);
56  virtual ~TCShowerAnalysis();
57 
58  void reconfigure(fhicl::ParameterSet const& pset);
59  void beginJob();
60  void analyze(const art::Event& evt);
61 
62  protected:
63 
64  void reset();
65 
66  void truthMatcher(std::vector<art::Ptr<recob::Hit>>all_hits, std::vector<art::Ptr<recob::Hit>> shower_hits, const simb::MCParticle *&MCparticle, double &Efrac, double &Ecomplet);
67 
68  private:
69 
70  TTree* fTree;
71  int run;
72  int subrun;
73  int event;
77  int nshws; //number of showers
78  int shwid[kMaxShowers]; //recob::Shower::ID()
79  float shwdcosx[kMaxShowers]; //shower direction cosin
82  float shwstartx[kMaxShowers]; //shower start position (cm)
85  double shwdedx[kMaxShowers][2]; //shower dE/dx of the initial track measured on the 3 plane (MeV/cm)
86  int shwbestplane[kMaxShowers]; //recommended plane for energy and dE/dx information
87 
90 
91  std::string fHitModuleLabel;
92  std::string fShowerModuleLabel;
93  std::string fGenieGenModuleLabel;
94  std::string fDigitModuleLabel;
95 
97 
98  }; // class TCShowerAnalysis
99 
100 } // shower
101 
102 // -------------------------------------------------
103 
105  EDAnalyzer(pset),
106  fHitModuleLabel (pset.get< std::string >("HitModuleLabel", "trajcluster" ) ),
107  fShowerModuleLabel (pset.get< std::string >("ShowerModuleLabel", "tcshower" ) ),
108  fGenieGenModuleLabel (pset.get< std::string >("GenieGenModuleLabel", "generator") ),
109  fDigitModuleLabel (pset.get< std::string >("DigitModuleLabel", "largeant") ),
110  fCalorimetryAlg (pset.get< fhicl::ParameterSet >("CalorimetryAlg") ) {
111  this->reconfigure(pset);
112 } // TCShowerTemplateMaker
113 
114 // -------------------------------------------------
115 
117 } // ~TCShowerTemplateMaker
118 
119 // -------------------------------------------------
120 
122 } // reconfigure
123 
124 // -------------------------------------------------
125 
127 
129 
130  fTree = tfs->make<TTree>("tcshowerana", "tcshowerana");
131  fTree->Branch("run",&run,"run/I");
132  fTree->Branch("subrun",&subrun,"subrun/I");
133  fTree->Branch("event",&event,"event/I");
134 
135  fTree->Branch("nuPDG_truth",&nuPDG_truth,"nuPDG_truth/I");
136  fTree->Branch("ccnc_truth",&ccnc_truth,"ccnc_truth/I");
137  fTree->Branch("mode_truth",&mode_truth,"mode_truth/I");
138 
139  fTree->Branch("nshws",&nshws,"nshws/I");
140  fTree->Branch("shwid",shwid,"shwid[nshws]/I");
141  fTree->Branch("shwdcosx",shwdcosx,"shwdcosx[nshws]/F");
142  fTree->Branch("shwdcosy",shwdcosy,"shwdcosy[nshws]/F");
143  fTree->Branch("shwdcosz",shwdcosz,"shwdcosz[nshws]/F");
144  fTree->Branch("shwstartx",shwstartx,"shwstartx[nshws]/F");
145  fTree->Branch("shwstarty",shwstarty,"shwstarty[nshws]/F");
146  fTree->Branch("shwstartz",shwstartz,"shwstartz[nshws]/F");
147  fTree->Branch("shwdedx",shwdedx,"shwdedx[nshws][2]/D");
148  fTree->Branch("shwbestplane",shwbestplane,"shwbestplane[nshws]/I");
149 
150  fTree->Branch("highestHitsPDG",&highestHitsPDG,"highestHitsPDG/I");
151  fTree->Branch("highestHitsFrac",&highestHitsFrac,"highestHitsFrac/D");
152 
153 } // beginJob
154 
155 // -------------------------------------------------
156 
158 
159  run = evt.run();
160  subrun = evt.subRun();
161  event = evt.id().event();
162 
163  art::Handle< std::vector<recob::Hit> > hitListHandle;
164  std::vector<art::Ptr<recob::Hit> > hitlist;
165  if (evt.getByLabel(fHitModuleLabel,hitListHandle))
166  art::fill_ptr_vector(hitlist, hitListHandle);
167 
169  std::vector<art::Ptr<sim::SimChannel> > simchanlist;
170  if (evt.getByLabel(fDigitModuleLabel,scListHandle))
171  art::fill_ptr_vector(simchanlist, scListHandle);
172 
173  art::Handle< std::vector<recob::Shower> > showerListHandle;
174  std::vector<art::Ptr<recob::Shower> > showerlist;
175  if (evt.getByLabel(fShowerModuleLabel,showerListHandle))
176  art::fill_ptr_vector(showerlist, showerListHandle);
177 
178  art::Handle< std::vector<simb::MCTruth> > mctruthListHandle;
179  std::vector<art::Ptr<simb::MCTruth> > mclist;
180  if (evt.getByLabel(fGenieGenModuleLabel,mctruthListHandle))
181  art::fill_ptr_vector(mclist, mctruthListHandle);
182 
183  art::FindManyP<recob::Hit> shwfm(showerListHandle, evt, fShowerModuleLabel);
184 
185  //shower information
186  if (showerListHandle.isValid()) {
187  nshws = showerlist.size();
188 
189  for (int i = 0; i < std::min(int(showerlist.size()),kMaxShowers); ++i) {
190  shwid[i] = showerlist[i]->ID();
191  shwdcosx[i] = showerlist[i]->Direction().X();
192  shwdcosy[i] = showerlist[i]->Direction().Y();
193  shwdcosz[i] = showerlist[i]->Direction().Z();
194  shwstartx[i] = showerlist[i]->ShowerStart().X();
195  shwstarty[i] = showerlist[i]->ShowerStart().Y();
196  shwstartz[i] = showerlist[i]->ShowerStart().Z();
197  for (size_t j = 0; j<(showerlist[i]->dEdx()).size(); ++j){
198  shwdedx[i][j] = showerlist[i]->dEdx()[j];
199  }
200  shwbestplane[i] = showerlist[i]->best_plane();
201  }
202 
203  } // shower info
204 
205 
206  if (mclist.size()) {
207  art::Ptr<simb::MCTruth> mctruth = mclist[0];
208  if (mctruth->NeutrinoSet()) {
209 
210  nuPDG_truth = mctruth->GetNeutrino().Nu().PdgCode();
211  ccnc_truth = mctruth->GetNeutrino().CCNC();
212  mode_truth = mctruth->GetNeutrino().Mode();
213 
214  if (showerlist.size()) { // only looks at the first shower since this is for tcshower
215  std::vector< art::Ptr<recob::Hit> > showerhits = shwfm.at(0);
216  // get shower truth info
217  const simb::MCParticle *particle;
218  double tmpEfrac = 0.0;
219  double tmpEcomplet = 0;
220  truthMatcher(hitlist, showerhits, particle, tmpEfrac, tmpEcomplet);
221  if (particle) {
222  std::cout << "shower pdg: "<< particle->PdgCode() << " efrac " << tmpEfrac << std::endl;
223 
224  highestHitsPDG = particle->PdgCode();
225  highestHitsFrac = tmpEfrac;
226  }
227  }
228  }
229  }
230 
231  fTree->Fill();
232 
233 } // analyze
234 
235 // -------------------------------------------------
236 
238 
239  run = -99999;
240  subrun = -99999;
241  event = -99999;
242 
243  nuPDG_truth = -99999;
244  ccnc_truth = -99999;
245  mode_truth = -99999;
246 
247  nshws = 0;
248  for (int i = 0; i < kMaxShowers; ++i){
249  shwid[i] = -99999;
250  shwdcosx[i] = -99999;
251  shwdcosy[i] = -99999;
252  shwdcosz[i] = -99999;
253  shwstartx[i] = -99999;
254  shwstarty[i] = -99999;
255  shwstartz[i] = -99999;
256  for (int j = 0; j < 2; ++j){
257  shwdedx[i][j] = -99999;
258  }
259  shwbestplane[i] = -99999;
260  }
261 
262  highestHitsPDG = -99999;
263  highestHitsFrac = -99999;
264 
265  return;
266 
267 } // reset
268 
269 // -------------------------------------------------
270 
271 void shower::TCShowerAnalysis::truthMatcher(std::vector<art::Ptr<recob::Hit>> all_hits, std::vector<art::Ptr<recob::Hit>> shower_hits, const simb::MCParticle *&MCparticle, double &Efrac, double &Ecomplet){
272 
273  MCparticle=0;
274  Efrac=1.0;
275  Ecomplet=0;
276 
279  std::map<int,double> trkID_E;
280  for(size_t j = 0; j < shower_hits.size(); ++j){
281  art::Ptr<recob::Hit> hit = shower_hits[j];
282  //For know let's use collection plane to look at the shower reconstruction
283  if( hit->View() != 1) continue;
284  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToEveTrackIDEs(hit);
285  for(size_t k = 0; k < TrackIDs.size(); k++){
286  if (trkID_E.find(std::abs(TrackIDs[k].trackID))==trkID_E.end()) trkID_E[std::abs(TrackIDs[k].trackID)] = 0;
287  trkID_E[std::abs(TrackIDs[k].trackID)] += TrackIDs[k].energy;
288  }
289  }
290  double max_E = -999.0;
291  double total_E = 0.0;
292  int TrackID = -999;
293  double partial_E=0.0;
294  //double noEM_E = 0.0; //non electromagnetic energy is defined as energy from charged pion and protons
295  if( !trkID_E.size() ) return; //Ghost shower???
296  for(std::map<int,double>::iterator ii = trkID_E.begin(); ii!=trkID_E.end(); ++ii){
297  total_E += ii->second;
298  if((ii->second)>max_E){
299  partial_E = ii->second;
300  max_E = ii->second;
301  TrackID = ii->first;
302  }
303  //int ID = ii->first;
304  // const simb::MCParticle *particle = pi_serv->TrackIDToParticle(ID);
305  //if( abs(particle->PdgCode()) == 211 || particle->PdgCode() == 2212 ){
306  //if( particle->PdgCode() != 22 && abs(particle->PdgCode()) != 11){
307  //noEM_E += ii->second;
308  //}
309 
310  }
311  MCparticle = pi_serv->TrackIdToParticle_P(TrackID);
312 
313  // Efrac = 1-(partial_E/total_E);
314  Efrac = partial_E/total_E;
315 
316  //completeness
317  double totenergy =0;
318  for(size_t k = 0; k < all_hits.size(); ++k){
319  art::Ptr<recob::Hit> hit = all_hits[k];
320  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToEveTrackIDEs(hit);
321  for(size_t l = 0; l < TrackIDs.size(); ++l){
322  if(std::abs(TrackIDs[l].trackID)==TrackID) {
323  totenergy += TrackIDs[l].energy;
324  }
325  }
326  }
327  Ecomplet = partial_E/totenergy;
328 
329 } // truthMatcher
330 
331 // -------------------------------------------------
332 
const simb::MCParticle * TrackIdToParticle_P(int const &id)
SubRunNumber_t subRun() const
Definition: Event.h:72
int PdgCode() const
Definition: MCParticle.h:216
int CCNC() const
Definition: MCNeutrino.h:152
double shwdedx[kMaxShowers][2]
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:74
TCShowerAnalysis(fhicl::ParameterSet const &pset)
intermediate_table::iterator iterator
std::vector< TrackID > TrackIDs
Declaration of signal hit object.
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:150
STL namespace.
calo::CalorimetryAlg fCalorimetryAlg
geo::View_t View() const
View for the plane of the hit.
Definition: Hit.h:233
object containing MC flux information
bool isValid() const
Definition: Handle.h:190
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.
parameter set interface
void analyze(const art::Event &evt)
void truthMatcher(std::vector< art::Ptr< recob::Hit >>all_hits, std::vector< art::Ptr< recob::Hit >> shower_hits, const simb::MCParticle *&MCparticle, double &Efrac, double &Ecomplet)
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
Declaration of cluster object.
const int kMaxShowers
Detector simulation of raw signals on wires.
T * make(ARGS...args) const
Utility object to perform functions of association.
const std::vector< sim::TrackIDE > HitToEveTrackIDEs(recob::Hit const &hit)
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
Int_t min
Definition: plot.C:26
object containing MC truth information necessary for making RawDigits and doing back tracking ...
void reconfigure(fhicl::ParameterSet const &pset)
EventNumber_t event() const
Definition: EventID.h:117
bool NeutrinoSet() const
Definition: MCTruth.h:75
TCEvent evt
Definition: DataStructs.cxx:5
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
RunNumber_t run() const
Definition: Event.h:77
int Mode() const
Definition: MCNeutrino.h:153
EventID id() const
Definition: Event.h:56
art framework interface to geometry description
Event finding and building.