ATLAS Offline Software
Loading...
Searching...
No Matches
TFCSEnergyAndHitGANV2.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include "CLHEP/Random/RandFlat.h"
8#include "CLHEP/Random/RandGauss.h"
9#include "HepPDT/ParticleData.hh"
10#include "HepPDT/ParticleDataTable.hh"
17#include "TF1.h"
18#include "TFile.h"
19#include "TH2D.h"
20
21#if defined(__FastCaloSimStandAlone__)
22#include "CLHEP/Random/TRandomEngine.h"
23#else
24#include <CLHEP/Random/RanluxEngine.h>
25#endif
26
27#include <fstream>
28#include <iostream>
29#include <limits>
30
31//=============================================
32//======= TFCSEnergyAndHitGANV2 =========
33//=============================================
34
36 const char *title)
37 : TFCSParametrizationBinnedChain(name, title) {
39}
40
42 if (m_slice != nullptr) {
43 delete m_slice;
44 }
45}
46
48 if (get_Binning().find(calosample) == get_Binning().cend())
49 return false;
50 if (get_Binning().at(calosample).GetNbinsX() == 1)
51 return false;
52 return true;
53}
54
55unsigned int TFCSEnergyAndHitGANV2::get_nr_of_init(unsigned int bin) const {
56 if (bin >= m_bin_ninit.size())
57 return 0;
58 return m_bin_ninit[bin];
59}
60
62 unsigned int ninit) {
63 if (bin >= m_bin_ninit.size()) {
64 m_bin_ninit.resize(bin + 1, 0);
65 m_bin_ninit.shrink_to_fit();
66 }
67 m_bin_ninit[bin] = ninit;
68}
69
70// initialize lwtnn network
72 int const &pid, int const &etaMin,
73 const std::string &FastCaloGANInputFolderName) {
74
75 // initialize all necessary constants
76 // FIXME eventually all these could be stored in the .json file
77
79 "Using FastCaloGANInputFolderName: " << FastCaloGANInputFolderName);
80 // get neural net JSON file as an std::istream object
81 const int etaMax = etaMin + 5;
82
84 set_pdgid(pid);
85 if (pid == 11)
86 add_pdgid(-pid);
87 if (pid == 211)
88 add_pdgid(-pid);
89 set_eta_min(etaMin / 100.0);
90 set_eta_max(etaMax / 100.0);
91 set_eta_nominal((etaMin + etaMax) / 200.0);
92
93 int pidForXml = pid;
94 if (pid != 22 && pid != 11) {
95 pidForXml = 211;
96 }
97
98 const int etaMid = (etaMin + etaMax) / 2;
99 m_param.InitialiseFromXML(pidForXml, etaMid, FastCaloGANInputFolderName);
100 m_param.Print();
101 m_slice = new TFCSGANEtaSlice(pid, etaMin, etaMax, m_param);
102 m_slice->Print();
103 return m_slice->LoadGAN();
104}
105
107 TFCSSimulationState &simulstate, const TFCSTruthState *,
108 const TFCSExtrapolationState *) const {
109 return std::string(
110 Form("layer=%d", simulstate.getAuxInfo<int>("GANlayer"_FCShash)));
111}
112
114 TFCSSimulationState &simulstate, const TFCSTruthState *truth,
115 const TFCSExtrapolationState *extrapol) const {
116 if (!truth) {
117 ATH_MSG_ERROR("Invalid truth pointer");
118 return false;
119 }
120
121 if (!extrapol) {
122 ATH_MSG_ERROR("Invalid extrapolation pointer");
123 return false;
124 }
125
126 const int pdgId = truth->pdgid();
127 const float charge = HepPDT::ParticleID(pdgId).charge();
128
129 float Einit;
130 const float Ekin = truth->Ekin();
131 if (OnlyScaleEnergy())
132 Einit = simulstate.E();
133 else
134 Einit = Ekin;
135
136 ATH_MSG_VERBOSE("Momentum " << truth->P() << " pdgId " << truth->pdgid());
137 // check that the network exists
138 if (!m_slice->IsGanCorrectlyLoaded()) {
139 ATH_MSG_WARNING("GAN not loaded correctly.");
140 return false;
141 }
142 // This lock is an attempt to fix ATLASSIM-7031. remove if not necessary
143 // Hold until NetworkOutputs goes out of scope
144 std::scoped_lock lock(m_mutex);
146 m_slice->GetNetworkOutputs(truth, extrapol, simulstate);
147 ATH_MSG_VERBOSE("network outputs size: " << outputs.size());
148
149 const TFCSGANXMLParameters::Binning &binsInLayers = m_param.GetBinning();
150 const auto ganVersion = m_param.GetGANVersion();
151 const TFCSGANEtaSlice::FitResultsPerLayer &fitResults =
152 m_slice->GetFitResults(); // used only if GAN version > 1
153
154 ATH_MSG_DEBUG("energy voxels size = " << outputs.size());
155
156 double totalEnergy = 0;
157 for (const auto & output : outputs) {
158 totalEnergy += output.second;
159 }
160 if (totalEnergy < 0) {
161 ATH_MSG_WARNING("Energy from GAN is negative, skipping particle");
162 return false;
163 }
164
165 ATH_MSG_VERBOSE("Get binning");
166
167 simulstate.set_E(0);
168
169 int vox = 0;
170 for (const auto &[layer, h] : binsInLayers) {
171 // attempt to debug intermittent ci issues described in
172 // https://its.cern.ch/jira/browse/ATLASSIM-7031
173 if (h.IsZombie()) {
174 ATH_MSG_INFO("Got truth state: ");
175 truth->Print();
176
177 ATH_MSG_INFO("Got extrapolation state: ");
178 extrapol->Print();
179
180 ATH_MSG_INFO("Got simulation state: ");
181 simulstate.Print();
182
183 ATH_MSG_INFO("Got GAN XML parameters: ");
184 m_param.Print();
185
186 ATH_MSG_ERROR("Histogram pointer for layer " << layer << " is broken");
187 return false;
188 }
189
190 const int xBinNum = h.GetNbinsX();
191 const int yBinNum = h.GetNbinsY();
192 const TAxis *x = h.GetXaxis();
193
194 // If only one bin in r means layer is empty, no value should be added
195 if (xBinNum == 1) {
196 ATH_MSG_VERBOSE(" Layer "
197 << layer
198 << " has only one bin in r, this means is it not used, "
199 "skipping (this is needed to keep correct "
200 "syncronisation of voxel and layers)");
201 // delete h;
202 continue;
203 }
204
205 ATH_MSG_VERBOSE(" Getting energy for Layer " << layer);
206
207 // First fill energies
208 for (int ix = 1; ix <= xBinNum; ++ix) {
209 double binsInAlphaInRBin = GetAlphaBinsForRBin(x, ix, yBinNum);
210 for (int iy = 1; iy <= binsInAlphaInRBin; ++iy) {
211 const double energyInVoxel = outputs.at(std::to_string(vox));
212 ATH_MSG_VERBOSE(" Vox " << vox << " energy " << energyInVoxel
213 << " binx " << ix << " biny " << iy);
214
215 if (energyInVoxel <= 0) {
216 vox++;
217 continue;
218 }
219
220 simulstate.add_E(layer, Einit * energyInVoxel);
221 vox++;
222 }
223 }
224 }
225
226 for (unsigned int ichain = m_bin_start.back(); ichain < size(); ++ichain) {
227 ATH_MSG_DEBUG("now run for all bins: " << chain()[ichain]->GetName());
228 if (simulate_and_retry(chain()[ichain], simulstate, truth, extrapol) !=
229 FCSSuccess) {
230 return FCSFatal;
231 }
232 }
233
234 vox = 0;
235 for (const auto &[layer, h] : binsInLayers) {
236 const int xBinNum = h.GetNbinsX();
237 const int yBinNum = h.GetNbinsY();
238 const TAxis *x = h.GetXaxis();
239 const TAxis *y = h.GetYaxis();
240
241 simulstate.setAuxInfo<int>("GANlayer"_FCShash, layer);
243
244 // If only one bin in r means layer is empty, no value should be added
245 if (xBinNum == 1) {
246 ATH_MSG_VERBOSE(" Layer "
247 << layer
248 << " has only one bin in r, this means is it not used, "
249 "skipping (this is needed to keep correct "
250 "syncronisation of voxel and layers)");
251 // delete h;
252 continue;
253 }
254
255 if (get_number_of_bins() > 0) {
256 const int bin = get_bin(simulstate, truth, extrapol);
257 if (bin >= 0 && bin < (int)get_number_of_bins()) {
258 for (unsigned int ichain = m_bin_start[bin];
259 ichain < TMath::Min(m_bin_start[bin] + get_nr_of_init(bin),
260 m_bin_start[bin + 1]);
261 ++ichain) {
262 ATH_MSG_DEBUG("for " << get_variable_text(simulstate, truth, extrapol)
263 << " run init " << get_bin_text(bin) << ": "
264 << chain()[ichain]->GetName());
265 if (chain()[ichain]->InheritsFrom(
266 TFCSLateralShapeParametrizationHitBase::Class())) {
268 static_cast<TFCSLateralShapeParametrizationHitBase *>(chain()[ichain]);
269 if (sim->simulate_hit(hit, simulstate, truth, extrapol) !=
270 FCSSuccess) {
271 ATH_MSG_ERROR("error for "
272 << get_variable_text(simulstate, truth, extrapol)
273 << " run init " << get_bin_text(bin) << ": "
274 << chain()[ichain]->GetName());
275 return false;
276 }
277 } else {
278 ATH_MSG_ERROR("for "
279 << get_variable_text(simulstate, truth, extrapol)
280 << " run init " << get_bin_text(bin) << ": "
281 << chain()[ichain]->GetName()
282 << " does not inherit from "
283 "TFCSLateralShapeParametrizationHitBase");
284 return false;
285 }
286 }
287 } else {
288 ATH_MSG_WARNING("nothing to init for "
289 << get_variable_text(simulstate, truth, extrapol)
290 << ": " << get_bin_text(bin));
291 }
292 }
293
294 int binResolution = 5;
295 if (layer == 1 || layer == 5) {
296 binResolution = 1;
297 }
298
299 const double center_eta = hit.center_eta();
300 const double center_phi = hit.center_phi();
301 const double center_r = hit.center_r();
302 const double center_z = hit.center_z();
303
304 ATH_MSG_VERBOSE(" Layer " << layer << " Extrap eta " << center_eta
305 << " phi " << center_phi << " R " << center_r);
306
307 const float dist000 =
308 TMath::Sqrt(center_r * center_r + center_z * center_z);
309 const float eta_jakobi = TMath::Abs(2.0 * TMath::Exp(-center_eta) /
310 (1.0 + TMath::Exp(-2 * center_eta)));
311
312 int nHitsAlpha{};
313 int nHitsR{};
314
315 // Now create hits
316 for (int ix = 1; ix <= xBinNum; ++ix) {
317 const int binsInAlphaInRBin = GetAlphaBinsForRBin(x, ix, yBinNum);
318
319 // Horrible work around for variable # of bins along alpha direction
320 const int binsToMerge = yBinNum == 32 ? 32 / binsInAlphaInRBin : 1;
321 for (int iy = 1; iy <= binsInAlphaInRBin; ++iy) {
322 const double energyInVoxel = outputs.at(std::to_string(vox));
323 const int lowEdgeIndex = (iy - 1) * binsToMerge + 1;
324
325 ATH_MSG_VERBOSE(" Vox " << vox << " energy " << energyInVoxel
326 << " binx " << ix << " biny " << iy);
327
328 if (energyInVoxel <= 0) {
329 vox++;
330 continue;
331 }
332
333 if (std::abs(pdgId) == 22 || std::abs(pdgId) == 11) {
334 // maximum 10 MeV per hit, equaly distributed in alpha and r
335 int maxHitsInVoxel = energyInVoxel * truth->Ekin() / 10;
336 if (maxHitsInVoxel < 1)
337 maxHitsInVoxel = 1;
338 nHitsAlpha = std::sqrt(maxHitsInVoxel);
339 nHitsR = std::sqrt(maxHitsInVoxel);
340 } else {
341 // One hit per mm along r
342 nHitsR = x->GetBinUpEdge(ix) - x->GetBinLowEdge(ix);
343 if (yBinNum == 1) {
344 // nbins in alpha depend on circumference lenght
345 const double r = x->GetBinUpEdge(ix);
346 nHitsAlpha = ceil(2 * TMath::Pi() * r / binResolution);
347 } else {
348 // d = 2*r*sin (a/2r) this distance at the upper r must be 1mm for
349 // layer 1 or 5, 5mm otherwise.
350 const double angle = y->GetBinUpEdge(iy) - y->GetBinLowEdge(iy);
351 const double r = x->GetBinUpEdge(ix);
352 const double d = 2 * r * sin(angle / 2 * r);
353 nHitsAlpha = ceil(d / binResolution);
354 }
355
356 if (layer != 1 && layer != 5) {
357 // For layers that are not EMB1 or EMEC1 use a maximum of 10 hits
358 // per direction, a higher granularity is needed for the other
359 // layers
360 const int maxNhits = 10;
361 nHitsAlpha = std::min(maxNhits, std::max(1, nHitsAlpha));
362 nHitsR = std::min(maxNhits, std::max(1, nHitsR));
363 }
364 }
365
366 for (int ir = 0; ir < nHitsR; ++ir) {
367 double r =
368 x->GetBinLowEdge(ix) + x->GetBinWidth(ix) / (nHitsR + 1) * ir;
369
370 for (int ialpha = 1; ialpha <= nHitsAlpha; ++ialpha) {
371 if (ganVersion > 1) {
372 if (fitResults.at(layer)[ix - 1] != 0) {
373 int tries = 0;
374 double a = CLHEP::RandFlat::shoot(simulstate.randomEngine(),
375 x->GetBinLowEdge(ix),
376 x->GetBinUpEdge(ix));
377 double rand_r =
378 log((a - x->GetBinLowEdge(ix)) / (x->GetBinWidth(ix))) /
379 fitResults.at(layer)[ix - 1];
380 while ((rand_r < x->GetBinLowEdge(ix) ||
381 rand_r > x->GetBinUpEdge(ix)) &&
382 tries < 100) {
383 a = CLHEP::RandFlat::shoot(simulstate.randomEngine(),
384 x->GetBinLowEdge(ix),
385 x->GetBinUpEdge(ix));
386 rand_r =
387 log((a - x->GetBinLowEdge(ix)) / (x->GetBinWidth(ix))) /
388 fitResults.at(layer)[ix - 1];
389 tries++;
390 }
391 if (tries >= 100) {
392 ATH_MSG_VERBOSE(" Too many tries for bin ["
393 << x->GetBinLowEdge(ix) << "-"
394 << x->GetBinUpEdge(ix) << "] having slope "
395 << fitResults.at(layer)[ix - 1]
396 << " will use grid (old method)");
397 } else {
398 r = rand_r;
399 }
400 }
401 }
402
403 double alpha;
404 if (binsInAlphaInRBin == 1) {
405 alpha = CLHEP::RandFlat::shoot(simulstate.randomEngine(),
406 -TMath::Pi(), TMath::Pi());
407 } else {
408 alpha =
409 y->GetBinLowEdge(lowEdgeIndex) +
410 y->GetBinWidth(iy) * binsToMerge / (nHitsAlpha + 1) * ialpha;
411
412 if (m_param.IsSymmetrisedAlpha()) {
413 if (CLHEP::RandFlat::shoot(simulstate.randomEngine(),
414 -TMath::Pi(), TMath::Pi()) < 0) {
415 alpha = -alpha;
416 }
417 }
418 }
419
420 hit.reset();
421 hit.E() = Einit * energyInVoxel / (nHitsAlpha * nHitsR);
422
423 if (layer <= 20) {
424 float delta_eta_mm = r * cos(alpha);
425 float delta_phi_mm = r * sin(alpha);
426
427 ATH_MSG_VERBOSE("delta_eta_mm " << delta_eta_mm
428 << " delta_phi_mm "
429 << delta_phi_mm);
430
431 // Particles with negative eta are expected to have the same shape
432 // as those with positive eta after transformation: delta_eta -->
433 // -delta_eta
434 if (center_eta < 0.)
435 delta_eta_mm = -delta_eta_mm;
436 // We derive the shower shapes for electrons and positively
437 // charged hadrons. Particle with the opposite charge are expected
438 // to have the same shower shape after the transformation:
439 // delta_phi --> -delta_phi
440 if ((charge < 0. && pdgId != 11) || pdgId == -11)
441 delta_phi_mm = -delta_phi_mm;
442
443 const float delta_eta = delta_eta_mm / eta_jakobi / dist000;
444 const float delta_phi = delta_phi_mm / center_r;
445
446 hit.eta() = center_eta + delta_eta;
447 hit.phi() = TVector2::Phi_mpi_pi(center_phi + delta_phi);
448
449 ATH_MSG_VERBOSE(" Hit eta " << hit.eta() << " phi " << hit.phi()
450 << " layer " << layer);
451 } else { // FCAL is in (x,y,z)
452 const float hit_r = r * cos(alpha) + center_r;
453 float delta_phi = r * sin(alpha) / center_r;
454 // We derive the shower shapes for electrons and positively
455 // charged hadrons. Particle with the opposite charge are expected
456 // to have the same shower shape after the transformation:
457 // delta_phi --> -delta_phi
458 if ((charge < 0. && pdgId != 11) || pdgId == -11)
459 delta_phi = -delta_phi;
460 const float hit_phi =
461 TVector2::Phi_mpi_pi(center_phi + delta_phi);
462 hit.x() = hit_r * cos(hit_phi);
463 hit.y() = hit_r * sin(hit_phi);
464 hit.z() = center_z;
465 ATH_MSG_VERBOSE(" Hit x " << hit.x() << " y " << hit.y()
466 << " layer " << layer);
467 }
468
469 if (get_number_of_bins() > 0) {
470 const int bin = get_bin(simulstate, truth, extrapol);
471 if (bin >= 0 && bin < (int)get_number_of_bins()) {
472 for (unsigned int ichain =
474 ichain < m_bin_start[bin + 1]; ++ichain) {
476 "for " << get_variable_text(simulstate, truth, extrapol)
477 << " run " << get_bin_text(bin) << ": "
478 << chain()[ichain]->GetName());
479 if (chain()[ichain]->InheritsFrom(
480 TFCSLateralShapeParametrizationHitBase::Class())) {
483 *>(chain()[ichain]);
484 if (sim->simulate_hit(hit, simulstate, truth, extrapol) !=
485 FCSSuccess) {
487 "error for "
488 << get_variable_text(simulstate, truth, extrapol)
489 << " run init " << get_bin_text(bin) << ": "
490 << chain()[ichain]->GetName());
491 return false;
492 }
493 } else {
495 "for " << get_variable_text(simulstate, truth, extrapol)
496 << " run init " << get_bin_text(bin) << ": "
497 << chain()[ichain]->GetName()
498 << " does not inherit from "
499 "TFCSLateralShapeParametrizationHitBase");
500 return false;
501 }
502 }
503 } else {
505 "nothing to do for "
506 << get_variable_text(simulstate, truth, extrapol) << ": "
507 << get_bin_text(bin));
508 }
509 } else {
510 ATH_MSG_WARNING("no bins defined, is this intended?");
511 }
512 }
513 }
514 vox++;
515 }
516 }
517
518 ATH_MSG_VERBOSE("Number of voxels " << vox);
519 ATH_MSG_VERBOSE("Done layer " << layer);
520 }
521
522 if (simulstate.E() > std::numeric_limits<double>::epsilon()) {
523 for (int ilayer = 0; ilayer < CaloCell_ID_FCS::MaxSample; ++ilayer) {
524 simulstate.set_Efrac(ilayer, simulstate.E(ilayer) / simulstate.E());
525 }
526 }
527
528 ATH_MSG_VERBOSE("Done particle");
529 return true;
530}
531
533 TFCSSimulationState &simulstate, const TFCSTruthState *truth,
534 const TFCSExtrapolationState *extrapol) const {
535 for (unsigned int ichain = 0; ichain < m_bin_start[0]; ++ichain) {
536 ATH_MSG_DEBUG("now run for all bins: " << chain()[ichain]->GetName());
537 if (simulate_and_retry(chain()[ichain], simulstate, truth, extrapol) !=
538 FCSSuccess) {
539 return FCSFatal;
540 }
541 }
542
543 ATH_MSG_VERBOSE("Fill Energies");
544 if (!fillEnergy(simulstate, truth, extrapol)) {
545 ATH_MSG_WARNING("Could not fill energies ");
546 // bail out but do not stop the job
547 return FCSSuccess;
548 }
549
550 return FCSSuccess;
551}
552
553void TFCSEnergyAndHitGANV2::Print(Option_t *option) const {
555 TString opt(option);
556 const bool shortprint = opt.Index("short") >= 0;
557 const bool longprint =
558 msgLvl(MSG::DEBUG) || (msgLvl(MSG::INFO) && !shortprint);
559 TString optprint = opt;
560 optprint.ReplaceAll("short", "");
561
562 TString prefix = "- ";
563 for (unsigned int ichain = 0; ichain < size(); ++ichain) {
564 if (ichain == 0 && ichain != m_bin_start.front()) {
565 prefix = "> ";
566 if (longprint)
567 ATH_MSG_INFO(optprint << prefix << "Run for all bins");
568 }
569 for (unsigned int ibin = 0; ibin < get_number_of_bins(); ++ibin) {
570 if (ichain == m_bin_start[ibin]) {
571 if (ibin < get_number_of_bins() - 1)
572 if (ichain == m_bin_start[ibin + 1])
573 continue;
574 prefix = Form("%-2d", ibin);
575 if (longprint)
576 ATH_MSG_INFO(optprint << prefix << "Run for " << get_bin_text(ibin));
577 }
578 }
579 if (ichain == m_bin_start.back()) {
580 prefix = "< ";
581 if (longprint)
582 ATH_MSG_INFO(optprint << prefix << "Run for all bins");
583 }
584 chain()[ichain]->Print(opt + prefix);
585 }
586}
587
589 const TFCSTruthState *truth,
590 const TFCSExtrapolationState *extrapol) {
592 ATH_MSG_NOCLASS(logger, "Start lwtnn test" << std::endl);
593 std::string path =
594 "/eos/atlas/atlascerngroupdisk/proj-simul/AF3_Run3/"
595 "InputsToBigParamFiles/FastCaloGANWeightsVer02/";
596 test_path(path, simulstate, truth, extrapol, "lwtnn");
597
598 ATH_MSG_NOCLASS(logger, "Start onnx test" << std::endl);
599 path =
600 "/eos/atlas/atlascerngroupdisk/proj-simul/AF3_Run3/"
601 "InputsToBigParamFiles/FastCaloGANWeightsONNXVer08/";
602 test_path(path, simulstate, truth, extrapol, "onnx");
603 ATH_MSG_NOCLASS(logger, "Finish all tests" << std::endl);
604}
605
606void TFCSEnergyAndHitGANV2::test_path(const std::string &path,
607 TFCSSimulationState *simulstate,
608 const TFCSTruthState *truth,
609 const TFCSExtrapolationState *extrapol,
610 const std::string &outputname, int pid) {
612 ATH_MSG_NOCLASS(logger, "Running test on " << path << std::endl);
613 if (!simulstate) {
614 simulstate = new TFCSSimulationState();
615#if defined(__FastCaloSimStandAlone__)
616 simulstate->setRandomEngine(new CLHEP::TRandomEngine());
617#else
618 simulstate->setRandomEngine(new CLHEP::RanluxEngine());
619#endif
620 }
621 if (!truth) {
622 ATH_MSG_NOCLASS(logger, "New particle");
624 t->SetPtEtaPhiM(65536, 0, 0, ParticleConstants::chargedPionMassInMeV);
625 t->set_pdgid(pid);
626 truth = t;
627 }
628 if (!extrapol) {
630 e->set_IDCaloBoundary_eta(truth->Eta());
631 for (int i = 0; i < 24; ++i) {
632 e->set_eta(i, TFCSExtrapolationState::SUBPOS_ENT, truth->Eta());
633 e->set_eta(i, TFCSExtrapolationState::SUBPOS_EXT, truth->Eta());
634 e->set_eta(i, TFCSExtrapolationState::SUBPOS_MID, truth->Eta());
635 e->set_phi(i, TFCSExtrapolationState::SUBPOS_ENT, 0);
636 e->set_phi(i, TFCSExtrapolationState::SUBPOS_EXT, 0);
637 e->set_phi(i, TFCSExtrapolationState::SUBPOS_MID, 0);
638 e->set_r(i, TFCSExtrapolationState::SUBPOS_ENT, 1500 + i * 10);
639 e->set_r(i, TFCSExtrapolationState::SUBPOS_EXT, 1510 + i * 10);
640 e->set_r(i, TFCSExtrapolationState::SUBPOS_MID, 1505 + i * 10);
641 e->set_z(i, TFCSExtrapolationState::SUBPOS_ENT, 3500 + i * 10);
642 e->set_z(i, TFCSExtrapolationState::SUBPOS_EXT, 3510 + i * 10);
643 e->set_z(i, TFCSExtrapolationState::SUBPOS_MID, 3505 + i * 10);
644 }
645 extrapol = e;
646 }
647
648 TFCSEnergyAndHitGANV2 GAN("GAN", "GAN");
649 GAN.setLevel(MSG::INFO);
650 const int etaMin = 20;
651 const int etaMax = etaMin + 5;
652 ATH_MSG_NOCLASS(logger, "Initialize Networks");
653 GAN.initializeNetwork(pid, etaMin, path);
654 for (int i = 0; i < 24; ++i)
655 if (GAN.is_match_calosample(i)) {
657 Form("center%d", i), Form("center layer %d", i));
658 c->set_calosample(i);
659 c->setExtrapWeight(0.5);
660 c->setLevel(MSG::INFO);
661 c->set_pdgid(pid);
662 if (pid == 11)
663 c->add_pdgid(-pid);
664 if (pid == 211)
665 c->add_pdgid(-pid);
666 c->set_eta_min(etaMin / 100.0);
667 c->set_eta_max(etaMax / 100.0);
668 c->set_eta_nominal((etaMin + etaMax) / 200.0);
669
670 GAN.push_back_in_bin(c, i);
671 GAN.set_nr_of_init(i, 1);
672 }
673
674 GAN.Print();
675
676 ATH_MSG_NOCLASS(logger, "Writing GAN to " << outputname);
677 const std::string outname = "FCSGANtest_" + outputname + ".root";
678 TFile *fGAN = TFile::Open(outname.c_str(), "recreate");
679 fGAN->cd();
680 // GAN.Write();
681 fGAN->WriteObjectAny(&GAN, "TFCSEnergyAndHitGANV2", "GAN");
682
683 fGAN->ls();
684 fGAN->Close();
685 delete fGAN;
686
687 ATH_MSG_NOCLASS(logger, "Open " << outname);
688 fGAN = TFile::Open(outname.c_str());
689 TFCSEnergyAndHitGANV2 *GAN2 = (TFCSEnergyAndHitGANV2 *)(fGAN->Get("GAN"));
690 GAN2->setLevel(MSG::INFO);
691 GAN2->Print();
692
693 ATH_MSG_NOCLASS(logger, "Before running GAN2->simulate()");
694 GAN2->simulate(*simulstate, truth, extrapol);
695 simulstate->Print();
696 delete fGAN;
697}
698
700 if (bins < 4)
701 return 4;
702 else if (bins < 8)
703 return 8;
704 else if (bins < 16)
705 return 16;
706 else
707 return 32;
708}
709
711 int yBinNum) const {
712 double binsInAlphaInRBin = yBinNum;
713 if (yBinNum == 32) {
714 ATH_MSG_DEBUG("yBinNum is special value 32");
715 const double widthX = x->GetBinWidth(ix);
716 const double radious = x->GetBinCenter(ix);
717 double circumference = radious * 2. * TMath::Pi();
718 if (m_param.IsSymmetrisedAlpha()) {
719 circumference = radious * TMath::Pi();
720 }
721
722 const double bins = circumference / widthX;
723 binsInAlphaInRBin = GetBinsInFours(bins);
724 ATH_MSG_DEBUG("Bin in alpha: " << binsInAlphaInRBin << " for r bin: " << ix
725 << " (" << x->GetBinLowEdge(ix) << "-"
726 << x->GetBinUpEdge(ix) << ")");
727 }
728 return binsInAlphaInRBin;
729}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
double charge(const T &p)
Definition AtlasPID.h:997
static Double_t a
#define ATH_MSG_NOCLASS(logger_name, x)
Definition MLogging.h:52
static const std::vector< std::string > bins
A number of constexpr particle constants to avoid hardcoding them directly in various places.
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
double angle(const GeoTrf::Vector2D &a, const GeoTrf::Vector2D &b)
#define y
#define x
Header file for AthHistogramAlgorithm.
Cut down AthMessaging.
Definition MLogging.h:176
bool msgLvl(const MSG::Level lvl) const
Check whether the logging system is active at the provided verbosity level.
Definition MLogging.h:222
virtual void setLevel(MSG::Level lvl)
Update outputlevel.
Definition MLogging.cxx:105
std::vector< int > m_bin_ninit
bool initializeNetwork(int const &pid, int const &etaMin, const std::string &FastCaloGANInputFolderName)
static void unit_test(TFCSSimulationState *simulstate=nullptr, const TFCSTruthState *truth=nullptr, const TFCSExtrapolationState *extrapol=nullptr)
unsigned int get_nr_of_init(unsigned int bin) const
virtual int get_bin(TFCSSimulationState &simulstate, const TFCSTruthState *, const TFCSExtrapolationState *) const override
use the layer to be done as binning of the GAN chain
static void test_path(const std::string &path, TFCSSimulationState *simulstate=nullptr, const TFCSTruthState *truth=nullptr, const TFCSExtrapolationState *extrapol=nullptr, const std::string &outputname="unnamed", int pid=211)
virtual const std::string get_variable_text(TFCSSimulationState &simulstate, const TFCSTruthState *, const TFCSExtrapolationState *) const override
int GetAlphaBinsForRBin(const TAxis *x, int ix, int yBinNum) const
static int GetBinsInFours(double const bins)
virtual void Print(Option_t *option="") const override
const TFCSGANXMLParameters::Binning & get_Binning() const
virtual bool is_match_calosample(int calosample) const override
TFCSEnergyAndHitGANV2(const char *name=nullptr, const char *title=nullptr)
bool fillEnergy(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const
virtual FCSReturnCode simulate(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override
Method in all derived classes to do some simulation.
void set_nr_of_init(unsigned int bin, unsigned int ninit)
TFCSGANXMLParameters m_param
void Print(Option_t *option="") const
std::map< int, std::vector< double > > FitResultsPerLayer
std::map< std::string, double > NetworkOutputs
std::map< int, TH2D > Binning
virtual FCSReturnCode simulate_hit(Hit &hit, TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol)
simulated one hit position with some energy.
void Print(Option_t *option="") const
Print object information.
virtual unsigned int get_number_of_bins() const
TFCSParametrizationBinnedChain(const char *name=nullptr, const char *title=nullptr)
std::vector< unsigned int > m_bin_start
Contains the index where the TFCSParametrizationBase* instances to run for a given bin start.
virtual void push_back_in_bin(TFCSParametrizationBase *param, unsigned int bin)
virtual const std::string get_bin_text(int bin) const
print the range of a bin; for bin -1, print the allowed range
FCSReturnCode simulate_and_retry(TFCSParametrizationBase *parametrization, TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const
const Chain_t & chain() const
virtual unsigned int size() const override
Some derived classes have daughter instances of TFCSParametrizationBase objects The size() and operat...
virtual void set_eta_max(double max)
virtual void add_pdgid(int id)
virtual void set_eta_nominal(double min)
virtual void set_pdgid(int id)
virtual void set_eta_min(double min)
void Print(Option_t *option="") const
void set_E(int sample, double Esample)
const T getAuxInfo(std::uint32_t index) const
void add_E(int sample, double Esample)
void set_Efrac(int sample, double Efracsample)
void setAuxInfo(std::uint32_t index, const T &val)
CLHEP::HepRandomEngine * randomEngine()
void setRandomEngine(CLHEP::HepRandomEngine *engine)
void Print(Option_t *option="") const
int pdgid() const
double Ekin() const
int ir
counter of the current depth
Definition fastadd.cxx:49
int r
Definition globals.cxx:22
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138
static Root::TMsgLogger logger("iLumiCalc")
constexpr double chargedPionMassInMeV
the mass of the charged pion (in MeV)