ATLAS Offline Software
Loading...
Searching...
No Matches
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
9int 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"
19#else
22#endif
23
24// FrameWork includes
27
32
42
43#undef NDEBUG
44#include <cassert>
45#include <memory>
46#include "TFile.h"
47#include "TSystem.h"
48
50
54
56
59
60using namespace asg::msgUserCode;
61
62int main( int argc, char* argv[]) {std::cout << __PRETTY_FUNCTION__ << std::endl;
64
65#ifdef XAOD_STANDALONE
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();
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
Scalar phi() const
phi method
macros for messaging and checking status codes
#define ANA_CHECK(EXP)
check whether the given expression was successful
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
const bool debug
static void enableFailure() noexcept
This module implements the central registry for handling systematic uncertainties with CP tools.
const SystematicSet & recommendedSystematics() const
returns: the recommended set of systematics
static SystematicRegistry & getInstance()
Get the singleton instance of the registry for the curren thread.
Class to wrap a set of SystematicVariations.
const_iterator end() const
description: const iterator to the end of the set
std::set< SystematicVariation >::const_iterator const_iterator
const_iterator begin() const
description: const iterator to the beginning of the set
DataVector adapter that acts like it holds const pointers.
void record(const T *p, const std::string &key)
Definition TestStore.h:81
an "initializing" ToolHandle for stand-alone applications
StatusCode setProperty(const std::string &name, T2 &&value)
StatusCode retrieve()
initialize the tool, will succeed if the tool was already initialized
void setTypeAndName(const std::string &typeAndName)
ReadStats & stats()
Access the object belonging to the current thread.
Definition IOStats.cxx:17
static IOStats & instance()
Singleton object accessor.
Definition IOStats.cxx:11
float met() const
Returns .
void printSmartSlimmingBranchList(bool autoIncludeLinks=false) const
Print the accessed variables, formatted for smart slimming.
Tool for accessing xAOD files outside of Athena.
@ kClassAccess
Access auxiliary data using the aux containers.
A relatively simple transient store for objects created in analysis.
Definition TStore.h:45
uint64_t bitmask_t
Type for status word bit mask.
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
TestStore store
Definition TestStore.cxx:23
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
StatusCode buildMETSum(const std::string &totalName, xAOD::MissingETContainer *metCont)
@ Muon
The object is a muon.
Definition ObjectType.h:48
@ Electron
The object is an electron.
Definition ObjectType.h:46
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition Init.cxx:31
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
MissingET_v1 MissingET
Version control by type defintion.
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, const EventContext &ctx)
Function making a shallow copy of a constant container.
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...
MissingETAuxContainer_v1 MissingETAuxContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
MissingETAssociationMap_v1 MissingETAssociationMap
Version control by type defintion.
@ Track
Indicator for MET contribution from reconstructed charged particle tracks.