All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackHitAna_module.cc
Go to the documentation of this file.
1 // TrackHitAna_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 TrackHitAna_module
18 #define TrackHitAna_module
19 
20 // LArSoft includes
22 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
25 #include "lardataobj/RawData/raw.h"
34 
35 //#include "cetlib/search_path.h"
36 #include "cetlib/cpu_timer.h"
40 
41 // Framework includes
42 #include "art/Framework/Core/EDAnalyzer.h"
43 #include "art/Framework/Principal/Event.h"
44 #include "art/Framework/Principal/Handle.h"
45 #include "art/Framework/Principal/View.h"
46 #include "art/Framework/Services/Registry/ServiceHandle.h"
47 #include "art_root_io/TFileService.h"
48 #include "art/Framework/Core/ModuleMacros.h"
49 #include "art/Utilities/make_tool.h"
50 #include "canvas/Utilities/InputTag.h"
51 #include "canvas/Persistency/Common/FindManyP.h"
52 #include "messagefacility/MessageLogger/MessageLogger.h"
53 #include "fhiclcpp/ParameterSet.h"
54 #include "cetlib_except/exception.h"
55 
57 
58 // C++ Includes
59 #include <map>
60 #include <vector>
61 #include <tuple>
62 #include <algorithm>
63 #include <iostream>
64 #include <string>
65 #include <cmath>
66 
67 #include <iostream>
68 #include <fstream>
69 
70 namespace TrackHitAna
71 {
72 //-----------------------------------------------------------------------
73 //-----------------------------------------------------------------------
74 // class definition
75 
76 class TrackHitAna : public art::EDAnalyzer
77 {
78 public:
79 
80  // Standard constructor and destructor for an ART module.
81  explicit TrackHitAna(fhicl::ParameterSet const& pset);
82  virtual ~TrackHitAna();
83 
84  // This method is called once, at the start of the job. In this
85  // example, it will define the histograms and n-tuples we'll write.
86  void beginJob();
87  void endJob();
88 
89  // This method is called once, at the start of each run. It's a
90  // good place to read databases or files that may have
91  // run-dependent information.
92  void beginRun(const art::Run& run);
93 
94  // This method reads in any parameters from the .fcl files. This
95  // method is called 'reconfigure' because it might be called in the
96  // middle of a job; e.g., if the user changes parameter values in an
97  // interactive event display.
98  void reconfigure(fhicl::ParameterSet const& pset);
99 
100  // The analysis routine, called once per event.
101  void analyze (const art::Event& evt);
102 
103 private:
104 
105  // Traverse PFParticle hierarchy
106  int traversePFParticleHierarchy(art::Handle<std::vector<recob::PFParticle>>&,
107  size_t,
108  const art::FindManyP<recob::Track>&,
109  const art::FindManyP<recob::Vertex>&,
110  int&,
111  int&) const;
112 
113  // The following typedefs will, obviously, be useful
114  double length(const recob::Track* track);
115 
116  double projectedLength(const recob::Track* track);
117 
118  // The parameters we'll read from the .fcl file.
119  std::vector<art::InputTag> fHitProducerLabelVec;
120  std::vector<art::InputTag> fWireProducerLabelVec;
121  std::vector<art::InputTag> fPFParticleProducerLabelVec;
122  std::vector<art::InputTag> fTrackProducerLabelVec;
123 
124  // The variables that will go into the n-tuple.
125  int fEvent;
126  int fRun;
127  int fSubRun;
129 
130  // Keep track of the hit histogramming tools here
131  std::vector<std::unique_ptr<IHitHistogramTool>> fHitHistogramToolVec;
132 
133  std::vector<std::vector<double>> fChannelPedVec;
134 
135  // Other variables that will be shared between different methods.
136  const geo::GeometryCore* fGeometry; // pointer to Geometry service
137  const lariov::DetPedestalProvider& fPedestalRetrievalAlg; ///< Keep track of an instance to the pedestal retrieval alg
138 }; // class TrackHitAna
139 
140 
141 //-----------------------------------------------------------------------
142 //-----------------------------------------------------------------------
143 // class implementation
144 
145 //-----------------------------------------------------------------------
146 // Constructor
147 TrackHitAna::TrackHitAna(fhicl::ParameterSet const& parameterSet)
148  : EDAnalyzer(parameterSet),
149  fPedestalRetrievalAlg(*lar::providerFrom<lariov::DetPedestalService>())
150 
151 {
152  fGeometry = lar::providerFrom<geo::Geometry>();
153 
154  // Read in the parameters from the .fcl file.
155  this->reconfigure(parameterSet);
156 }
157 
158 //-----------------------------------------------------------------------
159 // Destructor
160 TrackHitAna::~TrackHitAna()
161 {}
162 
163 //-----------------------------------------------------------------------
164 void TrackHitAna::beginJob()
165 {
166  // Get the detector length, to determine the maximum bin edge of one
167  // of the histograms.
168  //double detectorLength = fGeometry->DetLength();
169 
170  // Access ART's TFileService, which will handle creating and writing
171  // histograms and n-tuples for us.
172  art::ServiceHandle<art::TFileService> tfs;
173 
174  // The arguments to 'make<whatever>' are the same as those passed
175  // to the 'whatever' constructor, provided 'whatever' is a ROOT
176  // class that TFileService recognizes.
177  for (auto& hitHistTool : fHitHistogramToolVec) hitHistTool->initializeHists(tfs, "AllHits");
178 
179  // zero out the event counter
180  fNumEvents = 0;
181 }
182 
183 //-----------------------------------------------------------------------
184 void TrackHitAna::beginRun(const art::Run& /*run*/)
185 {
186  // How to convert from number of electrons to GeV. The ultimate
187  // source of this conversion factor is
188  // ${LARSIM_DIR}/include/SimpleTypesAndConstants/PhysicalConstants.h.
189 // art::ServiceHandle<sim::LArG4Parameters> larParameters;
190 // fElectronsToGeV = 1./larParameters->GeVToElectrons();
191 }
192 
193 //-----------------------------------------------------------------------
194 void TrackHitAna::reconfigure(fhicl::ParameterSet const& p)
195 {
196  // Read parameters from the .fcl file. The names in the arguments
197  // to p.get<TYPE> must match names in the .fcl file.
198  fHitProducerLabelVec = p.get< std::vector<art::InputTag> >("HitModuleLabel", std::vector<art::InputTag>() = {"gauss"});
199  fPFParticleProducerLabelVec = p.get< std::vector<art::InputTag> >("PFParticleProducerLabel", std::vector<art::InputTag>() = {"cluster3d"});
200  fTrackProducerLabelVec = p.get< std::vector<art::InputTag> >("TrackProducerLabel", std::vector<art::InputTag>() = {"trackkalmanhit"});
201  fWireProducerLabelVec = p.get< std::vector<art::InputTag> >("WireProducerLabel", std::vector<art::InputTag>() = {"caldata"});
202 
203  // Implement the tools for handling the responses
204  const std::vector<fhicl::ParameterSet>& hitHistogramToolVec = p.get<std::vector<fhicl::ParameterSet>>("HitHistogramToolList");
205 
206  for(auto& hitHistogramTool : hitHistogramToolVec)
207  fHitHistogramToolVec.push_back(art::make_tool<IHitHistogramTool>(hitHistogramTool));
208 
209  return;
210 }
211 
212 //-----------------------------------------------------------------------
213 void TrackHitAna::analyze(const art::Event& event)
214 {
215  // Start by fetching some basic event information for our n-tuple.
216  fEvent = event.id().event();
217  fRun = event.run();
218  fSubRun = event.subRun();
219 
220  fNumEvents++;
221 
222  for(const auto& hitLabel : fHitProducerLabelVec)
223  {
224  // Make a pass through all hits to make contrasting plots
225  art::Handle< std::vector<recob::Hit> > hitHandle;
226  event.getByLabel(hitLabel, hitHandle);
227 
228  if (hitHandle.isValid())
229  {
231  art::fill_ptr_vector(allHitVec, hitHandle);
232 
233  for(auto& hitHistTool : fHitHistogramToolVec) hitHistTool->fillHistograms(allHitVec);
234  }
235  }
236 
237  return;
238 }
239 
240 void TrackHitAna::endJob()
241 {
242  // Make a call to normalize histograms if so desired
243  for(auto& hitHistTool : fHitHistogramToolVec) hitHistTool->endJob(fNumEvents);
244 
245  return;
246 }
247 
248 // Length of reconstructed track.
249 //----------------------------------------------------------------------------
250 double TrackHitAna::length(const recob::Track* track)
251 {
252  double result(0.);
253 /*
254  TVector3 disp(track->LocationAtPoint(0));
255  TVector3 lastPoint(track->LocationAtPoint(0));
256  TVector3 lastDir(0.,0.,0.);
257  int n(track->NumberTrajectoryPoints());
258 
259  for(int i = 1; i < n; ++i)
260  {
261  const TVector3& pos = track->LocationAtPoint(i);
262 
263  TVector3 trajDir = pos - lastPoint;
264 
265  if (trajDir.Mag2()) trajDir.SetMag(1.);
266 
267 // if (lastDir.Dot(trajDir) >= 0.)
268 // {
269  disp -= pos;
270  result += disp.Mag();
271  disp = pos;
272 // }
273 
274  lastPoint = pos;
275  lastDir = trajDir;
276  }
277 */
278  return result;
279 }
280 
281 // Length of reconstructed track.
282 //----------------------------------------------------------------------------
283 double TrackHitAna::projectedLength(const recob::Track* track)
284 {
285  double result(0.);
286 /*
287  TVector3 lastPoint(track->LocationAtPoint(0));
288  TVector3 lastDir(track->DirectionAtPoint(0));
289  int n(track->NumberTrajectoryPoints());
290 
291  for(int i = 1; i < n; ++i)
292  {
293  const TVector3& newPoint = track->LocationAtPoint(i);
294 
295  TVector3 lastToNewPoint = newPoint - lastPoint;
296  double arcLenToDoca = lastDir.Dot(lastToNewPoint);
297 
298  result += arcLenToDoca;
299  lastPoint = lastPoint + arcLenToDoca * lastDir;
300  lastDir = track->DirectionAtPoint(i);
301  }
302 */
303  return result;
304 }
305 
306 int TrackHitAna::traversePFParticleHierarchy(art::Handle<std::vector<recob::PFParticle>>& pfParticleHandle,
307  size_t pfParticleIdx,
308  const art::FindManyP<recob::Track>& trackAssns,
309  const art::FindManyP<recob::Vertex>& vertexAssns,
310  int& nTracks,
311  int& nVertices) const
312 {
313  // So far no daughters...
314  int nDaughters(0);
315 /*
316  // Get pointer to PFParticle
317  art::Ptr<recob::PFParticle> pfParticle(pfParticleHandle,pfParticleIdx);
318 
319  // Recover tracks/vertices associated to this PFParticle
320  std::vector<art::Ptr<recob::Track>> pfPartTrackVec = trackAssns.at(pfParticle.key());
321  std::vector<art::Ptr<recob::Vertex>> pfPartVertexVec = vertexAssns.at(pfParticle.key());
322 
323  nTracks += pfPartTrackVec.size();
324  nVertices += pfPartVertexVec.size();
325  nDaughters += pfParticle->Daughters().size();
326 
327  for(auto& daughterIdx : pfParticle->Daughters())
328  {
329  nDaughters += traversePFParticleHierarchy(pfParticleHandle, daughterIdx, trackAssns, vertexAssns, nTracks, nVertices);
330  }
331 */
332  return nDaughters;
333 }
334 
335 // This macro has to be defined for this module to be invoked from a
336 // .fcl file; see TrackHitAna.fcl for more information.
337 DEFINE_ART_MODULE(TrackHitAna)
338 
339 } // namespace TrackHitAna
340 
341 #endif // TrackHitAna_module
process_name opflashCryo1 flashfilter analyze
Utilities related to art service access.
std::vector< std::unique_ptr< IHitHistogramTool > > fHitHistogramToolVec
std::vector< art::InputTag > fPFParticleProducerLabelVec
T::provider_type const * providerFrom()
Returns a constant pointer to the provider of specified service.
Definition: ServiceUtil.h:77
std::vector< art::InputTag > fHitProducerLabelVec
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
Definition of basic raw digits.
for pfile in ack l reconfigure(.*) override"` do echo "checking $
process_name use argoneut_mc_hitfinder track
std::vector< std::vector< double > > fChannelPedVec
void beginRun(const art::Run &run)
Collect all the RawData header files together.
std::vector< art::InputTag > fTrackProducerLabelVec
Description of geometry of one entire detector.
Declaration of cluster object.
Definition of data types for geometry description.
Provides recob::Track data product.
int traversePFParticleHierarchy(art::Handle< std::vector< recob::PFParticle >> &, size_t, const art::FindManyP< recob::Track > &, const art::FindManyP< recob::Vertex > &, int &, int &) const
void reconfigure(fhicl::ParameterSet const &pset)
double length(const recob::Track *track)
std::vector< art::InputTag > fWireProducerLabelVec
TrackHitAna(fhicl::ParameterSet const &pset)
std::vector< art::Ptr< recob::Hit >> HitPtrVec
Interface for filling histograms.
art::ServiceHandle< art::TFileService > tfs
double projectedLength(const recob::Track *track)
TCEvent evt
Definition: DataStructs.cxx:8
const geo::GeometryCore * fGeometry
void analyze(const art::Event &evt)
const lariov::DetPedestalProvider & fPedestalRetrievalAlg
Keep track of an instance to the pedestal retrieval alg.
art framework interface to geometry description
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track: