All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
icaruscode/icaruscode/Analysis/HitEfficiencyAna_module.cc
Go to the documentation of this file.
1 // HitEfficiencyAna_module.cc
2 // A basic "skeleton" to read in art::Event records from a file,
3 // access their information, and do something with them.
4 
5 // See
6 // <https://cdcvs.fnal.gov/redmine/projects/larsoftsvn/wiki/Using_the_Framework>
7 // for a description of the ART classes used here.
8 
9 // Almost everything you see in the code below may have to be changed
10 // by you to suit your task. The example task is to make histograms
11 // and n-tuples related to dE/dx of particle tracks in the detector.
12 
13 // As you try to understand why things are done a certain way in this
14 // example ("What's all this stuff about 'auto const&'?"), it will help
15 // to read ADDITIONAL_NOTES.txt in the same directory as this file.
16 
17 #ifndef HitEfficiencyAna_module
18 #define HitEfficiencyAna_module
19 
20 // LArSoft includes
22 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
28 
29 //#include "cetlib/search_path.h"
30 #include "cetlib/cpu_timer.h"
34 
35 // Framework includes
36 #include "art/Framework/Core/EDAnalyzer.h"
37 #include "art/Framework/Principal/Event.h"
38 #include "art/Framework/Principal/Handle.h"
39 #include "art/Framework/Principal/View.h"
40 #include "art/Framework/Services/Registry/ServiceHandle.h"
41 #include "art_root_io/TFileService.h"
42 #include "art/Framework/Core/ModuleMacros.h"
43 #include "art/Utilities/make_tool.h"
44 #include "canvas/Utilities/InputTag.h"
45 #include "canvas/Persistency/Common/FindManyP.h"
46 #include "messagefacility/MessageLogger/MessageLogger.h"
47 #include "fhiclcpp/ParameterSet.h"
48 #include "cetlib_except/exception.h"
49 
50 #include "nusimdata/SimulationBase/MCParticle.h"
51 
53 
54 #include "TTree.h"
55 
56 // C++ Includes
57 #include <map>
58 #include <vector>
59 #include <tuple>
60 #include <algorithm>
61 #include <iostream>
62 #include <string>
63 #include <cmath>
64 
65 #include <iostream>
66 #include <fstream>
67 
69 {
70 //-----------------------------------------------------------------------
71 //-----------------------------------------------------------------------
72 // class definition
73 
74 class HitEfficiencyAna : public art::EDAnalyzer
75 {
76 public:
77 
78  // Standard constructor and destructor for an ART module.
79  explicit HitEfficiencyAna(fhicl::ParameterSet const& pset);
80  virtual ~HitEfficiencyAna();
81 
82  // This method is called once, at the start of the job. In this
83  // example, it will define the histograms and n-tuples we'll write.
84  void beginJob();
85  void endJob();
86 
87  // This method is called once, at the start of each run. It's a
88  // good place to read databases or files that may have
89  // run-dependent information.
90  void beginRun(const art::Run& run);
91 
92  // This method reads in any parameters from the .fcl files. This
93  // method is called 'reconfigure' because it might be called in the
94  // middle of a job; e.g., if the user changes parameter values in an
95  // interactive event display.
96  void reconfigure(fhicl::ParameterSet const& pset);
97 
98  // The analysis routine, called once per event.
99  void analyze (const art::Event& evt);
100 
101 private:
102 
103  // The variables that will go into the n-tuple.
104  int fEvent;
105  int fRun;
106  int fSubRun;
108 
109  // Introduce a temporary event counter to handle the current issue where all input
110  // files start with the same run/subrun/event sequence
112 
113  // Keep track of the hit histogramming tools here
114  std::vector<std::unique_ptr<IHitEfficiencyHistogramTool>> fHitHistogramToolVec;
115 
116  // And our tuple
117  mutable TTree* fTree;
118 
119  // Other variables that will be shared between different methods.
120  const geo::GeometryCore* fGeometry; // pointer to Geometry service
121  const lariov::DetPedestalProvider& fPedestalRetrievalAlg; ///< Keep track of an instance to the pedestal retrieval alg
122 }; // class HitEfficiencyAna
123 
124 
125 //-----------------------------------------------------------------------
126 //-----------------------------------------------------------------------
127 // class implementation
128 
129 //-----------------------------------------------------------------------
130 // Constructor
131 HitEfficiencyAna::HitEfficiencyAna(fhicl::ParameterSet const& parameterSet)
132  : EDAnalyzer(parameterSet),
133  fNumCalls(0),
134  fPedestalRetrievalAlg(*lar::providerFrom<lariov::DetPedestalService>())
135 
136 {
137  fGeometry = lar::providerFrom<geo::Geometry>();
138 
139  // Read in the parameters from the .fcl file.
140  this->reconfigure(parameterSet);
141 }
142 
143 //-----------------------------------------------------------------------
144 // Destructor
145 HitEfficiencyAna::~HitEfficiencyAna()
146 {}
147 
148 //-----------------------------------------------------------------------
149 void HitEfficiencyAna::beginJob()
150 {
151  // Get the detector length, to determine the maximum bin edge of one
152  // of the histograms.
153  //double detectorLength = fGeometry->DetLength();
154 
155  // Access ART's TFileService, which will handle creating and writing
156  // histograms and n-tuples for us.
157  art::ServiceHandle<art::TFileService> tfs;
158 
159  // First task is to create a TTree..
160  fTree = tfs->makeAndRegister<TTree>("HitEffic_t","Hit Efficiency Tuple");
161 
162  fTree->Branch("Run", &fRun, "Run/I");
163  fTree->Branch("SubRun", &fSubRun, "SubRun/I");
164  fTree->Branch("Event", &fEvent, "Event/I");
165 
166  // The arguments to 'make<whatever>' are the same as those passed
167  // to the 'whatever' constructor, provided 'whatever' is a ROOT
168  // class that TFileService recognizes.
169  for (auto& hitHistTool : fHitHistogramToolVec)
170  {
171  hitHistTool->initializeHists(tfs, "HitEffic");
172  hitHistTool->initializeTuple(fTree);
173  }
174 
175  // zero out the event counter
176  fNumEvents = 0;
177 }
178 
179 //-----------------------------------------------------------------------
180 void HitEfficiencyAna::beginRun(const art::Run& /*run*/)
181 {
182  // How to convert from number of electrons to GeV. The ultimate
183  // source of this conversion factor is
184  // ${LARSIM_DIR}/include/SimpleTypesAndConstants/PhysicalConstants.h.
185 // art::ServiceHandle<sim::LArG4Parameters> larParameters;
186 // fElectronsToGeV = 1./larParameters->GeVToElectrons();
187 }
188 
189 //-----------------------------------------------------------------------
190 void HitEfficiencyAna::reconfigure(fhicl::ParameterSet const& p)
191 {
192  // Read parameters from the .fcl file. The names in the arguments
193  // to p.get<TYPE> must match names in the .fcl file.
194  // Implement the tools for handling the responses
195  const std::vector<fhicl::ParameterSet>& hitHistogramToolVec = p.get<std::vector<fhicl::ParameterSet>>("HitEfficiencyHistogramToolList");
196 
197  for(auto& hitHistogramTool : hitHistogramToolVec)
198  fHitHistogramToolVec.push_back(art::make_tool<IHitEfficiencyHistogramTool>(hitHistogramTool));
199 
200  return;
201 }
202 
203 //-----------------------------------------------------------------------
204 void HitEfficiencyAna::analyze(const art::Event& event)
205 {
206  // Start by fetching some basic event information for our n-tuple.
207  fEvent = event.id().event();
208  fRun = ++fNumCalls; //event.run();
209  fSubRun = event.subRun();
210 
211  fNumEvents++;
212 
213  // Make a pass through all hits to make contrasting plots
214  std::cout << "-- Run: " << fRun << ", SubRun: " << fSubRun << ", Event: " << fEvent << " -------" << std::endl;
215  for(auto& hitHistTool : fHitHistogramToolVec) hitHistTool->fillHistograms(event);
216 
217  fTree->Fill();
218 
219  return;
220 }
221 
222 void HitEfficiencyAna::endJob()
223 {
224  // Make a call to normalize histograms if so desired
225  for(auto& hitHistTool : fHitHistogramToolVec) hitHistTool->endJob(fNumEvents);
226 
227  return;
228 }
229 
230 // This macro has to be defined for this module to be invoked from a
231 // .fcl file; see HitEfficiencyAna.fcl for more information.
232 DEFINE_ART_MODULE(HitEfficiencyAna)
233 
234 } // namespace HitEfficiencyAna
235 
236 #endif // HitEfficiencyAna_module
process_name opflashCryo1 flashfilter analyze
Utilities related to art service access.
T::provider_type const * providerFrom()
Returns a constant pointer to the provider of specified service.
Definition: ServiceUtil.h:77
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
for pfile in ack l reconfigure(.*) override"` do echo "checking $
Description of geometry of one entire detector.
Definition of data types for geometry description.
std::vector< std::unique_ptr< IHitEfficiencyHistogramTool > > fHitHistogramToolVec
const lariov::DetPedestalProvider & fPedestalRetrievalAlg
Keep track of an instance to the pedestal retrieval alg.
object containing MC truth information necessary for making RawDigits and doing back tracking ...
art::ServiceHandle< art::TFileService > tfs
TCEvent evt
Definition: DataStructs.cxx:8
art framework interface to geometry description
BEGIN_PROLOG could also be cout