-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgramselecsim.cc
381 lines (301 loc) · 13 KB
/
gramselecsim.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
// gramselecsim.cc
// Apply electronics-simulation models to the electron clusters that
// come from GramsReadoutSim.
// 25-Oct-2022 Satoshi Takashima
// Note that all comments were added by William Seligman
// 12-Jun-2024 William Seligman
// Substantial structural revisions for the new tree format of the files.
// Our function(s) for the electronics response.
#include "UtilFunctions.h"
#include "AddNoise.h"
#include "ADConvert.h"
#include "ElecStructure.h"
#include "PreampProcessor.h"
#include "LoadOptionFile.h"
// For processing command-line and XML file options.
#include "Options.h" // in util/
// For copying and accessing the detectory geometry.
#include "Geometry.h" // in util/
// From GramsDataObj
#include "EventID.h"
#include "ElectronClusters.h"
#include "ReadoutID.h"
#include "ReadoutMap.h"
#include "ReadoutWaveforms.h"
// ROOT includes
#include "TFile.h"
#include "TTree.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"
#include "TRandom.h"
// C++ includes
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <ctime>
///////////////////////////////////////
int main(int argc,char **argv)
{
auto options = util::Options::GetInstance();
auto result = options->ParseOptions(argc, argv, "gramselecsim");
if (! result) {
std::cerr << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: Aborting job due to failure to parse options"
<< std::endl;
exit(EXIT_FAILURE);
}
bool debug;
options->GetOption("debug",debug);
bool help;
options->GetOption("help",help);
if (help) {
options->PrintHelp();
exit(EXIT_SUCCESS);
}
bool verbose;
options->GetOption("verbose",verbose);
if (verbose || debug) {
options->PrintOptions();
}
// For any model that requires random-number generation, get and set
// the random number seed.
int seed;
options->GetOption("rngseed",seed);
// Note that the default random-number generator in ROOT is
// TRandom3.
gRandom->SetSeed(seed);
// This program reads two trees; or, if you wish, a single tree
// that's divided into two files, with different columns in each
// file.
// One tree is the output from GramsDetSim.
std::string inputClustersFileName;
std::string inputClustersTreeName;
options->GetOption("inputElecClustersFile", inputClustersFileName);
options->GetOption("inputElecClustersTree", inputClustersTreeName);
auto inputClusters = TFile::Open(inputClustersFileName.c_str());
if (!inputClusters || inputClusters->IsZombie()) {
std::cerr << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: Could not open file '" << inputClustersFileName << "'"
<< std::endl;
exit(EXIT_FAILURE);
}
auto clustersTree = inputClusters->Get<TTree>(inputClustersTreeName.c_str());
if (!clustersTree) {
std::cerr << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: Could not open tree '" << inputClustersTreeName << "'"
<< std::endl;
exit(EXIT_FAILURE);
}
// The other is the output from GramsReadoutSim.
std::string inputMapFileName;
std::string inputMapTreeName;
options->GetOption("inputElecMapFile", inputMapFileName);
options->GetOption("inputElecMapTree", inputMapTreeName);
auto inputMap = TFile::Open(inputMapFileName.c_str());
if (!inputMap || inputMap->IsZombie()) {
std::cerr << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: Could not open file '" << inputMapFileName << "'"
<< std::endl;
exit(EXIT_FAILURE);
}
auto mapTree = inputMap->Get<TTree>(inputMapTreeName.c_str());
if (!mapTree) {
std::cerr << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: Could not open tree '" << inputMapTreeName << "'"
<< std::endl;
exit(EXIT_FAILURE);
}
if (verbose || debug)
std::cout << "gramselecsim: input clusters file = '" << inputClustersFileName
<< "', input tree = '" << inputClustersTreeName
<< "'" << std::endl
<< "gramselecsim: input map file = '" << inputMapFileName
<< "', input tree = '" << inputMapTreeName
<< "'" << std::endl;
// Save the options from the input Map file, to provide a historical
// record of the analysis chain.
options->CopyInputNtuple(inputMap);
// Define the trees from the above two files as friends with each
// other. Note that when these trees were created in GramsDetSim and
// GramsElecSim, we took care to make sure that each had the same
// number of rows, indexed by EventID.
mapTree->AddFriend(clustersTree);
// Define the TTreeReader for this combined tree.
auto reader = new TTreeReader(mapTree);
// Create a TTreeReaderValue for each column in the combined tree
// whose value we'll use. Note that we have two columns named
// "EventID" in the combined tree, so specify which one to use.
std::string eventColumn = inputMapTreeName + ".EventID";
TTreeReaderValue<grams::EventID> inputEventID = {*reader, eventColumn.c_str()};
TTreeReaderValue<grams::ElectronClusters> clusters = {*reader, "ElectronClusters"};
TTreeReaderValue<grams::ReadoutMap> readoutMap = {*reader, "ReadoutMap"};
// These functions, defined in GramsElecSim/include/Elecstructure.h,
// return the ROOT ntuple specification for how to store the options
// values in the output ntuple.
// (As of Oct-2022, these were not used; as of 12-Jun-2024, they are
// now obsolete. See Elecstructure.h for more information.)
std::string general_header_type = gramselecsim::GeneralHeaderType();
std::string preamp_header_type = gramselecsim::PreampHeaderType();
std::string noise_header_type = gramselecsim::NoiseHeaderType();
std::string adc_header_type = gramselecsim::ADCHeaderType();
// Now read in the options associated with the output file and ntuple.
std::string outputFileName;
options->GetOption("outputElecFile", outputFileName);
std::string outputTreeName;
options->GetOption("outputElecTree", outputTreeName);
// Open the output file.
auto output = TFile::Open(outputFileName.c_str(),"RECREATE");
// Write the options to the output file in order to preserve them.
options->WriteNtuple(output);
// Copy the detector geometry from the input file to the output
// file.
auto geometry = util::Geometry::GetInstance();
if (debug)
std::cout << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: About to copy geometry"
<< std::endl;
geometry->CopyGeometry(inputMap,output);
if (debug)
std::cout << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: geometry copied"
<< std::endl;
// Define our output tree.
TTree* outputTree = new TTree(outputTreeName.c_str(), "Electronics Response");
// Define the columns of the output tree. Note that we include
// EventID in the output, so that this new tree can be friends with
// the input trees in other analysis programs.
auto eventID = new grams::EventID();
auto readoutWaveforms = new grams::ReadoutWaveforms();
// By experimenting, it turns out that setting the splitlevel to 0
// improves potential issues with ROOT's TBrowser.
outputTree->Branch("EventID", &eventID , 32000, 0);
outputTree->Branch("ReadoutWaveforms", &readoutWaveforms, 32000, 0);
if (debug) {
std::cout << "gramselecsim main: output tree defined" << std::endl;
}
// The original author of this program set up a routine to load
// values from the options XML file, then share those options among
// the various subroutines.
if (debug) {
std::cout << "gramselecsim main: about to access options via LoadOptionFile" << std::endl;
}
// Read in the options for the various electronics-simulation models.
auto optionloader = gramselecsim::LoadOptionFile::GetInstance();
optionloader->Load();
gramselecsim::general_header header_gen = optionloader->GeneralHeader();
gramselecsim::preamp_header header_pre = optionloader->PreampHeader();
gramselecsim::noise_header header_noise = optionloader->NoiseHeader();
gramselecsim::adc_header header_adc = optionloader->ADCHeader();
if (debug) {
std::cout << "gramselecsim main: ElecStructure options accessed" << std::endl;
}
const double sample_freq = header_adc.sample_freq;
// The number of time bins for the analog waveform.
const int num_tbin = int(header_gen.time_window / header_gen.timebin_width);
if (verbose) {
std::cout << "gramselecsim main: number of analog time bins=" << num_tbin << std::endl;
}
// Algorithms for computing waveforms. Use make_shared for these
// model routines so that we don't have to worry about deleting them
// later.
auto adconverter = std::make_shared<gramselecsim::ADConvert>();
auto addNoise = std::make_shared<gramselecsim::AddNoise>();
auto preampProcessor = std::make_shared<gramselecsim::PreampProcessor>(num_tbin);
if (debug) {
std::cout << "gramselecsim main: model routines defined" << std::endl;
}
// For timing how long this routine takes.
time_t t1 = time(NULL);
if (debug) {
std::cout << "gramselecsim main: about to process input" << std::endl;
}
// For each row in the input tree:
while ( (*reader).Next() ) {
// Copy the event ID from input to output.
(*eventID) = (*inputEventID);
// Clear out any waveform information from the previous event.
readoutWaveforms->clear();
// For each readout cell that received any electron clusters:
for ( const auto& [ readoutID, clusterKeys ] : (*readoutMap) ) {
// We'll create new waveforms for each readout cell.
grams::ReadoutWaveform readoutWaveform;
readoutWaveform.readoutID = readoutID;
// For accumulating the electrons arriving within each time bin.
std::vector<int> num_arrival_electrons( num_tbin, 0 );
// for each electron cluster assigned to this readout cell:
for ( const auto& clusterKey: clusterKeys ) {
// Find the key for this cluster in our list of electron
// clusters. (By the way, this is the point at which we're
// making use of columns in two different files.)
const auto search = clusters->find( clusterKey );
if ( search == clusters->cend() ) {
// This should not happen. It means that GramsReadoutSim
// inserted a cluster key that was never defined in
// GramsDetSim.
std::cerr << "File " << __FILE__ << " Line " << __LINE__ << " " << std::endl
<< "gramselecsim: Aborting due to mis-match between:" << std::endl
<< "file " << inputClustersFileName << " tree " << inputClustersTreeName
<< " and "
<< "file " << inputMapFileName << " tree " << inputMapTreeName
<< std::endl;
exit(EXIT_FAILURE);
}
// We found the cluster's key in the list of electron
// clusters. Fetch that cluster; remember that a map consists
// of pairs (first,second).
const auto& cluster = (*search).second;
if (debug) {
std::cout << "gramselecsim main: about to process cluster: " << std::endl
<< cluster << std::endl;
}
// The integer time bin in which the cluster arrived.
int ti = std::max(0,
std::min(num_tbin-1, int(std::floor( cluster.TAtAnode() / header_gen.timebin_width)))
);
// Accumulate the number of electrons to arrive at the cell
// within each time bin.
num_arrival_electrons[ ti ] += cluster.NumElectrons();
} // for each cluster within a readout cell
if (debug) {
std::cout << "gramselecsim main: about to compute waveform for "
<< "ReadoutID=" << readoutID << std::endl;
std::cout << "gramselecsim main: AddNoise..." << std::endl;
}
// Add noise to the number of electrons.
const auto num_arrival_electron_with_noise
= addNoise->ProcessElectronNoise( num_arrival_electrons );
if (debug) {
std::cout << "gramselecsim main: PreAmp..." << std::endl;
}
//Add a response function
readoutWaveform.analog = preampProcessor->ConvoluteResponse( num_arrival_electron_with_noise );
if (debug) {
std::cout << "gramselecsim main: ADConvert..." << std::endl;
}
// Convert analog into digital
readoutWaveform.digital = adconverter->Process( readoutWaveform.analog );
// Add the waveforms to our list of readout cells with a signal.
readoutWaveforms->insert( std::make_pair( readoutID, readoutWaveform ) );
} // for each cell with arriving electrons
if (debug) {
std::cout << "gramselecsim main: Readout waveforms for event " << (*eventID)
<< ":" << std::endl
<< (*readoutWaveforms)
<< std::endl;
}
outputTree->Fill();
if (debug) {
std::cout << "gramselecsim main: Output ntuple filled" << std::endl;
}
} // for each event
if (verbose) {
time_t t2 = time(NULL);
std::cout << "Time: " << t2 - t1 << "s" << std::endl;
}
// Build an index for this tree. This will allow downstream
// programs to quickly access a given EventID within the tree.
outputTree->BuildIndex("EventID.Index()");
outputTree->Write();
output->Close();
}