ATLAS Offline Software
FastCaloSimV2ParamSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // class header include
7 
8 
9 // FastCaloSim includes
14 //for FastCaloSim-GPU
15 #ifdef USE_GPU
18 #include "AthCUDACore/Info.h"
19 #endif
20 
25 #include "CaloIdentifier/TileID.h"
29 
30 // StoreGate
31 #include "StoreGate/StoreGateSvc.h"
32 
34 #include "CaloDetDescr/CaloDetDescrElement.h"
37 
39 
40 #include "TFile.h"
41 #include <fstream>
42 
45  : base_class(name, svc)
46  , m_detStore( "StoreGateSvc/DetectorStore", name )
47 
48 {
49  declareProperty( "DetStore",
50  m_detStore,
51  "Handle to a StoreGateSvc/DetectorStore instance: it will be used to "
52  "retrieve data during the course of the job" );
53  declareProperty("ParamsInputFilename" , m_paramsFilename);
54  declareProperty("ParamsInputObject" , m_paramsObject);
55  declareProperty("PrintParametrization" , m_printParametrization);
56  declareProperty("CompressMemory" , m_CompressMemory);
57  declareProperty("RunOnGPU" , m_runOnGPU);
58 }
59 
62 {
63  const FCALDetectorManager *fcalManager{};
64  ATH_CHECK(detStore()->retrieve(fcalManager));
65 
66  m_caloGeo = std::make_unique<CaloGeometryFromCaloDDM>();
67 
68  const CaloDetDescrManager* caloMgr = detStore()->tryConstRetrieve<CaloDetDescrManager>(caloMgrStaticKey);
69  if(caloMgr) {
70  m_caloGeo->LoadGeometryFromCaloDDM(caloMgr);
71  }
72  else {
73  std::unique_ptr<CaloDetDescrManager> caloMgrPtr = buildCaloDetDescrNoAlign(serviceLocator()
75  m_caloGeo->LoadGeometryFromCaloDDM(caloMgrPtr.get());
76  ATH_CHECK(detStore()->record(std::move(caloMgrPtr), caloMgrStaticKey));
77  }
78 
79  if (!m_caloGeo->LoadFCalChannelMapFromFCalDDM(fcalManager)) {
80  ATH_MSG_FATAL("Found inconsistency between FCal_Channel map and GEO file. Please, check if they are configured properly.");
81  return StatusCode::FAILURE;
82  }
83 
84  const std::string fileName = m_paramsFilename; //?
86  std::unique_ptr<TFile> paramsFile(TFile::Open( inputFile.c_str(), "READ" ));
87  if (paramsFile == nullptr) {
88  ATH_MSG_ERROR("file = "<<m_paramsFilename<< " not found");
89  return StatusCode::FAILURE;
90  }
91  ATH_MSG_INFO("Opened parametrization file = "<<m_paramsFilename);
92  paramsFile->ls();
93  m_param=static_cast<TFCSParametrizationBase*>(paramsFile->Get(m_paramsObject.c_str()));
94  if (!m_param) {
95  ATH_MSG_WARNING("file = "<<m_paramsFilename<< ", object "<< m_paramsObject<<" not found");
96  return StatusCode::FAILURE;
97  }
98 
99  paramsFile->Close();
100 
101  if(m_CompressMemory) m_param->RemoveDuplicates();
102  m_param->set_geometry(m_caloGeo.get());
103  m_param->setLevel(msg().level());
104  if (m_printParametrization) {
105  m_param->Print("short");
106  }
107  if(m_CompressMemory) m_param->RemoveNameTitle();
108 
109 #ifdef USE_GPU
110 
111  // if no GPU is found then run on CPU
112  if( !m_runOnGPU )
113  return StatusCode::SUCCESS;
114  if( AthCUDA::Info::instance().nDevices() == 0 ) {
115  ATH_MSG_WARNING( "no GPU device is found, will run everything on the CPU." );
116  m_runOnGPU=false;
117  return StatusCode::SUCCESS;
118  }
119 
120  // FCS-GPU
121  // copy the geometry and parameterization files to GPU
122  ATH_MSG_INFO("start copying all parametrization files to GPU");
123  m_param->Copy2GPU();
124  //random seed for curand in GPU
125  unsigned long long seed=23424352;
126  m_rd4h = CaloGpuGeneral::Rand4Hits_init( MAXHITS, MAXBINS, seed, true );
127  //construct and load Geometry in GPU
128  ATH_MSG_INFO("start loading Geometry in GPU");
129  m_gl = new GeoLoadGpu();
130  CaloGeometry* calogeo=m_caloGeo.get();
131  m_gl->set_ncells( calogeo->get_cells()->size() );
132 
133  if(calogeo->get_cells()->size() ==0 ) return StatusCode::SUCCESS;
134 
135  m_gl->set_max_sample( CaloGeometry::MAX_SAMPLING);
136  int nrgns= calogeo->get_tot_regions();
137  m_gl->set_nregions( nrgns);
138 
139 
140  if(!convert_cellmap(calogeo->get_cells(),&m_cellmap_gpu)){
141  ATH_MSG_ERROR("can not convert the CaloDetDescrElement to CaloDetDescrElement_GPU ");
142  }
143  m_gl->set_cellmap( &m_cellmap_gpu);
144  GeoRegion* GR_ptr = (GeoRegion*)malloc( nrgns *sizeof( GeoRegion ));
145  m_gl->set_regions( GR_ptr );
146 
148 
149  m_gl->set_sample_index_h( si );
150  //set regions
151  int i = 0;
152  for ( int is = 0; is < CaloGeometry::MAX_SAMPLING; ++is ) {
153  si[is].index = i;
154  int nr = calogeo->get_n_regions( is );
155  si[is].size = nr;
156  for ( int ir = 0; ir < nr; ++ir ) region_data_cpy( calogeo->get_region( is, ir), &GR_ptr[i++] );
157  }
158 
159  if(m_gl->LoadGpu())
160  ATH_MSG_INFO("calo geometry loaded successfully in GPU");
161  else
162  ATH_MSG_ERROR("can not load calo geoemtry in GPU");
163 #endif
164 
165  return StatusCode::SUCCESS;
166 }
167 
170 {
171 #ifdef USE_GPU
172  if( m_runOnGPU ) {
173  simulstate.set_gpu_rand (m_rd4h );
174  simulstate.set_geold( m_gl );
175  }
176  else
177  simulstate.set_geold( nullptr );
178 #endif
179 
180  FCSReturnCode status = m_param->simulate(simulstate, truth, extrapol);
181  if (status != FCSSuccess) {
182  return StatusCode::FAILURE;
183  }
184 
185  return StatusCode::SUCCESS;
186 }
187 
188 #ifdef USE_GPU
189 //convert the cellmap to GPU EDM
190 bool ISF::FastCaloSimV2ParamSvc::convert_cellmap(t_cellmap* cellmap, t_cellmap_Gpu* newcellmap){
191  for(t_cellmap::iterator ic=cellmap->begin();ic!=cellmap->end();++ic) {
192  const CaloDetDescrElement* cell=(*ic).second;
193  const CaloDetDescrElement_Gpu* newcell=new CaloDetDescrElement_Gpu(cell->identify().get_compact(),
194  cell->calo_hash(),
195  cell->getSampling(),
196  cell->eta(),
197  cell->phi(),
198  cell->deta(),
199  cell->dphi(),
200  cell->r(),
201  cell->eta_raw(),
202  cell->phi_raw(),
203  cell->r_raw(),
204  cell->dr(),
205  cell->x(),
206  cell->y(),
207  cell->z(),
208  cell->x_raw(),
209  cell->y_raw(),
210  cell->z_raw(),
211  cell->dx(),
212  cell->dy(),
213  cell->dz());
214  newcellmap->insert(std::make_pair(Identifier_Gpu(cell->identify().get_compact()),newcell));
215  }
216  if(cellmap->size()!=newcellmap->size())return false;
217  return true;
218 }
219 
220 void ISF::FastCaloSimV2ParamSvc::region_data_cpy( CaloGeometryLookup* glkup, GeoRegion* gr ) {
221 
222  // Copy all parameters
223  gr->set_xy_grid_adjustment_factor( glkup->xy_grid_adjustment_factor() );
224  gr->set_index( glkup->index() );
225 
226  int neta = glkup->cell_grid_eta();
227  int nphi = glkup->cell_grid_phi();
228  ATH_MSG_DEBUG(" copy region " << glkup->index() << "neta= " << neta<< ", nphi= "<<nphi) ;
229 
230  gr->set_cell_grid_eta( neta );
231  gr->set_cell_grid_phi( nphi );
232 
233  gr->set_mineta( glkup->mineta() );
234  gr->set_minphi( glkup->minphi() );
235  gr->set_maxeta( glkup->maxeta() );
236  gr->set_maxphi( glkup->maxphi() );
237 
238  gr->set_mineta_raw( glkup->mineta_raw() );
239  gr->set_minphi_raw( glkup->minphi_raw() );
240  gr->set_maxeta_raw( glkup->maxeta_raw() );
241  gr->set_maxphi_raw( glkup->maxphi_raw() );
242 
243  gr->set_mineta_correction( glkup->mineta_correction() );
244  gr->set_minphi_correction( glkup->minphi_correction() );
245  gr->set_maxeta_correction( glkup->maxeta_correction() );
246  gr->set_maxphi_correction( glkup->maxphi_correction() );
247 
248  gr->set_eta_correction( glkup->eta_correction() );
249  gr->set_phi_correction( glkup->phi_correction() );
250  gr->set_deta( glkup->deta() );
251  gr->set_dphi( glkup->dphi() );
252  gr->set_deta_double( glkup->deta_double() );
253  gr->set_dphi_double( glkup->dphi_double() );
254 
255  // now cell array copy from GeoLookup Object
256  // new cell_grid is a unsigned long array
257  long long* cells = (long long*)malloc( sizeof( long long ) * neta* nphi );
258  gr->set_cell_grid( cells );
259  // new cell_grid is a unsigned long array
260  if ( neta != (int)( *( glkup->cell_grid() ) ).size() )
261  ATH_MSG_INFO("neta " << neta << ", vector eta size " << ( *( glkup->cell_grid() ) ).size() );
262 
263  for ( int ie = 0; ie < neta; ++ie ) {
264 
265  for ( int ip = 0; ip < nphi; ++ip ) {
266 
267  auto c = ( *( glkup->cell_grid() ) )[ie][ip];
268  if ( c ) {
269  cells[ie * nphi + ip] = c->calo_hash( );
270 
271  } else {
272  cells[ie * nphi + ip] = -1;
273  }
274  }
275  }
276 }
277 #endif
278 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
RunTileCalibRec.cells
cells
Definition: RunTileCalibRec.py:271
FCSReturnCode
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
Definition: TFCSParametrizationBase.h:41
AthCUDA::Info::instance
static const Info & instance()
Singleton accessor function.
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
FCALDetectorManager
A manager class providing access to readout geometry information for the forward calorimeter.
Definition: FCALDetectorManager.h:29
CaloGeometry::MAX_SAMPLING
static const int MAX_SAMPLING
Definition: CaloGeometry.h:29
CaloGeometryLookup::mineta_raw
float mineta_raw() const
Definition: CaloGeometryLookup.h:45
LArEM_ID.h
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
ISF::FastCaloSimV2ParamSvc::m_runOnGPU
bool m_runOnGPU
Definition: FastCaloSimV2ParamSvc.h:65
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CaloDetDescrElement_Gpu
Definition: CaloDetDescrElement_g.h:10
ISF::FastCaloSimV2ParamSvc::m_CompressMemory
bool m_CompressMemory
Definition: FastCaloSimV2ParamSvc.h:64
LArNewCalib_Delay_OFC_Cali.paramsFile
paramsFile
Definition: LArNewCalib_Delay_OFC_Cali.py:183
Info.h
FCALDetectorManager.h
CaloDetDescrElement
This class groups all DetDescr information related to a CaloCell. Provides a generic interface for al...
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:66
ISF::FastCaloSimV2ParamSvc::m_paramsObject
std::string m_paramsObject
Definition: FastCaloSimV2ParamSvc.h:57
ISF::FastCaloSimV2ParamSvc::m_printParametrization
bool m_printParametrization
Definition: FastCaloSimV2ParamSvc.h:63
CaloGeometryLookup::deta
const MeanAndRMS & deta()
Definition: CaloGeometryLookup.h:68
t_cellmap_Gpu
std::map< Identifier_Gpu, const CaloDetDescrElement_Gpu * > t_cellmap_Gpu
Definition: GeoLoadGpu.h:15
gr
#define gr
CaloGpuGeneral::Rand4Hits_init
void * Rand4Hits_init(long long, unsigned short, unsigned long long, bool)
Definition: CaloGpuGeneral.cxx:8
TFCSExtrapolationState
Definition: TFCSExtrapolationState.h:13
RunActsMaterialValidation.extrapol
extrapol
Definition: RunActsMaterialValidation.py:90
CaloGpuGeneral.h
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
CaloDetDescrManager.h
Definition of CaloDetDescrManager.
TileID.h
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
CaloGeometry
Definition: CaloGeometry.h:27
PlotCalibFromCool.ie
ie
Definition: PlotCalibFromCool.py:420
FastCaloSimV2ParamSvc.h
TFCSParametrizationBase.h
TFCSParametrizationBase
Definition: TFCSParametrizationBase.h:46
AtlasDetectorID.h
This class provides an interface to generate or decode an identifier for the upper levels of the dete...
IdDictParser.h
CaloGeometryLookup::eta_correction
const MeanAndRMS & eta_correction()
Definition: CaloGeometryLookup.h:77
GeoLoadGpu.h
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
CaloGeometryLookup::minphi_raw
float minphi_raw() const
Definition: CaloGeometryLookup.h:47
ISF::FastCaloSimV2ParamSvc::simulate
virtual StatusCode simulate(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override final
Simulation Call.
Definition: FastCaloSimV2ParamSvc.cxx:169
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
lumiFormat.i
int i
Definition: lumiFormat.py:85
ISF::FastCaloSimV2ParamSvc::initialize
virtual StatusCode initialize() override final
Athena algorithm's interface methods.
Definition: FastCaloSimV2ParamSvc.cxx:61
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ISF::FastCaloSimV2ParamSvc::m_detStore
ServiceHandle< StoreGateSvc > m_detStore
Pointer to StoreGate (detector store by default)
Definition: FastCaloSimV2ParamSvc.h:54
CaloGeometryLookup
Definition: CaloGeometryLookup.h:25
ISF::FastCaloSimV2ParamSvc::FastCaloSimV2ParamSvc
FastCaloSimV2ParamSvc(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters.
Definition: FastCaloSimV2ParamSvc.cxx:44
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TrigInDetValidation_Base.malloc
malloc
Definition: TrigInDetValidation_Base.py:132
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
Identifier_Gpu
Definition: Identifier_g.h:15
FCSSuccess
@ FCSSuccess
Definition: TFCSParametrizationBase.h:41
ISF::FastCaloSimV2ParamSvc::m_paramsFilename
std::string m_paramsFilename
Definition: FastCaloSimV2ParamSvc.h:56
CaloGeometryLookup::dphi
const MeanAndRMS & dphi()
Definition: CaloGeometryLookup.h:69
grepfile.ic
int ic
Definition: grepfile.py:33
Rg_Sample_Index::index
int index
Definition: GeoGpu_structs.h:12
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
CaloDetDescrBuilder.h
Rg_Sample_Index
Definition: GeoGpu_structs.h:10
CaloCellContainer.h
CaloGeometryLookup::mineta
float mineta() const
Definition: CaloGeometryLookup.h:40
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
GeoLoadGpu
Definition: GeoLoadGpu.h:17
MAXBINS
#define MAXBINS
Definition: Args.h:12
ir
int ir
counter of the current depth
Definition: fastadd.cxx:49
CaloGeometryLookup::minphi
float minphi() const
Definition: CaloGeometryLookup.h:42
GeoRegion
Definition: GeoRegion.h:18
CaloGeometryLookup::maxeta
float maxeta() const
Definition: CaloGeometryLookup.h:41
CaloGeometryLookup::maxeta_raw
float maxeta_raw() const
Definition: CaloGeometryLookup.h:46
TFCSTruthState.h
CaloGeometryLookup::phi_correction
const MeanAndRMS & phi_correction()
Definition: CaloGeometryLookup.h:78
CaloDetDescrManager
This class provides the client interface for accessing the detector description information common to...
Definition: CaloDetDescrManager.h:473
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TFCSExtrapolationState.h
t_cellmap
std::map< Identifier, const CaloDetDescrElement * > t_cellmap
Definition: CaloGeometryLookup.h:20
CaloGeometryLookup::cell_grid_eta
int cell_grid_eta() const
Definition: CaloGeometryLookup.h:82
MAXHITS
#define MAXHITS
Definition: Args.h:11
CaloGeometryLookup::cell_grid_phi
int cell_grid_phi() const
Definition: CaloGeometryLookup.h:83
merge.status
status
Definition: merge.py:17
TFCSSimulationState.h
TFCSTruthState
Definition: TFCSTruthState.h:13
StoreGateSvc.h
python.compressB64.c
def c
Definition: compressB64.py:93
CaloGeometryLookup::maxphi_raw
float maxphi_raw() const
Definition: CaloGeometryLookup.h:48
TFCSSimulationState
Definition: TFCSSimulationState.h:32
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
CaloGeometryLookup::maxphi
float maxphi() const
Definition: CaloGeometryLookup.h:43
buildCaloDetDescrNoAlign
std::unique_ptr< CaloDetDescrManager > buildCaloDetDescrNoAlign(ISvcLocator *svcLocator, IMessageSvc *msgSvc)
Definition: CaloDetDescrBuilder.cxx:791
Rg_Sample_Index::size
int size
Definition: GeoGpu_structs.h:11
CaloGeometryLookup::index
int index() const
Definition: CaloGeometryLookup.h:33