ATLAS Offline Software
HIEventShapeIndex.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include <iomanip>
8 #include <sstream>
9 #include <iostream>
10 #include <cmath>
11 #include <algorithm>
12 #include <iterator>
13 
15 {
16 }
17 
19 {
20 }
21 
22 
23 unsigned int HIEventShapeIndex::setBinning(const TH2* h2, bool asMask)
24 {
25  if(h2==nullptr)
26  {
27  //ATH_MSG_ERROR("Bad histogram pointer");
28  return 0;
29  }
30 
31  unsigned int count=0;
32  std::set<unsigned int> used_indices;
33  for(int ybin=1; ybin<h2->GetNbinsY(); ybin++)
34  {
35  auto mItr=m_edges.emplace_hint(m_edges.end(),ybin-1,std::vector<range_index_t>());
36  for(int xbin=1; xbin<h2->GetNbinsX(); xbin++)
37  {
38  if(h2->GetBinContent(xbin,ybin) < 0) continue;
39  unsigned int v=h2->GetBinContent(xbin,ybin);
40 
41  if(asMask) v=count;
42  else
43  {
44  auto sItr=used_indices.find(v);
45  if(sItr==used_indices.end()) used_indices.insert(v);
46  else
47  {
48  //ATH_MSG_ERROR("Could not set binning. initialization histogram has duplicate indices " << v);
49  //m_edges.clear();
50  //return 0;
51  continue;
52  }
53  }
54  mItr->second.push_back(range_index_t(h2->GetXaxis()->GetBinLowEdge(xbin),h2->GetXaxis()->GetBinUpEdge(xbin),v));
55  count++;
56  }
57  }
58  m_size=count;
59  return count;
60 }
61 
63 {
64  unsigned int index=0;
66  {
67  for(unsigned int layer=0; layer < static_cast<int>(HI::TowerBins::numLayers()); layer++)
68  {
69  auto mItr=m_edges.emplace_hint(m_edges.end(),layer,std::vector<range_index_t>(HI::TowerBins::numEtaBins(),range_index_t()));
70  for(unsigned int eb=0; eb<HI::TowerBins::numEtaBins(); eb++,index++)
71  {
72 
73  mItr->second[eb].eta_min=HI::TowerBins::getBinLowEdgeEta(eb);
74  mItr->second[eb].eta_max=HI::TowerBins::getBinUpEdgeEta(eb);
75  mItr->second[eb].index=index;
76  }
77  }
78  }
80  {
81  float deta=HI::TowerBins::getBinSizeEta();
82  for(int layer=0; layer < static_cast<int>(HI::TowerBins::numLayers()); layer++)
83  {
86  unsigned int num_eta_bins= static_cast<unsigned int>(std::round(std::abs((emax-emin)/deta)));
87  auto mItr=m_edges.emplace_hint(m_edges.end(),layer,std::vector<range_index_t>(2*num_eta_bins,range_index_t()));
88  for(unsigned int k=0; k<num_eta_bins; k++)
89  {
90  mItr->second[k].eta_min=roundToTenth(-emax+k*deta);
91  mItr->second[k].eta_max=roundToTenth(-emax+k*deta+deta);
92  mItr->second[k].index=roundToTenth(k+index);
93  mItr->second[k+num_eta_bins].eta_min=roundToTenth(emin+k*deta);
94  mItr->second[k+num_eta_bins].eta_max=roundToTenth(emin+k*deta+deta);
95  mItr->second[k+num_eta_bins].index=roundToTenth(k+num_eta_bins+index);
96  }
97  index+=2*num_eta_bins;
98  }
99  }
100  m_size=index;
101  return index;
102 
103 }
104 
105 unsigned int HIEventShapeIndex::getIndexFromBin(unsigned int ebin, int layer) const
106 {
107  return getLayer(layer)->second.at(ebin).index;
108 }
109 
110 unsigned int HIEventShapeIndex::getIndex_Internal(float eta, int layer, bool etaIndex) const
111 {
112  auto mItr=getLayer(layer);
113  const std::vector<range_index_t>& vec=mItr->second;
114  unsigned int vsize=vec.size();
115 
116  if(vsize==0)
117  {
118  std::cerr << "Layer " << layer << " has no range in calorimeter" << std::endl;
119  throw std::range_error("Bad binning request");
120  return 0;
121  }
122 
123  float eta_c=eta;
124  if(m_edges.size()>1)
125  {
126  float abs_eta=std::abs(eta);
129 
130  if(abs_eta < rmin) eta_c=rmin+std::copysign(1e-2,eta);
131  else if(abs_eta > rmax) eta_c=rmax-std::copysign(1e-2,eta);
132  }
133 
134  unsigned int pIndex=0;
135  for(; pIndex < vsize-1; pIndex++)
136  {
137  const range_index_t& p=vec.at(pIndex);
138  if(p(eta_c)) break;
139  }
140 
141  return (etaIndex) ? pIndex : vec.at(pIndex).index;
142 }
143 
144 
145 unsigned int HIEventShapeIndex::getIndex(float eta, int layer) const
146 {
147  return getIndex_Internal(eta,layer,false);
148 }
149 
150 unsigned int HIEventShapeIndex::getEtaBin(float eta, int layer) const
151 {
152  return getIndex_Internal(eta,layer,true);
153 }
154 
156 {
157  if(shape_container==nullptr)
158  {
159  //ATH_MSG_WARNING("No default HIEventShapeContainer to query. Returning nullptr_t.");
160  return nullptr;
161  }
162  unsigned int bin=getIndex(eta,layer);
163  //correct for round-off errors in cell eta values, just need to make sure they end up in the right bin
164  if( bin >= shape_container->size() ) bin=getIndex(eta-std::copysign(1e-2,eta),layer);
165 
166  return shape_container->at(bin);
167 }
168 
170 {
171  if(shape_container==nullptr)
172  {
173  //ATH_MSG_WARNING("No default HIEventShapeContainer to query. Returning nullptr_t.");
174  return nullptr;
175  }
176  unsigned int bin=getIndex(eta,layer);
177  //correct for round-off errors in cell eta values, just need to make sure they end up in the right bin
178  if( bin >= shape_container->size() ) bin=getIndex(eta-std::copysign(1e-2,eta),layer);
179 
180  return shape_container->at(bin);
181 }
182 
183 
184 xAOD::HIEventShape* HIEventShapeIndex::getShape(float eta, int layer, std::unique_ptr<xAOD::HIEventShapeContainer>& shape_container) const
185 {
186  unsigned int bin=getIndex(eta,layer);
187  //correct for round-off errors in cell eta values, just need to make sure they end up in the right bin
188  if( bin >= shape_container->size() ) bin=getIndex(eta-std::copysign(1e-2,eta),layer);
189 
190  return shape_container->at(bin);
191 }
192 
193 std::map<int,std::vector<HIEventShapeIndex::range_index_t> >::const_iterator HIEventShapeIndex::getLayer(int layer) const
194 {
195  auto mItr=m_edges.find(layer);
196 
197  if(mItr==m_edges.end())
198  {
199  //ATH_MSG_ERROR("Layer " << layer << " has no binning specification");
200  std::cerr << "Layer " << layer << " has no binning specification" << std::endl;
201  throw std::range_error("Bad binning request");
202  }
203  return mItr;
204 }
205 
206 void HIEventShapeIndex::initializeEventShapeContainer(std::unique_ptr<xAOD::HIEventShapeContainer>& shape_container, unsigned int num_harmonics) const
207 {
208  if(getNumBins()==0)
209  {
210  //ATH_MSG_WARNING("Cannot initialize HIEventShapeContainer. Index has not been initialized.");
211  return;
212 
213  }
214  bool isEmpty=(shape_container->size()==0);
215  if(isEmpty) shape_container->resize(getNumBins());
216  else if(shape_container->size()!=getNumBins())
217  {
218  //ATH_MSG_WARNING("Cannot initialize HIEventShapeContainer. Number of elements incompatible w/ requested binning.");
219  return;
220 
221  }
222 
223  for(const auto& pp : m_edges)
224  {
225  for(auto ri : pp.second)
226  {
228  if(isEmpty)
229  {
231  //shape_container->push_back(slice);
232  shape_container->operator[](ri.index)=slice;
233  }
234  else slice=shape_container->at(ri.index);
235 
236  slice->setEtaMin(ri.eta_min);
237  slice->setEtaMax(ri.eta_max);
238  slice->setLayer(pp.first);
239  slice->etCos().assign(num_harmonics,0);
240  slice->etSin().assign(num_harmonics,0);
241  }
242  }
243 }
244 
245 
246 std::string HIEventShapeIndex::print() const
247 {
248  std::stringstream ss;
249  ss.precision(3);
250  for(const auto& pp : m_edges)
251  {
252  for(auto ri : pp.second)
253  {
254  ss << std::setw(10) << pp.first
255  << std::setw(10) << ri.eta_min
256  << std::setw(10) << ri.eta_max
257  << std::setw(10) << ri.index
258  << std::endl;
259  }
260  }
261  return ss.str();
262 }
HI::TOWER
@ TOWER
Definition: HIEventDefs.h:16
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
HIEventShapeIndex::getEtaBin
unsigned int getEtaBin(float eta, int layer) const
Definition: HIEventShapeIndex.cxx:150
HIEventShapeIndex.h
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
index
Definition: index.py:1
HIEventShapeIndex::getIndexFromBin
unsigned int getIndexFromBin(unsigned int ebin, int layer) const
Definition: HIEventShapeIndex.cxx:105
HICaloRange::getRangeMax
float getRangeMax(int layer) const
Definition: HICaloRange.h:20
HIEventShapeIndex::roundToTenth
float roundToTenth(float d) const
Definition: HIEventShapeIndex.h:66
HICaloRange::getRangeMin
float getRangeMin(int layer) const
Definition: HICaloRange.h:19
HICaloRange::getRange
static const HICaloRange & getRange()
Definition: HICaloRange.cxx:13
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
HIEventShapeIndex::HIEventShapeIndex
HIEventShapeIndex()
Definition: HIEventShapeIndex.cxx:14
bin
Definition: BinsDiffFromStripMedian.h:43
xAOD::HIEventShape
HIEventShape_v2 HIEventShape
Definition of the latest event info version.
Definition: HIEventShape.h:16
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
HIEventShapeIndex::getShape
xAOD::HIEventShape * getShape(float eta, int layer, xAOD::HIEventShapeContainer *shape_container) const
Definition: HIEventShapeIndex.cxx:155
HI::TowerBins::getBinLowEdgeEta
float getBinLowEdgeEta(unsigned int eb)
Definition: HIEventDefs.h:38
HIEventShapeIndex::~HIEventShapeIndex
~HIEventShapeIndex()
Definition: HIEventShapeIndex.cxx:18
HICaloRange.h
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
HIEventShapeIndex::initializeEventShapeContainer
void initializeEventShapeContainer(std::unique_ptr< xAOD::HIEventShapeContainer > &shape_container, unsigned int num_harmonics) const
Definition: HIEventShapeIndex.cxx:206
HIEventShapeIndex::getLayer
std::map< int, std::vector< range_index_t > >::const_iterator getLayer(int layer) const
Definition: HIEventShapeIndex.cxx:193
xAOD::HIEventShape_v2
Interface class for the HI reconstruction EDM.
Definition: HIEventShape_v2.h:31
HI::TowerBins::getBinUpEdgeEta
float getBinUpEdgeEta(unsigned int eb)
Definition: HIEventDefs.h:39
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
perfmonmt-refit.slice
slice
Definition: perfmonmt-refit.py:52
HIEventShapeIndex::print
std::string print() const
Definition: HIEventShapeIndex.cxx:246
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
HIEventShapeIndex::setBinning
unsigned int setBinning(const TH2 *h2, bool asMask)
Definition: HIEventShapeIndex.cxx:23
HIEventShapeIndex::getNumBins
unsigned int getNumBins() const
Definition: HIEventShapeIndex.h:37
HIEventShapeIndex::range_index_t
Definition: HIEventShapeIndex.h:44
HI::COMPACT
@ COMPACT
Definition: HIEventDefs.h:16
JetTagCalibConfig.scheme
scheme
Definition: JetTagCalibConfig.py:16
HIEventShapeIndex::getIndex_Internal
unsigned int getIndex_Internal(float eta, int layer, bool etaIndex) const
Definition: HIEventShapeIndex.cxx:110
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
HI::TowerBins::numEtaBins
constexpr unsigned int numEtaBins()
Definition: HIEventDefs.h:19
DataVector::resize
void resize(size_type sz)
Resizes the collection to the specified number of elements.
HI::TowerBins::getBinSizeEta
constexpr float getBinSizeEta()
Definition: HIEventDefs.h:31
HIEventShapeIndex::m_size
unsigned int m_size
Definition: HIEventShapeIndex.h:61
HI::TowerBins::numLayers
constexpr unsigned int numLayers()
Definition: HIEventDefs.h:23
HIEventShapeIndex::getIndex
unsigned int getIndex(float eta, int layer) const
Definition: HIEventShapeIndex.cxx:145
python.PyAthena.v
v
Definition: PyAthena.py:154
DeMoScan.index
string index
Definition: DeMoScan.py:364
HI::BinningScheme
BinningScheme
Definition: HIEventDefs.h:16
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
HIEventShapeIndex::m_edges
std::map< int, std::vector< range_index_t > > m_edges
Definition: HIEventShapeIndex.h:59
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
fitman.k
k
Definition: fitman.py:528