ATLAS Offline Software
JetAttributeHisto.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #define toGeV 1/1000.
7 
8 namespace jet {
9  template<typename T>
11 
12  class HistoFiller {
13  public:
14  virtual ~HistoFiller() = default;
15  virtual void fill(const xAOD::Jet &, float /*weight*/ ){};
16  // allows to avoid invalid attribute
17  virtual bool isValid(const xAOD::Jet &){return false;}
18  };
19 
20  // ------------------
21  template<typename ATYPE, typename HTYPE>
23  AccessorAndHisto(const std::string & attname, HTYPE* h, bool gev) : m_accessor(attname), m_h(h), m_toGeV1(gev ? toGeV :1) {}
24 
25  protected:
27  HTYPE* m_h;
28  const float m_toGeV1;
29  };
30 
31  template<typename ATYPE1,typename ATYPE2, typename HTYPE>
32  struct AccessorAndHisto2 : public AccessorAndHisto<ATYPE1, HTYPE> {
33  AccessorAndHisto2(const std::string & att1,const std::string & att2 , HTYPE* h, bool gev1, bool gev2) : AccessorAndHisto<ATYPE1,HTYPE>(att1,h, gev1), m_accessor2(att2), m_toGeV2(gev2 ? toGeV : 1) {}
34  protected:
36  const float m_toGeV2;
37  };
38  // ------------------
39 
40 // shotcuts to have more compact code
41 #define scale1 this->m_toGeV1
42 #define scale2 this->m_toGeV2
43 
44  // ------------------
45  // 1D hist fillers
46  template<typename T>
47  struct AttFiller : public HistoFiller, public AccessorAndHisto<T, TH1F> {
48  AttFiller(const std::string & attname, TH1F* h, bool gev1) : AccessorAndHisto<T, TH1F>(attname, h, gev1) {}
49 
50  virtual void fill(const xAOD::Jet & j, float weight){this->m_h->Fill( this->m_accessor(j)*scale1, weight ); };
51  virtual bool isValid(const xAOD::Jet &j){return this->m_accessor.isAvailable(j);}
52  };
53 
54 
55  template<typename T>
56  struct VecAttFiller : public HistoFiller, public AccessorAndHisto<std::vector<T>, TH1F> {
57  VecAttFiller(const std::string & attname, TH1F* h, bool gev1) : AccessorAndHisto<std::vector<T>, TH1F>(attname, h, gev1) {}
58 
59  virtual void fill(const xAOD::Jet & j, float weight){
60  const std::vector<T> & vec = this->m_accessor( j);
61  for(const T& v : vec ) this->m_h->Fill( v *scale1, weight );
62  }
63  virtual bool isValid(const xAOD::Jet & j){return this->m_accessor.isAvailable(j);}
64 
65  };
66 
67  template<typename T>
68  struct VecAttIndexFiller : public HistoFiller, public AccessorAndHisto<std::vector<T>, TH1F> {
69  VecAttIndexFiller(const std::string & attname, TH1F* h, size_t index, bool gev1) : AccessorAndHisto<std::vector<T>,TH1F>(attname,h, gev1), m_index(index) {}
70 
71  virtual void fill(const xAOD::Jet & j, float weight){
72  const std::vector<T> & vec = this->m_accessor( j);
73  if( vec.size() > m_index) this->m_h->Fill( vec[m_index]*scale1, weight );
74  }
75  virtual bool isValid(const xAOD::Jet & j){return this->m_accessor.isAvailable(j);}
76 
77  protected:
78  size_t m_index;
79  };
80 
81 
82  // ------------------
83  // 2D hist fillers
84  template<typename T,typename HTYPE>
85  struct AttvsAttFiller : public HistoFiller, public AccessorAndHisto2<T,T, HTYPE> {
86  AttvsAttFiller(const std::string & att1,const std::string & att2 , HTYPE* h, bool gev1, bool gev2) : AccessorAndHisto2<T,T, HTYPE>(att1,att2,h, gev1, gev2) {}
87 
88  virtual void fill(const xAOD::Jet & j, float weight){this->m_h->Fill( this->m_accessor(j)*scale1, this->m_accessor2(j)*scale2, weight ); };
89  virtual bool isValid(const xAOD::Jet &j){return (this->m_accessor.isAvailable(j))&&(this->m_accessor2.isAvailable(j));}
90 
91  };
92 
93  template<typename T,typename HTYPE>
94  struct AttvsVecAttIndexFiller : public HistoFiller, public AccessorAndHisto2<std::vector<T>,T, HTYPE> {
95  AttvsVecAttIndexFiller(const std::string & att1,const std::string & att2 , HTYPE* h, size_t index , bool gev1, bool gev2, bool swapAxis=false) : AccessorAndHisto2<std::vector<T>,T, HTYPE>(att1,att2,h, gev1, gev2) , m_index(index), m_swap(swapAxis){}
96 
97  virtual void fill(const xAOD::Jet & j, float weight){
98  const std::vector<T> & vec = this->m_accessor( j);
99  if( vec.size() > m_index) {
100  if( m_swap) this->m_h->Fill( this->m_accessor2(j)*scale2, vec[m_index]*scale1, weight ) ;
101  else this->m_h->Fill( vec[m_index] *scale1, this->m_accessor2(j)*scale2, weight) ;
102  }
103  }
104 
105  virtual bool isValid(const xAOD::Jet &j){return (this->m_accessor.isAvailable(j))&&(this->m_accessor2.isAvailable(j));}
106 
107  protected:
108  size_t m_index;
109  bool m_swap;
110  };
111 
112 
113 #undef scale1
114 #undef scale2
115 
116  // ------------------
117  // ------------------
118  struct AttTypes {
119  enum Supported {
121  Int=1,
122  Float=2,
125 
129  };
130  static Supported fromString(const std::string &n){
131  static const std::map< std::string , Supported > m( { {"int",Int}, {"float",Float}, {"vector<float>",VectFloat}, {"vector<int>",VectInt}, } );
132  auto itr = m.find( n );
133  return ( itr != m.end() ? itr->second : Unknown );
134  }
135  static Supported fromString(const std::string &n1, const std::string &n2){
137  Supported t2 = fromString(n2);
138  switch( (Supported) (10*t1+t2) ) {
139  case Float_Float:
140  return Float_Float;
141  case Float_VectFloat:
142  return Float_VectFloat;
143  case VectFloat_Float:
144  return VectFloat_Float;
145  default:
146  break;
147  }
148  return Unknown;
149  }
150 
151  };
152 
153 
154 }
155 
156 
157 
158 // ****************************************************
159 // ****************************************************
160 
161 
163  , m_histoDef(this)
164  , m_attTypes()
165  , m_selectedIndex(-1)
166  , m_doTProfile(false)
167  , m_histoFiller(nullptr)
168 {
169  declareProperty("HistoDef", m_histoDef);
170  declareProperty("SelectIndex", m_selectedIndex);
171  declareProperty("DoTProfile", m_doTProfile);
172  declareProperty("AttributeTypes", m_attTypes);
173  declareProperty("AttributeNames", m_attNames);
174  declareProperty("AttributeInGeV", m_attGeV);
175 
176 }
177 
179  if(m_histoFiller) delete m_histoFiller;
180 }
182  CHECK( m_histoDef.retrieve() );
184  if(m_attNames.empty() ) {
185  ATH_MSG_ERROR("Differing no attribute name specified ");
186  return StatusCode::FAILURE;
187  }
188 
189  if( m_attNames.size() != m_attTypes.size() ){
190  ATH_MSG_ERROR("Differing number of attributes names and types ");
191  return StatusCode::FAILURE;
192  }
193 
194  m_attGeV.resize(m_attNames.size(), false); // re-size it to 2 entry with false by default. No effect if already size 2
195 
196  return StatusCode::SUCCESS;
197 }
198 
199 
200 using namespace jet;
202  //std::string attname = m_histoDef->attributeName();
203 
204  if( m_attTypes.size() == 1 ){
205  bool gev1 = m_attGeV[0];
206  AttTypes::Supported typ = AttTypes::fromString(m_attTypes[0]);
207  switch( typ ){
208  case AttTypes::Int:
209  m_histoFiller = new AttFiller<int>(m_attNames[0],
210  bookHisto( m_histoDef->buildTH1F() ), gev1 );
211  break;
212  case AttTypes::Float :
213  m_histoFiller = new AttFiller<float>(m_attNames[0],
214  bookHisto( m_histoDef->buildTH1F() ), gev1 );
215  break;
216  case AttTypes::VectFloat :
217  {
218  if(m_selectedIndex==-1)
219  m_histoFiller = new VecAttFiller<float>(m_attNames[0],
220  bookHisto( m_histoDef->buildTH1F() ), gev1 );
221  else
222  m_histoFiller = new VecAttIndexFiller<float>(m_attNames[0],
223  bookHisto( m_histoDef->buildTH1F() ) , m_selectedIndex , gev1
224  );
225  }
226  break;
227  case AttTypes::VectInt :
228  {
229  if(m_selectedIndex==-1)
230  m_histoFiller = new VecAttFiller<int>(m_attNames[0],
231  bookHisto( m_histoDef->buildTH1F() ), gev1 );
232  else
233  m_histoFiller = new VecAttIndexFiller<int>(m_attNames[0],
234  bookHisto( m_histoDef->buildTH1F() ) , m_selectedIndex , gev1
235  );
236  }
237  break;
238  default:
239  {
240  ATH_MSG_ERROR("Do not support attribute type "<< m_attTypes[0] << " "<< typ);
241  return 1;
242  }
243  } // switch
244  }// attType.size()==1
245 
246  else if( m_attTypes.size() == 2 ){
247  std::string att1 = m_attTypes[0];
248  std::string att2 = m_attTypes[1];
249  std::string n1 = m_attNames[0];
250  std::string n2 = m_attNames[1];
251  bool gev1 = m_attGeV[0];
252  bool gev2 = m_attGeV[1];
253 
254  AttTypes::Supported typ = AttTypes::fromString(att1,att2);
255 
256  switch( typ ){
258  {
259  if(m_doTProfile)
260  m_histoFiller = new AttvsAttFiller<float, TProfile>(n1, n2,
261  bookHisto( m_histoDef->buildTProfile() ), gev1, gev2 );
262  else
263  m_histoFiller = new AttvsAttFiller<float, TH2F>(n1, n2,
264  bookHisto( m_histoDef->buildTH2F() ),gev1, gev2 );
265  }
266  break;
269  {
270  if(m_selectedIndex==-1) { ATH_MSG_ERROR("Must select an index >-1 for vector<float> vs float attribute histogramming"); return 1;}
271  bool swapAxis = (typ != AttTypes::VectFloat_Float);
272  if(swapAxis) att1.swap(att2);
273  if(m_doTProfile)
274  m_histoFiller = new AttvsVecAttIndexFiller<float, TProfile>(n1, n2,
275  bookHisto( m_histoDef->buildTProfile() ),
276  m_selectedIndex, gev1, gev2, swapAxis);
277 
278  else
279  m_histoFiller = new AttvsVecAttIndexFiller<float, TH2F>(n1, n2,
280  bookHisto( m_histoDef->buildTH2F() ),
281  m_selectedIndex, gev1, gev2, swapAxis);
282  }
283  break;
284  default:
285  {
286  ATH_MSG_ERROR("Do not support attribute types "<< m_attTypes[0] << " with "<< m_attTypes[1]);
287  return 1;
288  }
289  } // switch
290  } //size()==2
291  if(m_histoFiller==nullptr) {
292  ATH_MSG_ERROR("Do not support attribute types __ ");
293  return 1;
294  }
295 
296  return 0;
297 }
298 
300  m_histoFiller->fill(j, weight);
301  return 0;
302 }
303 
305  if (cont.empty() ) return 0;
306  const xAOD::Jet * j0 = cont[0];
307  if ( !m_histoFiller->isValid(*j0) ){
308  return 0;
309  }
311 }
312 
313 
314 void JetAttributeHisto::renameAndRegister(TH1* h, const std::string & subD, Interval_t i){
315 
316  // if(m_selectedIndex >-1 ) {
317  // TString s="_"; s+= m_selectedIndex;
318  // h->SetName( h->GetName()+s);
319  // }
321  if(m_selectedIndex >-1 ) {
322  TString s=" (at index "; s+= m_selectedIndex; s+=")";
323  h->SetTitle( h->GetTitle()+s);
324  }
325 }
JetAttributeHisto::buildHistos
virtual int buildHistos()
Definition: JetAttributeHisto.cxx:201
jet::AttFiller
Definition: JetAttributeHisto.cxx:47
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
scale2
#define scale2
Definition: JetAttributeHisto.cxx:42
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
JetAttributeHisto::renameAndRegister
virtual void renameAndRegister(TH1 *h, const std::string &subD="", Interval_t i=all)
Rename an histo according to the tool's path. redefine here to take m_selectedIndex into account.
Definition: JetAttributeHisto.cxx:314
jet::AttvsVecAttIndexFiller::AttvsVecAttIndexFiller
AttvsVecAttIndexFiller(const std::string &att1, const std::string &att2, HTYPE *h, size_t index, bool gev1, bool gev2, bool swapAxis=false)
Definition: JetAttributeHisto.cxx:95
JetAttributeHisto::JetAttributeHisto
JetAttributeHisto(const std::string &t)
Definition: JetAttributeHisto.cxx:162
jet::VecAttFiller::fill
virtual void fill(const xAOD::Jet &j, float weight)
Definition: JetAttributeHisto.cxx:59
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
jet::AccessorAndHisto2::m_toGeV2
const float m_toGeV2
Definition: JetAttributeHisto.cxx:36
jet::AttFiller::fill
virtual void fill(const xAOD::Jet &j, float weight)
Definition: JetAttributeHisto.cxx:50
jet::AccessorAndHisto::AccessorAndHisto
AccessorAndHisto(const std::string &attname, HTYPE *h, bool gev)
Definition: JetAttributeHisto.cxx:23
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
jet::VecAttIndexFiller
Definition: JetAttributeHisto.cxx:68
JetAttributeHisto::m_attGeV
std::vector< bool > m_attGeV
Definition: JetAttributeHisto.h:56
jet::AttTypes::Float_VectFloat
@ Float_VectFloat
Definition: JetAttributeHisto.cxx:127
jet::AttTypes::Float_Float
@ Float_Float
Definition: JetAttributeHisto.cxx:126
jet::VecAttIndexFiller::VecAttIndexFiller
VecAttIndexFiller(const std::string &attname, TH1F *h, size_t index, bool gev1)
Definition: JetAttributeHisto.cxx:69
jet::HistoFiller::~HistoFiller
virtual ~HistoFiller()=default
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
jet::VecAttIndexFiller::m_index
size_t m_index
Definition: JetAttributeHisto.cxx:78
jet::AttTypes::Supported
Supported
Definition: JetAttributeHisto.cxx:119
JetHistoBase::fillHistosFromContainer
virtual int fillHistosFromContainer(const xAOD::JetContainer &cont, float weight)
Definition: JetHistoBase.cxx:24
JetAttributeHisto::m_attNames
std::vector< std::string > m_attNames
Definition: JetAttributeHisto.h:55
JetAttributeHisto::fillHistosFromContainer
virtual int fillHistosFromContainer(const xAOD::JetContainer &cont, float weight)
Definition: JetAttributeHisto.cxx:304
jet::AttTypes::fromString
static Supported fromString(const std::string &n1, const std::string &n2)
Definition: JetAttributeHisto.cxx:135
jet::VecAttFiller::VecAttFiller
VecAttFiller(const std::string &attname, TH1F *h, bool gev1)
Definition: JetAttributeHisto.cxx:57
JetAttributeHisto::initialize
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: JetAttributeHisto.cxx:181
JetAttributeHisto::m_attTypes
std::vector< std::string > m_attTypes
Definition: JetAttributeHisto.h:54
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
jet::AttvsVecAttIndexFiller::m_index
size_t m_index
Definition: JetAttributeHisto.cxx:108
JetHistoBase
Definition: JetHistoBase.h:27
jet::AttvsAttFiller::fill
virtual void fill(const xAOD::Jet &j, float weight)
Definition: JetAttributeHisto.cxx:88
jet::AttFiller::AttFiller
AttFiller(const std::string &attname, TH1F *h, bool gev1)
Definition: JetAttributeHisto.cxx:48
jet::AccessorAndHisto::m_toGeV1
const float m_toGeV1
Definition: JetAttributeHisto.cxx:28
jet::HistoFiller::isValid
virtual bool isValid(const xAOD::Jet &)
Definition: JetAttributeHisto.cxx:17
jet::AccessorAndHisto2::m_accessor2
Accessor< ATYPE2 > m_accessor2
Definition: JetAttributeHisto.cxx:35
jet::VecAttFiller
Definition: JetAttributeHisto.cxx:56
jet::AccessorAndHisto::m_h
HTYPE * m_h
Definition: JetAttributeHisto.cxx:27
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
jet::VecAttIndexFiller::fill
virtual void fill(const xAOD::Jet &j, float weight)
Definition: JetAttributeHisto.cxx:71
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
jet::AttTypes::Unknown
@ Unknown
Definition: JetAttributeHisto.cxx:120
h
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
jet::AttvsAttFiller::AttvsAttFiller
AttvsAttFiller(const std::string &att1, const std::string &att2, HTYPE *h, bool gev1, bool gev2)
Definition: JetAttributeHisto.cxx:86
jet::AttvsVecAttIndexFiller
Definition: JetAttributeHisto.cxx:94
JetAttributeHisto::m_doTProfile
bool m_doTProfile
Definition: JetAttributeHisto.h:59
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
jet::AttvsVecAttIndexFiller::m_swap
bool m_swap
Definition: JetAttributeHisto.cxx:109
jet::AttTypes::VectFloat_Float
@ VectFloat_Float
Definition: JetAttributeHisto.cxx:128
jet::AttvsVecAttIndexFiller::isValid
virtual bool isValid(const xAOD::Jet &j)
Definition: JetAttributeHisto.cxx:105
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
jet::AttvsAttFiller
Definition: JetAttributeHisto.cxx:85
jet::AccessorAndHisto
Definition: JetAttributeHisto.cxx:22
JetAttributeHisto::fillHistosFromJet
virtual int fillHistosFromJet(const xAOD::Jet &j, float weight)
Definition: JetAttributeHisto.cxx:299
jet::AccessorAndHisto2::AccessorAndHisto2
AccessorAndHisto2(const std::string &att1, const std::string &att2, HTYPE *h, bool gev1, bool gev2)
Definition: JetAttributeHisto.cxx:33
jet::AttTypes::fromString
static Supported fromString(const std::string &n)
Definition: JetAttributeHisto.cxx:130
jet::AttTypes::VectInt
@ VectInt
Definition: JetAttributeHisto.cxx:124
jet::AttvsAttFiller::isValid
virtual bool isValid(const xAOD::Jet &j)
Definition: JetAttributeHisto.cxx:89
HistoGroupBase::Interval_t
Interval_t
Redefinition of fill intervals as in ManagedMonitorToolBase.
Definition: HistoGroupBase.h:37
jet::AttTypes::Float
@ Float
Definition: JetAttributeHisto.cxx:122
Rtt_histogram.n1
n1
Definition: Rtt_histogram.py:21
python.PyAthena.v
v
Definition: PyAthena.py:154
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
ALFA_EventTPCnv_Dict::t2
std::vector< ALFA_RawDataContainer_p1 > t2
Definition: ALFA_EventTPCnvDict.h:44
JetHistoBase::initialize
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: JetHistoBase.cxx:16
jet::AttTypes::Int
@ Int
Definition: JetAttributeHisto.cxx:121
jet::AccessorAndHisto2
Definition: JetAttributeHisto.cxx:32
JetAttributeHisto.h
toGeV
#define toGeV
Definition: JetAttributeHisto.cxx:6
jet::AttTypes
Definition: JetAttributeHisto.cxx:118
jet::HistoFiller::fill
virtual void fill(const xAOD::Jet &, float)
Definition: JetAttributeHisto.cxx:15
jet::VecAttIndexFiller::isValid
virtual bool isValid(const xAOD::Jet &j)
Definition: JetAttributeHisto.cxx:75
jet::AccessorAndHisto::m_accessor
Accessor< ATYPE > m_accessor
Definition: JetAttributeHisto.cxx:26
jet::AttTypes::VectFloat
@ VectFloat
Definition: JetAttributeHisto.cxx:123
JetAttributeHisto::~JetAttributeHisto
virtual ~JetAttributeHisto()
Definition: JetAttributeHisto.cxx:178
JetAttributeHisto::m_selectedIndex
int m_selectedIndex
Definition: JetAttributeHisto.h:58
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
HistoGroupBase::renameAndRegister
virtual void renameAndRegister(TH1 *h, const std::string &subD="", Interval_t ityp=useToolInterval)
Rename an histo according to the tool's path.
Definition: HistoGroupBase.cxx:50
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
scale1
#define scale1
Definition: JetAttributeHisto.cxx:41
jet::HistoFiller
Definition: JetAttributeHisto.cxx:12
jet::AttFiller::isValid
virtual bool isValid(const xAOD::Jet &j)
Definition: JetAttributeHisto.cxx:51
JetAttributeHisto::m_histoDef
ToolHandle< HistoDefinitionTool > m_histoDef
Definition: JetAttributeHisto.h:53
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
jet::AttvsVecAttIndexFiller::fill
virtual void fill(const xAOD::Jet &j, float weight)
Definition: JetAttributeHisto.cxx:97
jet::VecAttFiller::isValid
virtual bool isValid(const xAOD::Jet &j)
Definition: JetAttributeHisto.cxx:63