All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FinalStateParticleFilter_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // FinalStateParticleFilter class:
4 // Algoritm to produce a filtered event file having
5 // events with user-defined final state particles
6 //
7 // saima@ksu.edu
8 //
9 ////////////////////////////////////////////////////////////////////////
10 
11 //Framework Includes
12 #include "art/Framework/Core/EDFilter.h"
13 #include "art/Framework/Core/ModuleMacros.h"
14 #include "art/Framework/Principal/Event.h"
15 #include "fhiclcpp/ParameterSet.h"
16 #include "art/Framework/Principal/Handle.h"
17 #include "canvas/Persistency/Common/Ptr.h"
18 #include "art/Framework/Services/Registry/ServiceHandle.h"
19 #include "art_root_io/TFileService.h"
20 
21 //Larsoft Includes
22 #include "nusimdata/SimulationBase/MCParticle.h"
23 #include "nusimdata/SimulationBase/MCTruth.h"
24 
25 // ROOT includes
26 #include "TH1.h"
27 
28 namespace filt {
29 
30  class FinalStateParticleFilter : public art::EDFilter {
31 
32  public:
33 
34  explicit FinalStateParticleFilter(fhicl::ParameterSet const& );
35 
36  bool filter(art::Event& evt);
37  void beginJob();
38 
39 
40  private:
41 
42  std::string fGenieModuleLabel;
43  std::vector<int> fPDG;
44  std::vector<int> fStatusCode;
46  TH1D* fTotalEvents;
47 
48  bool isSubset(std::vector<int> const& a, std::vector<int> const& b) const;
49 
50  }; // class FinalStateParticleFilter
51 
52 }
53 
54 namespace filt{
55 
56  //-------------------------------------------------
57  FinalStateParticleFilter::FinalStateParticleFilter(fhicl::ParameterSet const & pset)
58  : EDFilter{pset}
59  {
60  fGenieModuleLabel = pset.get< std::string >("GenieModuleLabel");
61  fPDG = pset.get< std::vector<int> >("PDG");
62  }
63 
64  //-------------------------------------------------
66  {
67  art::ServiceHandle<art::TFileService const> tfs;
68  fSelectedEvents = tfs->make<TH1D>("fSelectedEvents", "Number of Selected Events", 3, 0, 3); //counts the number of selected events
69  fTotalEvents = tfs->make<TH1D>("fTotalEvents", "Total Events", 3, 0, 3); //counts the initial number of events in the unfiltered root input file
70  }
71 
72  //-------------------------------------------------
74  {
75 
76  //const TDatabasePDG* databasePDG = TDatabasePDG::Instance();
77 
78  art::Handle< std::vector<simb::MCTruth> > mclist;
79  evt.getByLabel(fGenieModuleLabel,mclist);
80  art::Ptr<simb::MCTruth> mc(mclist,0);
81 
82  fTotalEvents->Fill(1);
83 
84  std::vector<int> finalstateparticles;
85 
86  //get a vector of final state particles
87  for(int i = 0; i < mc->NParticles(); ++i){
88  simb::MCParticle part(mc->GetParticle(i));
89  if(part.StatusCode()== 1)
90  finalstateparticles.push_back(part.PdgCode());
91  }
92 
93  if(isSubset(fPDG, finalstateparticles)){
94  fSelectedEvents->Fill(1);
95  std::cout << "this is a selected event" << std::endl;
96  }
97 
98  return isSubset(fPDG, finalstateparticles); // returns true if the user-defined fPDG exist(s) in the final state particles
99 
100  } // bool
101  //} // namespace
102 
103 //------------------------------------------------
104 
105 bool FinalStateParticleFilter::isSubset(std::vector<int> const& a, std::vector<int> const& b) const
106 {
107  for (auto const a_int : a) {
108  bool found = false;
109  for (auto const b_int : b) {
110  if (a_int == b_int){
111  found = true;
112  break;
113  }
114  }
115 
116  if (!found){
117  return false;
118  }
119  }
120  return true;
121 }
122 }
123 
124 //--------------------------------------------------
125 
126 namespace filt {
127 
128  DEFINE_ART_MODULE(FinalStateParticleFilter)
129 
130 } //namespace filt
createEngine fPDG
process_name gaushit a
FinalStateParticleFilter(fhicl::ParameterSet const &)
then filt
bool isSubset(std::vector< int > const &a, std::vector< int > const &b) const
art::ServiceHandle< art::TFileService > tfs
TCEvent evt
Definition: DataStructs.cxx:8
BEGIN_PROLOG could also be cout