ATLAS Offline Software
Loading...
Searching...
No Matches
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$
12
13
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
24namespace D3PD {
25
26
32RootD3PDSvc::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{
97 CHECK( AthService::initialize() );
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
175StatusCode 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
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
Service to create a Root-based D3PD tree.
Root-based D3PD tree.
Define an abstract interface for building a D3PD tree.
Definition ID3PD.h:37
virtual StatusCode stop() override
Standard Gaudi stop method.
virtual StatusCode initialize() override
Standard Gaudi initialize method.
std::string m_indexMinor
Property: Minor variable name for index making, or null.
Definition RootD3PDSvc.h:87
virtual StatusCode finalize() override
Standard Gaudi finalize method.
int m_entryOffsetLen
Property: Basket entry offset buffer size, or -1 to use the Root default.
Definition RootD3PDSvc.h:93
RootD3PDSvc(const std::string &name, ISvcLocator *svcloc)
Constructor.
static void cleanup()
Make sure all files are closed before exiting, to prevent crashes.
bool m_doBranchRef
Property: If true, then add BranchRef info to the tuple.
Definition RootD3PDSvc.h:77
std::vector< std::string > m_allowedNames
Property: Allowed names for the created D3PD.
ServiceHandle< ITHistSvc > m_histSvc
Property: Gaudi THistSvc.
std::vector< std::string > m_vetoedNames
Property: Vetoed names for the created D3PD.
long long m_autoFlush
Property: Value to set for ROOT's AutoFlush parameter.
int m_basketSize
Property: Basket buffer size, or -1 to use the Root default.
Definition RootD3PDSvc.h:90
std::vector< std::unique_ptr< RootD3PD > > m_d3pds
Remember all the tuples we've made.
virtual StatusCode make(const std::string &name, ID3PD *&d3pd) override
Create a new D3PD tree.
std::string m_indexMajor
Property: Major variable name for index making, or null.
Definition RootD3PDSvc.h:85
std::string m_masterTree
Property: Name of the master tree.
Definition RootD3PDSvc.h:82
Root-based D3PD tree.
Definition RootD3PD.h:52
const TTree * tree() const
Return the underlying root tree.
Definition RootD3PD.cxx:676
const std::string & master() const
Return the name of the master tree.
Definition RootD3PD.cxx:690
Block filler tool for noisy FEB information.
TChain * tree