The Gaudi Framework  v33r0 (d5ea422b)
THistSvc.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDISVC_THISTSVC_H
12 #define GAUDISVC_THISTSVC_H
13 
14 // system includes:
15 #include <map>
16 #include <set>
17 #include <string>
18 #include <vector>
19 
20 // Gaudi includes:
21 #include "GaudiKernel/IFileMgr.h"
24 #include "GaudiKernel/ITHistSvc.h"
25 #include "GaudiKernel/MsgStream.h"
26 #include "GaudiKernel/Service.h"
27 
28 // ROOT includes:
29 #include "TEfficiency.h"
30 #include "TGraph.h"
31 #include "TH1.h"
32 #include "TH2.h"
33 #include "TH3.h"
34 #include "TList.h"
35 #include "TObject.h"
36 #include "TTree.h"
37 
38 class IIncidentSvc;
39 
40 class THistSvc : public extends<Service, ITHistSvc, IIncidentListener, IIoComponent> {
41 public:
42  using extends::extends;
43 
44  StatusCode initialize() override;
45  StatusCode reinitialize() override;
46  StatusCode finalize() override;
47 
48 public:
49  // Methods from ITHistSvc
52 
54  StatusCode regHist( const std::string& name ) override;
58  StatusCode regHist( const std::string& name, std::unique_ptr<TH1> hist ) override;
63  StatusCode regHist( const std::string& name, std::unique_ptr<TH1> hist, TH1* hist_ptr ) override;
66  StatusCode regHist( const std::string& name, TH1* ) override;
67 
69  StatusCode getHist( const std::string& name, TH1*&, size_t index = 0 ) const override;
71  StatusCode getHist( const std::string& name, TH2*&, size_t index = 0 ) const override;
73  StatusCode getHist( const std::string& name, TH3*&, size_t index = 0 ) const override;
74 
76 
79 
81  StatusCode regTree( const std::string& name ) override;
86  StatusCode regTree( const std::string& name, TTree* ) override;
88  StatusCode getTree( const std::string& name, TTree*& ) const override;
89 
91 
94 
96  StatusCode regGraph( const std::string& name ) override;
101  virtual StatusCode regGraph( const std::string& name, TGraph* ) override;
103  StatusCode getGraph( const std::string& name, TGraph*& ) const override;
104 
106  StatusCode regEfficiency( const std::string& name ) override;
111  virtual StatusCode regEfficiency( const std::string& name, TEfficiency* ) override;
113  StatusCode getEfficiency( const std::string& name, TEfficiency*& ) const override;
114 
116 
119 
131  StatusCode getShared( const std::string& name, LockedHandle<TH1>& ) const override;
133  StatusCode getShared( const std::string& name, LockedHandle<TH2>& ) const override;
135  StatusCode getShared( const std::string& name, LockedHandle<TH3>& ) const override;
137  StatusCode getShared( const std::string& name, LockedHandle<TGraph>& ) const override;
140 
142 
145 
147  StatusCode deReg( const std::string& name ) override;
149  StatusCode deReg( TObject* obj ) override;
150 
152  StatusCode merge( const std::string& id ) override;
154  StatusCode merge( TObject* ) override;
155 
158  bool exists( const std::string& name ) const override;
160  bool existsHist( const std::string& name ) const override;
162  bool existsTree( const std::string& name ) const override;
164  bool existsGraph( const std::string& name ) const override;
166  bool existsEfficiency( const std::string& name ) const override;
167 
169 
172 
173  std::vector<std::string> getHists() const override;
174  std::vector<std::string> getTrees() const override;
175  std::vector<std::string> getGraphs() const override;
176  std::vector<std::string> getEfficiencies() const override;
177 
178  StatusCode getTHists( TDirectory* td, TList&, bool recurse = false ) const override;
179  StatusCode getTHists( const std::string& name, TList&, bool recurse = false ) const override;
180  StatusCode getTHists( TDirectory* td, TList& tl, bool recurse = false, bool reg = false ) override;
181  StatusCode getTHists( const std::string& name, TList& tl, bool recurse = false, bool reg = false ) override;
182 
183  StatusCode getTTrees( TDirectory* td, TList&, bool recurse = false ) const override;
184  StatusCode getTTrees( const std::string& name, TList&, bool recurse = false ) const override;
185  StatusCode getTTrees( TDirectory* td, TList& tl, bool recurse = false, bool reg = false ) override;
186  StatusCode getTTrees( const std::string& name, TList& tl, bool recurse = false, bool reg = false ) override;
187 
188  StatusCode getTEfficiencies( TDirectory* td, TList&, bool recurse = false ) const override;
189  StatusCode getTEfficiencies( const std::string& name, TList&, bool recurse = false ) const override;
190  StatusCode getTEfficiencies( TDirectory* td, TList& tl, bool recurse = false, bool reg = false ) override;
191  StatusCode getTEfficiencies( const std::string& name, TList& tl, bool recurse = false, bool reg = false ) override;
192 
194 
195 public:
196  // Methods from other interfaces
197  // From IIncidentListener
198  void handle( const Incident& ) override;
199 
200  // From IIoComponent
201  StatusCode io_reinit() override;
202 
203 private:
206 
209  public:
212 
213  private:
214  TDirectory* m_gDirectory;
215  TFile* m_gFile;
218  };
219 
222 
224  static Mode charToMode( const char typ ) {
225  switch ( typ ) {
226  case 'O':
227  return READ;
228  case 'A':
229  return APPEND;
230  case 'R':
231  return UPDATE;
232  case 'S':
233  return SHARE;
234  default:
235  return INVALID;
236  }
237  }
238 
240  struct THistID {
241  std::string id{""};
242  bool temp{true};
243  TObject* obj{nullptr};
244  TFile* file{nullptr};
246  histMut_t* mutex{nullptr};
247  bool shared{false};
248 
249  THistID() = default;
250  THistID( const THistID& rhs ) = default;
251  THistID( std::string& i, bool& t, TObject* o, TFile* f ) : id( i ), temp( t ), obj( o ), file( f ) {}
252  THistID( std::string& i, bool& t, TObject* o, TFile* f, Mode m )
253  : id( i ), temp( t ), obj( o ), file( f ), mode( m ) {}
254 
255  void reset() {
256  id = "";
257  temp = true;
258  obj = nullptr;
259  file = nullptr;
260  mode = INVALID;
261  mutex = nullptr;
262  shared = false;
263  }
264 
265  bool operator<( THistID const& rhs ) const { return ( obj < rhs.obj ); }
266 
267  friend std::ostream& operator<<( std::ostream& ost, const THistID& hid ) {
268  ost << "id: " << hid.id << " t: " << hid.temp << " s: " << hid.shared << " M: " << hid.mode << " m: " << hid.mutex
269  << " o: " << hid.obj << " " << hid.obj->IsA()->GetName();
270  return ost;
271  }
272  };
273 
276 
278 
282 
286 
287  // containers for fast lookups
288  // same uid for all elements in vec
290  // all THistIDs
292  // uid: /stream/name -> vhid
294  // name -> vhid
297 
301 
302  // Container holding all TObjects and vhid*s
304 
307  streamMap m_fileStreams; // fileName->streams
308 
309  // stream->filename of shared files
311 
313 
316 
317  template <typename T>
318  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared );
319  template <typename T>
320  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared, THistID*& hid );
321  template <typename T>
322  T* getHist_i( const std::string& name, const size_t& ind = 0, bool quiet = false ) const;
323  template <typename T>
324  T* readHist_i( const std::string& name ) const;
325 
326  template <typename T>
328  template <typename T>
330 
332 
335 
336  template <typename T>
337  T* readHist( const std::string& name ) const;
338  TTree* readTree( const std::string& name ) const;
339 
341  void updateFiles();
343  StatusCode connect( const std::string& );
344  TDirectory* changeDir( const THistSvc::THistID& hid ) const;
346  void removeDoubleSlash( std::string& ) const;
347 
348  void MergeRootFile( TDirectory*, TDirectory* );
349 
350  bool findStream( const std::string& name, std::string& root, std::string& rem, TFile*& file ) const;
351  void parseString( const std::string& id, std::string& root, std::string& rem ) const;
352 
354  void setupInputFile();
355 
357  void setupOutputFile();
358 
360  void copyFileLayout( TDirectory*, TDirectory* );
361 
362  size_t findHistID( const std::string& id, const THistID*& hid, const size_t& index = 0 ) const;
363 
364  void dump() const;
365 
367  StatusCode merge( const THistID& );
369  StatusCode merge( vhid_t* );
370 
373 
375 
378 
379  Gaudi::Property<int> m_autoSave{this, "AutoSave", 0};
380  Gaudi::Property<int> m_autoFlush{this, "AutoFlush", 0};
381  Gaudi::Property<bool> m_print{this, "PrintAll", false};
382  Gaudi::Property<int> m_maxFileSize{this, "MaxFileSize", 10240,
383  "maximum file size in MB. if exceeded,"
384  " will cause an abort. -1 to never check."};
385  Gaudi::Property<int> m_compressionLevel{this, "CompressionLevel", 1, [this]( auto& ) {
386  this->warning()
387  << "\"CompressionLevel\" Property has been deprecated. "
388  << "Set it via the \"CL=\" parameter in the \"Output\" Property"
389  << endmsg;
390  }};
393 
395 
396  IIncidentSvc* p_incSvc = nullptr;
397  IFileMgr* p_fileMgr = nullptr;
398 
399  bool m_signaledStop = false;
400  bool m_delayConnect = false;
401  bool m_okToConnect = false;
402 
404 
406 };
407 
408 // Include template implementation
409 #include "THistSvc.icc"
410 
411 #endif // GAUDISVC_THISTSVC_H
void dump() const
Definition: THistSvc.cpp:2005
bool existsEfficiency(const std::string &name) const override
Check if TEfficiency with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:673
GlobalDirectoryRestore(THistSvcMutex_t &mut)
Definition: THistSvc.cpp:1445
Helper struct that bundles the histogram ID with a mutex, TFile and TObject*.
Definition: THistSvc.h:240
bool m_delayConnect
Definition: THistSvc.h:400
void MergeRootFile(TDirectory *, TDirectory *)
Definition: THistSvc.cpp:1787
std::vector< std::string > getTrees() const override
Definition: THistSvc.cpp:691
StatusCode initialize() override
Definition: THistSvc.cpp:78
StatusCode deReg(const std::string &name) override
Deregister object with given name and give up ownership (without deletion!)
Definition: THistSvc.cpp:566
THistID(std::string &i, bool &t, TObject *o, TFile *f, Mode m)
Definition: THistSvc.h:252
LockedHandle< T > regShared_i(const std::string &id, std::unique_ptr< T > hist)
Definition: THistSvc.icc:271
std::string m_curstream
Definition: THistSvc.h:403
Implementation of property with value of concrete type.
Definition: Property.h:370
bool m_signaledStop
Definition: THistSvc.h:399
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
StatusCode regShared(const std::string &name, std::unique_ptr< TH1 >, LockedHandle< TH1 > &) override
Register shared object of type TH1 and return LockedHandle for that object.
Definition: THistSvc.cpp:475
std::vector< std::string > getEfficiencies() const override
Definition: THistSvc.cpp:707
std::mutex histMut_t
Definition: THistSvc.h:205
StatusCode getShared(const std::string &name, LockedHandle< TH1 > &) const override
Retrieve shared object with given name as TH1 through LockedHandle.
Definition: THistSvc.cpp:521
IIncidentSvc * p_incSvc
Definition: THistSvc.h:396
T * readHist_i(const std::string &name) const
Definition: THistSvc.icc:217
histMut_t * mutex
Definition: THistSvc.h:246
T * readHist(const std::string &name) const
Definition: THistSvc.cpp:1460
std::lock_guard< THistSvcMutex_t > m_lock
Definition: THistSvc.h:217
StatusCode getTEfficiencies(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:1096
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:915
StatusCode writeObjectsToFile()
Definition: THistSvc.cpp:1543
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
Definition: THistSvc.cpp:649
StatusCode getGraph(const std::string &name, TGraph *&) const override
Return TGraph with given name.
Definition: THistSvc.cpp:443
bool existsGraph(const std::string &name) const override
Check if graph with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:677
StatusCode rootOpenAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:2085
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
Definition: THistSvc.cpp:1928
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1767
STL class.
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1834
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: THistSvc.h:391
objMap_t m_tobjs
Definition: THistSvc.h:303
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:716
LockedHandle< T > getShared_i(const std::string &name) const
Definition: THistSvc.icc:305
std::vector< std::string > m_Wstream
Definition: THistSvc.h:277
STL class.
#define FILEMGR_CALLBACK_ARGS
Definition: IFileMgr.h:287
void setupOutputFile()
call-back method to handle output stream property
Definition: THistSvc.cpp:1908
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1870
hlist_t m_hlist
Definition: THistSvc.h:298
idMap_t m_ids
Definition: THistSvc.h:300
void setupInputFile()
call-back method to handle input stream property
Definition: THistSvc.cpp:1884
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:284
Gaudi::Property< int > m_autoSave
Definition: THistSvc.h:379
std::string id
Definition: THistSvc.h:241
Gaudi::Property< std::vector< std::string > > m_inputfile
Definition: THistSvc.h:392
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
Definition: THistSvc.cpp:402
std::vector< std::string > m_Rstream
Definition: THistSvc.h:277
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
constexpr double m
bool m_okToConnect
Definition: THistSvc.h:401
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:38
Gaudi::Property< bool > m_print
Definition: THistSvc.h:381
std::unordered_map< TObject *, std::pair< vhid_t *, size_t > > objMap_t
Definition: THistSvc.h:296
void handle(const Incident &) override
Definition: THistSvc.cpp:1299
Gaudi::Property< int > m_maxFileSize
Definition: THistSvc.h:382
std::vector< std::string > getHists() const override
Definition: THistSvc.cpp:683
std::vector< THistID > vhid_t
Definition: THistSvc.h:289
std::multimap< std::string, std::string > streamMap
Definition: THistSvc.h:306
static Mode charToMode(const char typ)
Convert a char to a Mode enum.
Definition: THistSvc.h:224
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1745
StatusCode rootOpenErrAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:2107
Gaudi::Property< int > m_compressionLevel
Definition: THistSvc.h:385
std::unordered_multimap< std::string, vhid_t * > idMap_t
Definition: THistSvc.h:295
bool existsHist(const std::string &name) const override
Check if histogram with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:671
THistID(std::string &i, bool &t, TObject *o, TFile *f)
Definition: THistSvc.h:251
StatusCode finalize() override
Definition: THistSvc.cpp:189
std::list< vhid_t * > hlist_t
Definition: THistSvc.h:291
bool existsTree(const std::string &name) const override
Check if tree with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:681
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:27
StatusCode regHist(const std::string &name) override
Register a new ROOT histogram TH*X with a name.
Definition: THistSvc.cpp:329
std::recursive_mutex THistSvcMutex_t
Definition: THistSvc.h:204
Mode
Enumerating all possible file access modes.
Definition: THistSvc.h:221
StatusCode getHist(const std::string &name, TH1 *&, size_t index=0) const override
Return histogram with given name as TH1*, THistSvcMT still owns object.
Definition: THistSvc.cpp:349
TObject * obj
Definition: THistSvc.h:243
StatusCode regEfficiency(const std::string &name) override
Register a new TEfficiency with a given name.
Definition: THistSvc.cpp:452
friend std::ostream & operator<<(std::ostream &ost, const THistID &hid)
Definition: THistSvc.h:267
STL class.
StatusCode reinitialize() override
Definition: THistSvc.cpp:183
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1783
Base class for all Incidents (computing events).
Definition: Incident.h:27
uidMap_t m_uids
Definition: THistSvc.h:299
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:305
StatusCode connect(const std::string &)
Definition: THistSvc.cpp:1568
T * getHist_i(const std::string &name, const size_t &ind=0, bool quiet=false) const
Definition: THistSvc.icc:177
IFileMgr * p_fileMgr
Definition: THistSvc.h:397
std::set< std::string > m_alreadyConnectedOutFiles
list of already connected files.
Definition: THistSvc.h:285
bool operator<(THistID const &rhs) const
Definition: THistSvc.h:265
StatusCode regTree(const std::string &name) override
Register a new TTree with a given name.
Definition: THistSvc.cpp:376
std::map< std::string, std::string > m_sharedFiles
Definition: THistSvc.h:310
bool exists(const std::string &name) const override
Check if object with given name is managed by THistSvcMT exists calls existsHist and only works for T...
Definition: THistSvc.cpp:669
std::set< std::string > m_alreadyConnectedInFiles
list of already connected files.
Definition: THistSvc.h:281
StatusCode getEfficiency(const std::string &name, TEfficiency *&) const override
Return TEfficiency with given name.
Definition: THistSvc.cpp:466
StatusCode regGraph(const std::string &name) override
Register a new TGraph with a given name.
Definition: THistSvc.cpp:411
Gaudi::Property< int > m_autoFlush
Definition: THistSvc.h:380
STL class.
std::unordered_map< std::string, vhid_t * > uidMap_t
Definition: THistSvc.h:293
StatusCode io_reinit() override
callback method to reinitialize the internal state of the component for I/O purposes (e....
Definition: THistSvc.cpp:1344
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:405
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1960
TTree * readTree(const std::string &name) const
Definition: THistSvc.cpp:1464
Helper class that manages ROOts global directory and file.
Definition: THistSvc.h:208
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:33
streamMap m_fileStreams
Definition: THistSvc.h:307
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
Definition: THistSvc.cpp:1466
std::vector< std::string > getGraphs() const override
Definition: THistSvc.cpp:699