-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEMEnergyCalib_module.cc
295 lines (253 loc) · 10 KB
/
EMEnergyCalib_module.cc
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
////////////////////////////////////////////////////////////////////////////////////////////////
// Class: EMEnergyCalib
// Module Type: analyzer
// File: EMEnergyCalib_module.cc
// Author: Mike Wallbank ([email protected]), August 2015
//
// Analyser module to produce useful information for characterising
// em showers.
//
// Usage:
// lar -c energyCalib.fcl -s /path/to/files/with/hit/recon/*.root
//
// Description of intended use:
// Designed to be used to provide information for characterising showers.
// Also can be used along with getEnergyConversion.C macro in this directory to find the
// conversion between collected charge and total deposited energy for MC showers.
// See notes in the getEnergyConversion.C file for a description of this.
////////////////////////////////////////////////////////////////////////////////////////////////
// Framework includes:
#include "art/Framework/Core/ModuleMacros.h"
#include "art/Framework/Principal/Event.h"
#include "fhiclcpp/ParameterSet.h"
#include "art/Framework/Principal/Handle.h"
#include "canvas/Persistency/Common/Ptr.h"
#include "canvas/Persistency/Common/PtrVector.h"
#include "art/Framework/Services/Registry/ServiceHandle.h"
#include "art_root_io/TFileService.h"
#include "art_root_io/TFileDirectory.h"
#include "messagefacility/MessageLogger/MessageLogger.h"
#include "art/Framework/Core/EDAnalyzer.h"
// LArSoft includes
#include "lardata/DetectorInfoServices/DetectorClocksService.h"
#include "lardata/DetectorInfoServices/DetectorPropertiesService.h"
#include "larcore/Geometry/Geometry.h"
#include "larcorealg/Geometry/CryostatGeo.h"
#include "larcorealg/Geometry/TPCGeo.h"
#include "larcorealg/Geometry/PlaneGeo.h"
#include "lardataobj/RecoBase/Cluster.h"
#include "lardataobj/RecoBase/Hit.h"
#include "larsim/MCCheater/BackTrackerService.h"
#include "larsim/MCCheater/ParticleInventoryService.h"
#include "lardata/Utilities/AssociationUtil.h"
#include "larevt/Filters/ChannelFilter.h"
#include "nusimdata/SimulationBase/MCParticle.h"
#include "nug4/ParticleNavigation/ParticleList.h"
#include "lardataobj/Simulation/sim.h"
// ROOT & C++ includes
#include <string>
#include <vector>
#include <map>
#include "TTree.h"
#include "TBranch.h"
#include "TLeaf.h"
#include "TVector3.h"
const int kMaxHits = 10000;
namespace emshower {
class EMEnergyCalib;
}
class emshower::EMEnergyCalib : public art::EDAnalyzer {
public:
EMEnergyCalib(fhicl::ParameterSet const& pset);
void analyze(art::Event const& evt);
void reconfigure(fhicl::ParameterSet const& p);
void reset();
int FindTrackID(detinfo::DetectorClocksData const& clockData,
art::Ptr<recob::Hit> const& hit);
private:
// Variables for the tree
TTree* fTree;
double trueEnergy;
double depositU;
double depositV;
double depositZ;
double correctedChargeU;
double correctedChargeV;
double correctedChargeZ;
double vertexDetectorDist;
int nhits;
int hit_tpc [kMaxHits];
int hit_plane [kMaxHits];
int hit_wire [kMaxHits];
int hit_channel [kMaxHits];
double hit_peakT [kMaxHits];
double hit_charge [kMaxHits];
int hit_truetrackid[kMaxHits];
int hit_clusterid [kMaxHits];
std::string fHitsModuleLabel;
std::string fClusterModuleLabel;
art::ServiceHandle<art::TFileService> tfs;
art::ServiceHandle<cheat::BackTrackerService> backtracker;
art::ServiceHandle<cheat::ParticleInventoryService> particleinventory;
art::ServiceHandle<geo::Geometry> geom;
};
emshower::EMEnergyCalib::EMEnergyCalib(fhicl::ParameterSet const& pset) : EDAnalyzer(pset)
{
this->reconfigure(pset);
fTree = tfs->make<TTree>("EMEnergyCalib","EMEnergyCalib");
fTree->Branch("TrueEnergy", &trueEnergy);
fTree->Branch("DepositU", &depositU);
fTree->Branch("DepositV", &depositV);
fTree->Branch("DepositZ", &depositZ);
fTree->Branch("CorrectedChargeU", &correctedChargeU);
fTree->Branch("CorrectedChargeV", &correctedChargeV);
fTree->Branch("CorrectedChargeZ", &correctedChargeZ);
fTree->Branch("VertexDetectorDist",&vertexDetectorDist);
fTree->Branch("NHits", &nhits);
fTree->Branch("Hit_TPC", hit_tpc, "hit_tpc[NHits]/I");
fTree->Branch("Hit_Plane", hit_plane, "hit_plane[NHits]/I");
fTree->Branch("Hit_Wire", hit_wire, "hit_wire[NHits]/I");
fTree->Branch("Hit_Channel", hit_channel, "hit_channel[NHits]/I");
fTree->Branch("Hit_PeakT", hit_peakT, "hit_peakT[NHits]/D");
fTree->Branch("Hit_Charge", hit_charge, "hit_charge[NHits]/D");
fTree->Branch("Hit_TrueTrackID", hit_truetrackid,"hit_truetrackid[NHits]/I");
fTree->Branch("Hit_ClusterID", hit_clusterid, "hit_clusterid[NHits]/I");
}
void emshower::EMEnergyCalib::reconfigure(fhicl::ParameterSet const& pset) {
fHitsModuleLabel = pset.get<std::string>("HitsModuleLabel");
fClusterModuleLabel = pset.get<std::string>("ClusterModuleLabel");
}
void emshower::EMEnergyCalib::analyze(art::Event const& evt) {
/// Analyse function to save information for calibrating shower energies
/// This is written assuming single particle per event
this->reset();
// Get the hits out of the event record
art::Handle<std::vector<recob::Hit> > hitHandle;
std::vector<art::Ptr<recob::Hit> > hits;
if (evt.getByLabel(fHitsModuleLabel,hitHandle))
art::fill_ptr_vector(hits, hitHandle);
// Get the clusters out of the event record
art::Handle<std::vector<recob::Cluster> > clusterHandle;
std::vector<art::Ptr<recob::Cluster> > clusters;
if (evt.getByLabel(fClusterModuleLabel,clusterHandle))
art::fill_ptr_vector(clusters, clusterHandle);
art::FindManyP<recob::Cluster> fmc(hitHandle, evt, fClusterModuleLabel);
// Lifetime-corrected charge
correctedChargeU = 0;
correctedChargeV = 0;
correctedChargeZ = 0;
// Look at the hits
auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(evt, clockData);
for (unsigned int hitIt = 0; hitIt < hits.size(); ++hitIt) {
if (hitIt >= kMaxHits) continue;
// Get the hit
art::Ptr<recob::Hit> hit = hits.at(hitIt);
double correctedHitCharge = ( hit->Integral() * TMath::Exp( (sampling_rate(clockData) * hit->PeakTime()) / (detProp.ElectronLifetime()*1e3) ) );
switch (hit->WireID().Plane) {
case 0:
correctedChargeU += correctedHitCharge;
break;
case 1:
correctedChargeV += correctedHitCharge;
break;
case 2:
correctedChargeZ += correctedHitCharge;
break;
}
// Fill hit level info
hit_tpc [hitIt] = hit->WireID().TPC;
hit_plane [hitIt] = hit->WireID().Plane;
hit_wire [hitIt] = hit->WireID().Wire;
hit_peakT [hitIt] = hit->PeakTime();
hit_charge [hitIt] = hit->Integral();
hit_channel [hitIt] = hit->Channel();
// Find the true track this hit is associated with
hit_truetrackid[hitIt] = this->FindTrackID(clockData, hit);
// Find the cluster index this hit it associated with (-1 if unclustered)
if (fmc.isValid()) {
std::vector<art::Ptr<recob::Cluster> > clusters = fmc.at(hitIt);
if (clusters.size() != 0) {
hit_clusterid[hitIt] = clusters.at(0)->ID();
}
else hit_clusterid[hitIt] = -1;
}
}
// Event level information
nhits = hits.size();
const sim::ParticleList& trueParticles = particleinventory->ParticleList();
const simb::MCParticle* trueParticle = trueParticles.Primary(0);
trueEnergy = trueParticle->Momentum().E();
// Find the energy deposited on each plane in the TPC
const std::vector<art::Ptr< sim::SimChannel > >& simChannels = backtracker->SimChannels();
for (auto channelIt = simChannels.begin(); channelIt != simChannels.end(); ++channelIt) {
int plane = geom->View((*channelIt)->Channel());
for (auto const& tdcIt : (*channelIt)->TDCIDEMap()) {
for (auto const& ideIt : tdcIt.second) {
switch (plane) {
case geo::kU:
depositU += ideIt.energy;
break;
case geo::kV:
depositV += ideIt.energy;
break;
case geo::kZ:
depositZ += ideIt.energy;
break;
}
}
}
}
// Find the distance between the particle vertex and the edge of the detector
TVector3 vertex = TVector3(trueParticle->Vx(),trueParticle->Vy(),trueParticle->Vz());
TVector3 end = TVector3(trueParticle->EndX(),trueParticle->EndY(),trueParticle->EndZ());
TVector3 direction = TVector3(trueParticle->Px(),trueParticle->Py(),trueParticle->Pz()).Unit();
int distanceStep = 1, steps = 0;
TVector3 pos;
bool inTPC = true;
while (inTPC) {
pos = end + ( (steps*distanceStep) * direction );
double currentPos[3]; currentPos[0] = pos.X(); currentPos[1] = pos.Y(); currentPos[2] = pos.Z();
if (!geom->FindTPCAtPosition(currentPos).isValid)
inTPC = false;
++steps;
}
vertexDetectorDist = (end - pos).Mag();
// Put energies in GeV units
depositU /= 1000;
depositV /= 1000;
depositZ /= 1000;
fTree->Fill();
return;
}
int emshower::EMEnergyCalib::FindTrackID(detinfo::DetectorClocksData const& clockData,
art::Ptr<recob::Hit> const& hit) {
double particleEnergy = 0;
int likelyTrackID = 0;
std::vector<sim::TrackIDE> trackIDs = backtracker->HitToTrackIDEs(clockData, hit);
for (unsigned int idIt = 0; idIt < trackIDs.size(); ++idIt) {
if (trackIDs.at(idIt).energy > particleEnergy) {
particleEnergy = trackIDs.at(idIt).energy;
likelyTrackID = TMath::Abs(trackIDs.at(idIt).trackID);
}
}
return likelyTrackID;
}
void emshower::EMEnergyCalib::reset() {
trueEnergy = 0;
depositU = 0;
depositV = 0;
depositZ = 0;
vertexDetectorDist = 0;
nhits = 0;
for (int hit = 0; hit < kMaxHits; ++hit) {
hit_tpc[hit] = 0;
hit_plane[hit] = 0;
hit_wire[hit] = 0;
hit_channel[hit] = 0;
hit_peakT[hit] = 0;
hit_charge[hit] = 0;
hit_clusterid[hit] = 0;
}
}
DEFINE_ART_MODULE(emshower::EMEnergyCalib)