ATLAS Offline Software
TTreeMgr.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 // 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( 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  if( ! m_event.contains( efe.branchName(), *ti ) ) {
230  continue;
231  }
232 
233  // If everything is right, let's make the branch:
234  auto br =
235  std::make_unique< xAODTEventBranch >( *m_eventTree, m_event, *ti,
236  efe.branchName().c_str(),
237  efe.className().c_str() );
238  m_eventTree->AddBranch( std::move( br ) );
239  }
240 
241  // Return the newly created object:
242  return m_eventTree.get();
243  }
244 
245  ::TTree* TTreeMgr::metaTree() {
246 
247  // If the tree is available, return it right away:
248  if( m_metaTree ) {
249  return m_metaTree.get();
250  }
251 
252  // Make sure that the persistent TTree is available:
253  if( ! m_event.m_inMetaTree ) {
254  ::Error( "xAOD::TTreeMgr::metaTree",
255  XAOD_MESSAGE( "Couldn't get the input file's metadata "
256  "tree. Did you use readFrom(...)?" ) );
257  return 0;
258  }
259 
260  {
261  // Remember which directory we are in now:
262  ::TDirectory* dir = gDirectory;
263 
264  // Create the transient tree:
265  gROOT->cd();
266  m_metaTree.reset( new xAODTMetaTree( m_event ) );
267 
268  // Go back to the original directory:
269  dir->cd();
270  }
271 
272  // Loop over the branches of the persistent metadata tree:
273  TObjArray* branches = m_event.m_inMetaTree->GetListOfBranches();
274  for( ::Int_t i = 0; i < branches->GetEntries(); ++i ) {
275 
276  // Access the branch object:
277  ::TBranch* br = dynamic_cast< ::TBranch* >( branches->At( i ) );
278  if( ! br ) {
279  ::Error( "xAOD::TTreeMgr::metaTree",
280  XAOD_MESSAGE( "Couldn't access branch %i as a TBranch" ),
281  i );
282  continue;
283  }
284 
285  // Skip the EventFormat branch. That must not be disturbed by the
286  // generic metadata handling.
287  if( ! strcmp( br->GetName(), "EventFormat" ) ) continue;
288 
289  // Apply the user specified filtering:
290  if( m_enableMetaObj.size() ) {
291  bool accept = false;
292  for( const std::string& name : m_enableMetaObj ) {
293  TRegexp re( name.c_str() );
294  Ssiz_t dummy = 0;
295  if( re.Index( br->GetName(), &dummy ) >= 0 ) {
296  accept = true;
297  break;
298  }
299  }
300  if( ! accept ) {
301  continue;
302  }
303  }
304  {
305  bool accept = true;
306  for( const std::string& name : m_suppressMetaObj ) {
307  TRegexp re( name.c_str() );
308  Ssiz_t dummy = 0;
309  if( re.Index( br->GetName(), &dummy ) >= 0 ) {
310  accept = false;
311  break;
312  }
313  }
314  if( ! accept ) {
315  continue;
316  }
317  }
318 
319  // Check whether we have a dictionary for this type:
320  ::TClass* cl = ::TClass::GetClass( br->GetClassName(), kTRUE, kTRUE );
321  if( ! cl ) continue;
322 
323  // Check if we have a type-info for this class:
324  const std::type_info* ti = cl->GetTypeInfo();
325  if( ! ti ) continue;
326 
327  // Don't add auxiliary store objects:
328  if( cl->InheritsFrom( "SG::IConstAuxStore" ) ) {
329  continue;
330  }
331 
332  // If everything is right, let's make the branch:
333  auto mbr =
334  std::make_unique< xAODTMetaBranch >( *m_metaTree, m_event, *ti,
335  br->GetName(),
336  br->GetClassName() );
337  m_metaTree->AddBranch( std::move( mbr ) );
338  }
339 
340  // Return the now created object:
341  return m_metaTree.get();
342  }
343 
345 
346  // Return a reference to the object:
347  return m_event;
348  }
349 
350 } // 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:39
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:245
xAOD::TEvent::m_inMetaTree
::TTree * m_inMetaTree
Pointer to the metadata tree in the input file.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:433
xAODTMetaTree
Transient TTree for interactive xAOD metadata access.
Definition: xAODTMetaTree.h:37
xAOD::TTreeMgr::TTreeMgr
TTreeMgr(TEvent::EAuxMode mode=TEvent::kUndefinedAccess)
Constructor, with an optional access mode selector.
Definition: TTreeMgr.cxx:24
runLayerRecalibration.branches
list branches
Definition: runLayerRecalibration.py:98
xAOD::TTreeMgr::m_event
TEvent m_event
The internal TEvent object.
Definition: TTreeMgr.h:85
xAOD::TEvent::m_inTree
::TTree * m_inTree
The main tree that we are reading from.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:422
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:1325
xAOD::TTreeMgr::m_metaTree
std::unique_ptr< xAODTMetaTree > m_metaTree
The transient metadata tree.
Definition: TTreeMgr.h:82
file
TFile * file
Definition: tile_monitor.h:29
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:20
xAOD::EventFormatElement::className
const std::string & className() const
Get the class name of this branch/key.
Definition: EventFormatElement.cxx:36
python.xAODType.dummy
dummy
Definition: xAODType.py:4
xAODTMetaBranch.h
Preparation.mode
mode
Definition: Preparation.py:94
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
xAOD::TEvent::contains
::Bool_t contains(const std::string &key)
Function checking if an object is available from the store.
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:623
xAOD::EventFormat_v1
Event format metadata for xAOD files.
Definition: EventFormat_v1.h:38
xAOD::TTreeMgr::event
TEvent & event()
Get the TEvent object being used by the transient tree(s)
Definition: TTreeMgr.cxx:344
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::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:98
re
const boost::regex re(r_e)
xAOD::TEvent::inputEventFormat
const EventFormat * inputEventFormat() const
Get information about the input objects.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1604
xAOD::TEvent::readFrom
StatusCode readFrom(::TFile *file, Bool_t useTreeCache=kTRUE, const char *treeName=EVENT_TREE_NAME)
Connect the object to a new input file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:365
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: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:84
PlotCalibFromCool.br
br
Definition: PlotCalibFromCool.py:355