ATLAS Offline Software
CalibrationNtupleMakerTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 // CalibrationNtupleMakerTool.cxx, (c) ATLAS Detector software
9 
11 
12 #include "xAODJet/JetContainer.h"
14 
17 
19 
20 // Gaudi
21 #include "GaudiKernel/ITHistSvc.h"
22 #include "GaudiKernel/SystemOfUnits.h"
23 
24 // Root
25 #include "TTree.h"
26 #include "TString.h"
27 #include "TH1F.h"
28 
29 #include <vector>
30 
31 using Gaudi::Units::GeV;
32 
33 CalibrationNtupleMakerTool::CalibrationNtupleMakerTool(const std::string& name, ISvcLocator* pSvcLocator) :
34  AthAlgorithm(name, pSvcLocator),
35  m_treeFolder("/calibration/"),
36  m_treeDescription("Calibration Ntuple"),
37  m_truthJetContainerName("MyAntiKt10TruthJets"),
38  m_vertexContainerName("PrimaryVertices"),
39  m_recoIsoDR(1.5),
40  m_recoIsoPtCut(100.*GeV),
41  m_trueIsoDR(2.5),
42  m_trueIsoPtCut(100.*GeV),
43  m_matchingCut(0.6),
44  m_h_events(nullptr),
45  m_index(nullptr),
46  m_etaCalo(nullptr),
47  m_etaDetCalo(nullptr),
48  m_phiCalo(nullptr),
49  m_eCalo(nullptr),
50  m_mCalo(nullptr),
51  m_etaCorr(nullptr),
52  m_etaDetCorr(nullptr),
53  m_phiCorr(nullptr),
54  m_eCorr(nullptr),
55  m_mCorr(nullptr),
56  m_etaTrue(nullptr),
57  m_phiTrue(nullptr),
58  m_eTrue(nullptr),
59  m_mTrue(nullptr)
60  {
61  declareProperty("FolderName" , m_treeFolder);
62  declareProperty("Description", m_treeDescription);
63 
64  // the jets collections to calibrate
65  declareProperty("JetCollections" , m_collectionNames);
66  declareProperty("TruthJetContainerName", m_truthJetContainerName);
67  declareProperty("VertexContainerName" , m_vertexContainerName);
68 
69  declareProperty("RecoIsolationDR" , m_recoIsoDR);
70  declareProperty("RecoIsolationPtCut" , m_recoIsoPtCut);
71  declareProperty("TruthIsolationDR" , m_trueIsoDR);
72  declareProperty("TruthIsolationPtCut", m_trueIsoPtCut);
73  declareProperty("MatchingCut" , m_matchingCut);
74  }
75 
77 = default;
78 
80 {
81  ATH_MSG_INFO( "initialize()" );
82 
83  if (bookTree().isFailure()){
84  ATH_MSG_FATAL( "Could not book the TTree object" );
85  return StatusCode::FAILURE;
86  }
87 
89 
90  return StatusCode::SUCCESS;
91 }
92 
94 {
95  ATH_MSG_INFO( "bookTree()" );
96 
97 
98  for (auto& name : m_collectionNames) {
99 
100  // creating the tree for thw jet collection
101  TTree * tree = new TTree (name.c_str(), m_treeDescription.c_str());
102 
103  // add the branches
104  tree->Branch("EventWeight" , &m_eventWeight );
105 
106  tree->Branch("eta_calo" , &m_etaCalo );
107  tree->Branch("eta_det_calo" , &m_etaDetCalo );
108  tree->Branch("phi_calo" , &m_phiCalo );
109  tree->Branch("E_calo" , &m_eCalo );
110  tree->Branch("m_calo" , &m_mCalo );
111 
112  tree->Branch("eta_corr1" , &m_etaCorr );
113  tree->Branch("eta_det_corr1" , &m_etaDetCorr );
114  tree->Branch("phi_corr1" , &m_phiCorr );
115  tree->Branch("E_corr1" , &m_eCorr );
116  tree->Branch("m_corr1" , &m_mCorr );
117 
118  tree->Branch("eta_true" , &m_etaTrue );
119  tree->Branch("phi_true" , &m_phiTrue );
120  tree->Branch("E_true" , &m_eTrue );
121  tree->Branch("m_true" , &m_mTrue );
122 
123  tree->Branch("index" , &m_index );
124  tree->Branch("mu" , &m_mu );
125  tree->Branch("NPV" , &m_npv );
126 
127 
128  m_trees.insert(std::pair<std::string, TTree*>(name, tree));
129 
130  }
131 
132  // now register the Tree
133  SmartIF<ITHistSvc> tHistSvc{service("THistSvc")};
134  ATH_CHECK( tHistSvc.isValid() );
135 
136  if (tHistSvc) {
137  for (const auto& name : m_collectionNames) {
138  if((tHistSvc->regTree(m_treeFolder+name, m_trees.at(name))).isFailure()) {
139  ATH_MSG_ERROR( "initialize() Could not register the validation Tree!" );
140  return StatusCode::FAILURE;
141  }
142  }
143  }
144 
145  // now register the Event histogram
146  m_h_events = new TH1F("h_events","total events", 10, 0, 10);
147 
148  if (tHistSvc and (tHistSvc->regHist(m_treeFolder+m_h_events->GetName(), m_h_events)).isFailure()) {
149  ATH_MSG_ERROR( "Can not register histogram" << m_h_events->GetName() );
150  return StatusCode::FAILURE;
151  }
152 
153  ATH_MSG_INFO("Calibration Tree booked and registered successfully!");
154 
155  return StatusCode::SUCCESS;
156 
157 }
158 
160 {
161  m_h_events->Fill(0);
162 
164  if(!evt.isValid()) {
165  ATH_MSG_FATAL( "Unable to retrieve Event Info" );
166  }
167  float ev_weight = evt->mcEventWeight();
168 
169  const auto *const truths = getContainer<xAOD::JetContainer>(m_truthJetContainerName);
170  if (not truths) return StatusCode::FAILURE;
171 
172  const auto *const vertices = getContainer<xAOD::VertexContainer>(m_vertexContainerName);
173  if (not vertices) return StatusCode::FAILURE;
174 
175  // get mu
176  float mu= evt->averageInteractionsPerCrossing();
177 
178  //get NPV
179  float npv = 0.;
180 
181  for (const auto vertex : *vertices) {
182  if (vertex->nTrackParticles()>=2)
183  npv++;
184  }
185 
186  for (auto& name : m_collectionNames) {
187  const auto *const jets = getContainer<xAOD::JetContainer>(name);
188 
189  m_etaCalo ->clear();
190  m_etaDetCalo ->clear();
191  m_phiCalo ->clear();
192  m_eCalo ->clear();
193  m_mCalo ->clear();
194  m_etaCorr ->clear();
195  m_etaDetCorr ->clear();
196  m_phiCorr ->clear();
197  m_eCorr ->clear();
198  m_mCorr ->clear();
199  m_etaTrue ->clear();
200  m_phiTrue ->clear();
201  m_eTrue ->clear();
202  m_mTrue ->clear();
203  m_index ->clear();
204 
205  for (const auto truth: *truths) {
206 
207  // here we match to the reco
208  int index = 0;
209  std::vector<const xAOD::Jet*> matched = {};
210 
211  int Nmatches = Matched(truth, jets, matched, index);
212 
213  // skip truth jets that don't match any reco jets
214  if (Nmatches==0) continue;
215 
216  // skip the jets that are not isolated
217  if ( m_recoIsoDR > 0 ) {
218  double DRminReco = DRmin(matched.at(0),jets,m_recoIsoPtCut);
219  if ( DRminReco < m_recoIsoDR ) continue;
220  }
221 
222  if ( m_trueIsoDR > 0 ) {
223  double DRminTruth = DRmin(truth,truths,m_trueIsoPtCut);
224  if ( DRminTruth < m_trueIsoDR ) continue;
225  }
226 
227  //Storing variables
228  m_etaTrue->push_back(truth->eta());
229  m_phiTrue->push_back(truth->phi());
230  m_mTrue->push_back(truth->m()/GeV);
231  m_eTrue->push_back(truth->e()/GeV);
232 
233  m_etaCalo->push_back(jets->at(index)->eta());
234  m_phiCalo->push_back(jets->at(index)->phi());
235  m_mCalo->push_back(jets->at(index)->m()/GeV);
236  m_eCalo->push_back(jets->at(index)->e()/GeV);
237 
238  float detectorEta = DetectorEta(jets->at(index));
239  m_etaDetCalo->push_back(detectorEta);
240 
241  m_etaCorr->push_back(jets->at(index)->eta());
242  m_phiCorr->push_back(jets->at(index)->phi());
243  m_mCorr->push_back(jets->at(index)->m()/GeV);
244  m_eCorr->push_back(jets->at(index)->e()/GeV);
245  m_etaDetCorr->push_back(detectorEta);
246 
247  m_index->push_back(index);
248 
249  }
250 
251  m_eventWeight = ev_weight;
252  m_mu = mu;
253  m_npv = npv;
254 
255  m_trees.at(name)->Fill();
256 
257  }
258 
259  return StatusCode::SUCCESS;
260 
261 }
262 
263 int CalibrationNtupleMakerTool::Matched(const xAOD::Jet* truth, const xAOD::JetContainer* jets, std::vector<const xAOD::Jet*>& matched, int& index) const {
264 
265  int Nmatches = 0;
266  double drmin = 999.;
267  int Min_index=-1;
268 
269  for (unsigned int ind = 0; ind < jets->size(); ind++) {
270  double dr = truth->p4().DeltaR(jets->at(ind)->p4());
271  if (dr < m_matchingCut) ++Nmatches;
272  //find minimum:
273  if (dr < drmin) {
274  drmin = dr;
275  Min_index = ind;
276  }
277  }
278 
279  if (drmin<m_matchingCut) {
280  matched.push_back(jets->at(Min_index));
281  index = Min_index;
282  }
283 
284  return Nmatches;
285 }
286 
288 
289  double DRmin=9999;
290  for (const auto jet : *jets) {
291  if (PtMin>0. and jet->pt()<PtMin) continue;
292  double Dr = myjet->p4().DeltaR(jet->p4());
293  if (Dr>0.0001 and Dr<DRmin)
294  DRmin=Dr;
295  }
296  return DRmin;
297 }
298 
300 
301  xAOD::IParticle::FourMom_t corrP4(0,0,0,0);
302 
303  const auto& partLinks = jet->constituentLinks();
304  for (const xAOD::IParticleLink& link : partLinks) {
305 
306  if (not link.isValid()) {
307  ATH_MSG_WARNING("Got an invalid element link. Returning jet eta...");
308  return jet->eta();
309  }
310 
311  const xAOD::TrackCaloCluster* tcc = dynamic_cast<const xAOD::TrackCaloCluster*>(*link);
312 
313  static const SG::AuxElement::Accessor< float > acc_detEta( "DetectorEta" );
314  float det_eta = tcc->eta();
315 
316  if (acc_detEta.isAvailable(*tcc)) {
317  det_eta = acc_detEta(*tcc);
318  } else
319  ATH_MSG_WARNING("DetectorEta decoration not found for TCCs! Using eta...");
320 
321  double pt = tcc->p4().P()/cosh(det_eta);
322 
324  p4CorrCl.SetPtEtaPhiE(pt, det_eta, tcc->p4().Phi(), tcc->p4().E());
325  if(tcc->p4().E() < 0.) p4CorrCl*=-1.;
326  corrP4 += p4CorrCl;
327  }
328 
329  return corrP4.Eta();
330 }
331 
333 {
334  ATH_MSG_INFO( "finalize()" );
335 
336  delete m_index;
337 
338  delete m_etaCalo;
339  delete m_etaDetCalo;
340  delete m_phiCalo;
341  delete m_eCalo;
342  delete m_mCalo;
343 
344  delete m_etaCorr;
345  delete m_etaDetCorr;
346  delete m_phiCorr;
347  delete m_eCorr;
348  delete m_mCorr;
349 
350  delete m_etaTrue;
351  delete m_phiTrue;
352  delete m_eTrue;
353  delete m_mTrue;
354 
355  return StatusCode::SUCCESS;
356 
357 }
CalibrationNtupleMakerTool::m_trueIsoDR
double m_trueIsoDR
Definition: CalibrationNtupleMakerTool.h:62
CalibrationNtupleMakerTool::initialize
StatusCode initialize()
standard Athena-Algorithm method
Definition: CalibrationNtupleMakerTool.cxx:79
CalibrationNtupleMakerTool::m_etaDetCalo
std::vector< double > * m_etaDetCalo
Definition: CalibrationNtupleMakerTool.h:76
CalibrationNtupleMakerTool::m_collectionNames
std::vector< std::string > m_collectionNames
Definition: CalibrationNtupleMakerTool.h:52
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
CalibrationNtupleMakerTool::m_etaCorr
std::vector< double > * m_etaCorr
Definition: CalibrationNtupleMakerTool.h:81
CalibrationNtupleMakerTool::m_recoIsoPtCut
double m_recoIsoPtCut
Definition: CalibrationNtupleMakerTool.h:61
xAOD::TrackCaloCluster_v1
Class describing a TrackCaloCluster.
Definition: TrackCaloCluster_v1.h:25
CalibrationNtupleMakerTool::m_etaCalo
std::vector< double > * m_etaCalo
Definition: CalibrationNtupleMakerTool.h:75
tree
TChain * tree
Definition: tile_monitor.h:30
CalibrationNtupleMakerTool::~CalibrationNtupleMakerTool
virtual ~CalibrationNtupleMakerTool()
Default Destructor.
TrackCaloClusterContainer.h
CalibrationNtupleMakerTool::DRmin
static double DRmin(const xAOD::Jet *myjet, const xAOD::JetContainer *jets, double PtMin)
Definition: CalibrationNtupleMakerTool.cxx:287
test_pyathena.pt
pt
Definition: test_pyathena.py:11
CalibrationNtupleMakerTool::m_evt
SG::ReadHandleKey< xAOD::EventInfo > m_evt
Definition: CalibrationNtupleMakerTool.h:93
CalibrationNtupleMakerTool::m_eTrue
std::vector< double > * m_eTrue
Definition: CalibrationNtupleMakerTool.h:89
xAOD::TrackCaloCluster_v1::p4
virtual FourMom_t p4() const
The full 4-momentum of the particle.
Definition: TrackCaloCluster_v1.cxx:36
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
python.TurnDataReader.dr
dr
Definition: TurnDataReader.py:112
RunExEngineTest.PtMin
PtMin
Definition: RunExEngineTest.py:49
xAOD::IParticle::FourMom_t
TLorentzVector FourMom_t
Definition of the 4-momentum type.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:69
CalibrationNtupleMakerTool::m_vertexContainerName
std::string m_vertexContainerName
Definition: CalibrationNtupleMakerTool.h:58
xAOD::TrackCaloCluster_v1::eta
virtual double eta() const
The pseudorapidity ( ) of the particle.
CalibrationNtupleMakerTool::m_index
std::vector< int > * m_index
Definition: CalibrationNtupleMakerTool.h:73
CalibrationNtupleMakerTool::m_trueIsoPtCut
double m_trueIsoPtCut
Definition: CalibrationNtupleMakerTool.h:63
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
CalibrationNtupleMakerTool::finalize
StatusCode finalize()
standard Athena-Algorithm method
Definition: CalibrationNtupleMakerTool.cxx:332
CalibrationNtupleMakerTool::m_treeFolder
std::string m_treeFolder
Definition: CalibrationNtupleMakerTool.h:54
CalibrationNtupleMakerTool::m_phiTrue
std::vector< double > * m_phiTrue
Definition: CalibrationNtupleMakerTool.h:88
CalibrationNtupleMakerTool::m_eventWeight
float m_eventWeight
Definition: CalibrationNtupleMakerTool.h:69
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CalibrationNtupleMakerTool::m_npv
float m_npv
Definition: CalibrationNtupleMakerTool.h:71
CalibrationNtupleMakerTool::m_recoIsoDR
double m_recoIsoDR
Definition: CalibrationNtupleMakerTool.h:60
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CalibrationNtupleMakerTool::m_mCorr
std::vector< double > * m_mCorr
Definition: CalibrationNtupleMakerTool.h:85
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
CalibrationNtupleMakerTool::m_treeDescription
std::string m_treeDescription
Definition: CalibrationNtupleMakerTool.h:55
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
AthAlgorithm
Definition: AthAlgorithm.h:47
CalibrationNtupleMakerTool::DetectorEta
float DetectorEta(const xAOD::Jet *jet)
Definition: CalibrationNtupleMakerTool.cxx:299
Dr
double Dr
Definition: LArDetectorConstructionTBEC.cxx:57
CalibrationNtupleMakerTool::m_eCorr
std::vector< double > * m_eCorr
Definition: CalibrationNtupleMakerTool.h:84
CalibrationNtupleMakerTool.h
CalibrationNtupleMakerTool::m_phiCalo
std::vector< double > * m_phiCalo
Definition: CalibrationNtupleMakerTool.h:77
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
CalibrationNtupleMakerTool::m_trees
std::map< std::string, TTree * > m_trees
Definition: CalibrationNtupleMakerTool.h:53
CalibrationNtupleMakerTool::bookTree
virtual StatusCode bookTree()
Definition: CalibrationNtupleMakerTool.cxx:93
python.ElectronD3PDObject.matched
matched
Definition: ElectronD3PDObject.py:138
CalibrationNtupleMakerTool::m_h_events
TH1 * m_h_events
Definition: CalibrationNtupleMakerTool.h:67
CalibrationNtupleMakerTool::Matched
int Matched(const xAOD::Jet *truth, const xAOD::JetContainer *jets, std::vector< const xAOD::Jet * > &matched, int &index) const
Definition: CalibrationNtupleMakerTool.cxx:263
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
xAOD::Jet_v1::p4
virtual FourMom_t p4() const
The full 4-momentum of the particle.
Definition: Jet_v1.cxx:71
VertexContainer.h
JetContainer.h
CalibrationNtupleMakerTool::m_eCalo
std::vector< double > * m_eCalo
Definition: CalibrationNtupleMakerTool.h:78
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CalibrationNtupleMakerTool::m_mCalo
std::vector< double > * m_mCalo
Definition: CalibrationNtupleMakerTool.h:79
CalibrationNtupleMakerTool::execute
StatusCode execute()
standard Athena-Algorithm method
Definition: CalibrationNtupleMakerTool.cxx:159
CalibrationNtupleMakerTool::m_truthJetContainerName
std::string m_truthJetContainerName
Definition: CalibrationNtupleMakerTool.h:57
CalibrationNtupleMakerTool::m_phiCorr
std::vector< double > * m_phiCorr
Definition: CalibrationNtupleMakerTool.h:83
CalibrationNtupleMakerTool::m_mTrue
std::vector< double > * m_mTrue
Definition: CalibrationNtupleMakerTool.h:90
SG::ConstAccessor< T, AuxAllocator_t< T > >::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
CalibrationNtupleMakerTool::m_etaDetCorr
std::vector< double > * m_etaDetCorr
Definition: CalibrationNtupleMakerTool.h:82
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
CalibrationNtupleMakerTool::m_matchingCut
double m_matchingCut
Definition: CalibrationNtupleMakerTool.h:65
CalibrationNtupleMakerTool::CalibrationNtupleMakerTool
CalibrationNtupleMakerTool(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Definition: CalibrationNtupleMakerTool.cxx:33
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
CalibrationNtupleMakerTool::m_etaTrue
std::vector< double > * m_etaTrue
Definition: CalibrationNtupleMakerTool.h:87
checkFileSG.ind
list ind
Definition: checkFileSG.py:118
CalibrationNtupleMakerTool::m_mu
float m_mu
Definition: CalibrationNtupleMakerTool.h:70