ATLAS Offline Software
Loading...
Searching...
No Matches
JetDumper.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5// JetDumper.h
6
7#ifndef JetDumper_H
8#define JetDumper_H
9
10// David Adams
11// January 2014
12//
13// ASG tool to dump information about jets, clusters or psuedojets.
14
15#include "AsgTools/AsgTool.h"
16#include <string>
17#include <vector>
18#include <sstream>
19#include <iomanip>
20#include "fastjet/PseudoJet.hh"
21#include "AthLinks/ElementLinkVector.h"
24#include "xAODJet/Jet.h"
29
30namespace xAOD {
31class IParticle;
32}
33
52
54: virtual public IJetExecuteTool,
55 public asg::AsgTool {
57
58public: // typedefs
59
60 typedef std::string Name;
61 typedef std::vector<Name> NameList;
63 typedef std::vector<const xAOD::IParticle*> APVector;
65 typedef std::vector<APEL> APELVector;
66 typedef std::vector<const xAOD::MuonSegment*> MSVector;
68 typedef std::vector<MSEL> MSELVector;
69
70public: // methods
71
72 // Ctor.
73 JetDumper(const std::string& myname);
74
75 // Process a jet.
76 // Returns 0 for success.
77 template<typename T>
78 int dump_object(const T* pjet, const std::string& objtypename="Unknown") const;
79
80 // Process a collection of jets.
81 // Returns 0 for success.
82 template<typename TList>
83 int dump_collection(const TList* pjets, const std::string& objtypename="Unknown") const;
84
85 // Process the current event.
86 int execute() const;
87
88 // Print state.
89 void print() const;
90
91private: // methods
92
93 // Write the label for a single object.
94 template<typename T>
95 std::string object_label(const T* pjet, const std::string& label) const;
96 std::string object_label(const fastjet::PseudoJet& jet, const std::string& label) const;
97
98 // Process a single jet after writing prefix.
99 template<typename T>
100 int dump_object_after_prefix(const T* pjet, const std::string& objtypename) const;
101 int dump_object_after_prefix(const fastjet::PseudoJet& jet, const std::string& objtypename) const;
102 int dump_object_after_prefix(const xAOD::MuonSegment* seg, const std::string& objtypename) const;
103
104 // Fetch a moment.
105 template<typename TObj, typename TMom>
106 void get_moment(TObj* pobj, std::string name, TMom& val) const;
107 template<typename TObj>
108 void get_moment(TObj* pobj, std::string name, FourVector& val) const;
109 template<typename TObj>
110 void get_moment(TObj* pobj, std::string name, std::string& val) const;
111 template<typename TObj>
112 void get_moment(TObj* pobj, std::string name, std::vector<int>&) const;
113 template<typename TObj>
114 void get_moment(TObj* pobj, std::string name, std::vector<float>&) const;
115 template<typename TMom>
116 void get_moment(const xAOD::Jet* pobj, std::string name, TMom& val) const;
117 void get_moment(const xAOD::Jet* pobj, const std::string& name, FourVector& val) const;
118 void get_moment(const xAOD::Jet* pobj, const std::string& name, std::string& val) const;
119 void get_moment(const xAOD::Jet* pobj, const std::string& name, std::vector<int>& vals) const;
120 void get_moment(const xAOD::Jet* pobj, const std::string& name, std::vector<float>& vals) const;
121 template<typename TObj>
122 std::string get_moment_as_string(const TObj& obj, std::string name) const;
123 std::string get_moment_as_string(const xAOD::Jet* pobj, const std::string& name) const;
124
125 template<typename TObj>
126 void getAssociatedParticles(TObj* pobj, std::string name, APVector& val) const;
127 void getAssociatedParticles(const xAOD::Jet* pobj, const std::string& name, APVector& val) const;
128 template<typename TObj>
129 void getAssociatedLinks(TObj* pobj, std::string name, APELVector& val) const;
130 void getAssociatedLinks(const xAOD::Jet* pobj, const std::string& name, APELVector& val) const;
131 template<typename TObj>
132 void getAssociatedParticles(TObj* pobj, std::string name, MSVector& val) const;
133 void getAssociatedParticles(const xAOD::Jet* pobj, const std::string& name, MSVector& val) const;
134 template<typename TObj>
135 void getAssociatedLinks(TObj* pobj, std::string name, MSELVector& val) const;
136 void getAssociatedLinks(const xAOD::Jet* pobj, const std::string& name, MSELVector& val) const;
137
138 // Extra information to appear at the end of the line
139 // with the object kinematics.
140 template<typename T>
141 void extra_info(const T*, std::ostream& out, int) const;
142 void extra_info(const xAOD::Jet* pjet, std::ostream& out, int iopt) const;
143 void extra_info(const fastjet::PseudoJet* ppsj, std::ostream& out, int linedetail) const;
144
145private: // data
146
147 // Job options.
148 std::string m_cname;
164};
165
166// Fetch moment keys.
167template<typename T, typename TMom>
169template<>
171template<>
173
174//**********************************************************************
175
176template<typename T>
177int JetDumper::dump_object(const T* pobj, const std::string& objtypename) const {
178 msg() << MSG::INFO << objtypename << " ";
179 return dump_object_after_prefix(pobj, objtypename);
180}
181
182template<typename T>
183int JetDumper::dump_object_after_prefix(const T* pjet, const std::string& objtypename) const {
184 const double mevtogev = 0.001;
185 const int wname = 30;
186 if ( pjet == 0 ) {
187 msg() << endmsg;
188 ATH_MSG_ERROR("Null jet.");
189 return 1;
190 }
191 // One line summary.
192 std::ostringstream ssjetline;
193 if ( m_linedetail > 0 ) {
194 ssjetline << "pT =" << std::setw(6) << std::fixed << std::setprecision(1) << mevtogev*pjet->pt() << " GeV";
195 ssjetline << ", m =" << std::setw(6) << std::fixed << std::setprecision(1) << mevtogev*pjet->m() << " GeV";
196 ssjetline << ", eta =" << std::setw(5) << std::fixed << std::setprecision(2) << pjet->eta();
197 ssjetline << ", phi =" << std::setw(5) << std::fixed << std::setprecision(2) << pjet->phi();
198 }
199 if ( m_linedetail > 1 ) extra_info(pjet, ssjetline, m_linedetail);
200 msg() << ssjetline.str() << endmsg;
201 // All properties.
202 if ( m_detail > 1 ) {
203 NameList fnames = m_fmoms;
204 NameList inames = m_imoms;
205 NameList bnames = m_bmoms;
206 NameList cbnames = m_cbmoms;
207 NameList snames = m_smoms;
208 NameList fvnames = m_fvmoms;
209 NameList elnames = m_elmoms;
210 NameList vinames = m_vimoms;
211 NameList vfnames = m_vfmoms;
212 NameList apnames = m_apmoms;
213 if ( fnames.size() == 0 ) fnames = get_moment_keys<T,float>(pjet);
214 ATH_MSG_INFO(" " << objtypename << " has " << fnames.size() << " float attributes:");
215 for ( NameList::const_iterator inam=fnames.begin(); inam!=fnames.end(); ++inam ) {
216 Name name = *inam;
217 float val;
218 get_moment(pjet, name, val);
219 if ( fabs(val) > m_floatmax ) {
220 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(16) << std::scientific << std::setprecision(3) << val);
221 } else {
222 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(12) << std::fixed << std::setprecision(3) << val);
223 }
224 }
225 if ( inames.size() == 0 ) inames = get_moment_keys<T,int>(pjet);
226 ATH_MSG_INFO(" " << objtypename << " has " << inames.size() << " int attributes:");
227 for ( NameList::const_iterator inam=inames.begin(); inam!=inames.end(); ++inam ) {
228 Name name = *inam;
229 int ival;
230 get_moment(pjet, name, ival);
231 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(12) << std::fixed << std::setprecision(3) << ival);
232 }
233 if ( bnames.size() == 0 ) bnames = get_moment_keys<T,int>(pjet);
234 ATH_MSG_INFO(" " << objtypename << " has " << bnames.size() << " bool attributes:");
235 for ( NameList::const_iterator inam=bnames.begin(); inam!=bnames.end(); ++inam ) {
236 Name name = *inam;
237 bool val;
238 get_moment(pjet, name, val);
239 std::string sval = val ? "true" : "false";
240 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(6) << sval);
241 }
242 if ( cbnames.size() == 0 ) cbnames = get_moment_keys<T,int>(pjet);
243 ATH_MSG_INFO(" " << objtypename << " has " << cbnames.size() << " cbool attributes:");
244 for ( NameList::const_iterator inam=cbnames.begin(); inam!=cbnames.end(); ++inam ) {
245 Name name = *inam;
246 char val;
247 get_moment(pjet, name, val);
248 std::string sval = val ? "true" : "false";
249 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(6) << sval);
250 }
251 ATH_MSG_INFO(" " << objtypename << " has " << snames.size() << " string attributes:");
252 for ( NameList::const_iterator inam=snames.begin(); inam!=snames.end(); ++inam ) {
253 Name name = *inam;
254 std::string sval;
255 get_moment(pjet, name, sval);
256 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(30) << sval);
257 }
258 ATH_MSG_INFO(" " << objtypename << " has " << vinames.size() << " vector<int> attributes:");
259 for ( NameList::const_iterator inam=vinames.begin(); inam!=vinames.end(); ++inam ) {
260 Name name = *inam;
261 std::vector<int> vals;
262 get_moment(pjet, name, vals);
263 if ( vals.size() == 0 ) {
264 ATH_MSG_INFO(std::setw(wname) << name << ": No entries.");
265 } else {
266 int widx = 1;
267 if ( vals.size() > 10 ) widx = 2;
268 for ( unsigned int ival=0; ival<vals.size(); ++ival ) {
269 ATH_MSG_INFO(std::setw(wname) << name << "[" << std::setw(widx) << ival << "]: "
270 << std::setw(8) << vals[ival]);
271 }
272 }
273 }
274 ATH_MSG_INFO(" " << objtypename << " has " << vfnames.size() << " vector<float> attributes:");
275 for ( NameList::const_iterator inam=vfnames.begin(); inam!=vfnames.end(); ++inam ) {
276 Name name = *inam;
277 std::vector<float> vals;
278 get_moment(pjet, name, vals);
279 if ( vals.size() == 0 ) {
280 ATH_MSG_INFO(std::setw(wname) << name << ": No entries.");
281 } else {
282 int widx = 1;
283 if ( vals.size() > 10 ) widx = 2;
284 for ( unsigned int ival=0; ival<vals.size(); ++ival ) {
285 ATH_MSG_INFO(std::setw(wname) << name << "[" << std::setw(widx) << ival << "]: "
286 << std::setw(12) << std::fixed << std::setprecision(3) << vals[ival]);
287 }
288 }
289 }
290 ATH_MSG_INFO(" " << objtypename << " has " << apnames.size() << " associated particle vectors:");
291 for ( NameList::const_iterator inam=apnames.begin(); inam!=apnames.end(); ++inam ) {
292 Name name = *inam;
293 if ( name.find("MuonSegment") != std::string::npos ) {
294 MSVector vals;
295 getAssociatedParticles(pjet, name, vals);
296 MSELVector els;
297 getAssociatedLinks(pjet, name, els);
298 std::string slab = "entries";
299 if ( vals.size() == 1 ) slab = "entry";
300 if ( vals.size() > 0 ) slab += ":";
301 ATH_MSG_INFO(std::setw(wname) << name << " has " << vals.size() << " " << slab);
302 if ( els.size() == vals.size() ) {
303 for (const MSEL& el : els) {
304 ATH_MSG_INFO(std::setw(wname+2) << "" << el.dataID() << "[" << el.index() << "]");
305 }
306 } else {
307 for (auto ppar : vals) {
308 ATH_MSG_INFO(std::setw(wname+2) << "" << ppar->container() << "[" << ppar->index() << "]");
309 }
310 }
311 } else {
312 APVector vals;
313 getAssociatedParticles(pjet, name, vals);
314 APELVector els;
315 getAssociatedLinks(pjet, name, els);
316 std::string slab = "entries";
317 if ( vals.size() == 1 ) slab = "entry";
318 if ( vals.size() > 0 ) slab += ":";
319 ATH_MSG_INFO(std::setw(wname) << name << " has " << vals.size() << " " << slab);
320 // Show container names if found, otherwise container addresses.
321 if ( els.size() == vals.size() ) {
322 for (const APEL& el : els) {
323 ATH_MSG_INFO(std::setw(wname+2) << "" << el.dataID() << "[" << el.index() << "]");
324 }
325 } else {
326 for (auto ppar : vals) {
327 ATH_MSG_INFO(std::setw(wname+2) << "" << ppar->container() << "[" << ppar->index() << "]");
328 }
329 }
330 }
331 }
332 if ( fvnames.size() == 0 ) fvnames = get_moment_keys<T,float>(pjet);
333 ATH_MSG_INFO(" " << objtypename << " has " << fvnames.size() << " four-vector attributes (pT m eta phi):");
334 for ( NameList::const_iterator inam=fvnames.begin(); inam!=fvnames.end(); ++inam ) {
335 Name name = *inam;
336 FourVector val;
337 get_moment(pjet, name, val);
338 ATH_MSG_INFO(std::setw(wname) << name << ":"
339 << std::setw(12) << std::fixed << std::setprecision(3) << val.Pt()
340 << std::setw(12) << std::fixed << std::setprecision(3) << val.M()
341 << std::setw(12) << std::fixed << std::setprecision(3) << val.Eta()
342 << std::setw(12) << std::fixed << std::setprecision(3) << val.Phi()
343 );
344 }
345 ATH_MSG_INFO(" " << objtypename << " has " << elnames.size() << " element-link attributes:");
346 for ( NameList::const_iterator inam=elnames.begin(); inam!=elnames.end(); ++inam ) {
347 std::string name = *inam;
348 std::string sel = get_moment_as_string(pjet, name);
349 ATH_MSG_INFO(std::setw(wname) << name << ": " << sel);
350 }
351 }
352 return 0;
353}
354
355template<typename T>
356std::string JetDumper::object_label(const T*, const std::string& label) const {
357 return label;
358}
359
360//**********************************************************************
361
362template<typename T>
363T& const_cast_ptr(T& obj) { return obj; }
364
365template<typename T>
366T* const_cast_ptr(T* pobj) { return const_cast<T*>(pobj); }
367
368template<typename TList>
369int JetDumper::dump_collection(const TList* pjets, const std::string& objtypename) const {
370 int njet = pjets->size();
371 ATH_MSG_INFO(" " << objtypename << " multiplicity: " << njet << " [" << m_cname << "]");
372 std::string line = "-------------------------------------------------------------------------";
373 int jstat = 0;
374 if ( m_detail > 0 ) {
375 unsigned int njetshow = njet;
376 if ( m_maxobj > 0 && m_maxobj < njet ) njetshow = m_maxobj;
377 typename TList::const_iterator itjet = pjets->begin();
378 typedef typename TList::const_iterator::reference TValue;
379 std::vector<std::string> labs;
380 unsigned int wlab = 0;
381 for ( unsigned int ijet=0; ijet<njetshow; ++ijet ) {
382 TValue pjet = *itjet++;
383 std::string lab = object_label(pjet, objtypename);
384 labs.push_back(lab);
385 if ( lab.size() > wlab ) wlab = lab.size();
386 }
387 itjet = pjets->begin();
388 int widx = 1;
389 if ( njetshow > 10 ) widx = 2;
390 if ( njetshow > 100 ) widx = 3;
391 if ( njetshow > 1000 ) widx = 4;
392 for ( unsigned int ijet=0; ijet<njetshow; ++ijet ) {
393 TValue pjet = *itjet++;
394 if ( m_detail > 1 && ijet==0 ) ATH_MSG_INFO(line);
395 // One line summary.
396 msg() << MSG::INFO << " ";
397 if ( m_prefix == 1 || m_prefix > 2 ) {
398 msg() << std::setw(wlab) << labs[ijet] << " ";
399 }
400 if ( m_prefix < 2 ) {
401 msg() << std::setw(widx) << ijet;
402 } else {
403 msg() << m_cname;
404 msg() << "[" << std::setw(widx) << ijet << "]";
405 }
406 msg() << ": ";
407 jstat += dump_object_after_prefix(pjet, objtypename);
408 if ( m_detail > 1 ) ATH_MSG_INFO(line);
409 }
410 }
411 return jstat;
412}
413
414//**********************************************************************
415
416template<typename TObj, typename TMom>
417void JetDumper::get_moment(TObj*, std::string, TMom& val) const {
418 val = 0;
419}
420
421template<typename TObj>
422void JetDumper::get_moment(TObj*, std::string, FourVector& val) const {
423 val = FourVector();
424}
425
426template<typename TObj>
427void JetDumper::get_moment(TObj*, std::string, std::string& val) const {
428 val = "";
429}
430
431template<typename TObj>
432void JetDumper::get_moment(TObj*, std::string, std::vector<int>& val) const {
433 static const std::vector<int> empty;
434 val = empty;
435}
436
437template<typename TObj>
438void JetDumper::get_moment(TObj*, std::string, std::vector<float>& val) const {
439 static const std::vector<float> empty;
440 val = empty;
441}
442
443template<typename TMom>
444void JetDumper::get_moment(const xAOD::Jet* pjet, std::string name, TMom& val) const {
445 val = pjet->getAttribute<TMom>(name);
446}
447
448template<typename TObj>
449void JetDumper::getAssociatedParticles(TObj*, std::string, APVector&) const { }
450
451template<typename TObj>
452void JetDumper::getAssociatedLinks(TObj*, std::string, APELVector&) const { }
453
454template<typename TObj>
455void JetDumper::getAssociatedParticles(TObj*, std::string, MSVector&) const { }
456
457template<typename TObj>
458void JetDumper::getAssociatedLinks(TObj*, std::string, MSELVector&) const { }
459
460template<typename TObj>
461std::string
462JetDumper::get_moment_as_string(const TObj&, std::string) const {
463 return " Not a jet.";
464}
465
466//**********************************************************************
467
468template<typename T, typename TMom>
472
473//**********************************************************************
474
475template<typename T>
476void JetDumper::extra_info(const T*, std::ostream& out, int) const {
477 out << ", Unknown type: " << typeid(T).name();
478}
479
480//**********************************************************************
481
482#endif
#define endmsg
#define ASG_TOOL_CLASS(CLASSNAME, INT1)
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
T & const_cast_ptr(T &obj)
Definition JetDumper.h:363
JetDumper::NameList get_moment_keys< xAOD::Jet, int >(const xAOD::Jet *pjet)
JetDumper::NameList get_moment_keys(const T *pobj)
Definition JetDumper.h:469
JetDumper::NameList get_moment_keys< xAOD::Jet, float >(const xAOD::Jet *pjet)
static const Attributes_t empty
IJetExecuteTool is a dual-use tool interface for generic tools, i.e. those that behave like algorithm...
JetDumper(const std::string &myname)
Definition JetDumper.cxx:24
void getAssociatedLinks(TObj *pobj, std::string name, APELVector &val) const
Definition JetDumper.h:452
NameList m_bmoms
Int moments.
Definition JetDumper.h:156
NameList m_fvmoms
String moments.
Definition JetDumper.h:159
NameList m_imoms
Float moments.
Definition JetDumper.h:155
std::string m_cname
Definition JetDumper.h:148
std::vector< MSEL > MSELVector
Definition JetDumper.h:68
void getAssociatedParticles(TObj *pobj, std::string name, APVector &val) const
Definition JetDumper.h:449
int m_detail
Definition JetDumper.h:149
std::vector< APEL > APELVector
Definition JetDumper.h:65
void get_moment(TObj *pobj, std::string name, TMom &val) const
Definition JetDumper.h:417
std::vector< const xAOD::MuonSegment * > MSVector
Definition JetDumper.h:66
std::string get_moment_as_string(const TObj &obj, std::string name) const
Definition JetDumper.h:462
float m_floatmax
Definition JetDumper.h:153
NameList m_smoms
Bool stored as char moments.
Definition JetDumper.h:158
NameList m_fmoms
Definition JetDumper.h:154
NameList m_cbmoms
Bool moments.
Definition JetDumper.h:157
int dump_object(const T *pjet, const std::string &objtypename="Unknown") const
Definition JetDumper.h:177
NameList m_vfmoms
Vector<int> moments.
Definition JetDumper.h:162
NameList m_vimoms
Element link moments.
Definition JetDumper.h:161
int dump_object_after_prefix(const T *pjet, const std::string &objtypename) const
Definition JetDumper.h:183
void extra_info(const T *, std::ostream &out, int) const
Definition JetDumper.h:476
int m_maxobj
Definition JetDumper.h:152
int m_prefix
Definition JetDumper.h:151
ElementLink< xAOD::MuonSegmentContainer > MSEL
Definition JetDumper.h:67
std::string Name
Definition JetDumper.h:60
NameList m_elmoms
Four-vector moments.
Definition JetDumper.h:160
std::vector< const xAOD::IParticle * > APVector
Definition JetDumper.h:63
std::string object_label(const T *pjet, const std::string &label) const
Definition JetDumper.h:356
int m_linedetail
Definition JetDumper.h:150
int execute() const
Method to be called for each event.
Definition JetDumper.cxx:63
ElementLink< xAOD::IParticleContainer > APEL
Definition JetDumper.h:64
int dump_collection(const TList *pjets, const std::string &objtypename="Unknown") const
Definition JetDumper.h:369
void print() const
Print the state of the tool.
Definition JetDumper.cxx:98
xAOD::JetFourMom_t FourVector
Definition JetDumper.h:62
std::vector< Name > NameList
Definition JetDumper.h:61
NameList m_apmoms
Vector<float> moments.
Definition JetDumper.h:163
Base class for the dual-use tool implementation classes.
Definition AsgTool.h:47
Class providing the definition of the 4-vector interface.
bool getAttribute(AttributeID type, T &value) const
Retrieve attribute moment by enum.
std::string label(const std::string &format, int i)
Definition label.h:19
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Jet_v1 Jet
Definition of the current "jet version".
MuonSegment_v1 MuonSegment
Reference the current persistent version:
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition JetTypes.h:17
MsgStream & msg
Definition testRead.cxx:32