ATLAS Offline Software
LArMinBiasAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "LArMinBiasAlg.h"
6 
7 
11 
12 #include "LArSimEvent/LArHit.h"
14 #include "TTree.h"
15 #include "GaudiKernel/ITHistSvc.h"
16 #include "CaloDetDescr/CaloDetDescrElement.h"
17 
18 
19 
20  //Constructor
21  LArMinBiasAlg:: LArMinBiasAlg(const std::string& name, ISvcLocator* pSvcLocator):
22  AthAlgorithm(name,pSvcLocator),
23  m_datasetID_lowPt(119995),
24  m_datasetID_highPt(119996),
25  m_weight_lowPt(39.8606),
26  m_weight_highPt(0.138128)
27  {
28  declareProperty("datasetID_lowPt",m_datasetID_lowPt);
29  declareProperty("datasetID_highPt",m_datasetID_highPt);
30  declareProperty("weight_highPt",m_weight_highPt);
31  declareProperty("weight_lowPt",m_weight_lowPt);
32  m_first=true;
33  }
34 
35  //__________________________________________________________________________
36  //Destructor
38  //__________________________________________________________________________
40  {
41 
42  ATH_MSG_INFO ( " LArMinBiasAlg initialize() " );
43 
44  ATH_CHECK( service("THistSvc",m_thistSvc) );
45 
46 
47  m_tree = new TTree("m_tree","Offset ntuple");
48  m_tree->Branch("ncell",&m_nsymcell,"ncell/I");
49  m_tree->Branch("nevt_total",&m_nevt_total,"nevt_total/I");
50  m_tree->Branch("identifier",m_identifier,"identifier[ncell]/I");
51  m_tree->Branch("region",m_region,"region[ncell]/I");
52  m_tree->Branch("ieta",m_ieta,"ieta[ncell]/I");
53  m_tree->Branch("layer",m_layer,"layer[ncell]/I");
54  m_tree->Branch("region",m_region,"region[ncell]/I");
55  m_tree->Branch("ieta",m_ieta,"ieta[ncell]/I");
56  m_tree->Branch("eta",m_eta,"eta[ncell]/F");
57  m_tree->Branch("phi",m_phi,"phi[ncell]/F");
58  m_tree->Branch("nevt",m_nevt,"nevt[ncell]/D");
59  m_tree->Branch("average",m_average,"average[ncell]/D");
60  m_tree->Branch("rms",m_rms,"rms[ncell]/D");
61  m_tree->Branch("reference",m_offset,"offset[ncell]/D");
62 
63  if( m_thistSvc->regTree("/file1/offset",m_tree).isFailure()) {
64  ATH_MSG_WARNING(" cannot register ntuple " );
65  return StatusCode::SUCCESS;
66  }
67 
68  const CaloIdManager* mgr = nullptr;
69  ATH_CHECK( detStore()->retrieve( mgr ) );
70  m_larem_id = mgr->getEM_ID();
71  m_calo_id = mgr->getCaloCell_ID();
72 
73 
75 
77 
79 
81 
82  m_n1=0;
83  m_n2=0;
84 
85  m_nevt_total=0;
86 
87  return StatusCode::SUCCESS;
88 
89  }
90  //__________________________________________________________________________
92  {
93  ATH_MSG_INFO("number of events in the two samples " << m_n1 << " " << m_n2);
94  this->fillNtuple();
95  ATH_MSG_INFO(" stop after fill ntuple");
96  return StatusCode::SUCCESS;
97  }
98  //__________________________________________________________________________
100  {
101  ATH_MSG_INFO(" finalize()");
102  return StatusCode::SUCCESS;
103  }
104 
105  //__________________________________________________________________________
107  {
108  //.............................................
109 
110  ATH_MSG_DEBUG(" LArMinBiasAlg execute()");
111 
112  const EventContext& ctx = Gaudi::Hive::currentContext();
113 
114  if (m_first) {
115 
117  ATH_CHECK(caloMgrHandle.isValid());
118 
121  const LArOnOffIdMapping* cabling{*cablingHdl};
122  if(!cabling) {
123  ATH_MSG_ERROR( "Do not have cabling mapping from key " << m_cablingKey.key() );
124  return StatusCode::FAILURE;
125  }
126 
128 
129  ATH_MSG_INFO(" --- first event " << m_ncell);
130  m_symCellIndex.resize(m_ncell,-1);
131  std::vector<int> doneCell;
132  doneCell.resize(m_ncell,-1);
133 
134  //m_readCell.resize(m_ncell,0);
135 
136  m_eCell.resize(m_ncell,0.);
137 
138  m_CellList.reserve(MAX_SYM_CELLS);
139  int nsym=0;
140  // loop over cells
141  // and find symmetry cells
142  for (unsigned int i=0;i<((unsigned int)(m_ncell));i++) {
143  IdentifierHash idHash=i;
144  Identifier id=m_calo_id->cell_id(idHash);
145  if (m_calo_id->is_tile(id)) continue;
146  // convert cell id to symmetric identifier
147  HWIdentifier hwid2 = mcsym->ZPhiSymOfl(id);
148  Identifier id2 = cabling->cnvToIdentifier(hwid2);
149  int i2 = (int) (m_calo_id->calo_cell_hash(id2));
150  if(i2>=m_ncell) {
151  ATH_MSG_WARNING("problem: i2: "<<i2<<" for id: "<<m_calo_id->print_to_string(id)<<" symmetrized: "<<m_calo_id->print_to_string(id2));
152  }
153  // we have already processed this hash => just need to associate cell i to the same symmetric cell
154  if (doneCell[i2]>=0) {
155  m_symCellIndex[i]=doneCell[i2];
156  }
157  // we have not already processed this hash, add an entry for this new symmetric cell
158  else {
159  doneCell[i2]=nsym;
160  m_symCellIndex[i] = nsym;
161  CellInfo cell;
162  const CaloDetDescrElement* calodde = (*caloMgrHandle)->get_element(id);
163  cell.eta = calodde->eta();
164  cell.phi = calodde->phi();
165  cell.region = m_calo_id->region(id);
166  cell.ieta = m_calo_id->eta(id);
167  cell.layer = m_calo_id->calo_sample(id);
168  cell.region = m_calo_id->region(id);
169  cell.ieta = m_calo_id->eta(id);
170  //cell.identifier = id2.get_identifier32().get_compact();
171  cell.identifier = id2;
172  cell.average=0.;
173  cell.offset=0.;
174  cell.rms=0.;
175  cell.nevt=0.;
176  m_CellList.push_back(cell);
177  nsym++;
178  }
179  }
180  ATH_MSG_INFO(" --- number of symmetric cells found " << nsym << " " << m_CellList.size());
181  if (nsym>=MAX_SYM_CELLS) ATH_MSG_ERROR(" More than "<<MAX_SYM_CELLS<<" number of symmetric cells... Fix array size for ntuple writing !!!! ");
182  m_nsymcell=nsym;
183  m_first=false;
184  }
185 
187  if (!eventInfo.isValid()) {
188  ATH_MSG_ERROR ("Could not retrieve EventInfo");
189  return StatusCode::FAILURE;
190  }
191  int channelNumber = eventInfo->mcChannelNumber();
192 
193  m_nevt_total++;
194 
195  double weight=1.;
196 
197 // Dataset ID for lowPt MinBias
198  if (channelNumber==m_datasetID_lowPt) {
200  m_n1+=1;
201  }
202 // Dataset ID for highPt MinBias
203  else if (channelNumber==m_datasetID_highPt) {
205  m_n2+=1;
206  }
207  else {
208  ATH_MSG_WARNING(" Neither low nor high Pt MinBias sample " << channelNumber << " set weight to 1.0 ");
209  weight=1.;
210  }
211 
212 
213  if ((m_nevt_total%100)==1) ATH_MSG_INFO(" ---- process event number " << m_nevt_total << " " << channelNumber << " weight " << weight);
214 
215  for (int i=0;i<m_ncell;i++) m_eCell[i]=0.;
216 
217  std::vector <std::string> HitContainer;
218  HitContainer.emplace_back("LArHitEMB");
219  HitContainer.emplace_back("LArHitEMEC");
220  HitContainer.emplace_back("LArHitHEC");
221  HitContainer.emplace_back("LArHitFCAL");
222  for (unsigned int iHitContainer=0;iHitContainer<HitContainer.size();iHitContainer++)
223  {
224  const LArHitContainer* hit_container ;
225  ATH_CHECK(evtStore()->retrieve(hit_container,HitContainer[iHitContainer]));
226  for (const LArHit* hit : *hit_container)
227  {
228  Identifier cellID=hit->cellID();
229  double energy = hit->energy();
230  double time =hit->time();
231  int index = (int) (m_calo_id->calo_cell_hash(cellID));
232  if (index < m_ncell && index>=0 && fabs(time)<25.) {
233  m_eCell[index] += energy;
234  }
235 
236  } // loop over hits in container
237  } // loop over containers
238 
239  for (int i=0;i<m_ncell;i++) {
240  addCell(i,m_eCell[i],0.,weight);
241  }
242 
243 
244  return StatusCode::SUCCESS;
245  }
246 
247  void LArMinBiasAlg::addCell(int index, double energy, double eshift, double weight)
248  {
249  if (index < m_ncell && index>=0) {
251  if (index2<0) return;
252  if (index2 >= ((int)(m_CellList.size())) ) {
253  ATH_MSG_INFO(" LArMinBiasAlg::addCell: for " << index << ", " << index2 << " is out-of bounds for list of size " << m_CellList.size());
254  return;
255  }
256  double oldN = m_CellList[index2].nevt;
257  double oldAverage = m_CellList[index2].average;
258  double oldRMS = m_CellList[index2].rms;
259  double oldAverage2 = m_CellList[index2].offset;
260 
261  double frac = oldN/(weight+oldN);
262  double Anew = weight+oldN;
263  double newAverage = frac*oldAverage + weight*energy/Anew;
264  double deltaE = energy-newAverage;
265  double newRMS = frac*(oldRMS + (newAverage-oldAverage)*(newAverage-oldAverage)) + weight*deltaE*deltaE/Anew;
266 
267  double newAverage2 = frac*oldAverage2 + weight*eshift/Anew;
268 
269  m_CellList[index2].nevt = Anew;
270  m_CellList[index2].average = newAverage;
271  m_CellList[index2].rms = newRMS;
272  m_CellList[index2].offset = newAverage2;
273 
274  }
275  }
276 
278  {
279 
280  ATH_MSG_INFO(" in fillNtuple " << m_nsymcell);
281  for (int i=0;i<m_nsymcell;i++) {
282  m_identifier[i] = m_CellList[i].identifier.get_identifier32().get_compact();
283  m_layer[i] = m_CellList[i].layer;
284  m_region[i] = m_CellList[i].region;
285  m_ieta[i] = m_CellList[i].ieta;
286  m_eta[i] = m_CellList[i].eta;
287  m_phi[i] = m_CellList[i].phi;
288  m_nevt[i] = m_CellList[i].nevt;
289  m_offset[i] = (float) (m_CellList[i].offset);
291  m_rms[i] = (float) (sqrt(m_CellList[i].rms));
292  }
293  m_tree->Fill();
294  ATH_MSG_INFO(" after tree fill ");
295 
296  for (int i=0;i<m_nsymcell;i++) {
297  m_CellList[i].nevt=0;
298  m_CellList[i].offset=0.;
299  m_CellList[i].average=0;
300  m_CellList[i].rms=0;
301  }
302  ATH_MSG_INFO(" end of fillNtuple ");
303 
304  }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
LArMinBiasAlg::m_mcSymKey
SG::ReadCondHandleKey< LArMCSym > m_mcSymKey
Definition: LArMinBiasAlg.h:63
LArMinBiasAlg::LArMinBiasAlg
LArMinBiasAlg(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Definition: LArMinBiasAlg.cxx:21
LArMinBiasAlg::m_nsymcell
int m_nsymcell
Definition: LArMinBiasAlg.h:83
LArMinBiasAlg::m_eCell
std::vector< double > m_eCell
Definition: LArMinBiasAlg.h:73
LArEM_ID.h
CaloCell_Base_ID::calo_cell_hash
IdentifierHash calo_cell_hash(const Identifier cellId) const
create hash id from 'global' cell id
CaloCell_Base_ID::region
int region(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
drawFromPickle.average
def average(lst)
Definition: drawFromPickle.py:38
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
LArMinBiasAlg::addCell
void addCell(int index, double e1, double e2, double wt=1.)
Definition: LArMinBiasAlg.cxx:247
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
LArMinBiasAlg::m_datasetID_lowPt
int m_datasetID_lowPt
Definition: LArMinBiasAlg.h:58
LArMinBiasAlg::m_nevt
double m_nevt[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:84
CaloDetDescrElement
This class groups all DetDescr information related to a CaloCell. Provides a generic interface for al...
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:66
LArMinBiasAlg::m_offset
double m_offset[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:93
LArMinBiasAlg::m_nevt_total
int m_nevt_total
Definition: LArMinBiasAlg.h:78
ReadCellNoiseFromCool.cabling
cabling
Definition: ReadCellNoiseFromCool.py:154
LArMinBiasAlg::m_tree
TTree * m_tree
Definition: LArMinBiasAlg.h:77
HitContainer
vector< DHit > HitContainer
Definition: PerfMonTestVectorAlg.cxx:17
LArMinBiasAlg::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: LArMinBiasAlg.h:112
LArHitContainer
Hit collection.
Definition: LArHitContainer.h:26
LArMinBiasAlg::m_larem_id
const LArEM_ID * m_larem_id
Definition: LArMinBiasAlg.h:71
CaloCell_Base_ID::calo_sample
int calo_sample(const Identifier id) const
returns an int taken from Sampling enum and describing the subCalo to which the Id belongs.
Definition: CaloCell_Base_ID.cxx:141
LArMinBiasAlg::m_n2
int m_n2
Definition: LArMinBiasAlg.h:80
LArMinBiasAlg::m_rms
double m_rms[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:92
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
CaloCell_Base_ID::is_tile
bool is_tile(const Identifier id) const
test if the id belongs to the Tiles
LArMinBiasAlg::m_n1
int m_n1
Definition: LArMinBiasAlg.h:79
HWIdentifier
Definition: HWIdentifier.h:13
LArMinBiasAlg::m_weight_lowPt
double m_weight_lowPt
Definition: LArMinBiasAlg.h:60
CaloCell_ID.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
LArMinBiasAlg::m_weight_highPt
double m_weight_highPt
Definition: LArMinBiasAlg.h:61
xAOD::EventInfo_v1::mcChannelNumber
uint32_t mcChannelNumber() const
The MC generator's channel number.
BchCleanup.mgr
mgr
Definition: BchCleanup.py:294
LArMCSym::ZPhiSymOfl
HWIdentifier ZPhiSymOfl(const Identifier notSymOffId) const
Find the symmetric HWID for an offline cell identifier.
Definition: LArMCSym.h:48
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
LArMinBiasAlg::m_CellList
std::vector< CellInfo > m_CellList
Definition: LArMinBiasAlg.h:108
LArMinBiasAlg::m_identifier
int m_identifier[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:87
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ParticleGun_FastCalo_ChargeFlip_Config.energy
energy
Definition: ParticleGun_FastCalo_ChargeFlip_Config.py:78
CaloIdManager
This class initializes the Calo (LAr and Tile) offline identifiers.
Definition: CaloIdManager.h:45
id2
HWIdentifier id2
Definition: LArRodBlockPhysicsV0.cxx:564
MAX_SYM_CELLS
#define MAX_SYM_CELLS
Definition: LArMinBiasAlg.h:34
LArMinBiasAlg::fillNtuple
void fillNtuple()
Definition: LArMinBiasAlg.cxx:277
LArMinBiasAlg::finalize
virtual StatusCode finalize() override
Definition: LArMinBiasAlg.cxx:99
lumiFormat.i
int i
Definition: lumiFormat.py:92
LArMinBiasAlg::m_caloMgrKey
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloMgrKey
Definition: LArMinBiasAlg.h:66
LArMinBiasAlg::initialize
virtual StatusCode initialize() override
Definition: LArMinBiasAlg.cxx:39
LArMinBiasAlg::m_average
double m_average[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:91
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
LArMinBiasAlg::m_thistSvc
ITHistSvc * m_thistSvc
Definition: LArMinBiasAlg.h:76
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
vector
Definition: MultiHisto.h:13
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
checkxAOD.frac
frac
Definition: Tools/PyUtils/bin/checkxAOD.py:256
LArMinBiasAlg::m_calo_id
const CaloCell_ID * m_calo_id
Definition: LArMinBiasAlg.h:72
LArMinBiasAlg::execute
virtual StatusCode execute() override
Definition: LArMinBiasAlg.cxx:106
LArMinBiasAlg::m_ncell
int m_ncell
Definition: LArMinBiasAlg.h:111
LArMinBiasAlg::~LArMinBiasAlg
~LArMinBiasAlg()
Default Destructor.
LArMinBiasAlg::CellInfo
Definition: LArMinBiasAlg.h:96
CaloCell_Base_ID::eta
int eta(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
LArMinBiasAlg.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
LArMinBiasAlg::m_symCellIndex
std::vector< int > m_symCellIndex
Definition: LArMinBiasAlg.h:109
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
AtlasDetectorID::print_to_string
std::string print_to_string(Identifier id, const IdContext *context=0) const
or provide the printout in string form
Definition: AtlasDetectorID.cxx:655
Trk::index2
@ index2
Definition: BoundarySurfaceFace.h:49
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
LArMinBiasAlg::m_first
float m_first
Definition: LArMinBiasAlg.h:110
LArHit
Class to store hit energy and time in LAr cell from G4 simulation.
Definition: LArHit.h:25
CaloCell_Base_ID::cell_id
Identifier cell_id(const int subCalo, const int barec_or_posneg, const int sampling_or_fcalmodule, const int region_or_dummy, const int eta, const int phi) const
Make a cell (== channel) ID from constituting fields and subCalo index; for (Mini)FCAL,...
LArMinBiasAlg::m_phi
float m_phi[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:90
LArHit.h
DeMoScan.index
string index
Definition: DeMoScan.py:362
LArMinBiasAlg::stop
virtual StatusCode stop() override
Definition: LArMinBiasAlg.cxx:91
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
LArHitContainer.h
LArMinBiasAlg::m_region
int m_region[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:86
LArMinBiasAlg::m_ieta
int m_ieta[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:88
LArMinBiasAlg::m_layer
int m_layer[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:85
beamspotnt.rms
rms
Definition: bin/beamspotnt.py:1266
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
CaloIdManager.h
CaloDetDescrElement::eta
float eta() const
cell eta
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:344
CaloDetDescrElement::phi
float phi() const
cell phi
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:346
IdentifierHash
Definition: IdentifierHash.h:38
LArMinBiasAlg::m_cablingKey
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingKey
Definition: LArMinBiasAlg.h:64
readCCLHist.float
float
Definition: readCCLHist.py:83
LArMinBiasAlg::m_eta
float m_eta[MAX_SYM_CELLS]
Definition: LArMinBiasAlg.h:89
LArMinBiasAlg::m_datasetID_highPt
int m_datasetID_highPt
Definition: LArMinBiasAlg.h:59
CaloCell_Base_ID::calo_cell_hash_max
size_type calo_cell_hash_max(void) const
cell 'global' hash table max size
LArOnOffIdMapping
Definition: LArOnOffIdMapping.h:20