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