34 #include "CaloDetDescr/CaloDetDescrElement.h"
43 #include "lwtnn/parse_json.hh"
47 #include "CLHEP/Random/RandGaussZiggurat.h"
48 #include "CLHEP/Random/RandFlat.h"
49 #include "CLHEP/Units/SystemOfUnits.h"
58 m_theContainer(nullptr),
59 m_rndGenSvc(
"AtRndmGenSvc",
name),
60 m_randomEngine(nullptr),
61 m_caloDetDescrManager(nullptr),
64 declareProperty(
"ParamsInputFilename" ,
m_paramsFilename=
"DNNCaloSim/DNNCaloSim_GAN_nn_v0.json",
" lwtnn json output describing the trained network");
65 declareProperty(
"ParamsInputArchitecture" ,
m_paramsInputArchitecture=
"GANv0",
"tag describing additional parameters necessary for the network");
82 ATH_MSG_INFO(m_screenOutputPrefix <<
"Initializing ...");
85 m_randomEngine = m_rndGenSvc->GetEngine( m_randomEngineName);
88 ATH_MSG_ERROR(
"Could not get random number engine from RandomNumberService. Abort.");
89 return StatusCode::FAILURE;
93 if(!m_caloDetDescrManager) {
104 const CaloIdManager* caloId_mgr = m_caloDetDescrManager->getCalo_Mgr();
107 m_caloGeo = std::make_unique<CaloGeometryFromCaloDDM>();
108 m_caloGeo->LoadGeometryFromCaloDDM(m_caloDetDescrManager);
109 if(!m_caloGeo->LoadFCalChannelMapFromFCalDDM(fcalManager) )
ATH_MSG_FATAL(
"Found inconsistency between FCal_Channel map and GEO file. Please, check if they are configured properly.");
113 if (initializeNetwork().isFailure())
116 return StatusCode::FAILURE;
122 if(m_FastCaloSimCaloExtrapolation.retrieve().isFailure())
125 return StatusCode::FAILURE;
128 m_windowCells.reserve(m_numberOfCellsForDNN);
132 return StatusCode::SUCCESS;
144 ATH_MSG_ERROR(
"Could not find json file " << m_paramsFilename );
145 return StatusCode::FAILURE;
154 if (m_graph==
nullptr){
155 ATH_MSG_ERROR(
"Could not create LightWeightGraph from " << m_paramsFilename );
156 return StatusCode::FAILURE;
164 ATH_MSG_INFO(
"Using ParamsInputArchitecture: " << m_paramsInputArchitecture );
165 if (m_paramsInputArchitecture==
"GANv0")
167 m_GANLatentSize = 300;
168 m_logTrueEnergyMean = 9.70406053;
169 m_logTrueEnergyScale = 1.76099569;
170 m_riImpactEtaMean = 3.47603256e-05;
171 m_riImpactEtaScale = 0.00722316;
172 m_riImpactPhiMean = -5.42153684e-05;
173 m_riImpactPhiScale = 0.00708241;
176 if (m_GANLatentSize==0){
177 ATH_MSG_ERROR(
"m_GANLatentSize uninitialized!.ParamsInputArchitecture= " << m_paramsInputArchitecture );
178 return StatusCode::FAILURE;
181 return StatusCode::SUCCESS;
188 return StatusCode::SUCCESS;
193 const EventContext& ctx = Gaudi::Hive::currentContext();
194 ATH_MSG_INFO(m_screenOutputPrefix <<
"setupEvent NEW EVENT! ");
198 StatusCode sc = evtStore()->record(m_theContainer, m_caloCellsOutputName);
201 ATH_MSG_FATAL( m_screenOutputPrefix <<
"cannot record CaloCellContainer " << m_caloCellsOutputName );
202 return StatusCode::FAILURE;
206 CHECK( m_caloCellMakerToolsSetup.retrieve() );
207 ATH_MSG_DEBUG(
"Successfully retrieve CaloCellMakerTools: " << m_caloCellMakerToolsSetup );
210 for (; itrTool != endTool; ++itrTool)
212 std::string chronoName=this->
name()+
"_"+ itrTool->name();
213 if (m_chrono) m_chrono->chronoStart(chronoName);
214 StatusCode sc = (*itrTool)->process(m_theContainer, ctx);
216 m_chrono->chronoStop(chronoName);
222 ATH_MSG_ERROR( m_screenOutputPrefix <<
"Error executing tool " << itrTool->name() );
223 return StatusCode::FAILURE;
235 const int ntrial=100;
236 ATH_MSG_INFO (
"Trial window building on " << ntrial <<
" dummy eta phi " );
237 for (
int i=0 ;
i< ntrial ;
i++){
238 const double eta = CLHEP::RandFlat::shoot(testsimulstate.
randomEngine(), 0.2, 0.25);
242 if (fillWindowCells(eta,phi,testImpactCellDDE).isFailure()){
243 ATH_MSG_WARNING(
"Could not build trial window cells vector with eta " << eta <<
" phi " << phi);
246 ATH_MSG_INFO (
"End of trial window building on " << ntrial <<
" dummy eta phi " );
253 return StatusCode::SUCCESS;
258 const EventContext& ctx = Gaudi::Hive::currentContext();
261 CHECK( m_caloCellMakerToolsRelease.retrieve() );
266 for (; itrTool != endTool; ++itrTool)
268 ATH_MSG_VERBOSE( m_screenOutputPrefix <<
"Calling tool " << itrTool->name() );
270 StatusCode sc = (*itrTool)->process(m_theContainer, ctx);
274 ATH_MSG_ERROR( m_screenOutputPrefix <<
"Error executing tool " << itrTool->name() );
278 return StatusCode::SUCCESS;
286 float aEtaRaw =
a->caloDDE()->eta_raw();
287 float bEtaRaw =
b->caloDDE()->eta_raw();
289 float aPhiRaw =
a->caloDDE()->phi_raw();
290 float bPhiRaw =
b->caloDDE()->phi_raw();
292 if ((aSampling) < (bSampling))
294 else if ((aSampling) > (bSampling))
297 if ((aEtaRaw) < (bEtaRaw))
299 else if ((aEtaRaw) > (bEtaRaw))
310 float aEtaRaw =
a->caloDDE()->eta_raw();
311 float bEtaRaw =
b->caloDDE()->eta_raw();
313 float aPhiRaw =
a->caloDDE()->phi_raw();
314 float bPhiRaw =
b->caloDDE()->phi_raw();
316 if ((aSampling) < (bSampling))
318 else if ((aSampling) > (bSampling))
321 if ((aEtaRaw) < (bEtaRaw))
323 else if ((aEtaRaw) > (bEtaRaw))
333 ATH_MSG_VERBOSE(
"NEW PARTICLE! DNNCaloSimSvc called with ISFParticle: " << isfp);
337 if(isfp.
ekin() < 10) {
338 ATH_MSG_VERBOSE(
"Skipping particle with Ekin: " << isfp.
ekin() <<
" MeV. Below the 10 MeV threshold.");
339 return StatusCode::SUCCESS;
346 if (fillNetworkInputs(isfp,
inputs,trueEnergy).isFailure()) {
349 return StatusCode::SUCCESS;
361 for (
auto & windowCell : m_windowCells) {
366 <<
" phi_raw " << windowCell->caloDDE()->phi_raw()
367 <<
" sampling " << windowCell->caloDDE()->getSampling()
368 <<
" energy " << windowCell->energy());
375 return StatusCode::SUCCESS;
390 trueEnergy = isfp.
ekin();
396 m_FastCaloSimCaloExtrapolation->extrapolate(
extrapol,&truth);
405 for (
int isubpos=0; isubpos< 3 ; isubpos++){
408 " isubpos=" << isubpos <<
409 " OK=" <<
extrapol.OK(isam,isubpos) <<
410 " eta=" <<
extrapol.eta(isam,isubpos) <<
411 " phi=" <<
extrapol.phi(isam,isubpos) <<
412 " r=" <<
extrapol.r(isam,isubpos) );
421 double etaExtrap=-999.;
422 double phiExtrap=-999.;
424 etaExtrap=
extrapol.eta(isam,isubpos);
425 phiExtrap=
extrapol.phi(isam,isubpos);
429 " isubpos=" << isubpos <<
430 " eta=" << etaExtrap <<
431 " phi=" << phiExtrap );
437 if (fillWindowCells(etaExtrap,phiExtrap,impactCellDDE).isFailure()){
439 return StatusCode::FAILURE;
448 double randGaussz = 0.;
451 int impactEtaIndex = m_emID->eta(impactCellDDE->
identify());
452 int impactPhiIndex = m_emID->phi(impactCellDDE->
identify());
453 double etaRawImpactCell=impactCellDDE->
eta_raw();
454 double phiRawImpactCell=impactCellDDE->
phi_raw();
457 <<
" phi_index " << impactPhiIndex
458 <<
" sampling " << m_emID->sampling(impactCellDDE->
identify()));
460 int pconf = impactPhiIndex % 4 ;
461 int econf = (impactEtaIndex + 1) % 2 ;
463 double riImpactEta = (etaExtrap - etaRawImpactCell);
470 riImpactEta = (riImpactEta - m_riImpactEtaMean)/m_riImpactEtaScale;
471 double riImpactPhi = (
CaloPhiRange::diff(phiExtrap, phiRawImpactCell) - m_riImpactPhiMean)/m_riImpactPhiScale;
478 for (
int i = 0;
i< m_GANLatentSize;
i ++)
480 randGaussz = CLHEP::RandGaussZiggurat::shoot(simulstate.
randomEngine(), 0., 1.);
486 inputs[
"E_true"].insert ( std::pair<std::string,double>(
"0", (
std::log(trueEnergy) - m_logTrueEnergyMean)/m_logTrueEnergyScale) );
488 for (
int i = 0;
i< 4;
i ++)
497 for (
int i = 0;
i< 2;
i ++){
506 inputs[
"ripos"].insert ( std::pair<std::string,double>(
"0", riImpactEta) );
507 inputs[
"ripos"].insert ( std::pair<std::string,double>(
"1", riImpactPhi ) );
509 return StatusCode::SUCCESS;
518 impactCellDDE=m_caloDetDescrManager->get_element(
CaloCell_ID::EMB2,etaExtrap,phiExtrap);
519 if (impactCellDDE==
nullptr){
520 ATH_MSG_WARNING(
"No cell found for this eta " << etaExtrap <<
" phi " << phiExtrap);
521 return StatusCode::FAILURE;
527 const int caloHashImpactCell=impactCellDDE->
calo_hash();
528 const double etaImpactCell=impactCellDDE->
eta();
529 const double phiImpactCell=impactCellDDE->
phi();
530 const double etaRawImpactCell=impactCellDDE->
eta_raw();
531 const double phiRawImpactCell=impactCellDDE->
phi_raw();
535 " eta=" << etaImpactCell <<
536 " phi=" << phiImpactCell <<
537 " eta raw=" << etaRawImpactCell <<
538 " phi raw=" << phiRawImpactCell );
548 m_windowCells.clear();
552 for(
CaloCell* theCell : * m_theContainer) {
553 sampling = theCell->caloDDE()->getSampling();
554 eta_raw = theCell->caloDDE()->eta_raw();
555 phi_raw = theCell->caloDDE()->phi_raw();
556 if (( eta_raw < etaRawImpactCell + m_etaRawBackCut) && (eta_raw > etaRawImpactCell - m_etaRawBackCut)) {
568 if ((sampling == 0) || (sampling == 1) ){
569 if ((eta_raw < etaRawImpactCell + m_etaRawMiddleCut) && (eta_raw > etaRawImpactCell - m_etaRawMiddleCut)) {
572 m_windowCells.push_back(theCell);
576 else if(sampling == 2) {
578 if ((eta_raw < etaRawImpactCell + m_etaRawMiddleCut) && (eta_raw > etaRawImpactCell - m_etaRawMiddleCut)) {
580 m_windowCells.push_back(theCell);
585 else if(sampling == 3){
588 m_windowCells.push_back(theCell);
594 if (nSqCuts != m_numberOfCellsForDNN){
595 ATH_MSG_WARNING(
"Total cells passing DNN selection is " << nSqCuts <<
" but should be " << m_numberOfCellsForDNN );
597 return StatusCode::FAILURE;
602 if (etaRawImpactCell < 0){
609 return StatusCode::SUCCESS;