ATLAS Offline Software
JetCalibTools_Example.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 /* **********************************************************************\
6  * *
7  * Name: JetCalibTools_Example *
8  * Purpose: Example code for JetCalibTools *
9  * *
10  * # Date Comments By *
11  * -- -------- -------------------------- ----------------------------- *
12  * 1 14/03/16 First Version J. Bossio (jbossios@cern.ch) *
13 \************************************************************************/
14 
15 // System include(s):
16 #include <memory>
17 
18 #include "AsgTools/ToolHandle.h"
19 #include "AsgTools/AsgTool.h"
20 
21 // ROOT
22 #include "TFile.h"
23 
24 #ifdef XAOD_STANDALONE
25 #include "xAODRootAccess/Init.h"
26 #include "xAODRootAccess/TEvent.h"
27 #include "xAODRootAccess/TStore.h"
31 #else
32 #include "POOLRootAccess/TEvent.h"
33 #include "StoreGate/StoreGateSvc.h"
34 #endif
35 
36 //xAOD EDM classes
38 #include "xAODJet/JetContainer.h"
42 #include "xAODCore/tools/IOStats.h"
44 #include "xAODCore/ShallowCopy.h"
45 
46 //JetCalibrationTool
48 
49 void usage() {
50  std::cout << "Running options:" << std::endl;
51  std::cout << " --help : To get the help you're reading" << std::endl;
52  std::cout << " --jetCalibConfig= : Specify the JetCalibTools config" << std::endl;
53  std::cout << " --jetColl= : Specify the jet collection" << std::endl;
54  std::cout << " --calibSeq= : Specify the calibration sequence for JetCalibTools" << std::endl;
55  std::cout << " --calibArea : Specify the calibration area for JetCalibTools" << std::endl;
56  std::cout << " --isData=TRUE : Specify isData true for JetCalibTools" << std::endl;
57  std::cout << " --isData=FALSE : Specify isData false for JetCalibTools" << std::endl;
58  std::cout << " --sample= : Specify input xAOD" << std::endl;
59  std::cout << " Example: Example --jetCalibConfig=JES_2015dataset_recommendation_Feb2016.config --jetColl=AntiKt4EMTopo --calibSeq=JetArea_Residual_Origin_EtaJES_GSC --isData=FALSE --sample=xAOD.root" << std::endl;
60 }
61 
62 //---------------
63 // Main Function
64 //---------------
65 
66 int main(int argc, char* argv[]){
67 
68  using namespace asg::msgUserCode;
69  ANA_CHECK_SET_TYPE (int);
70 
71  //---------------------------------------------
72  // Declaring input variables with default values
73  //---------------------------------------------
74  std::string isData = "";
75  std::string sample = "";
76  std::string jetColl = "";
77  std::string jetCalibConfig = "";
78  std::string calibSeq = "";
79  std::string calibArea = "";
80  bool isCollision = false;
81 
82  //---------------------------
83  // Decoding the user settings
84  //---------------------------
85  for (int i=1; i< argc; i++){
86 
87  std::string opt(argv[i]); std::vector< std::string > v;
88 
89  std::istringstream iss(opt);
90 
91  std::string item;
92  char delim = '=';
93 
94  while (std::getline(iss, item, delim)){
95  v.push_back(item);
96  }
97 
98  if ( opt.find("--help") != std::string::npos ) {
99  usage(); return 0;
100  }
101 
102  if ( opt.find("--sample=") != std::string::npos ) sample = v[1];
103 
104  if ( opt.find("--jetColl=") != std::string::npos ) jetColl = v[1];
105 
106  if ( opt.find("--jetCalibConfig=") != std::string::npos ) jetCalibConfig = v[1];
107 
108  if ( opt.find("--calibSeq=") != std::string::npos ) calibSeq = v[1];
109 
110  if ( opt.find("--calibArea=") != std::string::npos ) calibArea = v[1];
111 
112  if ( opt.find("--isData=") != std::string::npos ) isData = v[1];
113 
114  }//End: Loop over input options
115 
116  if(sample.empty()){
117  std::cout << "No input xAOD file specified, exiting" << std::endl;
118  return 1;
119  }
120  if(jetColl.empty()){
121  std::cout << "No jet collection specified, exiting" << std::endl;
122  return 1;
123  }
124  if(jetCalibConfig.empty()){
125  std::cout << "No JetCalibTools config specified, exiting" << std::endl;
126  return 1;
127  }
128  if(calibSeq.empty()){
129  std::cout << "No calibration sequence specified, exiting" << std::endl;
130  return 1;
131  }
132  if(isData.empty()){
133  std::cout << "isData not specified, exiting" << std::endl;
134  return 1;
135  }
136  else if(isData=="TRUE") isCollision = true;
137 
138  //--------------------
139  // Opening input file
140  //--------------------
141  std::unique_ptr< TFile > ifile( TFile::Open( sample.c_str(), "READ" ) );
142 
143  // Create a TEvent object.
144 #ifdef XAOD_STANDALONE
145  ANA_CHECK( xAOD::Init() );
147  ANA_CHECK( event.readFrom( ifile.get() ) );
148  ANA_MSG_WARNING(calibSeq);
149  // Create ONNX service for LargeRDNN calibration
150  asg::AsgServiceConfig config ("AthOnnx::IOnnxRuntimeSvc/OnnxRuntimeSvc");
151  std::shared_ptr<AthOnnx::IOnnxRuntimeSvc> service;
152  ANA_CHECK(config.makeService (service));
153 #else // Athena "Store" is the same StoreGate used by the TEvent
155  ANA_CHECK( event.readFrom( ifile.get() ) );
156 #endif
157 
158  //----------------------------------
159  // Initialization of JetCalibTools
160  //----------------------------------
161  const std::string name_JetCalibTools = "JetCalib_Example";
162 
163  // Call the constructor
164  JetCalibrationTool jetCalibrationTool(name_JetCalibTools.c_str());
165  ANA_CHECK( jetCalibrationTool.setProperty("JetCollection",jetColl.c_str()) );
166 
167  ANA_CHECK( jetCalibrationTool.setProperty("CalibSequence",calibSeq.c_str()) );
168 
169  ANA_CHECK( jetCalibrationTool.setProperty("ConfigFile",jetCalibConfig.c_str()) );
170 
171  ANA_CHECK( jetCalibrationTool.setProperty("IsData",isCollision) );
172 
173  if(!calibArea.empty()){
174  ANA_CHECK( jetCalibrationTool.setProperty("CalibArea",calibArea.c_str()) );
175  }
176 
177  // Initialize the tool
178  if(!(jetCalibrationTool.initialize().isSuccess())){
179  std::cout << "Initialization of JetCalibTools failed, exiting" << std::endl;
180  return 0;
181  }
182 
183  //------------------
184  // Loop over events
185  //------------------
186 
187  const Long64_t nevents = event.getEntries();
188  for(Long64_t ievent = 0; ievent < nevents; ++ievent){
189 
190  // Load the event:
191  if( event.getEntry( ievent ) < 0 ) {
192  std::cerr << "Failed to load entry " << ievent << std::endl;
193  return 1;
194  }
195 
196  // Show status
197  if(ievent % 100==0) std::cout << "Event " << ievent << " of " << nevents << std::endl;
198 
199  // Retrieve jet container
200  const xAOD::JetContainer* jets = nullptr;
201  ANA_CHECK( event.retrieve( jets, jetColl + "Jets" ) );
202 
203  // Shallow copy
204  auto jets_shallowCopy = xAOD::shallowCopyContainer( *jets );
205 
206  // Calibrate the shallow copy
207  ANA_CHECK( jetCalibrationTool.applyCalibration( *(jets_shallowCopy.first) ) );
208 
209  delete jets_shallowCopy.first;
210  delete jets_shallowCopy.second;
211 
212  }//END: Loop over events
213 
215 
216  return 0;
217 }
ShallowCopy.h
TestSUSYToolsAlg.ifile
ifile
Definition: TestSUSYToolsAlg.py:92
xAOD::IOStats::stats
ReadStats & stats()
Access the object belonging to the current thread.
Definition: IOStats.cxx:17
EventShape.h
JetCalibrationTool::initialize
StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: JetCalibrationTool.cxx:57
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
LArCellConditions.argv
argv
Definition: LArCellConditions.py:112
IOStats.h
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:97
ReturnCheck.h
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
POOL::TEvent::kClassAccess
@ kClassAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:45
PyPoolBrowser.item
item
Definition: PyPoolBrowser.py:129
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:132
JetCalibrationTool.h
ReadStats.h
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:100
POOL::TEvent::getEntry
int getEntry(long entry)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:184
lumiFormat.i
int i
Definition: lumiFormat.py:92
usage
void usage()
Definition: JetCalibTools_Example.cxx:49
Message.h
MessageCheck.h
macros for messaging and checking status codes
JetCalibrationTool
Definition: JetCalibrationTool.h:34
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
TEvent.h
Init.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
xAOD::IOStats::instance
static IOStats & instance()
Singleton object accessor.
Definition: IOStats.cxx:11
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::ReadStats::printSmartSlimmingBranchList
void printSmartSlimmingBranchList(bool autoIncludeLinks=false) const
Print the accessed variables, formatted for smart slimming.
POOL::TEvent
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:39
TEvent.h
pmontree.opt
opt
Definition: pmontree.py:16
AsgServiceConfig.h
item
Definition: ItemListSvc.h:43
asg::AsgServiceConfig
an object that can create a AsgService
Definition: AsgServiceConfig.h:25
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
EventInfo.h
JetCalibrationTool::applyCalibration
StatusCode applyCalibration(xAOD::JetContainer &) const override
Apply calibration to a jet container.
Definition: JetCalibrationTool.cxx:338
IOnnxRuntimeSvc.h
python.PyAthena.v
v
Definition: PyAthena.py:157
main
int main(int argc, char *argv[])
Definition: JetCalibTools_Example.cxx:66
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
POOL::TEvent::retrieve
StatusCode retrieve(const T *&obj)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:73
config
std::vector< std::string > config
Definition: fbtTestBasics.cxx:72
python.grid.isData
def isData(dataset)
Definition: grid.py:491
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
LArG4GenerateShowerLib.nevents
nevents
Definition: LArG4GenerateShowerLib.py:19
ToolHandle.h
AsgTool.h
StoreGateSvc.h
TStore.h
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:81
xAOD::Init
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition: Init.cxx:31