ATLAS Offline Software
Loading...
Searching...
No Matches
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
8namespace jet {
9 template<typename T>
11
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 {
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){
136 Supported t1 = fromString(n1);
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
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
200using namespace jet;
202 //std::string attname = m_histoDef->attributeName();
203
204 if( m_attTypes.size() == 1 ){
205 bool gev1 = m_attGeV[0];
207 switch( typ ){
208 case AttTypes::Int:
210 bookHisto( m_histoDef->buildTH1F() ), gev1 );
211 break;
212 case AttTypes::Float :
214 bookHisto( m_histoDef->buildTH1F() ), gev1 );
215 break;
217 {
218 if(m_selectedIndex==-1)
220 bookHisto( m_histoDef->buildTH1F() ), gev1 );
221 else
223 bookHisto( m_histoDef->buildTH1F() ) , m_selectedIndex , gev1
224 );
225 }
226 break;
227 case AttTypes::VectInt :
228 {
229 if(m_selectedIndex==-1)
231 bookHisto( m_histoDef->buildTH1F() ), gev1 );
232 else
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
255
256 switch( typ ){
258 {
259 if(m_doTProfile)
261 bookHisto( m_histoDef->buildTProfile() ), gev1, gev2 );
262 else
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)
275 bookHisto( m_histoDef->buildTProfile() ),
276 m_selectedIndex, gev1, gev2, swapAxis);
277
278 else
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 }
310 return JetHistoBase::fillHistosFromContainer(cont, weight);
311}
312
313
314void 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}
#define ATH_MSG_ERROR(x)
std::vector< size_t > vec
#define CHECK(...)
Evaluate an expression and check for errors.
#define scale2
#define scale1
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
bool empty() const noexcept
Returns true if the collection is empty.
virtual void renameAndRegister(TH1 *h, const std::string &subD="", Interval_t ityp=useToolInterval)
Rename an histo according to the tool's path.
Interval_t
Redefinition of fill intervals as in ManagedMonitorToolBase.
T * bookHisto(T *h, Interval_t ityp=useToolInterval)
register the histo h in this group (if h!=NULL). The histo name is changed if m_prefixedHistoName==tr...
virtual int fillHistosFromJet(const xAOD::Jet &j, float weight)
virtual int fillHistosFromContainer(const xAOD::JetContainer &cont, float weight)
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
ToolHandle< HistoDefinitionTool > m_histoDef
JetAttributeHisto(const std::string &t)
std::vector< std::string > m_attTypes
std::vector< bool > m_attGeV
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.
jet::HistoFiller * m_histoFiller
std::vector< std::string > m_attNames
JetHistoBase(const std::string &t)
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
virtual int fillHistosFromContainer(const xAOD::JetContainer &cont, float weight)
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:572
virtual bool isValid(const xAOD::Jet &)
virtual ~HistoFiller()=default
virtual void fill(const xAOD::Jet &, float)
Definition index.py:1
SG::AuxElement::Accessor< T > Accessor
STL namespace.
Jet_v1 Jet
Definition of the current "jet version".
JetContainer_v1 JetContainer
Definition of the current "jet container version".
AccessorAndHisto2(const std::string &att1, const std::string &att2, HTYPE *h, bool gev1, bool gev2)
Accessor< ATYPE2 > m_accessor2
AccessorAndHisto(const std::string &attname, HTYPE *h, bool gev)
Accessor< ATYPE > m_accessor
virtual bool isValid(const xAOD::Jet &j)
AttFiller(const std::string &attname, TH1F *h, bool gev1)
virtual void fill(const xAOD::Jet &j, float weight)
static Supported fromString(const std::string &n)
static Supported fromString(const std::string &n1, const std::string &n2)
virtual void fill(const xAOD::Jet &j, float weight)
AttvsAttFiller(const std::string &att1, const std::string &att2, HTYPE *h, bool gev1, bool gev2)
virtual bool isValid(const xAOD::Jet &j)
AttvsVecAttIndexFiller(const std::string &att1, const std::string &att2, HTYPE *h, size_t index, bool gev1, bool gev2, bool swapAxis=false)
virtual void fill(const xAOD::Jet &j, float weight)
virtual bool isValid(const xAOD::Jet &j)
virtual void fill(const xAOD::Jet &j, float weight)
VecAttFiller(const std::string &attname, TH1F *h, bool gev1)
virtual bool isValid(const xAOD::Jet &j)
virtual void fill(const xAOD::Jet &j, float weight)
virtual bool isValid(const xAOD::Jet &j)
VecAttIndexFiller(const std::string &attname, TH1F *h, size_t index, bool gev1)