forked from wilkinson-nu/edep-sim
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEDepSimRooTrackerKinematicsGenerator.cc
419 lines (369 loc) · 16.5 KB
/
EDepSimRooTrackerKinematicsGenerator.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
////////////////////////////////////////////////////////////
//
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <globals.hh>
#include <G4Event.hh>
#include <G4PrimaryVertex.hh>
#include <G4PrimaryParticle.hh>
#include <G4ParticleTable.hh>
#include <G4IonTable.hh>
#include <G4ParticleDefinition.hh>
#include <G4Tokenizer.hh>
#include <G4UnitsTable.hh>
#include <Randomize.hh>
#include <TFile.h>
#include <TBits.h>
#include <TObjString.h>
#include <TTree.h>
#include "EDepSimVertexInfo.hh"
#include "EDepSimKinemPassThrough.hh"
#include "kinem/EDepSimRooTrackerKinematicsGenerator.hh"
#include "EDepSimLog.hh"
EDepSim::RooTrackerKinematicsGenerator::RooTrackerKinematicsGenerator(
const G4String& name, const G4String& filename,
const G4String& treeName, const G4String& order,
int firstEvent)
: EDepSim::VKinematicsGenerator(name), fInput(NULL), fTree(NULL),
fNextEntry(0) {
fInput = TFile::Open(filename,"OLD");
if (!fInput->IsOpen()) {
throw std::runtime_error("EDepSim::RooTrackerKinematicsGenerator::"
" File Not Open");
}
EDepSimLog("Open a RooTracker tree from " << filename);
fTree = dynamic_cast<TTree*>(fInput->Get(treeName));
if (!fTree) {
throw std::runtime_error("EDepSim::RooTrackerKinematicsGenerator::"
" Tree not found by constructor");
}
EDepSimNamedInfo("rooTracker",
" File has " << fTree->GetEntries() << " entries");
// Find the basename of the input filename.
std::string::size_type start_pos = filename.rfind("/");
if (start_pos == std::string::npos) start_pos = 0; else ++start_pos;
std::string basename(filename,start_pos);
fFilename = basename + ":" + treeName;
// Count fatal errors while connecting to the rooTracker tree. Some of
// the branches are not used, or can be ignored, but *some* absolutely
// must be correct or the output is wrong.
int fatalErrors = 0;
int branchResult; // Should be zero.
fEvtFlags = NULL;
branchResult = fTree->SetBranchAddress("EvtFlags", &fEvtFlags);
fEvtCode = NULL;
branchResult = fTree->SetBranchAddress("EvtCode", &fEvtCode);
branchResult = fTree->SetBranchAddress("EvtNum", &fEvtNum);
if (branchResult) {
EDepSimError("Previous error causes a fatally malformed file");
++fatalErrors;
}
branchResult = fTree->SetBranchAddress("EvtXSec", &fEvtXSec);
branchResult = fTree->SetBranchAddress("EvtDXSec", &fEvtDXSec);
branchResult = fTree->SetBranchAddress("EvtWght", &fEvtWght);
branchResult = fTree->SetBranchAddress("EvtProb", &fEvtProb);
branchResult = fTree->SetBranchAddress("EvtVtx", fEvtVtx);
if (branchResult) {
EDepSimError("Previous error causes a fatally malformed file");
++fatalErrors;
}
branchResult = fTree->SetBranchAddress("StdHepN", &fStdHepN);
if (branchResult) {
EDepSimError("Previous error causes a fatally malformed file");
++fatalErrors;
}
branchResult = fTree->SetBranchAddress("StdHepPdg", fStdHepPdg);
if (branchResult) {
EDepSimError("Previous error causes a fatally malformed file");
++fatalErrors;
}
branchResult = fTree->SetBranchAddress("StdHepStatus", fStdHepStatus);
if (branchResult) {
EDepSimError("Previous error causes a fatally malformed file");
++fatalErrors;
}
branchResult = fTree->SetBranchAddress("StdHepX4", fStdHepX4);
branchResult = fTree->SetBranchAddress("StdHepP4", fStdHepP4);
if (branchResult) {
EDepSimError("Previous error causes a fatally malformed file");
++fatalErrors;
}
branchResult = fTree->SetBranchAddress("StdHepPolz", fStdHepPolz);
branchResult = fTree->SetBranchAddress("StdHepFd", fStdHepFd);
branchResult = fTree->SetBranchAddress("StdHepLd", fStdHepLd);
branchResult = fTree->SetBranchAddress("StdHepFm", fStdHepFm);
branchResult = fTree->SetBranchAddress("StdHepLm", fStdHepLm);
#define PARENT_PARTICLE_PASS_THROUGH
#ifdef PARENT_PARTICLE_PASS_THROUGH
branchResult = fTree->SetBranchAddress("NuParentPdg", &fNuParentPdg);
branchResult = fTree->SetBranchAddress("NuParentDecMode",&fNuParentDecMode);
branchResult = fTree->SetBranchAddress("NuParentDecP4", fNuParentDecP4);
branchResult = fTree->SetBranchAddress("NuParentDecX4", fNuParentDecX4);
branchResult = fTree->SetBranchAddress("NuParentProP4", fNuParentProP4);
branchResult = fTree->SetBranchAddress("NuParentProX4", fNuParentProX4);
branchResult = fTree->SetBranchAddress("NuParentProNVtx",&fNuParentProNVtx);
#endif
if (fatalErrors) {
EDepSimError("Fatally malformed rooTracker file");
throw std::runtime_error("Malformed rooTracker file");
}
// Set the input tree to the current rootracker tree that this class is
// using.
EDepSim::KinemPassThrough::GetInstance()->AddInputTree(fTree,
filename,
GetName());
// Generate a vector of entries to be used by this generator.
// Calculate the stride through the file. This could be cached, but
// recalculate each time for simplicity. Note that the stride should be a
// prime number, and not a divisor of the number of entries in the tree.
int entries = fTree->GetEntries();
fEntryVector.resize(entries);
int stride = 1;
while (order == "stride") {
stride = 17;
if (0 != entries%stride) break;
stride = 19;
if (0 != entries%stride) break;
stride = 23;
if (0 != entries%stride) break;
stride = 29;
if (0 != entries%stride) break;
throw std::runtime_error("EDepSim::RooTrackerKinematicsGenerator:: "
"File size cannot be divisible by 215441");
break; // throw std::runtime_error doesn't actually return
}
int entry = 0;
for (int i=0; i<entries; ++i) {
fEntryVector[i] = entry;
entry = (entry + stride)%entries;
}
if (order == "random") {
for (std::size_t i = fEntryVector.size()-1; i > 0; --i) {
int j = (i+1)*G4UniformRand();
std::swap(fEntryVector[i],fEntryVector[j]);
}
}
if (firstEvent > 0) {
EDepSimLog(" FIRST EVENT WILL BE " << firstEvent);
if (firstEvent >= entries) {
throw std::runtime_error("EDepSim::RooTrackerKinematicsGenerator::"
" First event after last event");
}
fEntryVector.erase(std::copy(fEntryVector.begin()+firstEvent,
fEntryVector.end(),fEntryVector.begin()),
fEntryVector.end());
}
}
EDepSim::RooTrackerKinematicsGenerator::~RooTrackerKinematicsGenerator() { }
G4String EDepSim::RooTrackerKinematicsGenerator::GetInputName() {
if (!fInput) return G4String("not-open");
return G4String(fInput->GetName());
}
EDepSim::VKinematicsGenerator::GeneratorStatus
EDepSim::RooTrackerKinematicsGenerator::GeneratePrimaryVertex(
G4Event* anEvent,
const G4LorentzVector&) {
if (!fInput) {
throw std::runtime_error("EDepSim::RooTrackerKinematicsGenerator::"
" File Not Open");
}
/// Check to see if the next event is there.
if (fNextEntry >= fEntryVector.size()) {
EDepSimLog("EDepSim::RooTrackerKinematicsGenerator: input file end.");
throw EDepSim::NoMoreEvents();
}
fInput->cd();
int entry = fEntryVector.at(fNextEntry);
// Get current entry to be used as new vertex - see comment below.
fTree->GetEntry(entry);
// Store current entry in the pass-through obj. N.B. To avoid mismatch
// and false results call EDepSim::KinemPassThrough::AddEntry(fTreePtr, X)
// where X is same as X in most recent call to fTreePtr->GetEntry(X).
// EDepSim::KinemPassThrough::GetInstance()->AddEntry(fTree, entry);
// NOTE: This is now done in EDepSim::RootPersistencyManager so that we can
// filter out the pass-through entries for events that don't leave any hits.
EDepSimVerbose("Use rooTracker event number " << fEvtNum
<< " (entry #" << entry << " in tree)");
std::string eventCode("not-specified");
if (fEvtCode) {
eventCode = fEvtCode->String();
}
else {
EDepSimError("Malformed input file -- missing EvtCode");
}
// Increment the next entry counter.
++fNextEntry;
// Set the default generator status. This should be overridden if the
// state changes.
GeneratorStatus generatorStatus = kSuccess;
// Check if this is an end-of-sequence marker.
if (fStdHepN == 1 && fStdHepStatus[0]<0) {
EDepSimLog("End of event");
return kEndEvent;
}
// Create a new vertex to add the new particles, and add the vertex to the
// event.
G4PrimaryVertex* theVertex
= new G4PrimaryVertex(G4ThreeVector(fEvtVtx[0]*m,
fEvtVtx[1]*m,
fEvtVtx[2]*m),
fEvtVtx[3]*second);
anEvent->AddPrimaryVertex(theVertex);
EDepSimNamedInfo("rooTracker","Vertex @ "
<< G4BestUnit(theVertex->GetPosition(), "Length")
<< " Time: " << G4BestUnit(theVertex->GetT0(), "Time"));
// Add an information field to the vertex.
EDepSim::VertexInfo *vertexInfo = new EDepSim::VertexInfo;
theVertex->SetUserInformation(vertexInfo);
// Fill the information fields for this vertex.
vertexInfo->SetReaction(eventCode);
// Set the file name for this event.
std::ostringstream fs;
fs << fFilename << ":" << entry;
vertexInfo->SetFilename(fs.str());
// Set the interaction number to that of the RooTracker pass-through tree.
vertexInfo->SetInteractionNumber(
EDepSim::KinemPassThrough::GetInstance()->LastEntryNumber());
vertexInfo->SetCrossSection(fEvtXSec*1E-38*cm2);
vertexInfo->SetDiffCrossSection(fEvtDXSec*1E-38*cm2);
vertexInfo->SetWeight(fEvtWght);
vertexInfo->SetProbability(fEvtProb);
// Add an informational vertex for storing the incoming
// neutrino particle and target nucleus.
G4PrimaryVertex* theIncomingVertex
= new G4PrimaryVertex(G4ThreeVector(fEvtVtx[0]*m,
fEvtVtx[1]*m,
fEvtVtx[2]*m),
fEvtVtx[3]*second);
vertexInfo->AddInformationalVertex(theIncomingVertex);
// Add an information field to the vertex.
EDepSim::VertexInfo *incomingVertexInfo = new EDepSim::VertexInfo;
incomingVertexInfo->SetName("initial-state");
incomingVertexInfo->SetReaction(eventCode);
theIncomingVertex->SetUserInformation(incomingVertexInfo);
// Fill the particles to be tracked (status ==1). These particles are
// attached to the primary vertex. Also save the incident neutrino
// particle and the incident target nucleus; these particles are attached
// to informational vertex.
G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
for (int cnt = 0; cnt < fStdHepN; ++cnt) {
G4ParticleDefinition* particleDef
= particleTable->FindParticle(fStdHepPdg[cnt]);
if (!particleDef) {
//maybe we have an ion; figure out if it makes any sense
int ionA = (fStdHepPdg[cnt]/10) % 1000;
int ionZ = (fStdHepPdg[cnt]/10000) % 1000;
int type = (fStdHepPdg[cnt]/100000000);
if (type == 10 && ionZ > 0 && ionA > ionZ) {
G4IonTable* ionTable = particleTable->GetIonTable();
particleDef = ionTable->GetIon(ionZ, ionA);
}
else if (type == 20) {
// This is a pseudo-particle so skip it.
continue;
}
}
// Determine a name for the particle.
std::string particleName =
particleDef ? particleDef->GetParticleName(): "unknown";
// Get the momentum.
G4LorentzVector momentum(fStdHepP4[cnt][0]*GeV,
fStdHepP4[cnt][1]*GeV,
fStdHepP4[cnt][2]*GeV,
fStdHepP4[cnt][3]*GeV);
if (fStdHepStatus[cnt] != 1) {
EDepSimVerbose("Untracked particle: " << cnt
<< " " << particleName
<< " with " << momentum.e()/MeV
<< " MeV "
<< " w/ mothers " << fStdHepFm[cnt]
<< " to " << fStdHepLm[cnt]);
}
// We are only interested in particles to be tracked (status==1)
// or incident neutrino/target nucleus (status==0).
if( !(fStdHepStatus[cnt] == 0 || fStdHepStatus[cnt] == 1)) {
continue;
}
if (!particleDef) {
EDepSimSevere(" Particle code " << fStdHepPdg[cnt]
<< " not recognized (not tracking)");
continue;
}
// create the particle.
G4PrimaryParticle* theParticle
= new G4PrimaryParticle(particleDef,
momentum.px(),
momentum.py(),
momentum.pz());
theParticle->SetPolarization(fStdHepPolz[cnt][0],
fStdHepPolz[cnt][1],
fStdHepPolz[cnt][2]);
if (fStdHepStatus[cnt] == 0) {
EDepSimNamedInfo(
"rooTracker",
"Incoming "
<< particleDef->GetParticleName()
<< " " << theParticle->GetPDGcode()
<< " " << momentum.e()/MeV << " MeV"
<< " " << momentum.m()/MeV << " MeV/c^2");
theIncomingVertex->SetPrimary(theParticle);
}
else if (fStdHepStatus[cnt] == 1){
EDepSimNamedInfo(
"rooTracker",
"Tracking "
<< particleDef->GetParticleName()
<< " " << theParticle->GetPDGcode()
<< " " << momentum.e()/MeV << " MeV"
<< " " << momentum.m()/MeV << " MeV/c^2");
theVertex->SetPrimary(theParticle);
}
}
#ifdef PARENT_PARTICLE_PASS_THROUGH
// Fill the particles at the decay vertex. These are the first info
// vertex.
G4PrimaryVertex* theDecayVertex
= new G4PrimaryVertex(G4ThreeVector(fNuParentDecX4[0]*m,
fNuParentDecX4[1]*m,
fNuParentDecX4[2]*m),
fNuParentDecX4[3]*second);
vertexInfo->AddInformationalVertex(theDecayVertex);
// Add an information field to the vertex.
EDepSim::VertexInfo *decayVertexInfo = new EDepSim::VertexInfo;
decayVertexInfo->SetName("beam-particle:Decay");
{
std::ostringstream tmp;
tmp << fNuParentDecMode;
decayVertexInfo->SetReaction(tmp.str());
}
theDecayVertex->SetUserInformation(decayVertexInfo);
G4PrimaryParticle* theDecayParticle
= new G4PrimaryParticle(fNuParentPdg,
fNuParentDecP4[0]*GeV,
fNuParentDecP4[1]*GeV,
fNuParentDecP4[2]*GeV);
theDecayVertex->SetPrimary(theDecayParticle);
// Fill the particles at the production vertex.
G4PrimaryVertex* theProductionVertex
= new G4PrimaryVertex(G4ThreeVector(fNuParentProX4[0]*m,
fNuParentProX4[1]*m,
fNuParentProX4[2]*m),
fNuParentProX4[3]*second);
decayVertexInfo->AddInformationalVertex(theProductionVertex);
// Add information about the production vertex.
EDepSim::VertexInfo *productionVertexInfo = new EDepSim::VertexInfo;
productionVertexInfo->SetName("beam-particle:Production");
productionVertexInfo->SetInteractionNumber(fNuParentProNVtx);
theProductionVertex->SetUserInformation(productionVertexInfo);
G4PrimaryParticle* theProductionParticle
= new G4PrimaryParticle(fNuParentPdg,
fNuParentProP4[0]*GeV,
fNuParentProP4[1]*GeV,
fNuParentProP4[2]*GeV);
theProductionVertex->SetPrimary(theProductionParticle);
#endif
return generatorStatus;
}