-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTrackHitAna_module.cc
More file actions
executable file
·341 lines (275 loc) · 12.1 KB
/
TrackHitAna_module.cc
File metadata and controls
executable file
·341 lines (275 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// TrackHitAna_module.cc
// A basic "skeleton" to read in art::Event records from a file,
// access their information, and do something with them.
// See
// <https://cdcvs.fnal.gov/redmine/projects/larsoftsvn/wiki/Using_the_Framework>
// for a description of the ART classes used here.
// Almost everything you see in the code below may have to be changed
// by you to suit your task. The example task is to make histograms
// and n-tuples related to dE/dx of particle tracks in the detector.
// As you try to understand why things are done a certain way in this
// example ("What's all this stuff about 'auto const&'?"), it will help
// to read ADDITIONAL_NOTES.txt in the same directory as this file.
#ifndef TrackHitAna_module
#define TrackHitAna_module
// LArSoft includes
#include "larcore/Geometry/Geometry.h"
#include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
#include "larcoreobj/SimpleTypesAndConstants/geo_types.h"
#include "lardataobj/RawData/RawDigit.h"
#include "lardataobj/RawData/raw.h"
#include "lardataobj/RecoBase/Hit.h"
#include "lardataobj/RecoBase/Cluster.h"
#include "lardataobj/RecoBase/PFParticle.h"
#include "lardataobj/RecoBase/Track.h"
#include "lardataobj/RecoBase/Vertex.h"
#include "lardataobj/RecoBase/SpacePoint.h"
#include "larevt/CalibrationDBI/Interface/DetPedestalService.h"
#include "larevt/CalibrationDBI/Interface/DetPedestalProvider.h"
//#include "cetlib/search_path.h"
#include "cetlib/cpu_timer.h"
#include "lardata/Utilities/AssociationUtil.h"
#include "lardata/DetectorInfoServices/LArPropertiesService.h"
#include "lardata/DetectorInfoServices/DetectorClocksService.h"
// Framework includes
#include "art/Framework/Core/EDAnalyzer.h"
#include "art/Framework/Principal/Event.h"
#include "art/Framework/Principal/Handle.h"
#include "art/Framework/Principal/View.h"
#include "art/Framework/Services/Registry/ServiceHandle.h"
#include "art_root_io/TFileService.h"
#include "art/Framework/Core/ModuleMacros.h"
#include "art/Utilities/make_tool.h"
#include "canvas/Utilities/InputTag.h"
#include "canvas/Persistency/Common/FindManyP.h"
#include "messagefacility/MessageLogger/MessageLogger.h"
#include "fhiclcpp/ParameterSet.h"
#include "cetlib_except/exception.h"
#include "icaruscode/Analysis/tools/IHitHistogramTool.h"
// C++ Includes
#include <map>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <iostream>
#include <fstream>
namespace TrackHitAna
{
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// class definition
class TrackHitAna : public art::EDAnalyzer
{
public:
// Standard constructor and destructor for an ART module.
explicit TrackHitAna(fhicl::ParameterSet const& pset);
virtual ~TrackHitAna();
// This method is called once, at the start of the job. In this
// example, it will define the histograms and n-tuples we'll write.
void beginJob();
void endJob();
// This method is called once, at the start of each run. It's a
// good place to read databases or files that may have
// run-dependent information.
void beginRun(const art::Run& run);
// This method reads in any parameters from the .fcl files. This
// method is called 'reconfigure' because it might be called in the
// middle of a job; e.g., if the user changes parameter values in an
// interactive event display.
void reconfigure(fhicl::ParameterSet const& pset);
// The analysis routine, called once per event.
void analyze (const art::Event& evt);
private:
// Traverse PFParticle hierarchy
int traversePFParticleHierarchy(art::Handle<std::vector<recob::PFParticle>>&,
size_t,
const art::FindManyP<recob::Track>&,
const art::FindManyP<recob::Vertex>&,
int&,
int&) const;
// The following typedefs will, obviously, be useful
double length(const recob::Track* track);
double projectedLength(const recob::Track* track);
// The parameters we'll read from the .fcl file.
std::vector<art::InputTag> fHitProducerLabelVec;
std::vector<art::InputTag> fWireProducerLabelVec;
std::vector<art::InputTag> fPFParticleProducerLabelVec;
std::vector<art::InputTag> fTrackProducerLabelVec;
// The variables that will go into the n-tuple.
int fEvent;
int fRun;
int fSubRun;
int fNumEvents;
// Keep track of the hit histogramming tools here
std::vector<std::unique_ptr<IHitHistogramTool>> fHitHistogramToolVec;
std::vector<std::vector<double>> fChannelPedVec;
// Other variables that will be shared between different methods.
const geo::GeometryCore* fGeometry; // pointer to Geometry service
const lariov::DetPedestalProvider& fPedestalRetrievalAlg; ///< Keep track of an instance to the pedestal retrieval alg
}; // class TrackHitAna
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// class implementation
//-----------------------------------------------------------------------
// Constructor
TrackHitAna::TrackHitAna(fhicl::ParameterSet const& parameterSet)
: EDAnalyzer(parameterSet),
fPedestalRetrievalAlg(*lar::providerFrom<lariov::DetPedestalService>())
{
fGeometry = lar::providerFrom<geo::Geometry>();
// Read in the parameters from the .fcl file.
this->reconfigure(parameterSet);
}
//-----------------------------------------------------------------------
// Destructor
TrackHitAna::~TrackHitAna()
{}
//-----------------------------------------------------------------------
void TrackHitAna::beginJob()
{
// Get the detector length, to determine the maximum bin edge of one
// of the histograms.
//double detectorLength = fGeometry->DetLength();
// Access ART's TFileService, which will handle creating and writing
// histograms and n-tuples for us.
art::ServiceHandle<art::TFileService> tfs;
// The arguments to 'make<whatever>' are the same as those passed
// to the 'whatever' constructor, provided 'whatever' is a ROOT
// class that TFileService recognizes.
for (auto& hitHistTool : fHitHistogramToolVec) hitHistTool->initializeHists(tfs, "AllHits");
// zero out the event counter
fNumEvents = 0;
}
//-----------------------------------------------------------------------
void TrackHitAna::beginRun(const art::Run& /*run*/)
{
// How to convert from number of electrons to GeV. The ultimate
// source of this conversion factor is
// ${LARSIM_DIR}/include/SimpleTypesAndConstants/PhysicalConstants.h.
// art::ServiceHandle<sim::LArG4Parameters> larParameters;
// fElectronsToGeV = 1./larParameters->GeVToElectrons();
}
//-----------------------------------------------------------------------
void TrackHitAna::reconfigure(fhicl::ParameterSet const& p)
{
// Read parameters from the .fcl file. The names in the arguments
// to p.get<TYPE> must match names in the .fcl file.
fHitProducerLabelVec = p.get< std::vector<art::InputTag> >("HitModuleLabel", std::vector<art::InputTag>() = {"gauss"});
fPFParticleProducerLabelVec = p.get< std::vector<art::InputTag> >("PFParticleProducerLabel", std::vector<art::InputTag>() = {"cluster3d"});
fTrackProducerLabelVec = p.get< std::vector<art::InputTag> >("TrackProducerLabel", std::vector<art::InputTag>() = {"trackkalmanhit"});
fWireProducerLabelVec = p.get< std::vector<art::InputTag> >("WireProducerLabel", std::vector<art::InputTag>() = {"caldata"});
// Implement the tools for handling the responses
const std::vector<fhicl::ParameterSet>& hitHistogramToolVec = p.get<std::vector<fhicl::ParameterSet>>("HitHistogramToolList");
for(auto& hitHistogramTool : hitHistogramToolVec)
fHitHistogramToolVec.push_back(art::make_tool<IHitHistogramTool>(hitHistogramTool));
return;
}
//-----------------------------------------------------------------------
void TrackHitAna::analyze(const art::Event& event)
{
// Start by fetching some basic event information for our n-tuple.
fEvent = event.id().event();
fRun = event.run();
fSubRun = event.subRun();
fNumEvents++;
for(const auto& hitLabel : fHitProducerLabelVec)
{
// Make a pass through all hits to make contrasting plots
art::Handle< std::vector<recob::Hit> > hitHandle;
event.getByLabel(hitLabel, hitHandle);
if (hitHandle.isValid())
{
IHitHistogramTool::HitPtrVec allHitVec;
art::fill_ptr_vector(allHitVec, hitHandle);
for(auto& hitHistTool : fHitHistogramToolVec) hitHistTool->fillHistograms(allHitVec);
}
}
return;
}
void TrackHitAna::endJob()
{
// Make a call to normalize histograms if so desired
for(auto& hitHistTool : fHitHistogramToolVec) hitHistTool->endJob(fNumEvents);
return;
}
// Length of reconstructed track.
//----------------------------------------------------------------------------
double TrackHitAna::length(const recob::Track* track)
{
double result(0.);
/*
TVector3 disp(track->LocationAtPoint(0));
TVector3 lastPoint(track->LocationAtPoint(0));
TVector3 lastDir(0.,0.,0.);
int n(track->NumberTrajectoryPoints());
for(int i = 1; i < n; ++i)
{
const TVector3& pos = track->LocationAtPoint(i);
TVector3 trajDir = pos - lastPoint;
if (trajDir.Mag2()) trajDir.SetMag(1.);
// if (lastDir.Dot(trajDir) >= 0.)
// {
disp -= pos;
result += disp.Mag();
disp = pos;
// }
lastPoint = pos;
lastDir = trajDir;
}
*/
return result;
}
// Length of reconstructed track.
//----------------------------------------------------------------------------
double TrackHitAna::projectedLength(const recob::Track* track)
{
double result(0.);
/*
TVector3 lastPoint(track->LocationAtPoint(0));
TVector3 lastDir(track->DirectionAtPoint(0));
int n(track->NumberTrajectoryPoints());
for(int i = 1; i < n; ++i)
{
const TVector3& newPoint = track->LocationAtPoint(i);
TVector3 lastToNewPoint = newPoint - lastPoint;
double arcLenToDoca = lastDir.Dot(lastToNewPoint);
result += arcLenToDoca;
lastPoint = lastPoint + arcLenToDoca * lastDir;
lastDir = track->DirectionAtPoint(i);
}
*/
return result;
}
int TrackHitAna::traversePFParticleHierarchy(art::Handle<std::vector<recob::PFParticle>>& pfParticleHandle,
size_t pfParticleIdx,
const art::FindManyP<recob::Track>& trackAssns,
const art::FindManyP<recob::Vertex>& vertexAssns,
int& nTracks,
int& nVertices) const
{
// So far no daughters...
int nDaughters(0);
/*
// Get pointer to PFParticle
art::Ptr<recob::PFParticle> pfParticle(pfParticleHandle,pfParticleIdx);
// Recover tracks/vertices associated to this PFParticle
std::vector<art::Ptr<recob::Track>> pfPartTrackVec = trackAssns.at(pfParticle.key());
std::vector<art::Ptr<recob::Vertex>> pfPartVertexVec = vertexAssns.at(pfParticle.key());
nTracks += pfPartTrackVec.size();
nVertices += pfPartVertexVec.size();
nDaughters += pfParticle->Daughters().size();
for(auto& daughterIdx : pfParticle->Daughters())
{
nDaughters += traversePFParticleHierarchy(pfParticleHandle, daughterIdx, trackAssns, vertexAssns, nTracks, nVertices);
}
*/
return nDaughters;
}
// This macro has to be defined for this module to be invoked from a
// .fcl file; see TrackHitAna.fcl for more information.
DEFINE_ART_MODULE(TrackHitAna)
} // namespace TrackHitAna
#endif // TrackHitAna_module