ATLAS Offline Software
xAODFileReadTest.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 // System include(s):
7 #include <cstring>
8 #include <memory>
9 #include <vector>
10 #include <string>
11 
12 // ROOT include(s):
13 #include <TFile.h>
14 #include <TError.h>
15 #include <TClass.h>
16 
17 // Local include(s):
18 #include "xAODRootAccess/Init.h"
19 #include "xAODRootAccess/TEvent.h"
21 
23 static const char* const APP_NAME = "xAODFileReadTest";
24 
26 #define R_CHECK( EXP ) \
27  do { \
28  const auto result = EXP; \
29  if( ! result.isSuccess() ) { \
30  Error( APP_NAME, XAOD_MESSAGE( "Failed to execute: %s" ), #EXP ); \
31  return 1; \
32  } \
33  } while( 0 )
34 
36 class TEventClass : public xAOD::TEvent {
37 public:
40 
43  // Get the event format object:
44  const xAOD::EventFormat* ef = this->inputEventFormat();
45  if( ! ef ) {
46  return StatusCode::FAILURE;
47  }
48  // Loop over the objects of the file:
49  for( auto ef_itr : *ef ) {
50  // A helper object:
51  const xAOD::EventFormatElement& efe = ef_itr.second;
52  // Skip auxiliary objects:
53  if( efe.branchName().rfind( "Aux." ) ==
54  ( efe.branchName().size() - 4 ) ) {
55  continue;
56  }
57  // And dynamic variables:
58  if( efe.parentName() != "" ) {
59  continue;
60  }
61  // Skip auxiliary stores with non-standard names:
62  ::TClass* cl = ::TClass::GetClass( efe.className().c_str(), kTRUE,
63  kTRUE );
64  if( ! cl ) continue;
65  if( cl->InheritsFrom( "SG::IConstAuxStore" ) ) {
66  continue;
67  }
68  // Get the type of the branch:
69  const std::type_info* ti = cl->GetTypeInfo();
70  if( ! ti ) {
71  ::Warning( "TEventClass::loadInputObjects",
72  "Couldn't find std::type_info object for type %s "
73  "(key=%s)", cl->GetName(), efe.branchName().c_str() );
74  continue;
75  }
76  // Check if the branch exists in the file:
77  if( ! this->contains( efe.branchName(), *ti ) ) {
78  continue;
79  }
80  // Try to load the object/container:
81  if( ! this->getInputObject( efe.branchName(), *ti ) ) {
82  Error( "TEventClass::loadInputObjects",
83  XAOD_MESSAGE( "Couldn't load object: %s" ),
84  efe.branchName().c_str() );
85  return StatusCode::FAILURE;
86  }
87  }
88  // Return gracefully:
89  return StatusCode::SUCCESS;
90  }
91 }; // class TEventClass
92 
93 int main( int argc, char* argv[] ) {
94 
95  // Initialise the environment:
96  R_CHECK( xAOD::Init() );
97 
98  // Provide usage instructions if not enough options were given:
99  if( ( argc < 2 ) ||
100  ( ! strcmp( argv[ 1 ], "-h" ) ) ||
101  ( ! strcmp( argv[ 1 ], "--help" ) ) ) {
102  Info( APP_NAME, "Usage: %s [options] <file1> [file2]...", APP_NAME );
103  Info( APP_NAME, "Options:" );
104  Info( APP_NAME, " -m <access mode>" );
105  Info( APP_NAME, " 0: Branch access" );
106  Info( APP_NAME, " 1: Class access (default)" );
107  Info( APP_NAME, " 2: Athena access" );
108  return 1;
109  }
110 
111  // Decode the command line options:
112  std::vector< std::string > fileNames;
114  for( int i = 1; i < argc; ++i ) {
115  if( ! strcmp( argv[ i ], "-m" ) ) {
116  if( i + 1 >= argc ) {
117  ::Error( APP_NAME,
118  XAOD_MESSAGE( "No access mode specified after -m" ) );
119  return 1;
120  } else {
121  const long mode = ::strtol( argv[ i + 1 ], 0, 10 );
122  auxMode = static_cast< xAOD::TEvent::EAuxMode >( mode );
123  ++i;
124  }
125  } else {
126  fileNames.push_back( argv[ i ] );
127  }
128  }
129 
130  // Create the "TEvent" object used for the test:
131  TEventClass event( auxMode );
132 
133  // Loop over the specified input files:
134  for( const std::string& fileName : fileNames ) {
135 
136  // Open the file:
137  std::unique_ptr< TFile > ifile( TFile::Open( fileName.c_str(), "READ" ) );
138  if( ( ! ifile.get() ) || ifile->IsZombie() ) {
139  Error( APP_NAME, XAOD_MESSAGE( "Couldn't open file: %s" ),
140  fileName.c_str() );
141  return 1;
142  }
143  Info( APP_NAME, "Opened file: %s", fileName.c_str() );
144 
145  // Give it to TEvent:
146  R_CHECK( event.readFrom( ifile.get() ) );
147 
148  // Loop over the file's events:
149  const Long64_t entries = event.getEntries();
150  for( Long64_t entry = 0; entry < entries; ++entry ) {
151 
152  // Load the event:
153  if( event.getEntry( entry ) < 0 ) {
154  Error( APP_NAME,
155  XAOD_MESSAGE( "Failed to load entry %i from file: %s" ),
156  static_cast< int >( entry ), fileName.c_str() );
157  return 1;
158  }
159 
160  // Load the event:
161  R_CHECK( event.loadInputObjects() );
162 
163  // Tell the user where we are:
164  if( ( entry % 1000 == 0 ) || ( entry +1 == entries ) ) {
165  Info( APP_NAME, "===>>> Loaded entry %i / %i <<<===",
166  static_cast< int >( entry ), static_cast< int >( entries ) );
167  }
168  }
169  }
170 
171  // Return gracefully:
172  return 0;
173 }
R_CHECK
#define R_CHECK(EXP)
Helper macro.
Definition: xAODFileReadTest.cxx:26
checkxAOD.fileNames
fileNames
Definition: Tools/PyUtils/bin/checkxAOD.py:73
xAOD::TEvent::getInputObject
const void * getInputObject(SG::sgkey_t key, const std::type_info &ti, bool silent=false) override
Function for retrieving an input object in a non-template way.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1938
main
int main(int argc, char *argv[])
Definition: xAODFileReadTest.cxx:93
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
xAOD::EventFormatElement::parentName
const std::string & parentName() const
Get the name of the parent auxiliary object.
Definition: EventFormatElement.cxx:42
xAOD::EventFormatElement
Class describing one branch of the ROOT file.
Definition: EventFormatElement.h:39
XAOD_MESSAGE
#define XAOD_MESSAGE(MESSAGE)
Simple macro for printing error/verbose messages.
Definition: Control/xAODRootAccess/xAODRootAccess/tools/Message.h:19
TEventClass
Class used for the test.
Definition: xAODFileReadTest.cxx:36
LArCellConditions.argv
argv
Definition: LArCellConditions.py:112
xAOD::EventFormatElement::branchName
const std::string & branchName() const
Get the branch/key name.
Definition: EventFormatElement.cxx:30
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:97
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:133
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
POOL::TEvent::getEntry
int getEntry(long entry)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:185
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Message.h
TEventClass::TEventClass
TEventClass(xAOD::TEvent::EAuxMode mode)
Constructor.
Definition: xAODFileReadTest.cxx:39
APP_NAME
#define APP_NAME
Definition: BoostedXbbTag.cxx:23
TEvent.h
xAOD::EventFormatElement::className
const std::string & className() const
Get the class name of this branch/key.
Definition: EventFormatElement.cxx:36
Init.h
Preparation.mode
mode
Definition: Preparation.py:94
xAOD::TEvent::contains
::Bool_t contains(const std::string &key)
Function checking if an object is available from the store.
run_gep.ifile
ifile
Definition: run_gep.py:65
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
xAOD::EventFormat_v1
Event format metadata for xAOD files.
Definition: EventFormat_v1.h:38
TEventClass::loadInputObjects
StatusCode loadInputObjects()
Function loading all interface objects of the event.
Definition: xAODFileReadTest.cxx:42
xAOD::TEvent::TEvent
TEvent(EAuxMode mode=kUndefinedAccess)
Default constructor.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:131
xAOD::TEvent::EAuxMode
EAuxMode
Auxiliary store "mode".
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:95
xAOD::TEvent::inputEventFormat
const EventFormat * inputEventFormat() const
Get information about the input objects.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1603
entries
double entries
Definition: listroot.cxx:49
L1Topo::Error
Error
The different types of error that can be flagged in the L1TopoRDO.
Definition: Error.h:16
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:26
LheEventFiller_Common.ef
ef
Definition: SFGen_i/share/common/LheEventFiller_Common.py:7
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