ATLAS Offline Software
TTreeMgr.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 // Local include(s):
10 
11 // ROOT include(s):
12 #include <TTree.h>
13 #include <TClass.h>
14 #include <TDirectory.h>
15 #include <TFile.h>
16 #include <TROOT.h>
17 #include <TRegexp.h>
18 
19 // System include(s).
20 #include <memory>
21 
22 namespace xAOD {
23 
25  : m_eventTree(), m_metaTree(),
26  m_event( mode ), m_eventTreeName( "UNKNOWN" ),
27  m_enableEventObj(), m_suppressEventObj(),
28  m_enableMetaObj(), m_suppressMetaObj() {
29 
30  }
31 
32  StatusCode TTreeMgr::readFrom( ::TFile* file, ::Bool_t useTreeCache,
33  const char* treeName ) {
34 
35  // Delete the current transient tree(s):
36  m_eventTree.reset();
37  m_metaTree.reset();
38 
39  // Give the file to the TEvent object:
40  RETURN_CHECK( "xAOD::TTreeMgr::readFrom",
41  m_event.readFrom( file, useTreeCache, treeName ) );
42  // Load the first event from the file.
43  if( m_event.getEntry( 0 ) < 0 ) {
44  ::Error( "xAOD::TTreeMgr::readFrom",
45  "Couldn't load the first event from file \"%s\"",
46  file->GetName() );
47  return StatusCode::FAILURE;
48  }
49 
50  // Remember the event tree name:
52 
53  // Return gracefully:
54  return StatusCode::SUCCESS;
55  }
56 
58  TTreeMgr::enableEventObj( const std::vector< std::string >& names ) {
59 
60  // If the event tree already exists, this call will have no effect:
61  if( m_eventTree ) {
62  ::Warning( "xAOD::TTreeMgr::enableEventObj",
63  "Event tree already created, can't filter its contents "
64  "anymore" );
65  return StatusCode::RECOVERABLE;
66  }
67 
68  // Remember the selection:
69  m_enableEventObj = names;
70 
71  // Return gracefully:
72  return StatusCode::SUCCESS;
73  }
74 
76  TTreeMgr::suppressEventObj( const std::vector< std::string >& names ) {
77 
78  // If the event tree already exists, this call will have no effect:
79  if( m_eventTree ) {
80  ::Warning( "xAOD::TTreeMgr::suppressEventObj",
81  "Event tree already created, can't filter its contents "
82  "anymore" );
83  return StatusCode::RECOVERABLE;
84  }
85 
86  // Remember the selection:
87  m_suppressEventObj = names;
88 
89  // Return gracefully:
90  return StatusCode::SUCCESS;
91  }
92 
94  TTreeMgr::enableMetaObj( const std::vector< std::string >& names ) {
95 
96  // If the metadata tree already exists, this call will have no effect:
97  if( m_metaTree ) {
98  ::Warning( "xAOD::TTreeMgr::enableMetaObj",
99  "Metadata tree already created, can't filter its contents "
100  "anymore" );
101  return StatusCode::RECOVERABLE;
102  }
103 
104  // Remember the selection:
105  m_enableMetaObj = names;
106 
107  // Return gracefully:
108  return StatusCode::SUCCESS;
109  }
110 
111  StatusCode
112  TTreeMgr::suppressMetaObj( const std::vector< std::string >& names ) {
113 
114  // If the metadata tree already exists, this call will have no effect:
115  if( m_metaTree ) {
116  ::Warning( "xAOD::TTreeMgr::suppressMetaObj",
117  "Metadata tree already created, can't filter its contents "
118  "anymore" );
119  return StatusCode::RECOVERABLE;
120  }
121 
122  // Remember the selection:
123  m_suppressMetaObj = names;
124 
125  // Return gracefully:
126  return StatusCode::SUCCESS;
127  }
128 
129  ::TTree* TTreeMgr::eventTree() {
130 
131  // If the tree is available, return it right away:
132  if( m_eventTree ) {
133  return m_eventTree.get();
134  }
135 
136  // Get the input file's format:
138  if( ! ef ) {
139  ::Error( "xAOD::TTreeMgr::eventTree",
140  XAOD_MESSAGE( "Couldn't get the input file's format "
141  "metadata. Did you use readFrom(...)?" ) );
142  return 0;
143  }
144 
145  {
146  // Remember which directory we are in now:
147  ::TDirectory* dir = gDirectory;
148 
149  // Create the transient tree:
150  gROOT->cd();
151  m_eventTree.reset( new xAODTEventTree( m_event,
152  m_eventTreeName.c_str() ) );
153 
154  // Go back to the original directory:
155  dir->cd();
156  }
157 
158  // Create a branch for each xAOD interface object:
159  for( const auto& ef_itr : *ef ) {
160 
161  // Convenience reference:
162  const xAOD::EventFormatElement& efe = ef_itr.second;
163 
164  // Just skip all branches ending in "Aux.":
165  if( efe.branchName().rfind( "Aux." ) ==
166  ( efe.branchName().size() - 4 ) ) {
167  continue;
168  }
169  // Also skip dynamic branches:
170  if( efe.parentName() != "" ) {
171  continue;
172  }
173 
174  // Apply the user specified filtering:
175  if( m_enableEventObj.size() ) {
176  bool accept = false;
177  for( const std::string& name : m_enableEventObj ) {
178  TRegexp re( name.c_str() );
179  Ssiz_t dummy = 0;
180  if( re.Index( efe.branchName().c_str(), &dummy ) >= 0 ) {
181  accept = true;
182  break;
183  }
184  }
185  if( ! accept ) {
186  continue;
187  }
188  }
189  {
190  bool accept = true;
191  for( const std::string& name : m_suppressEventObj ) {
192  TRegexp re( name.c_str() );
193  Ssiz_t dummy = 0;
194  if( re.Index( efe.branchName().c_str(), &dummy ) >= 0 ) {
195  accept = false;
196  break;
197  }
198  }
199  if( ! accept ) {
200  continue;
201  }
202  }
203 
204  // Don't bother if the branch is not available:
205  if( ! m_event.m_inTree->GetBranch( efe.branchName().c_str() ) ) {
206  continue;
207  }
208 
209  // Check if the class in question is known:
210  ::TClass* cl = ::TClass::GetClass( efe.className().c_str(), kTRUE,
211  kTRUE );
212  if( ! cl ) continue;
213 
214  // Don't add auxiliary store objects:
215  if( cl->InheritsFrom( "SG::IConstAuxStore" ) ) {
216  continue;
217  }
218 
219  // Check if we have a type-info for this class:
220  const std::type_info* ti = cl->GetTypeInfo();
221  if( ! ti ) {
222  ::Warning( "xAOD::TTreeMgr::eventTree",
223  "Couldn't find std::type_info object for type %s "
224  "(key=%s)", cl->GetName(), efe.branchName().c_str() );
225  continue;
226  }
227 
228  // Check if the object is actually available on the input:
229  static constexpr bool METADATA = false;
230  if( ! m_event.contains( efe.branchName(), *ti, METADATA ) ) {
231  continue;
232  }
233 
234  // If everything is right, let's make the branch:
235  auto br =
236  std::make_unique< xAODTEventBranch >( *m_eventTree, m_event, *ti,
237  efe.branchName().c_str(),
238  efe.className().c_str() );
239  m_eventTree->AddBranch( std::move( br ) );
240  }
241 
242  // Return the newly created object:
243  return m_eventTree.get();
244  }
245 
246  ::TTree* TTreeMgr::metaTree() {
247 
248  // If the tree is available, return it right away:
249  if( m_metaTree ) {
250  return m_metaTree.get();
251  }
252 
253  // Make sure that the persistent TTree is available:
254  if( ! m_event.m_inMetaTree ) {
255  ::Error( "xAOD::TTreeMgr::metaTree",
256  XAOD_MESSAGE( "Couldn't get the input file's metadata "
257  "tree. Did you use readFrom(...)?" ) );
258  return 0;
259  }
260 
261  {
262  // Remember which directory we are in now:
263  ::TDirectory* dir = gDirectory;
264 
265  // Create the transient tree:
266  gROOT->cd();
267  m_metaTree.reset( new xAODTMetaTree( m_event ) );
268 
269  // Go back to the original directory:
270  dir->cd();
271  }
272 
273  // Loop over the branches of the persistent metadata tree:
274  TObjArray* branches = m_event.m_inMetaTree->GetListOfBranches();
275  for( ::Int_t i = 0; i < branches->GetEntries(); ++i ) {
276 
277  // Access the branch object:
278  ::TBranch* br = dynamic_cast< ::TBranch* >( branches->At( i ) );
279  if( ! br ) {
280  ::Error( "xAOD::TTreeMgr::metaTree",
281  XAOD_MESSAGE( "Couldn't access branch %i as a TBranch" ),
282  i );
283  continue;
284  }
285 
286  // Skip the EventFormat branch. That must not be disturbed by the
287  // generic metadata handling.
288  if( ! strcmp( br->GetName(), "EventFormat" ) ) continue;
289 
290  // Apply the user specified filtering:
291  if( m_enableMetaObj.size() ) {
292  bool accept = false;
293  for( const std::string& name : m_enableMetaObj ) {
294  TRegexp re( name.c_str() );
295  Ssiz_t dummy = 0;
296  if( re.Index( br->GetName(), &dummy ) >= 0 ) {
297  accept = true;
298  break;
299  }
300  }
301  if( ! accept ) {
302  continue;
303  }
304  }
305  {
306  bool accept = true;
307  for( const std::string& name : m_suppressMetaObj ) {
308  TRegexp re( name.c_str() );
309  Ssiz_t dummy = 0;
310  if( re.Index( br->GetName(), &dummy ) >= 0 ) {
311  accept = false;
312  break;
313  }
314  }
315  if( ! accept ) {
316  continue;
317  }
318  }
319 
320  // Check whether we have a dictionary for this type:
321  ::TClass* cl = ::TClass::GetClass( br->GetClassName(), kTRUE, kTRUE );
322  if( ! cl ) continue;
323 
324  // Check if we have a type-info for this class:
325  const std::type_info* ti = cl->GetTypeInfo();
326  if( ! ti ) continue;
327 
328  // Don't add auxiliary store objects:
329  if( cl->InheritsFrom( "SG::IConstAuxStore" ) ) {
330  continue;
331  }
332 
333  // If everything is right, let's make the branch:
334  auto mbr =
335  std::make_unique< xAODTMetaBranch >( *m_metaTree, m_event, *ti,
336  br->GetName(),
337  br->GetClassName() );
338  m_metaTree->AddBranch( std::move( mbr ) );
339  }
340 
341  // Return the now created object:
342  return m_metaTree.get();
343  }
344 
346 
347  // Return a reference to the object:
348  return m_event;
349  }
350 
351 } // namespace xAOD
xAOD::TTreeMgr::m_eventTree
std::unique_ptr< xAODTEventTree > m_eventTree
The transient event tree.
Definition: TTreeMgr.h:80
xAOD::TTreeMgr::m_enableEventObj
std::vector< std::string > m_enableEventObj
Names to select for the event tree.
Definition: TTreeMgr.h:91
RETURN_CHECK
#define RETURN_CHECK(CONTEXT, EXP)
Helper macro for checking return codes in a compact form in the code.
Definition: ReturnCheck.h:26
xAOD::name
name
Definition: TriggerMenuJson_v1.cxx:29
xAODTEventTree
Transient TTree for interactive xAOD event data access.
Definition: xAODTEventTree.h:37
CutsMETMaker::accept
StatusCode accept(const xAOD::Muon *mu)
Definition: CutsMETMaker.cxx:18
xAOD::TTreeMgr::m_suppressMetaObj
std::vector< std::string > m_suppressMetaObj
Names to suppress from the metadata tree.
Definition: TTreeMgr.h:98
xAOD::TTreeMgr::enableEventObj
StatusCode enableEventObj(const std::vector< std::string > &names)
Object/container names that should be used in the event tree.
Definition: TTreeMgr.cxx:58
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::TTreeMgr::eventTree
::TTree * eventTree()
Get a pointer to the transient event tree.
Definition: TTreeMgr.cxx:129
xAOD::EventFormatElement
Class describing one branch of the ROOT file.
Definition: EventFormatElement.h:35
xAODTEventBranch.h
XAOD_MESSAGE
#define XAOD_MESSAGE(MESSAGE)
Simple macro for printing error/verbose messages.
Definition: Control/xAODRootAccess/xAODRootAccess/tools/Message.h:19
xAOD::TTreeMgr::m_eventTreeName
std::string m_eventTreeName
The name of the event tree to create.
Definition: TTreeMgr.h:88
xAOD::EventFormatElement::branchName
const std::string & branchName() const
Get the branch/key name.
Definition: EventFormatElement.cxx:30
ReturnCheck.h
xAOD::TTreeMgr::metaTree
::TTree * metaTree()
Get a pointer to the transient metadata tree.
Definition: TTreeMgr.cxx:246
xAOD::TEvent::m_inMetaTree
::TTree * m_inMetaTree
Pointer to the metadata tree in the input file.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:204
xAODTMetaTree
Transient TTree for interactive xAOD metadata access.
Definition: xAODTMetaTree.h:37
runLayerRecalibration.branches
list branches
Definition: runLayerRecalibration.py:98
xAOD::TTreeMgr::m_event
TEvent m_event
The internal TEvent object.
Definition: TTreeMgr.h:85
xAOD::Event::inputEventFormat
const EventFormat * inputEventFormat() const
Get information about the input objects.
Definition: EventCore.cxx:228
xAOD::TEvent::m_inTree
::TTree * m_inTree
The main tree that we are reading from.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:193
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
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
xAOD::TEvent::getEntry
::Int_t getEntry(::Long64_t entry, ::Int_t getall=0)
Function loading a given entry of the input TTree.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:650
xAOD::TTreeMgr::m_metaTree
std::unique_ptr< xAODTMetaTree > m_metaTree
The transient metadata tree.
Definition: TTreeMgr.h:82
CalibDbCompareRT.dummy
dummy
Definition: CalibDbCompareRT.py:59
file
TFile * file
Definition: tile_monitor.h:29
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:19
xAOD::EventFormatElement::className
const std::string & className() const
Get the class name of this branch/key.
Definition: EventFormatElement.cxx:36
xAODTMetaBranch.h
Preparation.mode
mode
Definition: Preparation.py:107
xAOD::TTreeMgr::readFrom
StatusCode readFrom(::TFile *file, ::Bool_t useTreeCache=kTRUE, const char *treeName="CollectionTree")
Read from the file given to the function.
Definition: TTreeMgr.cxx:32
TTreeMgr.h
xAOD::TTreeMgr::suppressEventObj
StatusCode suppressEventObj(const std::vector< std::string > &names)
Object/container names that should be vetoed from the event tree.
Definition: TTreeMgr.cxx:76
beamspotman.dir
string dir
Definition: beamspotman.py:619
xAOD::EventFormat_v1
Event format metadata for xAOD files.
Definition: EventFormat_v1.h:38
xAOD::TEvent::readFrom
StatusCode readFrom(::TFile *file, bool useTreeCache=true, std::string_view treeName=EVENT_TREE_NAME)
Connect the object to a new input file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:122
xAOD::TTreeMgr::event
TEvent & event()
Get the TEvent object being used by the transient tree(s)
Definition: TTreeMgr.cxx:345
xAOD::Event::contains
bool contains(const std::string &key)
Function checking if an object is available from the store.
xAOD::TTreeMgr::suppressMetaObj
StatusCode suppressMetaObj(const std::vector< std::string > &names)
Object/container names that should be suppressed in the metadata tree.
Definition: TTreeMgr.cxx:112
xAOD::TTreeMgr::enableMetaObj
StatusCode enableMetaObj(const std::vector< std::string > &names)
Object/container names that should be used in the metadata tree.
Definition: TTreeMgr.cxx:94
xAOD::TTreeMgr::TTreeMgr
TTreeMgr(TEvent::EAuxMode mode=TEvent::kClassAccess)
Constructor, with an optional access mode selector.
Definition: TTreeMgr.cxx:24
xAOD::TTreeMgr::m_suppressEventObj
std::vector< std::string > m_suppressEventObj
Names to suppress from the event tree.
Definition: TTreeMgr.h:93
xAOD::TEvent::EAuxMode
EAuxMode
Auxiliary store "mode".
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:69
re
const boost::regex re(r_e)
xAOD::TTreeMgr::m_enableMetaObj
std::vector< std::string > m_enableMetaObj
Names to select for the metatada tree.
Definition: TTreeMgr.h:96
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:25
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:57
PlotCalibFromCool.br
br
Definition: PlotCalibFromCool.py:355