ATLAS Offline Software
Loading...
Searching...
No Matches
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
29
30// StoreGate
32
34#include "CaloDetDescr/CaloDetDescrElement.h"
37
39
40#include "TFile.h"
41#include <fstream>
42
44ISF::FastCaloSimV2ParamSvc::FastCaloSimV2ParamSvc(const std::string& name, ISvcLocator* svc)
45 : base_class(name, svc)
46 , m_detStore( "StoreGateSvc/DetectorStore", name )
47
48{
49 declareProperty( "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; //?
85 std::string inputFile=PathResolverFindCalibFile(fileName);
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());
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
190bool 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
220void 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
#define MAXHITS
Definition Args.h:11
#define MAXBINS
Definition Args.h:12
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
This class provides an interface to generate or decode an identifier for the upper levels of the dete...
std::unique_ptr< CaloDetDescrManager > buildCaloDetDescrNoAlign(ISvcLocator *svcLocator, IMessageSvc *msgSvc)
Definition of CaloDetDescrManager.
static constexpr const char * caloMgrStaticKey
std::map< Identifier, const CaloDetDescrElement * > t_cellmap
#define gr
std::map< Identifier_Gpu, const CaloDetDescrElement_Gpu * > t_cellmap_Gpu
Definition GeoLoadGpu.h:15
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
static const Info & instance()
Singleton accessor function.
This class groups all DetDescr information related to a CaloCell.
This class provides the client interface for accessing the detector description information common to...
const MeanAndRMS & deta()
const MeanAndRMS & dphi()
const MeanAndRMS & eta_correction()
const MeanAndRMS & phi_correction()
static const int MAX_SAMPLING
A manager class providing access to readout geometry information for the forward calorimeter.
std::unique_ptr< CaloGeometryFromCaloDDM > m_caloGeo
ServiceHandle< StoreGateSvc > m_detStore
Pointer to StoreGate (detector store by default)
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
FastCaloSimV2ParamSvc(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters.
virtual StatusCode initialize() override final
Athena algorithm's interface methods.
virtual StatusCode simulate(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override final
Simulation Call.
TFCSParametrizationBase * m_param
int ir
counter of the current depth
Definition fastadd.cxx:49
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
void * Rand4Hits_init(long long, unsigned short, unsigned long long, bool)
MsgStream & msg
Definition testRead.cxx:32