ATLAS Offline Software
Loading...
Searching...
No Matches
xAODFileReadTest.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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"
21
23static 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
36class TEventClass : public xAOD::TEvent {
37public:
40
42 StatusCode loadInputObjects() {
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( const 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 static constexpr bool METADATA = false;
78 if( ! this->contains( efe.branchName(), *ti, METADATA ) ) {
79 continue;
80 }
81 // Try to load the object/container:
82 static constexpr bool SILENT = false;
83 if( ! this->getInputObject( efe.branchName(), *ti, SILENT, METADATA ) ) {
84 Error( "TEventClass::loadInputObjects",
85 XAOD_MESSAGE( "Couldn't load object: %s" ),
86 efe.branchName().c_str() );
87 return StatusCode::FAILURE;
88 }
89 }
90 // Return gracefully:
91 return StatusCode::SUCCESS;
92 }
93}; // class TEventClass
94//coverity[root_function]
95int main( int argc, char* argv[] ) {
96
97 // Initialise the environment:
99
100 // Provide usage instructions if not enough options were given:
101 if( ( argc < 2 ) ||
102 ( ! strcmp( argv[ 1 ], "-h" ) ) ||
103 ( ! strcmp( argv[ 1 ], "--help" ) ) ) {
104 Info( APP_NAME, "Usage: %s [options] <file1> [file2]...", APP_NAME );
105 Info( APP_NAME, "Options:" );
106 Info( APP_NAME, " -m <access mode>" );
107 Info( APP_NAME, " 0: Branch access" );
108 Info( APP_NAME, " 1: Class access (default)" );
109 Info( APP_NAME, " 2: Athena access" );
110 return 1;
111 }
112
113 // Decode the command line options:
114 std::vector< std::string > fileNames;
116 for( int i = 1; i < argc; ++i ) {
117 if( ! strcmp( argv[ i ], "-m" ) ) {
118 if( i + 1 >= argc ) {
119 ::Error( APP_NAME,
120 XAOD_MESSAGE( "No access mode specified after -m" ) );
121 return 1;
122 } else {
123 const long mode = ::strtol( argv[ i + 1 ], 0, 10 );
124 auxMode = static_cast< xAOD::TEvent::EAuxMode >( mode );
125 ++i;
126 }
127 } else {
128 fileNames.push_back( argv[ i ] );
129 }
130 }
131
132 // Create the "TEvent" object used for the test:
133 TEventClass event( auxMode );
134
135 // Loop over the specified input files:
136 for( const std::string& fileName : fileNames ) {
137
138 // Open the file:
139 std::unique_ptr< TFile > ifile( TFile::Open( fileName.c_str(), "READ" ) );
140 if( ( ! ifile.get() ) || ifile->IsZombie() ) {
141 Error( APP_NAME, XAOD_MESSAGE( "Couldn't open file: %s" ),
142 fileName.c_str() );
143 return 1;
144 }
145 Info( APP_NAME, "Opened file: %s", fileName.c_str() );
146
147 // Give it to TEvent:
148 R_CHECK( event.readFrom( ifile.get() ) );
149
150 // Loop over the file's events:
151 const Long64_t entries = event.getEntries();
152 for( Long64_t entry = 0; entry < entries; ++entry ) {
153
154 // Load the event:
155 if( event.getEntry( entry ) < 0 ) {
156 Error( APP_NAME,
157 XAOD_MESSAGE( "Failed to load entry %i from file: %s" ),
158 static_cast< int >( entry ), fileName.c_str() );
159 return 1;
160 }
161
162 // Load the event:
163 R_CHECK( event.loadInputObjects() );
164
165 // Tell the user where we are:
166 if( ( entry % 1000 == 0 ) || ( entry +1 == entries ) ) {
167 Info( APP_NAME, "===>>> Loaded entry %i / %i <<<===",
168 static_cast< int >( entry ), static_cast< int >( entries ) );
169 }
170 }
171 }
172
173 // Return gracefully:
174 return 0;
175}
#define APP_NAME
#define XAOD_MESSAGE(MESSAGE)
Simple macro for printing error/verbose messages.
Class used for the test.
TEventClass(xAOD::TEvent::EAuxMode mode)
Constructor.
StatusCode loadInputObjects()
Function loading all interface objects of the event.
Class describing one branch of the ROOT file.
const std::string & branchName() const
Get the branch/key name.
const std::string & parentName() const
Get the name of the parent auxiliary object.
const std::string & className() const
Get the class name of this branch/key.
const EventFormat * inputEventFormat() const
Get information about the input objects.
bool contains(const std::string &key)
Function checking if an object is available from the store.
const void * getInputObject(SG::sgkey_t key, const std::type_info &ti, bool silent) override
Function for retrieving an input object in a non-template way.
Tool for accessing xAOD files outside of Athena.
@ kClassAccess
Access auxiliary data using the aux containers.
TEvent(EAuxMode mode=kClassAccess)
Default constructor.
int main()
Definition hello.cxx:18
double entries
Definition listroot.cxx:49
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition Init.cxx:31
EventFormat_v1 EventFormat
Definition of the current event format version.
Definition EventFormat.h:16
#define R_CHECK(EXP)
Helper macro.