-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeadCellsModule.cpp
368 lines (298 loc) · 14.1 KB
/
DeadCellsModule.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
/*=================================================================
/
/ SuperNemo - Dead Cells Module
/
/ Paolo Franchini 2020 - [email protected]
/ https://github.com/pfranchini/SuperNEMO-DeadCellsModule
/
===================================================================*/
#include "DeadCellsModule.h"
// output file
dpp::output_module simOutput;
// event output
datatools::things workItem;
dpp::base_module::process_status status;
snemo::datamodel::calibrated_data *new_calibrated_data;
// metadata
FLSimulate::FLSimulateArgs flSimParameters;
// various variables
bool randomize = true;
std::string filename_output = "test-output.brio";
int N_dead_cells;
std::string dead_cells_list;
int dead_cells[2500][3]; // Define a matrix (side, layer, column) of dead cells
Long64_t hits = 0;
Long64_t killed = 0;
DPP_MODULE_REGISTRATION_IMPLEMENT(DeadCellsModule,"DeadCellsModule");
//=================================================================
DeadCellsModule::DeadCellsModule() : dpp::base_module() {}
DeadCellsModule::~DeadCellsModule() {
//if (is_initialized())
this->reset();
}
falaise::exit_code do_metadata( const FLSimulate::FLSimulateArgs& flSimParameters, datatools::multi_properties& flSimMetadata) {
falaise::exit_code code = falaise::EXIT_OK;
// System section:
datatools::properties& system_props = flSimMetadata.add_section("flsimulate", "flsimulate::section");
system_props.set_description("flsimulate basic system informations");
system_props.store_string("bayeux.version", bayeux::version::get_version(),
"Bayeux version");
system_props.store_string("falaise.version", falaise::version::get_version(),
"Falaise version");
system_props.store_string(
"application", "flsimulate",
"The simulation application used to produce Monte Carlo data");
system_props.store_string("application.version",
falaise::version::get_version(),
"The version of the simulation application");
system_props.store_string("userProfile", flSimParameters.userProfile,
"User profile");
system_props.store_integer("numberOfEvents", flSimParameters.numberOfEvents,
"Number of simulated events");
system_props.store_boolean("doSimulation", flSimParameters.doSimulation,
"Activate simulation");
system_props.store_boolean("doDigitization", flSimParameters.doDigitization,
"Activate digitization");
if (!flSimParameters.experimentalSetupUrn.empty())
system_props.store_string("experimentalSetupUrn",
flSimParameters.experimentalSetupUrn,
"Experimental setup URN");
system_props.store_boolean("embeddedMetadata",
flSimParameters.embeddedMetadata,
"Metadata embedding flag");
boost::posix_time::ptime start_run_timestamp = boost::posix_time::second_clock::universal_time();
system_props.store_string( "timestamp", boost::posix_time::to_iso_string(start_run_timestamp), "Run start timestamp");
if (flSimParameters.doSimulation) {
// Simulation section:
datatools::properties& simulation_props = flSimMetadata.add_section("flsimulate.simulation", "flsimulate::section");
simulation_props.set_description("Simulation setup parameters");
if (!flSimParameters.simulationSetupUrn.empty()) {
simulation_props.store_string("simulationSetupUrn",
flSimParameters.simulationSetupUrn,
"Simulation setup URN");
} else if (!flSimParameters.simulationManagerParams.manager_config_filename
.empty()) {
simulation_props.store_path(
"simulationManagerConfig",
flSimParameters.simulationManagerParams.manager_config_filename,
"Simulation manager configuration file");
}
}
if (flSimParameters.doDigitization) {
// Digitization section:
datatools::properties& digitization_props = flSimMetadata.add_section(
"flsimulate.digitization", "flsimulate::section");
digitization_props.set_description("Digitization setup parameters");
// Not implemented yet.
}
// Variants section:
datatools::properties& variants_props = flSimMetadata.add_section(
"flsimulate.variantService", "flsimulate::section");
variants_props.set_description("Variant setup");
if (!flSimParameters.variantConfigUrn.empty()) {
variants_props.store_string("configUrn", flSimParameters.variantConfigUrn,
"Variants setup configuration URN");
} else if (!flSimParameters.variantSubsystemParams.config_filename.empty()) {
variants_props.store_path(
"config", flSimParameters.variantSubsystemParams.config_filename,
"Variants setup configuration path");
}
if (!flSimParameters.variantProfileUrn.empty()) {
variants_props.store_string("profileUrn", flSimParameters.variantProfileUrn,
"Variants profile URN");
} else if (!flSimParameters.variantSubsystemParams.profile_load.empty()) {
variants_props.store_path(
"profile", flSimParameters.variantSubsystemParams.profile_load,
"Variants profile path");
}
if (flSimParameters.variantSubsystemParams.settings.size()) {
variants_props.store("settings",
flSimParameters.variantSubsystemParams.settings,
"Variants settings");
}
// Services section:
datatools::properties& services_props =
flSimMetadata.add_section("flsimulate.services", "flsimulate::section");
services_props.set_description("Services configuration");
if (!flSimParameters.servicesSubsystemConfigUrn.empty()) {
services_props.store_string("configUrn",
flSimParameters.servicesSubsystemConfigUrn,
"Services setup configuration URN");
} else if (!flSimParameters.servicesSubsystemConfig.empty()) {
services_props.store_path("config", flSimParameters.servicesSubsystemConfig,
"Services setup configuration path");
}
return code;
}
void dead_cells_service(int dc[2500][3]) {
try {
std::ifstream infile(dead_cells_list); // File for list of dead cells
Long64_t i=0;
int side, layer, column;
while (infile >> side >> layer >> column){
dead_cells[i][0]=side;
dead_cells[i][1]=layer;
dead_cells[i][2]=column;
i++;
}
N_dead_cells=i;
std::cout << "Read " << N_dead_cells << " dead cells" << std::endl;
}
catch (std::logic_error& e) {
std::cerr << "Problem in the list of dead cells " << e.what() << std::endl;
};
};
void DeadCellsModule::initialize(const datatools::properties& myConfig,
datatools::service_manager& flServices,
dpp::module_handle_dict_type& /*moduleDict*/){
// 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
try {
myConfig.fetch("filename_out", filename_output);
}
catch (std::logic_error& e) {
std::cerr << "Problem in the output file " << e.what() << std::endl;
};
// Extract option to create random dead cells or to read them (from a file)
try {
myConfig.fetch("random", randomize);
if (randomize)
try {
myConfig.fetch("N_dead_cells", N_dead_cells);
}
catch (std::logic_error& e) {
std::cerr << "Error in configuration files: number of dead cells not present " << e.what() << std::endl;
}
else
try {
myConfig.fetch("dead_cells", dead_cells_list);
}
catch (std::logic_error& e) {
std::cerr << "Error in configuration files: file with list of dead cells not present " << e.what() << std::endl;
}
} catch (std::logic_error& e) {}
std::cout << "Dead cells module initialized..." << std::endl;
std::cout << "Output file: " << filename_output << std::endl;
flSimParameters = FLSimulate::FLSimulateArgs::makeDefault();
datatools::service_manager services("DeadCells", "SuperNEMO Dead Cells");
datatools::properties services_config;
services_config.store("name", "DeadCells");
services_config.store("description","SuperNEMO Demonstrator Dead Cells module");
std::vector<std::string> scf = {"@falaise:snemo/demonstrator/geometry/" "GeometryService.conf"};
services_config.store("services.configuration_files", scf);
services.initialize(services_config);
// Output metadata management:
datatools::multi_properties flSimMetadata("name", "type", "Metadata associated to a flsimulate run");
do_metadata(flSimParameters, flSimMetadata);
// Metadata management:
simOutput.set_name("DeadCellsOutput");
simOutput.set_single_output_file(filename_output);
datatools::multi_properties& metadataStore = simOutput.grab_metadata_store();
metadataStore = flSimMetadata;
simOutput.initialize_simple();
// set up CD data bank
new_calibrated_data = &(workItem.add<snemo::datamodel::calibrated_data>("CD"));
// Define the dead cells:
if (randomize) {
// Random dead cells
bool isNew = false;
std::cout << "Generating " << N_dead_cells << " random dead cells..." << std::endl;
for (Long64_t i=0; i<N_dead_cells; i++){
// need to check if the dead_cell is not already in the list
isNew = false;
while (!isNew) { // try a random cell
dead_cells[i][0]=rand() % 2; // side
dead_cells[i][1]=rand() % 9; // layer
dead_cells[i][2]=rand() % 113; // column
isNew = true; // assume is new
for (Long64_t j=0; j<i; j++){ // look if is alredy in the matrix
if ( (dead_cells[i][0]==dead_cells[j][0]) && (dead_cells[i][1]==dead_cells[j][1]) && (dead_cells[i][2]==dead_cells[j][2]) ) {
isNew = false; // is already in the matrix so will try again
break;
}
}
}
}
// Save list of dead cells in a text file to be used in another module
std::ofstream deadcells_file;
std::string deadcells_filename = "random_dead_cells_"+std::to_string(N_dead_cells)+".txt";
deadcells_file.open(deadcells_filename);
for (Long64_t i=0; i<N_dead_cells; i++)
deadcells_file << dead_cells[i][0] << " " << dead_cells[i][1] << " " << dead_cells[i][2] << "\n";
deadcells_file.close();
}
else {
// Dead cells from service
std::cout << "Reading dead cells from file..." << std::endl;
dead_cells_service(dead_cells);
}
// print list of dead cells
/*for (Long64_t i=0; i<N_dead_cells; i++)
std::cout << dead_cells[i][0] << "-" << dead_cells[i][1] << "-" << dead_cells[i][2] << std::endl;
*/
this->_set_initialized(true);
}
//! [DeadCellsModule::Process]
dpp::base_module::process_status DeadCellsModule::process(datatools::things& event) {
bool kill=false;
int i = 0;
// Grab Calibrated Data bank
try {
// To Read
//const mctools::simulated_data& CD = event.get<mctools::simulated_data>("CD");
const auto& calData = event.get<snemo::datamodel::calibrated_data>("CD");
snemo::datamodel::calibrated_data::tracker_hit_handle_type new_hit_handle(new snemo::datamodel::calibrated_tracker_hit);
snemo::datamodel::calibrated_tracker_hit& new_calibrated_tracker_hit = new_hit_handle.grab();
// Loops over tracker hits for this event
for (const auto& trackerHitHdl : calData.calibrated_tracker_hits()) {
hits++; // counts the number of hits for all the events
kill=false;
// Find if this hit of this event is on any dead cell or not
for (Long64_t k=0; k<N_dead_cells; k++){
if ( (trackerHitHdl->get_side()==dead_cells[k][0])&&(trackerHitHdl->get_layer()==dead_cells[k][1])&&(trackerHitHdl->get_row()==dead_cells[k][2]) ){ // is dead
kill=true;
killed++;
/*i = &trackerHitHdl - &calData.calibrated_tracker_hits()[0];
std::cout << "hit to be removed: " << i << std::endl;
std::cout << "Tracker Hit: " << trackerHitHdl->get_x() << "," << trackerHitHdl->get_y() << "," << trackerHitHdl->get_z() << std::endl;
std::cout << " " << trackerHitHdl->get_side() << "," << trackerHitHdl->get_layer() << "," << trackerHitHdl->get_row() << std::endl;
*/
break; // ends loop since the hit is already on one dead cell
}
} // end loop in the killing procedure
if (!kill){ // keeps the Tracker data
new_hit_handle = trackerHitHdl;
new_calibrated_data->calibrated_tracker_hits().push_back(new_hit_handle);
}
} // end loop in the tracker hits
// Print output
/*const auto& calData2 = workItem.get<snemo::datamodel::calibrated_data>("CD");
for (const auto& hit : calData2.calibrated_tracker_hits()) {
std::cout << "Saved Hit: " << hit->get_x() << "," << hit->get_y() << "," << hit->get_z() << std::endl;
std::cout << " " << hit->get_side() << "," << hit->get_layer() << "," << hit->get_row() << std::endl;
}
*/
// Calorimeter data kept in any case
new_calibrated_data->calibrated_calorimeter_hits() = calData.calibrated_calorimeter_hits();
// write workItem in the new brio file
dpp::base_module::process_status status = simOutput.process(workItem);
if (status != dpp::base_module::PROCESS_OK)
std::cerr << "Output module failed" << std::endl;
// clear for next event
new_calibrated_data->reset();
}
catch (std::logic_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
return dpp::base_module::PROCESS_INVALID;
}// end try on CD bank
// returns a status, see ref dpp::processing_status_flags_type
return dpp::base_module::PROCESS_OK;
}
//! [DeadCellsModule::reset]
void DeadCellsModule::reset() {
// Print stats
std::cout << "Total number of hits in the tracker: " << hits << std::endl;
std::cout << "Killed hits: " << killed << std::endl;
std::cout << "END" << std::endl;
this->_set_initialized(false);
}