ProtoDUNETruthBeamFilter_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Filter to selects events with a given beam particle.
4 // Owen Goodwin
5 // owen.goodwin@postgrad.manchester.ac.uk
6 //
7 ////////////////////////////////////////////////////////////////////////
8 
9 // Framework includes
14 #include "art_root_io/TFileService.h"
17 #include "fhiclcpp/ParameterSet.h"
20 
21 
22 
23 
24 // ROOT includes
25 #include "TTree.h"
26 #include "TFile.h"
27 #include "TString.h"
28 #include "TH1.h"
29 #include "TH2.h"
30 // C++ Includes
31 #include <fstream>
32 #include <string>
33 #include <sstream>
34 #include <cmath>
35 #include <algorithm>
36 #include <iostream>
37 #include <vector>
38 
39 
40 
41 namespace protoana{
42  class ProtoDUNETruthBeamFilter;
43 }
44 
46 public:
47 
48  explicit ProtoDUNETruthBeamFilter(fhicl::ParameterSet const& pset);
49  virtual ~ProtoDUNETruthBeamFilter();
50 
51  void beginJob();
52  bool filter(art::Event& evt);
53  void reconfigure(fhicl::ParameterSet const& pset);
54  void endJob();
55 
56 private:
57 
58  bool fDebug; /// Controls the verbosity
59 
60  std::vector<int> fPDG; /// List of particle PDGs we want to keep
61 
62 
63 
65  TH1D* fTotalEvents;
66 
67 
68 
69 };
70 
71 //-----------------------------------------------------------------------
73 : EDFilter(pset)
74 {
75  this->reconfigure(pset);
76 
77 }
78 
79 //-----------------------------------------------------------------------
81 
82 //-----------------------------------------------------------------------
84 
86  fSelectedEvents = tfs->make<TH1D>("fSelectedEvents", "Number of Selected Events", 3, 0, 3); //counts the number of selected events
87  fTotalEvents = tfs->make<TH1D>("fTotalEvents", "Total Events", 3, 0, 3); //counts the initial number of events in the unfiltered root input file
88 
89 }
90 
91 //-----------------------------------------------------------------------
93 
94 
95  fPDG = pset.get< std::vector<int> >("PDG");
96  fDebug = pset.get<bool>("IsVerbose");
97 
98 }
99 
100 //-----------------------------------------------------------------------
102 
103 
104 
105  if(fDebug) std::cout << "Reading Event" << std::endl;
106 
107 
108  bool found = false;
109  // Access truth info
110  std::vector< art::Ptr<simb::MCTruth> > mclist;
111  auto mctruthhandle = evt.getHandle< std::vector<simb::MCTruth> >("generator");
112  if (mctruthhandle){
113  art::fill_ptr_vector(mclist,mctruthhandle);
114  }
115  else{
116  mf::LogError("protoana::ProtoDUNETruthBeamParticle::analyze") << "Requested protoDUNE beam generator information does not exist!";
117  return found;
118  }
119  fTotalEvents->Fill(1); //count total events
120 
121  art::Ptr<simb::MCTruth> mctruth;
122  mctruth = mclist[0];
123 
124 
125  for(int i = 0; i < mctruth->NParticles(); ++i){
126  const simb::MCParticle& part(mctruth->GetParticle(i));
127 
128  bool isgoodparticle = (part.Process() == "primary" && mctruth->Origin()==4); //primary particle and beam origin
129  if(!isgoodparticle) continue;
130  // Select partciles with the chosen pdg
131  for(unsigned int j = 0; j < fPDG.size(); j++){
132  if(part.PdgCode() == fPDG[j]){
133  found = true;
134  if(fDebug) std::cout << "this is a selected event with PDG "<< part.PdgCode() << std::endl;
135  fSelectedEvents->Fill(1); //count selected events
136  break;
137  }
138  }
139 
140  }
141 
142  return found;
143 
144 }
145 
146 
148 
150 
int PdgCode() const
Definition: MCParticle.h:212
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
simb::Origin_t Origin() const
Definition: MCTruth.h:74
int NParticles() const
Definition: MCTruth.h:75
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Particle class.
void reconfigure(fhicl::ParameterSet const &pset)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
T get(std::string const &key) const
Definition: ParameterSet.h:271
const simb::MCParticle & GetParticle(int i) const
Definition: MCTruth.h:76
TH1D * fSelectedEvents
List of particle PDGs we want to keep.
EDFilter(fhicl::ParameterSet const &pset)
Definition: EDFilter.h:21
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
std::vector< int > fPDG
Controls the verbosity.
QTextStream & endl(QTextStream &s)
ProtoDUNETruthBeamFilter(fhicl::ParameterSet const &pset)