00001
00002
00003 #ifndef JetDumper_H
00004 #define JetDumper_H
00005
00006
00007
00008
00009
00010
00011 #include "AsgTools/AsgTool.h"
00012 #include <string>
00013 #include <vector>
00014 #include <sstream>
00015 #include <iomanip>
00016 #ifdef USE_BOOST_FOREACH
00017 #include <boost/foreach.hpp>
00018 #endif
00019 #include "fastjet/PseudoJet.hh"
00020 #include "AthLinks/ElementLinkVector.h"
00021 #include "JetInterface/IJetExecuteTool.h"
00022 #include "xAODCaloEvent/CaloCluster.h"
00023 #include "xAODJet/Jet.h"
00024 #include "xAODMuon/MuonSegment.h"
00025 #include "xAODMuon/MuonSegmentContainer.h"
00026 #include "JetEDM/PseudoJetVector.h"
00027 #include "JetEDM/IConstituentUserInfo.h"
00028
00029 namespace xAOD {
00030 class IParticle;
00031 }
00032
00051
00052 class JetDumper
00053 : virtual public IJetExecuteTool,
00054 virtual public asg::AsgTool {
00055 ASG_TOOL_CLASS(JetDumper, IJetExecuteTool)
00056
00057 public:
00058
00059 typedef std::string Name;
00060 typedef std::vector<Name> NameList;
00061 typedef xAOD::JetFourMom_t FourVector;
00062 typedef std::vector<const xAOD::IParticle*> APVector;
00063 typedef ElementLink<xAOD::IParticleContainer> APEL;
00064 typedef std::vector<APEL> APELVector;
00065 typedef std::vector<const xAOD::MuonSegment*> MSVector;
00066 typedef ElementLink<xAOD::MuonSegmentContainer> MSEL;
00067 typedef std::vector<MSEL> MSELVector;
00068
00069 public:
00070
00071
00072 JetDumper(std::string myname);
00073
00074
00075
00076 template<typename T>
00077 int dump_object(const T* pjet) const;
00078
00079
00080
00081 template<typename TList>
00082 int dump_collection(const TList* pjets) const;
00083
00084
00085 int execute() const;
00086
00087
00088 void print() const;
00089
00090 private:
00091
00092
00093 template<typename T>
00094 std::string object_label(const T* pjet, std::string label) const;
00095 std::string object_label(const fastjet::PseudoJet& jet, std::string label) const;
00096
00097
00098 template<typename T>
00099 int dump_object_after_prefix(const T* pjet) const;
00100 int dump_object_after_prefix(const fastjet::PseudoJet& jet) const;
00101 int dump_object_after_prefix(const xAOD::MuonSegment* seg) const;
00102
00103
00104 template<typename TObj, typename TMom>
00105 void get_moment(TObj* pobj, std::string name, TMom& val) const;
00106 template<typename TObj>
00107 void get_moment(TObj* pobj, std::string name, FourVector& val) const;
00108 template<typename TObj>
00109 void get_moment(TObj* pobj, std::string name, std::string& val) const;
00110 template<typename TObj>
00111 void get_moment(TObj* pobj, std::string name, std::vector<int>&) const;
00112 template<typename TObj>
00113 void get_moment(TObj* pobj, std::string name, std::vector<float>&) const;
00114 template<typename TMom>
00115 void get_moment(const xAOD::Jet* pobj, std::string name, TMom& val) const;
00116 void get_moment(const xAOD::Jet* pobj, std::string name, FourVector& val) const;
00117 void get_moment(const xAOD::Jet* pobj, std::string name, std::string& val) const;
00118 void get_moment(const xAOD::Jet* pobj, std::string name, std::vector<int>& vals) const;
00119 void get_moment(const xAOD::Jet* pobj, std::string name, std::vector<float>& vals) const;
00120 template<typename TObj>
00121 std::string get_moment_as_string(const TObj& obj, std::string name) const;
00122 std::string get_moment_as_string(const xAOD::Jet* pobj, std::string name) const;
00123
00124 template<typename TObj>
00125 void getAssociatedParticles(TObj* pobj, std::string name, APVector& val) const;
00126 void getAssociatedParticles(const xAOD::Jet* pobj, std::string name, APVector& val) const;
00127 template<typename TObj>
00128 void getAssociatedLinks(TObj* pobj, std::string name, APELVector& val) const;
00129 void getAssociatedLinks(const xAOD::Jet* pobj, std::string name, APELVector& val) const;
00130 template<typename TObj>
00131 void getAssociatedParticles(TObj* pobj, std::string name, MSVector& val) const;
00132 void getAssociatedParticles(const xAOD::Jet* pobj, std::string name, MSVector& val) const;
00133 template<typename TObj>
00134 void getAssociatedLinks(TObj* pobj, std::string name, MSELVector& val) const;
00135 void getAssociatedLinks(const xAOD::Jet* pobj, std::string name, MSELVector& val) const;
00136
00137
00138
00139 template<typename T>
00140 void extra_info(const T*, std::ostream& out, int) const;
00141 void extra_info(const xAOD::Jet* pjet, std::ostream& out, int iopt) const;
00142 void extra_info(const fastjet::PseudoJet* ppsj, std::ostream& out, int linedetail) const;
00143
00144 private:
00145
00146
00147 std::string m_cname;
00148 int m_detail;
00149 int m_linedetail;
00150 int m_prefix;
00151 int m_maxobj;
00152 float m_floatmax;
00153 NameList m_fmoms;
00154 NameList m_imoms;
00155 NameList m_bmoms;
00156 NameList m_cbmoms;
00157 NameList m_smoms;
00158 NameList m_fvmoms;
00159 NameList m_elmoms;
00160 NameList m_vimoms;
00161 NameList m_vfmoms;
00162 NameList m_apmoms;
00163
00164
00165 mutable Name m_objtypename;
00166 };
00167
00168
00169 template<typename T, typename TMom>
00170 JetDumper::NameList get_moment_keys(const T* pobj);
00171 template<>
00172 JetDumper::NameList get_moment_keys<xAOD::Jet, float>(const xAOD::Jet* pjet);
00173 template<>
00174 JetDumper::NameList get_moment_keys<xAOD::Jet, int>(const xAOD::Jet* pjet);
00175
00176
00177
00178 template<typename T>
00179 int JetDumper::dump_object(const T* pobj) const {
00180 msg() << MSG::INFO << m_objtypename << " ";
00181 return dump_object_after_prefix(pobj);
00182 }
00183
00184 template<typename T>
00185 int JetDumper::dump_object_after_prefix(const T* pjet) const {
00186 const double mevtogev = 0.001;
00187 const int wname = 30;
00188 if ( pjet == 0 ) {
00189 msg() << endmsg;
00190 ATH_MSG_ERROR("Null jet.");
00191 return 1;
00192 }
00193
00194 std::ostringstream ssjetline;
00195 if ( m_linedetail > 0 ) {
00196 ssjetline << "pT =" << std::setw(6) << std::fixed << std::setprecision(1) << mevtogev*pjet->pt() << " GeV";
00197 ssjetline << ", m =" << std::setw(6) << std::fixed << std::setprecision(1) << mevtogev*pjet->m() << " GeV";
00198 ssjetline << ", eta =" << std::setw(5) << std::fixed << std::setprecision(2) << pjet->eta();
00199 ssjetline << ", phi =" << std::setw(5) << std::fixed << std::setprecision(2) << pjet->phi();
00200 }
00201 if ( m_linedetail > 1 ) extra_info(pjet, ssjetline, m_linedetail);
00202 msg() << ssjetline.str() << endmsg;
00203
00204 if ( m_detail > 1 ) {
00205 NameList fnames = m_fmoms;
00206 NameList inames = m_imoms;
00207 NameList bnames = m_bmoms;
00208 NameList cbnames = m_cbmoms;
00209 NameList snames = m_smoms;
00210 NameList fvnames = m_fvmoms;
00211 NameList elnames = m_elmoms;
00212 NameList vinames = m_vimoms;
00213 NameList vfnames = m_vfmoms;
00214 NameList apnames = m_apmoms;
00215 if ( fnames.size() == 0 ) fnames = get_moment_keys<T,float>(pjet);
00216 ATH_MSG_INFO(" " << m_objtypename << " has " << fnames.size() << " float attributes:");
00217 for ( NameList::const_iterator inam=fnames.begin(); inam!=fnames.end(); ++inam ) {
00218 Name name = *inam;
00219 float val;
00220 get_moment(pjet, name, val);
00221 if ( fabs(val) > m_floatmax ) {
00222 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(16) << std::scientific << std::setprecision(3) << val);
00223 } else {
00224 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(12) << std::fixed << std::setprecision(3) << val);
00225 }
00226 }
00227 if ( inames.size() == 0 ) inames = get_moment_keys<T,int>(pjet);
00228 ATH_MSG_INFO(" " << m_objtypename << " has " << inames.size() << " int attributes:");
00229 for ( NameList::const_iterator inam=inames.begin(); inam!=inames.end(); ++inam ) {
00230 Name name = *inam;
00231 int ival;
00232 get_moment(pjet, name, ival);
00233 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(12) << std::fixed << std::setprecision(3) << ival);
00234 }
00235 if ( bnames.size() == 0 ) bnames = get_moment_keys<T,int>(pjet);
00236 ATH_MSG_INFO(" " << m_objtypename << " has " << bnames.size() << " bool attributes:");
00237 for ( NameList::const_iterator inam=bnames.begin(); inam!=bnames.end(); ++inam ) {
00238 Name name = *inam;
00239 bool val;
00240 get_moment(pjet, name, val);
00241 std::string sval = val ? "true" : "false";
00242 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(6) << sval);
00243 }
00244 if ( cbnames.size() == 0 ) cbnames = get_moment_keys<T,int>(pjet);
00245 ATH_MSG_INFO(" " << m_objtypename << " has " << cbnames.size() << " cbool attributes:");
00246 for ( NameList::const_iterator inam=cbnames.begin(); inam!=cbnames.end(); ++inam ) {
00247 Name name = *inam;
00248 char val;
00249 get_moment(pjet, name, val);
00250 std::string sval = val ? "true" : "false";
00251 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(6) << sval);
00252 }
00253 ATH_MSG_INFO(" " << m_objtypename << " has " << snames.size() << " string attributes:");
00254 for ( NameList::const_iterator inam=snames.begin(); inam!=snames.end(); ++inam ) {
00255 Name name = *inam;
00256 std::string sval;
00257 get_moment(pjet, name, sval);
00258 ATH_MSG_INFO(std::setw(wname) << name << ":" << std::setw(30) << sval);
00259 }
00260 ATH_MSG_INFO(" " << m_objtypename << " has " << vinames.size() << " vector<int> attributes:");
00261 for ( NameList::const_iterator inam=vinames.begin(); inam!=vinames.end(); ++inam ) {
00262 Name name = *inam;
00263 std::vector<int> vals;
00264 get_moment(pjet, name, vals);
00265 if ( vals.size() == 0 ) {
00266 ATH_MSG_INFO(std::setw(wname) << name << ": No entries.");
00267 } else {
00268 int widx = 1;
00269 if ( vals.size() > 10 ) widx = 2;
00270 for ( unsigned int ival=0; ival<vals.size(); ++ival ) {
00271 ATH_MSG_INFO(std::setw(wname) << name << "[" << std::setw(widx) << ival << "]: "
00272 << std::setw(8) << vals[ival]);
00273 }
00274 }
00275 }
00276 ATH_MSG_INFO(" " << m_objtypename << " has " << vfnames.size() << " vector<float> attributes:");
00277 for ( NameList::const_iterator inam=vfnames.begin(); inam!=vfnames.end(); ++inam ) {
00278 Name name = *inam;
00279 std::vector<float> vals;
00280 get_moment(pjet, name, vals);
00281 if ( vals.size() == 0 ) {
00282 ATH_MSG_INFO(std::setw(wname) << name << ": No entries.");
00283 } else {
00284 int widx = 1;
00285 if ( vals.size() > 10 ) widx = 2;
00286 for ( unsigned int ival=0; ival<vals.size(); ++ival ) {
00287 ATH_MSG_INFO(std::setw(wname) << name << "[" << std::setw(widx) << ival << "]: "
00288 << std::setw(12) << std::fixed << std::setprecision(3) << vals[ival]);
00289 }
00290 }
00291 }
00292 ATH_MSG_INFO(" " << m_objtypename << " has " << apnames.size() << " associated particle vectors:");
00293 for ( NameList::const_iterator inam=apnames.begin(); inam!=apnames.end(); ++inam ) {
00294 Name name = *inam;
00295 if ( name.find("MuonSegment") != std::string::npos ) {
00296 MSVector vals;
00297 getAssociatedParticles(pjet, name, vals);
00298 MSELVector els;
00299 getAssociatedLinks(pjet, name, els);
00300 std::string slab = "entries";
00301 if ( vals.size() == 1 ) slab = "entry";
00302 if ( vals.size() > 0 ) slab += ":";
00303 ATH_MSG_INFO(std::setw(wname) << name << " has " << vals.size() << " " << slab);
00304 if ( els.size() == vals.size() ) {
00305 #ifdef USE_BOOST_FOREACH
00306 BOOST_FOREACH(const MSEL& el, els) {
00307 ElementLink<xAOD::MuonSegmentContainer> newel(el);
00308 #else
00309 for (const MSEL& el : els) {
00310 #endif
00311 ATH_MSG_INFO(std::setw(wname+2) << "" << el.dataID() << "[" << el.index() << "]");
00312 }
00313 } else {
00314 #ifdef USE_BOOST_FOREACH
00315 BOOST_FOREACH(const xAOD::MuonSegment* ppar, vals) {
00316 #else
00317 for (auto ppar : vals) {
00318 #endif
00319 ATH_MSG_INFO(std::setw(wname+2) << "" << ppar->container() << "[" << ppar->index() << "]");
00320 }
00321 }
00322 } else {
00323 APVector vals;
00324 getAssociatedParticles(pjet, name, vals);
00325 APELVector els;
00326 getAssociatedLinks(pjet, name, els);
00327 std::string slab = "entries";
00328 if ( vals.size() == 1 ) slab = "entry";
00329 if ( vals.size() > 0 ) slab += ":";
00330 ATH_MSG_INFO(std::setw(wname) << name << " has " << vals.size() << " " << slab);
00331
00332 if ( els.size() == vals.size() ) {
00333 #ifdef USE_BOOST_FOREACH
00334 BOOST_FOREACH(const APEL& el, els) {
00335 #else
00336 for (const APEL& el : els) {
00337 #endif
00338 ATH_MSG_INFO(std::setw(wname+2) << "" << el.dataID() << "[" << el.index() << "]");
00339 }
00340 } else {
00341 #ifdef USE_BOOST_FOREACH
00342 BOOST_FOREACH(const xAOD::IParticle* ppar, vals) {
00343 #else
00344 for (auto ppar : vals) {
00345 #endif
00346 ATH_MSG_INFO(std::setw(wname+2) << "" << ppar->container() << "[" << ppar->index() << "]");
00347 }
00348 }
00349 }
00350 }
00351 if ( fvnames.size() == 0 ) fvnames = get_moment_keys<T,float>(pjet);
00352 ATH_MSG_INFO(" " << m_objtypename << " has " << fvnames.size() << " four-vector attributes (pT m eta phi):");
00353 for ( NameList::const_iterator inam=fvnames.begin(); inam!=fvnames.end(); ++inam ) {
00354 Name name = *inam;
00355 FourVector val;
00356 get_moment(pjet, name, val);
00357 ATH_MSG_INFO(std::setw(wname) << name << ":"
00358 << std::setw(12) << std::fixed << std::setprecision(3) << val.Pt()
00359 << std::setw(12) << std::fixed << std::setprecision(3) << val.M()
00360 << std::setw(12) << std::fixed << std::setprecision(3) << val.Eta()
00361 << std::setw(12) << std::fixed << std::setprecision(3) << val.Phi()
00362 );
00363 }
00364 ATH_MSG_INFO(" " << m_objtypename << " has " << elnames.size() << " element-link attributes:");
00365 for ( NameList::const_iterator inam=elnames.begin(); inam!=elnames.end(); ++inam ) {
00366 std::string name = *inam;
00367 std::string sel = get_moment_as_string(pjet, name);
00368 ATH_MSG_INFO(std::setw(wname) << name << ": " << sel);
00369 }
00370 }
00371 return 0;
00372 }
00373
00374 template<typename T>
00375 std::string JetDumper::object_label(const T*, std::string label) const {
00376 return label;
00377 }
00378
00379
00380
00381 template<typename T>
00382 T& const_cast_ptr(T& obj) { return obj; }
00383
00384 template<typename T>
00385 T* const_cast_ptr(T* pobj) { return const_cast<T*>(pobj); }
00386
00387 template<typename TList>
00388 int JetDumper::dump_collection(const TList* pjets) const {
00389 int njet = pjets->size();
00390 ATH_MSG_INFO(" " << m_objtypename << " multiplicity: " << njet << " [" << m_cname << "]");
00391 std::string line = "-------------------------------------------------------------------------";
00392 int jstat = 0;
00393 if ( m_detail > 0 ) {
00394 unsigned int njetshow = njet;
00395 if ( m_maxobj > 0 && m_maxobj < njet ) njetshow = m_maxobj;
00396 typename TList::const_iterator itjet = pjets->begin();
00397 typedef typename TList::const_iterator::reference TValue;
00398 std::vector<std::string> labs;
00399 unsigned int wlab = 0;
00400 for ( unsigned int ijet=0; ijet<njetshow; ++ijet ) {
00401 TValue pjet = *itjet++;
00402 std::string lab = object_label(pjet, m_objtypename);
00403 labs.push_back(lab);
00404 if ( lab.size() > wlab ) wlab = lab.size();
00405 }
00406 itjet = pjets->begin();
00407 int widx = 1;
00408 if ( njetshow > 10 ) widx = 2;
00409 if ( njetshow > 100 ) widx = 3;
00410 if ( njetshow > 1000 ) widx = 4;
00411 for ( unsigned int ijet=0; ijet<njetshow; ++ijet ) {
00412 TValue pjet = *itjet++;
00413 if ( m_detail > 1 && ijet==0 ) ATH_MSG_INFO(line);
00414
00415 msg() << MSG::INFO << " ";
00416 if ( m_prefix == 1 || m_prefix > 2 ) {
00417 msg() << std::setw(wlab) << labs[ijet] << " ";
00418 }
00419 if ( m_prefix < 2 ) {
00420 msg() << std::setw(widx) << ijet;
00421 } else {
00422 msg() << m_cname;
00423 msg() << "[" << std::setw(widx) << ijet << "]";
00424 }
00425 msg() << ": ";
00426 jstat += dump_object_after_prefix(pjet);
00427 if ( m_detail > 1 ) ATH_MSG_INFO(line);
00428 }
00429 }
00430 return jstat;
00431 }
00432
00433
00434
00435 template<typename TObj, typename TMom>
00436 void JetDumper::get_moment(TObj*, std::string, TMom& val) const {
00437 val = 0;
00438 }
00439
00440 template<typename TObj>
00441 void JetDumper::get_moment(TObj*, std::string, FourVector& val) const {
00442 val = FourVector();
00443 }
00444
00445 template<typename TObj>
00446 void JetDumper::get_moment(TObj*, std::string, std::string& val) const {
00447 val = "";
00448 }
00449
00450 template<typename TObj>
00451 void JetDumper::get_moment(TObj*, std::string, std::vector<int>& val) const {
00452 static std::vector<int> empty;
00453 val = empty;
00454 }
00455
00456 template<typename TObj>
00457 void JetDumper::get_moment(TObj*, std::string, std::vector<float>& val) const {
00458 static std::vector<float> empty;
00459 val = empty;
00460 }
00461
00462 template<typename TMom>
00463 void JetDumper::get_moment(const xAOD::Jet* pjet, std::string name, TMom& val) const {
00464 val = pjet->getAttribute<TMom>(name);
00465 }
00466
00467 template<typename TObj>
00468 void JetDumper::getAssociatedParticles(TObj*, std::string, APVector&) const { }
00469
00470 template<typename TObj>
00471 void JetDumper::getAssociatedLinks(TObj*, std::string, APELVector&) const { }
00472
00473 template<typename TObj>
00474 void JetDumper::getAssociatedParticles(TObj*, std::string, MSVector&) const { }
00475
00476 template<typename TObj>
00477 void JetDumper::getAssociatedLinks(TObj*, std::string, MSELVector&) const { }
00478
00479 template<typename TObj>
00480 std::string
00481 JetDumper::get_moment_as_string(const TObj&, std::string) const {
00482 return " Not a jet.";
00483 }
00484
00485
00486
00487 template<typename T, typename TMom>
00488 JetDumper::NameList get_moment_keys(const T*) {
00489 return JetDumper::NameList();
00490 }
00491
00492
00493
00494 template<typename T>
00495 void JetDumper::extra_info(const T*, std::ostream& out, int) const {
00496 out << ", Unknown type: " << typeid(T).name();
00497 }
00498
00499
00500
00501 #endif