ATLAS Offline Software
example_rebuildTrackMET.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //Author: Russell Smith
6 //Email : rsmith@cern.ch
7 //Date : February 2015
8 #ifndef XAOD_ANALYSIS
9 int main() {
10  std::cout << "Examples are only defined in the analysis release." << std::endl;
11  return 0;
12 }
13 #else
14 
15 #ifdef XAOD_STANDALONE
16 #include "xAODRootAccess/Init.h"
17 #include "xAODRootAccess/TEvent.h"
18 #include "xAODRootAccess/TStore.h"
19 #else
20 #include "POOLRootAccess/TEvent.h"
21 #include "StoreGate/StoreGateSvc.h"
22 #endif
23 
24 // FrameWork includes
27 
32 
34 #include "xAODCore/ShallowCopy.h"
35 #include "xAODJet/JetContainer.h"
38 #include "xAODMuon/MuonContainer.h"
42 
43 #undef NDEBUG
44 #include <cassert>
45 #include <memory>
46 #include "TFile.h"
47 #include "TSystem.h"
48 
50 
52 #include "METUtilities/METMaker.h"
54 
56 
57 #include "xAODCore/tools/IOStats.h"
59 
60 using namespace asg::msgUserCode;
61 
62 int main( int argc, char* argv[]) {std::cout << __PRETTY_FUNCTION__ << std::endl;
63  ANA_CHECK_SET_TYPE (int);
64 
65 #ifdef XAOD_STANDALONE
66  ANA_CHECK (xAOD::Init()) ;
67  //enable status code failures
69  StatusCode::enableFailure();
70 #else
71  IAppMgrUI* app = POOL::Init(); //important to do this first!
72 #endif
73 
74  bool invisEle(false);
75  bool debug(false);
76  TString fileName = gSystem->Getenv("ASG_TEST_FILE_MC");
77  std::string jetType = "AntiKt4EMTopo";
78  for (int i=0; i<argc; ++i) {
79  if (std::string(argv[i]) == "-filen" && i+1<argc) {
80  fileName = argv[i+1];
81  } else if (std::string(argv[i]) == "-jetcoll" && i+1<argc) {
82  jetType = argv[i+1];
83  } else if (std::string(argv[i]) == "-invisEle") {
84  invisEle=true;
85  } else if (std::string(argv[i]) == "-debug") {
86  debug = true;
87  }
88  }
89 
90  //this test file should work. Feel free to contact me if there is a problem with the file.
91  std::unique_ptr< TFile > ifile( TFile::Open( fileName, "READ" ) );
92  assert( ifile.get() );
93 
94  // Create a TEvent object to read from file and a transient store in which to place items
95 #ifdef XAOD_STANDALONE
96  std::unique_ptr<xAOD::TEvent> event(new xAOD::TEvent( xAOD::TEvent::kClassAccess ) );
97  std::unique_ptr<xAOD::TStore> store (new xAOD::TStore());
98 #else // Athena "Store" is the same StoreGate used by the TEvent
99  std::unique_ptr<POOL::TEvent> event(new POOL::TEvent( POOL::TEvent::kClassAccess ));
100  ServiceHandle<StoreGateSvc>& store = event->evtStore();
101 #endif
102  ANA_CHECK( event->readFrom( ifile.get() ) );
103 
104  // creation and set properties of the tools
105  // if you need to set properties, you should do so before initialization
106 
108  jetCalibrationTool.setTypeAndName("JetCalibrationTool/jetCalibTool");
109  ANA_CHECK( jetCalibrationTool.setProperty("JetCollection", jetType) );
110  ANA_CHECK( jetCalibrationTool.setProperty("ConfigFile", "JES_MC15cRecommendation_May2016_rel21.config") );
111  ANA_CHECK( jetCalibrationTool.setProperty("CalibSequence", "JetArea_Residual_EtaJES_GSC") );
112  ANA_CHECK( jetCalibrationTool.setProperty("IsData", false) );
113  ANA_CHECK( jetCalibrationTool.retrieve() );
114 
116  metSystTool.setTypeAndName("met::METSystematicsTool/metSystTool");
117  ANA_CHECK( metSystTool.setProperty("ConfigJetTrkFile","JetTrackSyst.config" ));//this is needed to do jet track systematics
118  // ANA_CHECK( metSystTool.setProperty("UseDevArea" ,true )); // To get the configs from GROUPDATA/dev/METUtilities
119  ANA_CHECK( metSystTool.retrieve() );
120 
122  metMaker.setTypeAndName("met::METMaker/metMaker");
123  ANA_CHECK( metMaker.setProperty("DoMuonEloss", true) );
124  ANA_CHECK( metMaker.retrieve() );
125 
126  for(Long64_t ievent = 0; ievent < std::min(int(event->getEntries()), 100); ++ievent){
127  if(ievent % 10 == 0) std::cout << "event number: " << ievent << std::endl;
128  ANA_CHECK( event->getEntry(ievent) >= 0 );
129 
130  //retrieve the original containers
131 
132  //this contains all constituents not associated to a jet/misc term
133  const xAOD::MissingETContainer* coreMet = nullptr;
134  std::string coreMetKey = "MET_Core_" + jetType;
135  ANA_CHECK( event->retrieve(coreMet, coreMetKey) );
136  if(debug) std::cout << "Using core MET " << coreMet << std::endl;
137 
138  const xAOD::ElectronContainer* electrons = nullptr;
139  ANA_CHECK( event->retrieve(electrons, "Electrons") );
140 
141  const xAOD::MuonContainer* muons = nullptr;
142  ANA_CHECK( event->retrieve(muons, "Muons") );
143 
144  //this should probably be a calibrated jet container. See the METUtilities twiki for more info
145  const xAOD::JetContainer* uncalibJets = nullptr;
146  ANA_CHECK( event->retrieve(uncalibJets, jetType+"Jets"));//this retrieves and applies the correction
147  std::pair< xAOD::JetContainer*, xAOD::ShallowAuxContainer* > calibJetsPair = xAOD::shallowCopyContainer( *uncalibJets );//make a shallow copy to calibrate
148  xAOD::JetContainer *& calibJets = calibJetsPair.first;//create a reference to the first element of the pair (i.e. the JetContainer)
149  //Shallow copy is needed (see links below)
150  if(jetCalibrationTool->applyCalibration(*calibJets).isFailure())//apply the calibration
151  return 1;
152  if(!xAOD::setOriginalObjectLink(*uncalibJets, *calibJets)){//tell calib container what old container it matches
153  std::cout << "Failed to set the original object links" << std::endl;
154  return 1;
155  }
156  ANA_CHECK( store->record(calibJets, "CalibJets") );
157 
158  //retrieve the MET association map
159  const xAOD::MissingETAssociationMap* metMap = nullptr;
160  std::string metAssocKey = "METAssoc_" + jetType;
161  ANA_CHECK( event->retrieve(metMap, metAssocKey) );
162 
163  //get the set of recommended systematics
165  const CP::SystematicSet& recSysList = registry.recommendedSystematics();
166 
167  //start the loop over systematics
168  //For each systematic, we create an output MissingET container
169  for(CP::SystematicSet::const_iterator isys = recSysList.begin();
170  isys != recSysList.end();
171  ++isys) { // print the systematics on the first event
172  // When only using the METSystematicsTool, this will be the list of recommended systematics for that tool
173  // if(ievent == 0)
174 
175  // Create a MissingETContainer with its aux store for each systematic
176  xAOD::MissingETContainer* newMetContainer = new xAOD::MissingETContainer();
177  xAOD::MissingETAuxContainer* newMetAuxContainer = new xAOD::MissingETAuxContainer();
178  newMetContainer->setStore(newMetAuxContainer);
179 
180  xAOD::MissingETAssociationHelper metHelper(metMap);
181  //here we apply some basic cuts and rebuild the met at each step
182  //Electrons
185  for(const auto *el : *electrons) {
186  if(el->pt()>20e3 && el->eta()<2.47) {
187  if(invisEle) {
188  invisElectrons.push_back(el);
189  }// else {
190  metElectrons.push_back(el);
191  //}
192  }
193  }
194  if(!invisElectrons.empty()){
195  ANA_CHECK( metMaker->markInvisible(invisElectrons.asDataVector(),metHelper,newMetContainer) );
196  }
197 
198  ANA_CHECK( metMaker->rebuildMET("TrkEle", //name of metElectrons in metContainer
199  xAOD::Type::Electron, //telling the rebuilder that this is electron met
200  newMetContainer, //filling this met container
201  metElectrons.asDataVector(),//using these metElectrons that accepted our cuts
202  metHelper) //and this association map
203  );
204 
205  //Muons
207  for(const auto *mu : *muons) {
208  if(mu->pt()>20e3 && mu->eta()<2.4) metMuons.push_back(mu);
209  }
210  ANA_CHECK( metMaker->rebuildMET("TrkMuons",
212  newMetContainer,
213  metMuons.asDataVector(),
214  metHelper)
215  );
216 
217  // for rebuilding track MET
218  ANA_CHECK( metMaker->rebuildTrackMET("TrkJet", //name of jet track met
219  "PVSoftTrk", //name of soft track term met
220  newMetContainer,//adding to this new met container
221  calibJets, //using this jet collection to calculate jet track met
222  coreMet, //core met container
223  metHelper, //with this association map
224  false //don't apply jet jvt cut
225  )
226  );
227 
228  //this is kind of annoying, but applySystematicVariation only takes a SystematicSet, but *isys is a SystematicVariation.
229  //We use the SystematicSet constructor which just takes a SystematicVariation
230  CP::SystematicSet iSysSet( (*isys).name());
231  //tell the tool that we are using this SystematicSet (of one SystematicVariation for now)
232  //after this call, when we use applyCorrection, the given met term will be adjusted with this systematic applied
233  ANA_CHECK( metSystTool->applySystematicVariation(iSysSet) );
234 
235  metSystTool->setRandomSeed( int(1e6*(*newMetContainer)["PVSoftTrk"]->phi()) );
236 
237  xAOD::MissingET * softTrkMet = (*newMetContainer)["PVSoftTrk"];
238  ANA_CHECK( softTrkMet != nullptr); //check we retrieved the soft trk
239  ANA_CHECK( metSystTool->applyCorrection(*softTrkMet, metHelper) );
240  if(debug) std::cout << "Soft track met: " << softTrkMet->met();
241 
242  // when doing track MET
243  xAOD::MissingET * jetTrkMet = (*newMetContainer)["TrkJet"];
244  ANA_CHECK( jetTrkMet != nullptr);
245  ANA_CHECK( metSystTool->applyCorrection(*jetTrkMet, metHelper));//for jetTrkMet correction, we need the METAssociationMap
246  if(debug) std::cout << "Jet track met: " << jetTrkMet->met();
247 
248  //this builds the final track or cluster met sums, using systematic varied container
249  //In the future, you will be able to run both of these on the same container to easily output CST and TST
251 
252  //we record the container to the store, with a systematic indicated name
253  ANA_CHECK( store->record( newMetContainer, "FinalMETContainer_" + iSysSet.name() ));
254  ANA_CHECK( store->record( newMetAuxContainer, "FinalMETContainer_" + iSysSet.name() + "Aux."));
255  }
256 
257 #ifdef XAOD_STANDALONE // POOL::TEvent should handle this when changing events
258  //fill the containers stored in the event
259  //to the output file and clear the transient store
260  assert(event->fill());
261  store->clear();
262 #endif
263  }
264 
265 #ifndef XAOD_STANDALONE // POOL::TEvent should handle this when changing events
266  ANA_CHECK( app->finalize() );
267 #endif
268 
270 
271  return 0;
272 }
273 
274 #endif
ShallowCopy.h
python.Dso.registry
registry
Definition: Control/AthenaServices/python/Dso.py:159
SGTest::store
TestStore store
Definition: TestStore.cxx:23
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
xAOD::MissingETAuxContainer
MissingETAuxContainer_v1 MissingETAuxContainer
Definition: MissingETAuxContainer.h:16
xAOD::IOStats::stats
ReadStats & stats()
Access the object belonging to the current thread.
Definition: IOStats.cxx:17
IMETMaker::markInvisible
virtual StatusCode markInvisible(const xAOD::IParticleContainer *collection, xAOD::MissingETAssociationHelper &helper, xAOD::MissingETContainer *metCont)=0
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
CP::CorrectionCode::enableFailure
static void enableFailure() noexcept
Definition: CorrectionCode.h:64
IMETSystematicsTool::applyCorrection
virtual CP::CorrectionCode applyCorrection(xAOD::MissingET &met, const xAOD::MissingETAssociationHelper &helper) const =0
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
CP::SystematicSet::const_iterator
std::set< SystematicVariation >::const_iterator const_iterator
Definition: SystematicSet.h:52
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
xAOD::MissingETContainer
MissingETContainer_v1 MissingETContainer
Definition: Event/xAOD/xAODMissingET/xAODMissingET/MissingETContainer.h:16
IOStats.h
ShallowAuxContainer.h
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:100
MissingETBase::Types::bitmask_t
uint64_t bitmask_t
Type for status word bit mask.
Definition: MissingETBase.h:39
IMETMaker::rebuildMET
virtual StatusCode rebuildMET(const std::string &metKey, xAOD::Type::ObjectType metType, xAOD::MissingETContainer *metCont, const xAOD::IParticleContainer *collection, xAOD::MissingETAssociationHelper &helper, MissingETBase::UsageHandler::Policy objScale=MissingETBase::UsageHandler::PhysicsObject)=0
xAOD::MissingETAssociationMap_v1
Definition: MissingETAssociationMap_v1.h:29
POOL::Init
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:29
POOL::TEvent::kClassAccess
@ kClassAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:46
xAOD::phi
setEt phi
Definition: TrigEMCluster_v1.cxx:29
met::buildMETSum
StatusCode buildMETSum(const std::string &totalName, xAOD::MissingETContainer *metCont)
Definition: METHelpers.cxx:64
METHelpers.h
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:133
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
JetCalibrationTool.h
ReadStats.h
CheckAppliedSFs.e3
e3
Definition: CheckAppliedSFs.py:264
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
ElectronContainer.h
POOL::TEvent::getEntry
int getEntry(long entry)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:185
asg::StandaloneToolHandle::setProperty
StatusCode setProperty(const std::string &name, T2 &&value)
Definition: StandaloneToolHandle.h:105
lumiFormat.i
int i
Definition: lumiFormat.py:85
SystematicRegistry.h
POOL::TEvent::getEntries
long getEntries()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:124
asg::StandaloneToolHandle
an "initializing" ToolHandle for stand-alone applications
Definition: StandaloneToolHandle.h:44
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
IParticleContainer.h
MissingETAuxContainer.h
MessageCheck.h
macros for messaging and checking status codes
TEvent.h
xAOD::MissingET_v1
Principal data object for Missing ET.
Definition: MissingET_v1.h:25
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
main
int main()
Definition: example_rebuildTrackMET.cxx:9
CP::SystematicSet::end
const_iterator end() const
description: const iterator to the end of the set
Definition: SystematicSet.h:59
xAOD::MissingETAuxContainer_v1
Auxiliary data store for xAOD::MissingETContainer.
Definition: MissingETAuxContainer_v1.h:20
Init.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
TauJetContainer.h
xAOD::IOStats::instance
static IOStats & instance()
Singleton object accessor.
Definition: IOStats.cxx:11
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
asg::StandaloneToolHandle::retrieve
StatusCode retrieve()
initialize the tool, will succeed if the tool was already initialized
Definition: StandaloneToolHandle.h:147
xAOD::ReadStats::printSmartSlimmingBranchList
void printSmartSlimmingBranchList(bool autoIncludeLinks=false) const
Print the accessed variables, formatted for smart slimming.
xAOD::MissingETContainer_v1
Container for xAOD::MissingET_v1 objects.
Definition: MissingETContainer_v1.h:21
POOL::TEvent
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:40
TEvent.h
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
IMETSystematicsTool::setRandomSeed
virtual void setRandomSeed(int seed) const =0
SGTest::TestStore::record
void record(const T *p, const std::string &key)
Definition: TestStore.h:81
xAOD::MissingETAssociationHelper
Definition: MissingETAssociationHelper.h:26
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
MuonContainer.h
Muon
struct TBPatternUnitContext Muon
METMaker.h
asg::StandaloneToolHandle::setTypeAndName
void setTypeAndName(const std::string &typeAndName)
Definition: StandaloneToolHandle.h:101
MissingETBase::Source::Signal::Track
@ Track
Indicator for MET contribution from reconstructed charged particle tracks.
ConvertOldUJHistosToNewHistos.jetType
string jetType
Definition: ConvertOldUJHistosToNewHistos.py:121
xAOD::TStore
A relatively simple transient store for objects created in analysis.
Definition: TStore.h:44
JetContainer.h
ANA_CHECK_SET_TYPE
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:314
METSystematicsTool.h
CP::SystematicRegistry
This module implements the central registry for handling systematic uncertainties with CP tools.
Definition: SystematicRegistry.h:25
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
POOL::TEvent::retrieve
StatusCode retrieve(const T *&obj)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:74
xAOD::setOriginalObjectLink
bool setOriginalObjectLink(const IParticle &original, IParticle &copy)
This function should be used by CP tools when they make a deep copy of an object in their correctedCo...
Definition: IParticleHelpers.cxx:30
CP::SystematicSet::begin
const_iterator begin() const
description: const iterator to the beginning of the set
Definition: SystematicSet.h:55
IJetCalibrationTool::applyCalibration
virtual StatusCode applyCalibration(xAOD::JetContainer &jets) const =0
Apply calibration to a jet container.
IMETMaker::rebuildTrackMET
virtual StatusCode rebuildTrackMET(const std::string &metJetKey, const std::string &metSoftKey, xAOD::MissingETContainer *metCont, const xAOD::JetContainer *jets, const xAOD::MissingETContainer *metCoreCont, xAOD::MissingETAssociationHelper &helper, bool doJetJVT)=0
IParticleHelpers.h
MissingETAssociationMap.h
LArCellNtuple.ifile
string ifile
Definition: LArCellNtuple.py:133
StandaloneToolHandle.h
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
CP::ISystematicsTool::applySystematicVariation
virtual StatusCode applySystematicVariation(const SystematicSet &systConfig)=0
effects: configure this tool for the given list of systematic variations.
MissingETAssociationHelper.h
StoreGateSvc.h
PhotonContainer.h
MissingETContainer.h
TStore.h
InDetDD::electrons
@ electrons
Definition: InDetDD_Defs.h:17
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84
xAOD::MissingET_v1::met
float met() const
Returns .
CP::SystematicRegistry::getInstance
static SystematicRegistry & getInstance()
Get the singleton instance of the registry for the curren thread.
Definition: SystematicRegistry.cxx:25
xAOD::Init
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition: Init.cxx:31
ServiceHandle< StoreGateSvc >