ATLAS Offline Software
RootD3PDSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
14 #include "RootD3PDSvc.h"
15 #include "RootD3PD.h"
17 #include "TTree.h"
18 #include "TROOT.h"
19 #include "TFile.h"
20 #include "TCollection.h"
21 #include <cstdlib>
22 
23 
24 namespace D3PD {
25 
26 
32 RootD3PDSvc::RootD3PDSvc (const std::string& name,
33  ISvcLocator* svcloc)
34  : base_class (name, svcloc),
35  m_histSvc ("THistSvc", name)
36 {
37  // See comments on cleanup().
38  std::atexit (cleanup);
39 
40  declareProperty ("HistSvc", m_histSvc);
41  declareProperty ("DoBranchRef", m_doBranchRef = true);
42  declareProperty ("MasterTree", m_masterTree = "CollectionTree");
43  declareProperty ("IndexMajor", m_indexMajor = "RunNumber");
44  declareProperty ("IndexMinor", m_indexMinor = "EventNumber");
45  declareProperty ("BasketSize", m_basketSize = 32768);
46  declareProperty ("EntryOffsetLen", m_entryOffsetLen = 512);
47  declareProperty ("AutoFlush", m_autoFlush = -1,
48  "Value to set for ROOT's AutoFlush parameter. "
49  "(Tells how often the tree baskets will be flushed.) "
50  "0 disables flushing. "
51  "-1 (default) makes no changes to what THistSvc did. "
52  "Any other negative number gives the number of bytes "
53  "after which to flush. "
54  "A positive number gives the number of entries after which "
55  "to flush.");
56  declareProperty ("AllowedNames", m_allowedNames,
57  "Variable names allowed in the output D3PDs. An empty list "
58  "means that all variable names are allowed. Regular expressions "
59  "are allowed.");
60  declareProperty ("VetoedNames", m_vetoedNames,
61  "Variable names that are not allowed to end up in the "
62  "created D3PDs. Regular expressions are allowed.");
63 }
64 
65 
81 {
82  // Sometimes gDirectory is invalid at this point...
83  gDirectory = gROOT;
84  TIter it (gROOT->GetListOfFiles());
85  while (TObject* o = it.Next()) {
86  if (TFile* f = dynamic_cast<TFile*> (o))
87  f->Close();
88  }
89 }
90 
91 
96 {
98  CHECK( m_histSvc.retrieve() );
99  return StatusCode::SUCCESS;
100 }
101 
102 
107 {
108  return StatusCode::SUCCESS;
109 }
110 
111 
116 {
117  // Run through all the trees we've made.
118  for (size_t i = 0; i < m_d3pds.size(); i++) {
119  RootD3PD* d3pd = m_d3pds[i].get();
120 
121  // Make an index if requested.
122  if (!m_indexMajor.empty())
123  d3pd->tree()->BuildIndex (m_indexMajor.c_str(), m_indexMinor.c_str());
124 
125  // Was there a master tree specified?
126  if (!d3pd->master().empty()) {
127  // Yes --- try to find it
128  TDirectory* dir = d3pd->tree()->GetDirectory();
129  TTree* master =
130  dynamic_cast<TTree*> (dir->Get (d3pd->master().c_str()));
131  if (!master && d3pd->tree()->GetEntries() > 0)
132  CHECK( m_histSvc->getTree (d3pd->master(), master) );
133  if (master) {
134  // Make an index for the master if needed.
135  if (!master->GetTreeIndex()) {
136  // AANTupleStream will leave branch addresses in the master
137  // tree pointing at dead objects.
138  master->ResetBranchAddresses();
139 
140  master->BuildIndex (m_indexMajor.c_str(), m_indexMinor.c_str());
141  }
142 
143  // Make this tree a friend of the master.
144  master->AddFriend (d3pd->tree());
145  }
146  }
147 
148  {
149  TDirectory::TContext ctx (gDirectory, d3pd->tree()->GetDirectory());
150  d3pd->tree()->Write();
151  }
152  }
153 
154  // Get rid of the RootD3PD wrappers.
155  // (Doesn't delete the root trees themselves.)
156  m_d3pds.clear();
157 
158  return StatusCode::SUCCESS;
159 }
160 
161 
175 StatusCode RootD3PDSvc::make (const std::string& name, ID3PD* & d3pd)
176 {
177  std::string tname = name;
178  std::string::size_type ipos = name.rfind ('/');
179  std::string master = m_masterTree;
180  std::string poolfile;
181  if (ipos != std::string::npos) {
182  tname = name.substr (ipos+1);
183  if (!master.empty()) {
184  std::string sname = name.substr (0, ipos+1);
185  std::string::size_type jpos = sname.find (':');
186  if (sname.substr (0, jpos) == "pool" ||
187  sname.substr (0, jpos) == "/pool")
188  {
189  poolfile = sname.substr (jpos+1, std::string::npos);
190  if (!poolfile.empty() && poolfile[poolfile.size()-1] == '/')
191  poolfile.erase (poolfile.size()-1);
192  }
193  else {
194  if (jpos != std::string::npos){
195  sname.erase(jpos,sname.size()-jpos-1);
196  }
197  master = sname + master;
198  }
199  }
200  }
201  TTree* tree = new TTree (tname.c_str(), tname.c_str());
202 
203  if (m_doBranchRef)
204  tree->BranchRef();
205  if (m_autoFlush != -1)
206  tree->SetAutoFlush (m_autoFlush);
207  auto rd3pd = std::make_unique<RootD3PD>
208  (tree, master,
211 
212  if (!poolfile.empty())
213  rd3pd->setPoolFile (poolfile);
214  else
215  CHECK( m_histSvc->regTree (name, tree) );
216 
217  d3pd = rd3pd.get();
218  m_d3pds.push_back (std::move (rd3pd));
219  return StatusCode::SUCCESS;
220 }
221 
222 
223 } // namespace D3PD
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
D3PD::RootD3PD::tree
const TTree * tree() const
Return the underlying root tree.
Definition: RootD3PD.cxx:676
D3PD::ID3PD
Define an abstract interface for building a D3PD tree.
Definition: ID3PD.h:37
D3PD::RootD3PDSvc::m_histSvc
ServiceHandle< ITHistSvc > m_histSvc
Property: Gaudi THistSvc.
Definition: RootD3PDSvc.h:110
initialize
void initialize()
Definition: run_EoverP.cxx:894
tree
TChain * tree
Definition: tile_monitor.h:30
skel.it
it
Definition: skel.GENtoEVGEN.py:423
D3PD::RootD3PDSvc::m_vetoedNames
std::vector< std::string > m_vetoedNames
Property: Vetoed names for the created D3PD.
Definition: RootD3PDSvc.h:107
D3PD::RootD3PD
Root-based D3PD tree.
Definition: RootD3PD.h:52
D3PD::RootD3PDSvc::m_autoFlush
long long m_autoFlush
Property: Value to set for ROOT's AutoFlush parameter.
Definition: RootD3PDSvc.h:101
RootD3PD.h
Root-based D3PD tree.
master
Definition: master.py:1
D3PD::RootD3PDSvc::m_allowedNames
std::vector< std::string > m_allowedNames
Property: Allowed names for the created D3PD.
Definition: RootD3PDSvc.h:104
D3PD
Block filler tool for noisy FEB information.
Definition: InnerDetector/InDetMonitoring/InDetGlobalMonitoring/macros/EnhancedPrimaryVertexMonitoring/TrigD3PD/ChainGroup.h:21
D3PD::RootD3PDSvc::finalize
virtual StatusCode finalize() override
Standard Gaudi finalize method.
Definition: RootD3PDSvc.cxx:106
D3PD::RootD3PDSvc::m_d3pds
std::vector< std::unique_ptr< RootD3PD > > m_d3pds
Remember all the tuples we've made.
Definition: RootD3PDSvc.h:113
D3PD::RootD3PDSvc::initialize
virtual StatusCode initialize() override
Standard Gaudi initialize method.
Definition: RootD3PDSvc.cxx:95
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
lumiFormat.i
int i
Definition: lumiFormat.py:92
D3PD::RootD3PDSvc::stop
virtual StatusCode stop() override
Standard Gaudi stop method.
Definition: RootD3PDSvc.cxx:115
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
D3PD::RootD3PDSvc::m_basketSize
int m_basketSize
Property: Basket buffer size, or -1 to use the Root default.
Definition: RootD3PDSvc.h:90
RootD3PDSvc.h
Service to create a Root-based D3PD tree.
D3PD::RootD3PDSvc::m_indexMinor
std::string m_indexMinor
Property: Minor variable name for index making, or null.
Definition: RootD3PDSvc.h:87
D3PD::RootD3PDSvc::m_doBranchRef
bool m_doBranchRef
Property: If true, then add BranchRef info to the tuple.
Definition: RootD3PDSvc.h:77
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
D3PD::RootD3PD::master
const std::string & master() const
Return the name of the master tree.
Definition: RootD3PD.cxx:690
beamspotman.dir
string dir
Definition: beamspotman.py:623
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
errorcheck.h
Helpers for checking error return status codes and reporting errors.
D3PD::RootD3PDSvc::RootD3PDSvc
RootD3PDSvc(const std::string &name, ISvcLocator *svcloc)
Constructor.
Definition: RootD3PDSvc.cxx:32
D3PD::RootD3PDSvc::make
virtual StatusCode make(const std::string &name, ID3PD *&d3pd) override
Create a new D3PD tree.
Definition: RootD3PDSvc.cxx:175
D3PD::RootD3PDSvc::m_entryOffsetLen
int m_entryOffsetLen
Property: Basket entry offset buffer size, or -1 to use the Root default.
Definition: RootD3PDSvc.h:93
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
D3PD::RootD3PDSvc::m_masterTree
std::string m_masterTree
Property: Name of the master tree.
Definition: RootD3PDSvc.h:82
D3PD::RootD3PDSvc::cleanup
static void cleanup()
Make sure all files are closed before exiting, to prevent crashes.
Definition: RootD3PDSvc.cxx:80
D3PD::RootD3PDSvc::m_indexMajor
std::string m_indexMajor
Property: Major variable name for index making, or null.
Definition: RootD3PDSvc.h:85