ATLAS Offline Software
Loading...
Searching...
No Matches
FastCaloSimParamAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7
8// STL include(s):
9#include <sstream>
10#include <map>
11
14
15// athena includes
17
19#include "CaloGeoHelpers/CaloSampling.h"
21#include "CaloDetDescr/CaloDetDescrElement.h"
22
23
24// For MC Truth information:
26
27// geant includes
28#include "G4Version.hh"
29#include "TFile.h"
30
31
32using CLHEP::Hep3Vector;
33
34struct SortByE{
35 bool operator() (const ISF_FCS_Parametrization::FCS_StepInfo& step1, const ISF_FCS_Parametrization::FCS_StepInfo& step2) { return (step1.energy() > step2.energy()); }
36 bool operator() (const ISF_FCS_Parametrization::FCS_StepInfo* step1, const ISF_FCS_Parametrization::FCS_StepInfo* step2) { return (step1->energy() > step2->energy()); }
37};
38
39FastCaloSimParamAlg::FastCaloSimParamAlg(const std::string& name, ISvcLocator* pSvcLocator)
40 : AthAlgorithm(name, pSvcLocator)
41 , m_inputCollectionKey("EventSteps")
42 , m_outputCollectionKey("MergedEventSteps")
43{
44 declareProperty("InputCollectionName", m_inputCollectionKey, "");
45 declareProperty("OutputCollectionName", m_outputCollectionKey, "");
46 declareProperty("Clusterize", m_clusterize = true, "merge nearby hits");
47 declareProperty("Truncate", m_truncate = 2,"truncate hits with t>1000ns (if >=2)");
48 declareProperty("MaxDistance", m_maxDistance = 50000.,
49 "max distance squared after which the hits will be truncated");
50 declareProperty("MinEnergy", m_minEnergy = .99,
51 "energy border, that truncation won't cross");
52 declareProperty("MaxRadius", m_maxRadius = 25.,
53 "maximal radius squared until two hits will be combined");
54 declareProperty("MaxRadiusLAr", m_maxRadiusLAr = 25.,
55 "maximal radius in LAr squared until two hits will be combined");
56 declareProperty("MaxRadiusHEC", m_maxRadiusHEC = 100.,
57 "maximal radius in HEC squared until two hits will be combined");
58 declareProperty("MaxRadiusFCAL", m_maxRadiusFCAL = 100.,
59 "maximal radius in FCAL squared until two hits will be combined");
60 declareProperty("MaxRadiusTile", m_maxRadiusTile = 100.,
61 "maximal radius in Tile squared until two hits will be combined");
62
63 declareProperty("MaxTime", m_maxTime = 25., "max time difference to merge two hits (ns) ");
64 declareProperty("MaxTimeLAr", m_maxTimeLAr = 25., "max time difference to merge two hits (ns) ");
65 declareProperty("MaxTimeHEC", m_maxTimeHEC = 25., "max time difference to merge two hits (ns) ");
66 declareProperty("MaxTimeFCAL", m_maxTimeFCAL = 25., "max time difference to merge two hits (ns) ");
67 declareProperty("MaxTimeTile", m_maxTimeTile = 25., "max time difference to merge two hits (ns) ");
68
69
70 declareProperty("ContainmentEnergy", m_containmentEnergy = 0.95,
71 "energy fraction that will be inside containment borders");
72 declareProperty("LibStructFiles", m_lib_struct_files,
73 "List of files to read library structures from");
74 declareProperty("EnergyFraction", m_energyFraction = .02,
75 "the allowed amount of energy that can be deposited outside calorimeter region ");
76}
77
78
80{
81 ATH_MSG_DEBUG("Initializing");
82 ATH_CHECK(m_inputCollectionKey.initialize());
83 ATH_CHECK(m_outputCollectionKey.initialize());
84 ATH_CHECK(m_caloMgrKey.initialize());
85 ATH_MSG_DEBUG("FastCaloSimParamAlg " << this->name() << " initialized");
86 return StatusCode::SUCCESS;
87}
88
90{
93 ATH_CHECK(outputHandle.record(std::make_unique<ISF_FCS_Parametrization::FCS_StepInfoCollection>()));
94
95 // TODO would be more efficient to directly write the truncated
96 // input collection to the output collection rather than copying it
97 // first.
98 for (const auto *const step: *inputHandle ) {
99 auto&& stepCopy = std::make_unique<ISF_FCS_Parametrization::FCS_StepInfo>(*step);
100 outputHandle->push_back( stepCopy.release() );
101 }
102 ATH_CHECK(this->truncate(&*outputHandle));
103
104 if (m_clusterize) {
105 ATH_CHECK(this->clusterize(&*outputHandle));
106 }
107 else {
108 ATH_MSG_DEBUG("Not merging nearby hits: "<<m_clusterize);
109 }
110 return StatusCode::SUCCESS;
111}
112
114{
115 ATH_MSG_DEBUG("Initial clusterize size: "<<stepinfo->size()<<" - will merge steps in the same cell which are less than dR and dT to each other");
116 double total_energy1(0.);
117 for (const auto step: *stepinfo) {
118 total_energy1+=step->energy();
119 }
120 ATH_MSG_DEBUG("Check: total energy before clusterize "<<total_energy1);
121
122 SG::ReadCondHandle<CaloDetDescrManager> caloMgrHandle{m_caloMgrKey,Gaudi::Hive::currentContext()};
123 ATH_CHECK(caloMgrHandle.isValid());
124 const CaloDetDescrManager* calo_dd_man = *caloMgrHandle;
125
126 // Try this if it will be faster: split to cells first
127 std::map<Identifier, ISF_FCS_Parametrization::FCS_StepInfoCollection*> FCS_SIC_cells;
128 for (const auto step: *stepinfo) {
129 if (FCS_SIC_cells.find(step->identify()) != FCS_SIC_cells.end()) {// Already have a step for this cell
130 auto&& stepCopy = std::make_unique<ISF_FCS_Parametrization::FCS_StepInfo>(*step);
131 FCS_SIC_cells[step->identify()]->push_back( stepCopy.release() );
132 }
133 else { // First step for this cell
134 auto && new_fcs_sic = std::make_unique<ISF_FCS_Parametrization::FCS_StepInfoCollection>();
135 auto&& stepCopy = std::make_unique<ISF_FCS_Parametrization::FCS_StepInfo>(*step);
136 new_fcs_sic->push_back( stepCopy.release() );
137 FCS_SIC_cells.insert(std::pair<Identifier, ISF_FCS_Parametrization::FCS_StepInfoCollection*>(step->identify(),new_fcs_sic.release()));
138 }
139 }
140
141 ATH_MSG_DEBUG("Merging separately in each cell: Ncells: "<<FCS_SIC_cells.size());
142 // Then do merging for each cell
143 for (std::map<Identifier, ISF_FCS_Parametrization::FCS_StepInfoCollection*>::iterator it = FCS_SIC_cells.begin(); it!= FCS_SIC_cells.end(); ++it) {
144 std::stable_sort(FCS_SIC_cells[it->first]->begin(), FCS_SIC_cells[it->first]->end(), SortByE());
145 if (! calo_dd_man->get_element(it->first)) {
146 // Bad identifier
147 ATH_MSG_WARNING("Something wrong with identifier: "<<it->first);
148 continue;
149 }
150 else if ((it->second)->size()==1) {
151 continue; // Go to next iterator
152 }
153
154 const CaloCell_ID::CaloSample layer = calo_dd_man->get_element(it->first)->getSampling();
155 double dsame(0.);
156 double tsame(0.);
157 if (layer >= CaloCell_ID::PreSamplerB && layer <= CaloCell_ID::EME3) {
158 dsame = m_maxRadiusLAr;
159 tsame = m_maxTimeLAr;
160 }
161 else if (layer >= CaloCell_ID::HEC0 && layer <= CaloCell_ID::HEC3) {
162 dsame = m_maxRadiusHEC;
163 tsame = m_maxTimeHEC;
164 }
165 else if (layer >= CaloCell_ID::TileBar0 && layer <= CaloCell_ID::TileExt2) {
166 dsame = m_maxRadiusTile;
167 tsame = m_maxTimeTile;
168 }
169 else if (layer >=CaloCell_ID::FCAL0 && layer <= CaloCell_ID::FCAL2) {
170 dsame = m_maxRadiusFCAL;
171 tsame = m_maxTimeFCAL;
172 }
173 else {
174 dsame = m_maxRadius;
175 tsame = m_maxTime;
176 }
178 while ( it1 != (it->second)->end() ) {
180 while (it2 != (it->second)->end()) {
181 if (((*it1)->diff2(**it2) < dsame) && std::fabs((*it1)->time() - (*it2)->time()) < tsame ) {
182 **it1 += **it2;
183 it2 = (it->second)->erase(it2); // also calls delete on the pointer (because the DataVector owns its elements)
184 continue;
185 }
186 ++it2;
187 }
188 ++it1;
189 }
190 }
191 // Merge them back into a single list
192 ATH_MSG_VERBOSE("Copying back");
193 stepinfo->clear(); // also calls delete on all the removed elements (because the DataVector owns its elements)
194 for (std::map<Identifier, ISF_FCS_Parametrization::FCS_StepInfoCollection*>::iterator it = FCS_SIC_cells.begin(); it!= FCS_SIC_cells.end(); ++it) {
195 for (const auto step: *(it->second)) {
196 auto&& stepCopy = std::make_unique<ISF_FCS_Parametrization::FCS_StepInfo>(*step);
197 stepinfo->push_back( stepCopy.release() );
198 }
199 // Tidy up temporary FCS_StepInfoCollections as we go
200 it->second->clear(); // also calls delete on all the removed elements (because the DataVector owns its elements)
201 delete (it->second);
202 }
203 double total_energy2(0.);
204 for (const auto step: *stepinfo) {
205 total_energy2+=step->energy();
206 }
207 ATH_MSG_DEBUG("Check: total energy "<<total_energy2);
208 ATH_MSG_DEBUG("After clusterize: "<<stepinfo->size());
209 unsigned int nInvalid(0);
210 // Remove invalid FCS_StepInfo objects
212 while(stepIter != stepinfo->end()) {
213 if ((*stepIter)->valid()) {
214 ++stepIter;
215 continue;
216 }
217 ++nInvalid;
218 stepIter = stepinfo->erase(stepIter); // also calls delete on the pointer (because the DataVector owns its elements)
219 }
220 ATH_MSG_DEBUG("Removed "<<nInvalid<<" StepInfo objects. New collection size: "<<stepinfo->size());
221 return StatusCode::SUCCESS;
222}
223
225{
226 ATH_MSG_DEBUG("Initial truncate size: "<<stepinfo->size()<<" settings: "<<m_truncate);
227 if (m_truncate>0) {
229 while (stepIter != stepinfo->end()) {
230 if ((m_truncate>=2)&&((*stepIter)->time()>1000)) {
231 stepIter = stepinfo->erase(stepIter); // also calls delete on the pointer (because the DataVector owns its elements)
232 continue;
233 }
234 ++stepIter;
235 }
236 ATH_MSG_DEBUG("After truncate size: "<<stepinfo->size());
237 }
238 return StatusCode::SUCCESS;
239}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Definition of CaloDetDescrManager.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
CaloSampling::CaloSample CaloSample
Definition CaloCell_ID.h:53
CaloCell_ID::CaloSample getSampling() const
cell sampling
const CaloDetDescrElement * get_element(const Identifier &cellId) const
get element by its identifier
This class provides the client interface for accessing the detector description information common to...
value_type push_back(value_type pElem)
Add an element to the end of the collection.
DataModel_detail::iterator< DataVector > iterator
Definition DataVector.h:842
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
iterator erase(iterator position)
Remove element at a given position.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
void clear()
Erase all the elements in the collection.
DoubleProperty m_containmentEnergy
property, see LArG4GenShowerLib::LArG4GenShowerLib
DoubleProperty m_maxRadiusHEC
property, see LArG4GenShowerLib::LArG4GenShowerLib
DoubleProperty m_maxTimeLAr
DoubleProperty m_minEnergy
property, see LArG4GenShowerLib::LArG4GenShowerLib
virtual StatusCode execute() override final
FastCaloSimParamAlg(const std::string &name, ISvcLocator *pSvcLocator)
DoubleProperty m_maxRadiusLAr
property, see LArG4GenShowerLib::LArG4GenShowerLib
DoubleProperty m_maxTimeFCAL
DoubleProperty m_maxRadius
property, see LArG4GenShowerLib::LArG4GenShowerLib
virtual StatusCode initialize() override final
DoubleProperty m_maxDistance
StringArrayProperty m_lib_struct_files
DoubleProperty m_maxRadiusTile
property, see LArG4GenShowerLib::LArG4GenShowerLib
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloMgrKey
SG::WriteHandleKey< ISF_FCS_Parametrization::FCS_StepInfoCollection > m_outputCollectionKey
StatusCode clusterize(ISF_FCS_Parametrization::FCS_StepInfoCollection *stepinfo) const
DoubleProperty m_maxTimeTile
DoubleProperty m_maxRadiusFCAL
property, see LArG4GenShowerLib::LArG4GenShowerLib
BooleanProperty m_clusterize
DoubleProperty m_maxTimeHEC
SG::ReadHandleKey< ISF_FCS_Parametrization::FCS_StepInfoCollection > m_inputCollectionKey
StatusCode truncate(ISF_FCS_Parametrization::FCS_StepInfoCollection *stepinfo) const
DoubleProperty m_energyFraction
property, see LArG4GenShowerLib::LArG4GenShowerLib
Class for collection of StepInfo class (G4 hits) copied and modified version to ISF.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
float energy(int ind=0) const
Return energy of ind-th sub-hit.
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.
bool operator()(const ISF_FCS_Parametrization::FCS_StepInfo &step1, const ISF_FCS_Parametrization::FCS_StepInfo &step2)