ATLAS Offline Software
JetConstituentFiller.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // JetConstituentFiller.cxx
6 
8 #include <map>
11 #include "JetEDM/LabelIndex.h"
12 #include "xAODJet/JetContainer.h"
13 #include "fastjet/PseudoJet.hh"
15 #include <iostream>
16 
17 using PseudoJetVector = std::vector<fastjet::PseudoJet>;
18 using xAOD::IParticle;
21 using ParticleVector = std::vector<const IParticle *>;
22 using ParticleMap = std::vector<ParticleVector>;
23 using PtMap = std::vector<double>;
25 using xAOD::MuonSegment;
27 using MuonSegmentVector = std::vector<const MuonSegment *>;
29 using MuonSegmentMap = std::vector<MuonSegmentVector>;
30 
31 //**********************************************************************
32 
33 namespace {
34  enum ParType { UNDEF, IPART, MUSEG };
35 }
36 using ParTypeVector = std::vector<ParType>;
37 
38 //**********************************************************************
39 
40 int JetConstituentFiller::
41 extractConstituents(xAOD::Jet& jet, const NameList* pghostlabs,
42  const fastjet::PseudoJet* ppj2) {
43 
44  static const SG::AuxElement::Accessor<const fastjet::PseudoJet*> pjAccessor("PseudoJet");
45  const fastjet::PseudoJet* ppseudojet = nullptr;
46  if(pjAccessor.isAvailable(jet)) ppseudojet = pjAccessor(jet);
47 
48  if ( ppseudojet == nullptr ) {
49  ppseudojet = ppj2;
50  }
51  if ( ppseudojet == nullptr ) return -1;
52  int nbad = 0;
53  const PseudoJetVector& cons = ppseudojet->constituents();
55  MuonSegmentMap outms;
56  PtMap ptout;
57  ParTypeVector partypes;
58  const LabelIndex* pli = nullptr;
59  for ( PseudoJetVector::const_iterator icon=cons.begin(); icon!=cons.end(); ++icon ) {
60  if ( icon->has_user_info<IConstituentUserInfo>() ) {
61  const IConstituentUserInfo& cui = icon->user_info<IConstituentUserInfo>();
62  // Initialize with the first constituent.
63  if ( pli == nullptr ) {
64  pli = cui.labelMap();
65  if ( pli == nullptr ) return -2;
66  unsigned int maxli = pli->size() + 1; // +1 because LabelIndex starts at 1;
67  out.resize(maxli); // +1 because LabelIndex starts at 1
68  outms.resize(maxli);
69  for ( unsigned int ili=0; ili<maxli; ++ili ) {
70  ptout.push_back(0.0);
71  }
72  for ( ParType& partype : partypes ) partype = UNDEF;
73  for ( unsigned int idx=0; idx<maxli; ++idx ) {
74  std::string lab = pli->label(idx);
75  if ( lab.empty() ) {
76  partypes.push_back(UNDEF);
77  if ( idx ) return -3;
78  } else if ( lab.find("MuonSegment") != std::string::npos ) {
79  partypes.push_back(MUSEG);
80  } else {
81  partypes.push_back(IPART);
82  }
83  }
84  }
85  if ( pli != cui.labelMap() ) return -4;
86  const IParticle* ppar = cui.particle();
87  const MuonSegment* pms = nullptr;
88  // If this is not an IParticle, find the full type.
89  unsigned int icui = cui.index();
90  ParType partype = partypes[icui];
91  if ( ppar == nullptr ) {
92  const MuonSegmentCUI* pmscui = dynamic_cast<const MuonSegmentCUI*>(&cui);
93  if ( pmscui == nullptr ) return -5;
94  pms = pmscui->object();
95  if ( partype != MUSEG ) return -6;
96  } else {
97  if ( partype != IPART ) return -7;
98  }
99  Label lab = cui.label();
100  // Add to constituent list or associated object list.
101  if ( ! cui.isGhost() ) {
102  jet.addConstituent(ppar);
103  jet.setConstituentsSignalState( cui.constitScale() );
104  } else {
105  if ( partype == MUSEG ) {
106  outms[icui].push_back(pms);
107  } else if ( partype == IPART ) {
108  out[icui].push_back(ppar);
109  }
110  }
111  } else {
112  ++nbad;
113  }
114  }
115 
116  // Set ghost associated particles:
117  if (pli){
118  for ( size_t i=1; i<out.size(); ++i ) {
119  if ( pghostlabs) {
120  const NameList& ghostlabs = *pghostlabs;
121  if ( find(ghostlabs.begin(), ghostlabs.end(), pli->label(i)) == ghostlabs.end() ) {
122  nbad += out[i].size();
123  continue;
124  }
125  }
126  ParType& partype = partypes[i];
127  std::string cname = pli->label(i) + "Count";
128  std::string ptname = pli->label(i) + "Pt";
129  // Check if this is in the parent jet
130  int count_test; // dummy var to retrieve into -- we don't care about the value
131  const static SG::AuxElement::ConstAccessor<ElementLink<xAOD::JetContainer> > cacc_parent("Parent");
132  if(!m_isTrigger) {
133  if(cacc_parent.isAvailable(jet) && cacc_parent(jet).isValid()) {
134  if(!(*cacc_parent(jet))->getAttribute(cname,count_test)) {
135  nbad += out[i].size(); // Skip if the parent does not have this
136  continue;
137  }
138  }
139  }
140  if ( partype == MUSEG ) {
141  // Record associated muons.
142  jet.setAssociatedObjects(pli->label(i) , outms[i]);
143  jet.setAttribute<int>(cname, outms[i].size());
144  } else if ( partype == IPART ) {
145  // Record associated particles.
146  jet.setAssociatedObjects(pli->label(i), out[i]);
147  jet.setAttribute<int>(cname, out[i].size());
148  jet.setAttribute<float>(ptname, ptout[i]);
149  if ( ! outms[i].empty() ) return -9;
150  } else {
151  return -10;
152  }
153  }
154  }
155  return nbad;
156 }
157 
158 int JetConstituentFiller::extractConstituents(xAOD::Jet& jet, const fastjet::PseudoJet* ppj2) {
159  return extractConstituents(jet, nullptr, ppj2);
160 }
161 
162 //**********************************************************************
163 
164 PseudoJetVector JetConstituentFiller::constituentPseudoJets(const xAOD::Jet& jet, bool ignoreGhosts, bool requireJetStructure){
165 
166  static const SG::AuxElement::Accessor<const fastjet::PseudoJet*> pjAccessor("PseudoJet");
167  const fastjet::PseudoJet* jet_pj = nullptr;
168  if(pjAccessor.isAvailable(jet)) jet_pj = pjAccessor(jet);
169 
170  PseudoJetVector constituents;
171 
172  if(jet_pj && !ignoreGhosts ){ // Retrieve constituents from the PseudoJet
173  constituents = jet_pj->constituents(); // all constituents, including ghosts
174  // Removed block for (existing jet_pt and ignoreGhosts), because
175  // in the PseudoJetContainer model, the constituent user info is
176  // not attached, and hence it's not possible to know if a pj is
177  // ghost or constituent.
178  } else { // no PseudoJet in jet or need to reject ghosts. Build them from constituents.
179 
180  // For SoftDrop variables like zg, rg, the structure of the jet is needed and
181  // constituents have to be retrieved from the Pseudojet
182  if(jet_pj && requireJetStructure){
183  PseudoJetVector constituents_all = jet_pj->constituents(); // all constituents, including ghosts
184  //Filter out ghosts before calculating variables (comparison to constituents)
185  xAOD::JetConstituentVector constituents_tmp = jet.getConstituents();
186  constituents.reserve( jet.numConstituents() );
187  for(size_t i = 0; i < constituents_all.size(); i++){
188  for(size_t j = 0; j < constituents_tmp.size(); j++){
189  if(std::abs((constituents_all[i].px()-constituents_tmp[j].Px())/constituents_tmp[j].Px()) < 0.0001 &&
190  std::abs((constituents_all[i].py()-constituents_tmp[j].Py())/constituents_tmp[j].Py()) < 0.0001 &&
191  std::abs((constituents_all[i].pz()-constituents_tmp[j].Pz())/constituents_tmp[j].Pz()) < 0.0001){
192  constituents.push_back(constituents_all[i]);
193  break;
194  }
195  }
196  }
197  }
198  else{
199  xAOD::JetConstituentVector constituents_tmp = jet.getConstituents();
200  constituents.reserve( jet.numConstituents() );
201  for(size_t i = 0; i < constituents_tmp.size(); i++){
202  constituents.emplace_back( constituents_tmp[i].Px(), constituents_tmp[i].Py(), constituents_tmp[i].Pz(), constituents_tmp[i].E() );
203  }
204  }
205  }
206  return constituents;
207 }
208 //**********************************************************************
209 
Label
IndexedConstituentUserInfo::Label Label
Definition: IndexedConstituentUserInfo.cxx:11
jet::LabelIndex
Definition: LabelIndex.h:22
IndexedTConstituentUserInfo.h
test_pyathena.px
px
Definition: test_pyathena.py:18
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:66
MuonSegmentMap
std::vector< MuonSegmentVector > MuonSegmentMap
Definition: JetConstituentFiller.cxx:29
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
xAOD::JetConstituentVector::size
size_t size() const
number of constituents
Definition: JetConstituentVector.cxx:102
ParticleMap
std::vector< ParticleVector > ParticleMap
Definition: JetConstituentFiller.cxx:22
TrigConf::JetWindowSize::UNDEF
@ UNDEF
Definition: TriggerThresholdValue.h:17
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
jet::IConstituentUserInfo::particle
virtual const xAOD::IParticle * particle() const =0
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:214
jet::JetConstituentFiller::extractConstituents
int extractConstituents(xAOD::Jet &jet, const NameList *pghostlabs, const fastjet::PseudoJet *ppj=0)
Build and fill constituents of jet from its PseudoJet (or from ppj) Returns the number of pseudojet c...
Definition: JetConstituentFiller.cxx:41
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
xAOD::MuonSegment
MuonSegment_v1 MuonSegment
Reference the current persistent version:
Definition: Event/xAOD/xAODMuon/xAODMuon/MuonSegment.h:13
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
jet::LabelIndex::label
Label label(Index idx) const
Fetch the label for an index.
Definition: LabelIndex.cxx:41
MuonSegmentContainer.h
jet::IndexedTConstituentUserInfo
Definition: IndexedTConstituentUserInfo.h:31
jet::PseudoJetVector
std::vector< fastjet::PseudoJet > PseudoJetVector
Definition: PseudoJetVector.h:21
JetConstituentFiller.h
ParticleVector
std::vector< const IParticle * > ParticleVector
Definition: JetConstituentFiller.cxx:21
jet::JetConstituentFiller::m_isTrigger
bool m_isTrigger
Definition: JetConstituentFiller.h:57
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
lumiFormat.i
int i
Definition: lumiFormat.py:92
jet::IConstituentUserInfo::isGhost
virtual bool isGhost() const
Returns true if this constituent is a ghost.
Definition: IConstituentUserInfo.cxx:26
jet::IConstituentUserInfo::labelMap
virtual const LabelIndex * labelMap() const
Definition: IConstituentUserInfo.cxx:20
jet::IndexedTConstituentUserInfo::object
const T * object() const
Amg::pz
@ pz
Definition: GeoPrimitives.h:40
jet::IConstituentUserInfo::index
virtual Index index() const
Definition: IConstituentUserInfo.cxx:14
jet::IConstituentUserInfo::label
virtual Label label() const =0
jet::IConstituentUserInfo::constitScale
virtual xAOD::JetConstitScale constitScale() const
Return the constituent scale used to build the PseudoJet.
Definition: IConstituentUserInfo.cxx:32
IConstituentUserInfo.h
Amg::py
@ py
Definition: GeoPrimitives.h:39
Py
Definition: PyDataStore.h:24
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21
jet::JetConstituentFiller::constituentPseudoJets
static PseudoJetVector constituentPseudoJets(const xAOD::Jet &jet, bool ignoreGhosts=true, bool requireJetStructure=false)
Returns the jet's constituents as a vector of PseudoJet if ignoreGhosts==true, ghost constituents are...
Definition: JetConstituentFiller.cxx:164
LabelIndex.h
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
PseudoJetVector
std::vector< fastjet::PseudoJet > PseudoJetVector
Definition: JetConstituentFiller.cxx:17
MuonSegmentVector
std::vector< const MuonSegment * > MuonSegmentVector
Definition: JetConstituentFiller.cxx:27
jet::JetConstituentFiller
Definition: JetConstituentFiller.h:27
jet::JetConstituentFiller::NameList
std::vector< std::string > NameList
Definition: JetConstituentFiller.h:33
JetContainer.h
jet::IConstituentUserInfo
Definition: IConstituentUserInfo.h:26
jet::LabelIndex::size
Index size() const
Number of label stored in this map. WARNING the index starts at 1, so range is [1....
Definition: LabelIndex.cxx:76
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
Label
IConstituentUserInfo::Label Label
Definition: JetConstituentFiller.cxx:20
ParTypeVector
std::vector< ParType > ParTypeVector
Definition: JetConstituentFiller.cxx:36
SG::ConstAccessor< T, AuxAllocator_t< T > >::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
xAOD::JetConstituentVector
A vector of jet constituents at the scale used during jet finding.
Definition: JetConstituentVector.h:117
PtMap
std::vector< double > PtMap
Definition: JetConstituentFiller.cxx:23
IParticle
Definition: Event/EventKernel/EventKernel/IParticle.h:43