ATLAS Offline Software
JetCaloCalculations.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 
9 
10 #include "xAODPFlow/PFO.h"
11 #include "xAODPFlow/FlowElement.h"
12 
14 #include "PFlowUtils/FEHelpers.h"
15 #include "xAODJet/JetAttributes.h"
18 
19 namespace CaloConstitHelpers {
20 
22 
23 
29  virtual ~CaloClusterExtractor()= default;
30  virtual bool valid(JetConstitIterator & it ) const override {
31  return (dynamic_cast<const xAOD::CaloCluster*>(it->rawConstituent())!=nullptr);
32  }
33 
34  virtual double moment(JetConstitIterator & it, xAOD::CaloCluster::MomentType momentType) const override {
35  double m = 0;
36  static_cast<const xAOD::CaloCluster*>(it->rawConstituent())->retrieveMoment(momentType,m) ;
37  return m;
38  }
39 
40  virtual double time(JetConstitIterator & it) const override {
41  return static_cast<const xAOD::CaloCluster*>(it->rawConstituent())->time();
42  }
43 
44  virtual double energyHEC(JetConstitIterator & it) const override {
45  const xAOD::CaloCluster* cl = static_cast<const xAOD::CaloCluster*>(it->rawConstituent());
46  return cl->eSample( CaloSampling::HEC0) + cl->eSample( CaloSampling::HEC1) +
47  cl->eSample( CaloSampling::HEC2) + cl->eSample( CaloSampling::HEC3);
48  }
49 
50  };
51 
57  virtual ~PFOExtractor()= default;
58  virtual bool valid(JetConstitIterator & it ) const override {
59  const xAOD::PFO* pfo = dynamic_cast<const xAOD::PFO*>(it->rawConstituent());
60  if (pfo!=nullptr) return (!pfo->isCharged());
61  return false;
62  }
63 
64  virtual double moment(JetConstitIterator & it , xAOD::CaloCluster::MomentType momentType) const override {
65  float m=0.;
66  const xAOD::PFO* pfo = static_cast<const xAOD::PFO*>(it->rawConstituent()) ;
67  pfo->getClusterMoment(m, momentType );
68  return m;
69  }
70 
71  virtual double time(JetConstitIterator & it) const override {
72  float t=0.;
73  const xAOD::PFO* pfo = static_cast<const xAOD::PFO*>(it->rawConstituent()) ;
75  return t;
76  }
77 
78  virtual double energyHEC(JetConstitIterator & it ) const override {
79  float m=0.;
80  const xAOD::PFO* pfo = static_cast<const xAOD::PFO*>(it->rawConstituent()) ;
82  return m;
83  }
84 
85  };
86 
92  virtual ~FlowElementExtractor()= default;
93  virtual bool valid(JetConstitIterator & it ) const override {
94  const xAOD::FlowElement* fe = dynamic_cast<const xAOD::FlowElement*>(it->rawConstituent());
95  if (fe != nullptr) return (!fe->isCharged());
96  return false;
97  }
98 
99  virtual double moment(JetConstitIterator & it , xAOD::CaloCluster::MomentType momentType) const override {
100  float m=0.;
101  const xAOD::FlowElement* fe = static_cast<const xAOD::FlowElement*>(it->rawConstituent());
102  FEHelpers::getClusterMoment(*fe, momentType, m);
103  return m;
104  }
105 
106  virtual double time(JetConstitIterator & it) const override {
107  const xAOD::FlowElement* fe = static_cast<const xAOD::FlowElement*>(it->rawConstituent());
108  const static SG::AuxElement::ConstAccessor<float> accTiming("TIMING");
109 
110  float timing = -1;
111 
112  if(accTiming.isAvailable(*fe)){
113  timing = accTiming(*fe);
114  }
115  else{
116  if(fe->otherObjects().size() == 1 && fe->otherObject(0)){
117  const auto* neutralObject = (fe->otherObject(0));
118  const xAOD::CaloCluster* cluster = nullptr;
119 
120  if(neutralObject->type() == xAOD::Type::CaloCluster){
121  cluster = dynamic_cast<const xAOD::CaloCluster*> (neutralObject);
122  }
123  //If we have a PFO (in case of fe being a UFO), we need to get the associated cluster first
124  else {
125  const xAOD::FlowElement* pfo = dynamic_cast<const xAOD::FlowElement*>(neutralObject);
126  if(!pfo->otherObjects().empty() && pfo->otherObject(0) && pfo->otherObject(0)->type() == xAOD::Type::CaloCluster){
127  cluster = dynamic_cast<const xAOD::CaloCluster*> (pfo->otherObject(0));
128  }
129  }
130 
131  if(cluster){
132  timing = cluster->time();
133  }
134  }
135  }
136  return timing;
137  }
138 
139  virtual double energyHEC(JetConstitIterator & it ) const override {
140  const xAOD::FlowElement* fe = static_cast<const xAOD::FlowElement*>(it->rawConstituent());
141 
142  // Add up the four individual HEC layers
143  const static SG::AuxElement::ConstAccessor<float> accHEC0("LAYERENERGY_HEC0");
144  const static SG::AuxElement::ConstAccessor<float> accHEC1("LAYERENERGY_HEC1");
145  const static SG::AuxElement::ConstAccessor<float> accHEC2("LAYERENERGY_HEC2");
146  const static SG::AuxElement::ConstAccessor<float> accHEC3("LAYERENERGY_HEC3");
147 
148  float sum_HEC = 0.0;
149 
150  // The variables are available for PFOs but not UFOs
151  if(accHEC0.isAvailable(*fe) && accHEC1.isAvailable(*fe) && accHEC2.isAvailable(*fe) && accHEC3.isAvailable(*fe)){
152  sum_HEC = accHEC0(*fe) + accHEC1(*fe) + accHEC2(*fe) + accHEC3(*fe);
153  }
154  else{
155  for (size_t n = 0; n < fe->otherObjects().size(); ++n) {
156  if(! fe->otherObject(n)) continue;
157  const auto* neutralObject = (fe->otherObject(n));
158 
159  const xAOD::CaloCluster* cluster = nullptr;
160 
161  //If we have a cluster, we can directly access the calorimeter information
162  if(neutralObject->type() == xAOD::Type::CaloCluster){
163  cluster = dynamic_cast<const xAOD::CaloCluster*> (neutralObject);
164  }
165  //If we have a PFO (in case of fe being a UFO), we need to get the associated cluster first
166  else {
167  const xAOD::FlowElement* pfo = dynamic_cast<const xAOD::FlowElement*>(neutralObject);
168  if(!pfo->otherObjects().empty() && pfo->otherObject(0) && pfo->otherObject(0)->type() == xAOD::Type::CaloCluster){
169  cluster = dynamic_cast<const xAOD::CaloCluster*> (pfo->otherObject(0));
170  }
171  }
172  if(cluster){
173  sum_HEC += cluster->eSample( CaloSampling::HEC0 ) + cluster->eSample( CaloSampling::HEC1 ) + cluster->eSample( CaloSampling::HEC2 ) + cluster->eSample( CaloSampling::HEC3 );
174  }
175  }
176  }
177  return sum_HEC;
178  }
179 
180  };
181 
182 
186  static const CaloConstitExtractor nullEx;
187  static const CaloClusterExtractor clusteEx;
188  static const PFOExtractor pfoEx;
189  static const FlowElementExtractor feEx;
190 
191  if(jet->numConstituents() == 0 ) return &nullEx;
192 
193  // WARNING not entirely safe : assume all constits are of same type
194 
195  switch(jet->rawConstituent(0)->type() ) {
197  return &clusteEx;
199  return &pfoEx;
201  return &feEx;
202  default:
203  break;
204  }
205  return &nullEx;
206  }
207 
208 }
209 
210 
211 
212 // Implementation of JetCaloCalculator framwework
213 namespace jet {
214 
216  m_name(xAOD::JetAttributeAccessor::name(id)),
217  m_id(id)
218  {
219  // std::cout<< " JetCaloCalculatorBase" << " id "<< id <<std::endl;
220  }
221 
223 
224  if (! setupJet( jet ) ) return 0;
225  size_t nConstit = jet->numConstituents();
226  if( nConstit == 0) return true;
227 
228  // retrieve the cluster moment extractor for this jet.
230 
231 
232  xAOD::JetConstituentVector constits = jet->getConstituents();
233  // Use the constituent iterator
234  // IMPORTANT : use UncalibratedJetConstituent.
235  // By default the calculators will just use the kinematic from the iterator they are
236  // given. This allows to choose what scale we give them.
237  // Here we use UncalibratedJetConstituent as default, because most (or all) calo quantity to be calculated
238  // are based on EM scale cluster moments.
239  xAOD::JetConstituentVector::iterator it = constits.begin(constitscale );
240  xAOD::JetConstituentVector::iterator itE = constits.end(constitscale);
241 
242  for(; it != itE; ++it)
243  {
244  if( m_constitExtractor->valid(it) )
245  processConstituent(it); // (no support for weight now)
246  }
247 
248  return jetCalculation();
249 
250  }
251 
252 
253 
254  // ********************************************************
255  // JetCaloCalculations methods
256  // ********************************************************
258  m_calculators.push_back(c);
259  }
260 
261  // ********************************************************
263  for(size_t i=0;i < m_calculators.size();i++) m_calculators[i]->setupEvent();
264  return true;
265  }
266 
267 
268  // ********************************************************
269  std::vector<double> JetCaloCalculations::process(const xAOD::Jet* jet) const {
270  size_t nConstit = jet->numConstituents();
271  std::vector<double> results;
272  results.reserve(m_calculators.size());
273 
274  if( nConstit == 0) {
275  results.resize(m_calculators.size(),0);
276  return results;
277  }
278 
279  // Clone calculators : we are in a const method, we can't call the
280  // non const setupJet() and processConstituent() methods of our calculators.
281  std::vector<JetCaloCalculator*> clonedCalc;
282  clonedCalc.reserve(m_calculators.size());
283  for( const JetCaloCalculator *calc : m_calculators) clonedCalc.push_back( calc->clone() );
284 
285  // prepare each calculator
286  for(JetCaloCalculator* calc: clonedCalc) {
287  calc->setupJet(jet);
288  }
289 
290  // retrieve the cluster moment extractor for this jet.
292  // assign the extractor to the calculators :
293  for( JetCaloCalculator* calc : clonedCalc) calc->setExtractor(extractor);
294 
295  xAOD::JetConstituentVector constits = jet->getConstituents();
296  // Use the constituent iterator at UNCALIBRATED scale
299 
300  // loop over constituents
301  for(; it != itE; ++it)
302  {
303  //std::cout << " processing constit ---- "<< (*it)->pt() << " v= "<< extractor->valid(it) << std::endl;
304 
305  if( ! extractor->valid(it) ) continue; // this can happen for charged pflow objects
306  // loop over calculators for this constituent.
307  for(JetCaloCalculator* calc : clonedCalc) {
308  calc->processConstituent(it);
309  }
310  } // constituents loop
311 
312  // copy results & clear the cloned calc
313  for(JetCaloCalculator* calc: clonedCalc) {
314  results.push_back( calc->jetCalculation() );
315  delete calc;
316  }
317 
318  return results;
319  }
320 
321 
322  // std::vector<double> JetCaloCalculations::calculations(){
323 
324  // std::vector<double> v(m_calculators.size());
325  // for(size_t i=0;i < m_calculators.size();i++) {
326  // v[i] = m_calculators[i]->jetCalculation() ;
327  // }
328  // return v;
329  // }
330 
332  if(m_owncalculators) for(size_t i=0;i < m_calculators.size();i++) delete m_calculators[i];
333  m_calculators.clear();
334  }
335 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
jet::JetCaloCalculator::processConstituent
virtual bool processConstituent(xAOD::JetConstituentVector::iterator &)
Perform 1 calculation step using 1 constituent.
Definition: JetCaloCalculations.h:93
xAOD::CaloCluster_v1::time
flt_t time() const
Access cluster time.
FEHelpers.h
verify_menu_config.results
results
Definition: verify_menu_config.py:67
CaloConstitHelpers::CaloClusterExtractor::valid
virtual bool valid(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:33
CaloConstitHelpers::CaloClusterExtractor::~CaloClusterExtractor
virtual ~CaloClusterExtractor()=default
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
CaloConstitHelpers::PFOExtractor::time
virtual double time(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:74
CaloConstitHelpers::CaloConstitExtractor
Interface to retrieve calo informations from a jet constituent.
Definition: JetCaloCalculations.h:31
CaloConstitHelpers::CaloClusterExtractor::energyHEC
virtual double energyHEC(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:47
xAOD::JetConstituentVector::end
iterator end() const
iterator after the last constituent
Definition: JetConstituentVector.cxx:104
xAOD::PFODetails::eflowRec_TIMING
@ eflowRec_TIMING
Definition: Event/xAOD/xAODPFlow/xAODPFlow/PFODefs.h:58
CaloConstitHelpers::FlowElementExtractor::time
virtual double time(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:109
xAOD::PFO_v1::attribute
bool attribute(PFODetails::PFOAttributes AttributeType, T &anAttribute) const
get a PFO Variable via enum
skel.it
it
Definition: skel.GENtoEVGEN.py:396
JetAttributes.h
CaloConstitHelpers::PFOExtractor::~PFOExtractor
virtual ~PFOExtractor()=default
jet::JetCaloCalculations::process
virtual std::vector< double > process(const xAOD::Jet *jet) const
Perform all tasks correspondings to the associated JetCaloCalculators on Jet jet.
Definition: JetCaloCalculations.cxx:269
xAOD::IParticle::type
virtual Type::ObjectType type() const =0
The type of the object as a simple enumeration.
CaloConstitHelpers::JetConstitIterator
xAOD::JetConstituentVector::iterator JetConstitIterator
Definition: JetCaloCalculations.cxx:21
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
CaloCell_ID_FCS::HEC2
@ HEC2
Definition: FastCaloSim_CaloCell_ID.h:29
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
CaloConstitHelpers::FlowElementExtractor::moment
virtual double moment(JetConstitIterator &it, xAOD::CaloCluster::MomentType momentType) const override
Definition: JetCaloCalculations.cxx:102
jet::JetCaloCalculator::setupJet
virtual bool setupJet(const xAOD::Jet *)=0
CaloConstitHelpers::FlowElementExtractor
Definition: JetCaloCalculations.cxx:91
JetAccessorMap.h
xAOD::CaloCluster
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
Definition: Event/xAOD/xAODCaloEvent/xAODCaloEvent/CaloCluster.h:19
TruthTest.itE
itE
Definition: TruthTest.py:25
xAOD::JetAttribute::AttributeID
AttributeID
Definition: JetAttributes.h:24
jet::JetCaloCalculator::JetCaloCalculator
JetCaloCalculator()
Definition: JetCaloCalculations.h:81
xAOD::CaloCluster_v1::MomentType
MomentType
Enums to identify different moments.
Definition: CaloCluster_v1.h:120
CaloConstitHelpers::PFOExtractor::valid
virtual bool valid(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:61
JetCaloCalculations.h
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
xAOD::FlowElement_v1::isCharged
bool isCharged() const
Definition: FlowElement_v1.cxx:56
PFO.h
xAOD::JetConstituentVector::begin
iterator begin() const
iterator on the first constituent
Definition: JetConstituentVector.cxx:103
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
CaloCell_ID_FCS::HEC1
@ HEC1
Definition: FastCaloSim_CaloCell_ID.h:28
trigDumpTimers.timing
def timing(hist)
Definition: trigDumpTimers.py:13
jet::JetCaloCalculator::operator()
virtual double operator()(const xAOD::Jet *jet, xAOD::JetConstitScale s=xAOD::UncalibratedJetConstituent)
convenience function to perform the full calculation on a given jet
Definition: JetCaloCalculations.cxx:222
FlowElement.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
CaloCluster.h
CaloConstitHelpers::CaloClusterExtractor::moment
virtual double moment(JetConstitIterator &it, xAOD::CaloCluster::MomentType momentType) const override
Definition: JetCaloCalculations.cxx:37
beamspotman.n
n
Definition: beamspotman.py:731
xAOD::PFODetails::eflowRec_LAYERENERGY_HEC
@ eflowRec_LAYERENERGY_HEC
Definition: Event/xAOD/xAODPFlow/xAODPFlow/PFODefs.h:57
xAOD::FlowElement
FlowElement_v1 FlowElement
Definition of the current "pfo version".
Definition: FlowElement.h:16
jet::JetCaloCalculations::clear
void clear()
Definition: JetCaloCalculations.cxx:331
xAODType::ParticleFlow
@ ParticleFlow
The object is a particle-flow object.
Definition: ObjectType.h:41
CaloConstitHelpers::FlowElementExtractor::~FlowElementExtractor
virtual ~FlowElementExtractor()=default
jet::JetCaloCalculator::m_constitExtractor
const CaloConstitHelpers::CaloConstitExtractor * m_constitExtractor
Definition: JetCaloCalculations.h:115
jet::JetCaloCalculations::m_owncalculators
bool m_owncalculators
Definition: JetCaloCalculations.h:168
xAOD::PFO_v1::isCharged
bool isCharged() const
is a charged PFO
Definition: PFO_v1.cxx:251
CaloConstitHelpers::extractorForJet
const CaloConstitExtractor * extractorForJet(const xAOD::Jet *jet)
returns a pointer to a CaloConstitExtractor for a given jet.
Definition: JetCaloCalculations.cxx:185
xAOD::PFO_v1
Class describing a particle flow object.
Definition: PFO_v1.h:35
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:220
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
xAOD::JetConstitScale
JetConstitScale
Definition: JetTypes.h:20
CaloConstitHelpers::PFOExtractor::moment
virtual double moment(JetConstitIterator &it, xAOD::CaloCluster::MomentType momentType) const override
Definition: JetCaloCalculations.cxx:67
CaloConstitHelpers::FlowElementExtractor::energyHEC
virtual double energyHEC(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:142
CaloConstitHelpers
Definition: JetCaloCalculations.h:18
CaloConstitHelpers::PFOExtractor::energyHEC
virtual double energyHEC(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:81
CaloCell_ID_FCS::HEC0
@ HEC0
Definition: FastCaloSim_CaloCell_ID.h:27
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
jet::JetCaloCalculations::addCalculator
void addCalculator(JetCaloCalculator *c)
Definition: JetCaloCalculations.cxx:257
xAOD::CaloCluster_v1::eSample
float eSample(const CaloSample sampling) const
Definition: CaloCluster_v1.cxx:521
xAOD::PFO_v1::getClusterMoment
bool getClusterMoment(float &theMoment, xAOD::CaloCluster::MomentType momentType) const
Accessor for cluster moments.
Definition: PFO_v1.cxx:402
FEHelpers::getClusterMoment
bool getClusterMoment(const xAOD::FlowElement &fe, xAOD::CaloCluster::MomentType momentType, float &value)
Definition: FEHelpers.cxx:51
xAOD::UncalibratedJetConstituent
@ UncalibratedJetConstituent
Definition: JetTypes.h:21
xAOD::FlowElement_v1::otherObject
const xAOD::IParticle * otherObject(std::size_t i) const
Definition: FlowElement_v1.cxx:196
jet::JetCaloCalculator::jetCalculation
virtual double jetCalculation() const
return the result of the calculation
Definition: JetCaloCalculations.h:97
CaloConstitHelpers::CaloConstitExtractor::valid
virtual bool valid(xAOD::JetConstituentVector::iterator &) const
Definition: JetCaloCalculations.h:33
CaloConstitHelpers::FlowElementExtractor::valid
virtual bool valid(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:96
xAOD::JetConstituentVector
A vector of jet constituents at the scale used during jet finding.
Definition: JetConstituentVector.h:117
xAOD::JetConstituentVector::iterator
Definition: JetConstituentVector.h:121
CaloConstitHelpers::CaloClusterExtractor::time
virtual double time(JetConstitIterator &it) const override
Definition: JetCaloCalculations.cxx:43
jet::JetCaloCalculator
Definition: JetCaloCalculations.h:76
CaloCell_ID_FCS::HEC3
@ HEC3
Definition: FastCaloSim_CaloCell_ID.h:30
jet::JetCaloCalculations::m_calculators
std::vector< JetCaloCalculator * > m_calculators
Definition: JetCaloCalculations.h:167
beamspotnt.calc
calc
Definition: bin/beamspotnt.py:1252
python.compressB64.c
def c
Definition: compressB64.py:93
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:26
jet::JetCaloCalculations::setupEvent
bool setupEvent()
Definition: JetCaloCalculations.cxx:262
xAOD::FlowElement_v1::otherObjects
std::vector< const xAOD::IParticle * > otherObjects() const
Definition: FlowElement_v1.cxx:163
CaloConstitHelpers::PFOExtractor
Definition: JetCaloCalculations.cxx:56
xAOD::FlowElement_v1
A detector object made of other lower level object(s)
Definition: FlowElement_v1.h:25
CaloConstitHelpers::CaloClusterExtractor
Definition: JetCaloCalculations.cxx:28