forked from SuperNEMO-DBD/SensitivityModule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensitivityModule.cpp
368 lines (332 loc) · 17.8 KB
/
SensitivityModule.cpp
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
#include "SensitivityModule.h"
const double highEnergyLimit=0.150;// 150 keV
const double lowEnergyLimit=0.050; // 50 keV
const double electronMass=0.5109989461; // From pdg
const double speedOfLight=299792458 * 1e-9 * 1000; // Millimeters per nanosecond
using namespace std;
DPP_MODULE_REGISTRATION_IMPLEMENT(SensitivityModule,"SensitivityModule");
SensitivityModule::SensitivityModule() : dpp::base_module()
{
filename_output_="sim_data_output.root";
}
SensitivityModule::~SensitivityModule() {
if (is_initialized()) this->reset();
}
void SensitivityModule::initialize(const datatools::properties& myConfig,
datatools::service_manager& flServices,
dpp::module_handle_dict_type& /*moduleDict*/){
// Look for services
if (flServices.has("geometry")) {
const geomtools::geometry_service& GS = flServices.get<geomtools::geometry_service> ("geometry");
geometry_manager_ = &GS.get_geom_manager();
DT_THROW_IF(!geometry_manager_,
std::runtime_error,
"Null pointer to geometry manager return by geometry_service");
}
// Extract the filename_out key from the supplied config, if
// the key exists. datatools::properties throws an exception if
// the key isn't in the config, so catch this if thrown and don't do
// anything
try {
myConfig.fetch("filename_out",this->filename_output_);
} catch (std::logic_error& e) {}
// Use the method of PTD2ROOT to create a root file with just the branches we need for the sensitivity analysis
hfile_ = new TFile(filename_output_.c_str(),"RECREATE","Output file of Simulation data");
hfile_->cd();
tree_ = new TTree("SimData","SimData");
tree_->SetDirectory(hfile_);
// Truth quantities (only applicable to simulation)
tree_->Branch("higher_energy", &simdata_.higher_energy_);
tree_->Branch("lower_energy", &simdata_.lower_energy_);
tree_->Branch("higher_particle_type", &simdata_.higher_particle_type_);
tree_->Branch("lower_particle_type", &simdata_.lower_particle_type_);
tree_->Branch("total_energy", &simdata_.total_energy_);
tree_->Branch("og_energy", &simdata_.og_energy_);
tree_->Branch("og_momentum", &simdata_.og_momentum_);
tree_->Branch("og_vertex_x", &simdata_.og_vertex_x_);
tree_->Branch("og_vertex_y", &simdata_.og_vertex_y_);
tree_->Branch("og_vertex_z", &simdata_.og_vertex_z_);
tree_->Branch("og_momentum_x", &simdata_.og_momentum_x_);
tree_->Branch("og_momentum_y", &simdata_.og_momentum_y_);
tree_->Branch("og_momentum_z", &simdata_.og_momentum_z_);
tree_->Branch("step_start_vertex_x", &simdata_.step_start_vertex_x_);
tree_->Branch("step_start_vertex_y", &simdata_.step_start_vertex_y_);
tree_->Branch("step_start_vertex_z", &simdata_.step_start_vertex_z_);
tree_->Branch("step_stop_vertex_x", &simdata_.step_stop_vertex_x_);
tree_->Branch("step_stop_vertex_y", &simdata_.step_stop_vertex_y_);
tree_->Branch("step_stop_vertex_z", &simdata_.step_stop_vertex_z_);
//tree_->Branch("step_start_momentum_x", &simdata_.step_start_momentum_x_);
//tree_->Branch("step_start_momentum_y", &simdata_.step_start_momentum_y_);
//tree_->Branch("step_start_momentum_z", &simdata_.step_start_momentum_z_);
//tree_->Branch("step_stop_momentum_x", &simdata_.step_stop_momentum_x_);
//tree_->Branch("step_stop_momentum_y", &simdata_.step_stop_momentum_y_);
//tree_->Branch("step_stop_momentum_z", &simdata_.step_stop_momentum_z_);
tree_->Branch("step_start_time", &simdata_.step_start_time_);
tree_->Branch("step_stop_time", &simdata_.step_stop_time_);
tree_->Branch("step_start_momentum", &simdata_.step_start_momentum_);
tree_->Branch("step_stop_momentum", &simdata_.step_stop_momentum_);
tree_->Branch("step_entering_volume", &simdata_.step_entering_volume_);
tree_->Branch("step_exiting_volume", &simdata_.step_exiting_volume_);
//tree_->Branch("step_material", &simdata_.step_material_);
tree_->Branch("step_energy_loss", &simdata_.step_energy_loss_);
tree_->Branch("step_track_id", &simdata_.step_track_id_);
tree_->Branch("energy_loss", &simdata_.energy_loss_);
tree_->Branch("displacement", &simdata_.displacement_);
tree_->Branch("track_ids", &simdata_.track_ids_);
tree_->Branch("escaped_foil", &simdata_.escaped_foil_);
this->_set_initialized(true);
}
//! [SensitivityModule::Process]
dpp::base_module::process_status
SensitivityModule::process(datatools::things& workItem) {
double total_energy = 0.0;
double higher_energy = 0.0;
double lower_energy = 0.0;
double higher_momentum = 0.0;
double lower_momentum = 0.0;
int higher_type = 0;
int lower_type = 0;
int precision = 11;
bool store_event = true;
std::vector<int> event_ids;
std::vector<double> momenta;
std::vector<double> og_momenta_x;
std::vector<double> og_momenta_y;
std::vector<double> og_momenta_z;
std::vector<double> energies;
std::vector<double> energy_loss {0.0, 0.0};
std::vector<double> displacement {0.0, 0.0};
std::vector<bool> first_bool {false, false};
std::vector<bool> second_bool {false, false};
std::vector<bool> third_bool {false, false};
// std::vector<bool> escaped_foil {false, false};
// From SD bank (simulated data - i.e. generator level):
// Get (true) energy of two most energetic particles
// Get (true) primary vertex position
try
{
const mctools::simulated_data& sim_data = workItem.get<mctools::simulated_data>("SD");
if (sim_data.has_data())
{
simdata_.og_vertex_x_ = sim_data.get_vertex().x();
simdata_.og_vertex_y_ = sim_data.get_vertex().y();
simdata_.og_vertex_z_ = sim_data.get_vertex().z();
mctools::simulated_data::primary_event_type primary_event = sim_data.get_primary_event();
for (int i = 0; i < primary_event.get_number_of_particles(); i++)// should be 2 particles for 0nubb
{
genbb::primary_particle particle= primary_event.get_particle(i);
double energy = particle.get_kinetic_energy();
double type = particle.get_type();
double momentum = trunc(std::pow( std::pow(particle.get_momentum().x(), 2) +
std::pow(particle.get_momentum().y(), 2) +
std::pow(particle.get_momentum().z(), 2) , 0.5)
*std::pow(10, precision))/std::pow(10, precision);
total_energy += energy;
// Populate the two highest true energies
if (energy > higher_energy)
{
lower_energy = higher_energy;
lower_type = higher_type;
higher_energy = energy;
higher_type = type;
}
else if (energy > lower_energy)
{
lower_energy = energy;
lower_type = type;
}
if (momentum > higher_momentum)
{
lower_momentum = higher_momentum;
higher_momentum = momentum;
}
else if (momentum > lower_momentum)
{
lower_momentum = momentum;
}
simdata_.og_energy_.push_back(energy);
simdata_.og_momentum_.push_back(momentum);
simdata_.og_momentum_x_.push_back(particle.get_momentum().x());
simdata_.og_momentum_y_.push_back(particle.get_momentum().y());
simdata_.og_momentum_z_.push_back(particle.get_momentum().z());
// simdata_.escaped_foil_.push_back(false);
}
simdata_.higher_energy_ = higher_energy;
simdata_.lower_energy_ = lower_energy;
simdata_.total_energy_ = total_energy;
simdata_.higher_particle_type_ = higher_type;
simdata_.lower_particle_type_ = lower_type;
/*
* There will be many steps here that are irrelevant to this study
* I want the primary tracks up to the point they exit the mylar
* There is some ambiguity in which track corresponds to which particle - look at initial momenta
* Only store the tracks to visualize - large files
* I want the energy loss and the displacement in the foil for each particle
*/
// First select the tracks based on momentum
for (size_t i=0; i < sim_data.get_number_of_step_hits("__visu.tracks");i++) {
// const datatools::handle<mctools::base_step_hit> step_handle = the_step_handles[i];
const mctools::base_step_hit step = sim_data.get_step_hit("__visu.tracks", i);
double p_start = std::pow(std::pow(step.get_momentum_start().x(), 2) +
std::pow(step.get_momentum_start().y(), 2) +
std::pow(step.get_momentum_start().z(), 2), 0.5);
double p_stop = std::pow(std::pow(step.get_momentum_stop().x(), 2) +
std::pow(step.get_momentum_stop().y(), 2) +
std::pow(step.get_momentum_stop().z(), 2), 0.5);
double temp_p = trunc(p_start * std::pow(10, precision)) / (std::pow(10, precision));
int pos = 0;
for (int j = 0; j < simdata_.og_momentum_.size(); ++j) {
if (temp_p == simdata_.og_momentum_[j]) {
pos = j;
event_ids.push_back(step.get_track_id());
momenta.push_back(simdata_.og_momentum_[j]);
energies.push_back(simdata_.og_energy_[j]);
og_momenta_x.push_back(simdata_.og_momentum_x_[j]);
og_momenta_y.push_back(simdata_.og_momentum_y_[j]);
og_momenta_z.push_back(simdata_.og_momentum_z_[j]);
}
}
}
// Note this isn't great but it is the only way I could think of doing it
if (event_ids.size() != 2){store_event = false;}
for (size_t i=0; i < sim_data.get_number_of_step_hits("__visu.tracks");i++)
{
const mctools::base_step_hit step = sim_data.get_step_hit("__visu.tracks", i);
int track_id = step.get_track_id();
if (!std::count(event_ids.begin(), event_ids.end(), track_id)){continue;}
int pos = 0;
for (int j = 0; j < event_ids.size(); ++j) {
if (event_ids[j] == track_id){pos = j;}
}
bool exit_vol_bool = step.is_leaving_volume();
bool enter_vol_bool = step.is_entering_volume();
if (!first_bool[pos] && exit_vol_bool) {
first_bool[pos] = true;
}
else if (first_bool[pos] && !second_bool[pos] && enter_vol_bool) {
second_bool[pos] = true;
}
// This should be uncommented to look at the effect of the mylar
//else if (first_bool[pos] && second_bool[pos] && !third_bool[pos] && exit_vol_bool) {
// third_bool[pos] = true;
//}
// If the e has exited the foil
// This is a candidate
if (first_bool[pos] && second_bool[pos]){
//escaped_foil[pos] = true;
if (simdata_.escaped_foil_){simdata_.escaped_foil_ = true;}else{simdata_.escaped_foil_ = false;}
continue;
}
// const datatools::handle<mctools::base_step_hit> step_handle = the_step_handles[i];
double p_start = std::pow(std::pow(step.get_momentum_start().x(), 2) +
std::pow(step.get_momentum_start().y(), 2) +
std::pow(step.get_momentum_start().z(), 2), 0.5);
double p_stop = std::pow(std::pow(step.get_momentum_stop().x(), 2) +
std::pow(step.get_momentum_stop().y(), 2) +
std::pow(step.get_momentum_stop().z(), 2), 0.5);
double x_start = step.get_position_start().x();
double y_start = step.get_position_start().y();
double z_start = step.get_position_start().z();
double x_stop = step.get_position_stop().x();
double y_stop = step.get_position_stop().y();
double z_stop = step.get_position_stop().z();
double d = std::pow(std::pow(x_stop - x_start, 2) + std::pow(x_stop - x_start, 2) + std::pow(x_stop - x_start, 2), 0.5);
simdata_.step_start_vertex_x_.push_back(x_start);
simdata_.step_start_vertex_y_.push_back(y_start);
simdata_.step_start_vertex_z_.push_back(z_start);
simdata_.step_stop_vertex_x_.push_back(x_stop);
simdata_.step_stop_vertex_y_.push_back(y_stop);
simdata_.step_stop_vertex_z_.push_back(z_stop);
simdata_.step_start_momentum_x_.push_back(step.get_momentum_start().x());
simdata_.step_start_momentum_y_.push_back(step.get_momentum_start().y());
simdata_.step_start_momentum_z_.push_back(step.get_momentum_start().z());
simdata_.step_stop_momentum_x_.push_back(step.get_momentum_stop().x());
simdata_.step_stop_momentum_y_.push_back(step.get_momentum_stop().y());
simdata_.step_stop_momentum_z_.push_back(step.get_momentum_stop().z());
simdata_.step_start_time_.push_back(step.get_time_start());
simdata_.step_stop_time_.push_back(step.get_time_stop());
simdata_.step_start_momentum_.push_back(p_start);
simdata_.step_stop_momentum_.push_back(p_stop);
simdata_.step_entering_volume_.push_back(enter_vol_bool);
simdata_.step_exiting_volume_.push_back(exit_vol_bool);
simdata_.step_material_.push_back(step.get_material_name());
simdata_.step_energy_loss_.push_back(step.get_energy_deposit());
simdata_.step_track_id_.push_back(step.get_track_id());
energy_loss[pos] += step.get_energy_deposit();
displacement[pos] += d;
}
// As the particles and the tracks were not ordered the same (god knows why) we need to rearrange
// the vectors that are being stored so they all match by index in the ntuple
simdata_.energy_loss_ = energy_loss;
simdata_.displacement_ = displacement;
simdata_.track_ids_ = event_ids;
simdata_.og_momentum_ = momenta;
simdata_.og_energy_ = energies;
simdata_.og_momentum_x_ = og_momenta_x;
simdata_.og_momentum_y_ = og_momenta_y;
simdata_.og_momentum_z_ = og_momenta_z;
// simdata_.escaped_foil_ = escaped_foil;
}
} // end try for SD bank
catch (std::logic_error& e) {
std::cerr << "failed to grab SD bank : " << e.what() << std::endl;
return dpp::base_module::PROCESS_ERROR;
// This is OK, if it's data there will be no SD bank
} // end catch for SD bank
if (store_event){tree_->Fill();}
ResetVars();
// MUST return a status, see ref dpp::processing_status_flags_type
return dpp::base_module::PROCESS_OK;
}
//! [SensitivityModule::reset]
void SensitivityModule::reset() {
hfile_->cd();
tree_->Write();
hfile_->Close(); //
std::clog << "In reset: finished conversion, file closed " << std::endl;
// clean up
delete hfile_;
filename_output_ = "output.root";
this->_set_initialized(false);
}
void SensitivityModule::ResetVars()
{
simdata_.higher_energy_ = -1.;
simdata_.lower_energy_ = -1.;
simdata_.total_energy_ = -1.;
simdata_.higher_particle_type_ = -1.;
simdata_.lower_particle_type_ = -1.;
simdata_.og_vertex_x_ = -9999.;
simdata_.og_vertex_y_ = -9999.;;
simdata_.og_vertex_z_ = -9999.;;
simdata_.og_energy_.clear();
simdata_.og_momentum_.clear();
simdata_.og_momentum_x_.clear();
simdata_.og_momentum_y_.clear();
simdata_.og_momentum_z_.clear();
simdata_.step_start_vertex_x_.clear();
simdata_.step_start_vertex_y_.clear();
simdata_.step_start_vertex_z_.clear();
simdata_.step_stop_vertex_x_.clear();
simdata_.step_stop_vertex_y_.clear();
simdata_.step_stop_vertex_z_.clear();
simdata_.step_start_momentum_x_.clear();
simdata_.step_start_momentum_y_.clear();
simdata_.step_start_momentum_z_.clear();
simdata_.step_stop_momentum_x_.clear();
simdata_.step_stop_momentum_y_.clear();
simdata_.step_stop_momentum_z_.clear();
simdata_.step_start_time_.clear();
simdata_.step_stop_time_.clear();
simdata_.step_start_momentum_.clear();
simdata_.step_stop_momentum_.clear();
simdata_.step_entering_volume_.clear();
simdata_.step_exiting_volume_.clear();
simdata_.step_material_.clear();
simdata_.step_energy_loss_.clear();
simdata_.step_track_id_.clear();
simdata_.energy_loss_.clear();
simdata_.displacement_.clear();
simdata_.track_ids_.clear();
simdata_.escaped_foil_ = true;
}