The Gaudi Framework  v30r4 (9b837755)
THistSvc.h
Go to the documentation of this file.
1 #ifndef GAUDISVC_THISTSVC_H
2 #define GAUDISVC_THISTSVC_H
3 
4 // system includes:
5 #include <map>
6 #include <set>
7 #include <string>
8 #include <vector>
9 
10 // Gaudi includes:
11 #include "GaudiKernel/IFileMgr.h"
14 #include "GaudiKernel/ITHistSvc.h"
15 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/Service.h"
17 
18 // ROOT includes:
19 #include "TGraph.h"
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TH3.h"
23 #include "TList.h"
24 #include "TObject.h"
25 #include "TTree.h"
26 
27 class IIncidentSvc;
28 
29 class THistSvc : public extends<Service, ITHistSvc, IIncidentListener, IIoComponent>
30 {
31 public:
32  using extends::extends;
33 
34  StatusCode initialize() override;
35  StatusCode reinitialize() override;
36  StatusCode finalize() override;
37 
38 public:
39  // Methods from ITHistSvc
42 
44  StatusCode regHist( const std::string& name ) override;
48  StatusCode regHist( const std::string& name, std::unique_ptr<TH1> hist ) override;
53  StatusCode regHist( const std::string& name, std::unique_ptr<TH1> hist, TH1* hist_ptr ) override;
56  StatusCode regHist( const std::string& name, TH1* ) override;
57 
59  StatusCode getHist( const std::string& name, TH1*&, size_t index = 0 ) const override;
61  StatusCode getHist( const std::string& name, TH2*&, size_t index = 0 ) const override;
63  StatusCode getHist( const std::string& name, TH3*&, size_t index = 0 ) const override;
64 
66 
69 
71  StatusCode regTree( const std::string& name ) override;
73  StatusCode regTree( const std::string& name, std::unique_ptr<TTree> ) override;
76  StatusCode regTree( const std::string& name, TTree* ) override;
78  StatusCode getTree( const std::string& name, TTree*& ) const override;
79 
81 
84 
86  StatusCode regGraph( const std::string& name ) override;
88  StatusCode regGraph( const std::string& name, std::unique_ptr<TGraph> ) override;
91  virtual StatusCode regGraph( const std::string& name, TGraph* ) override;
93  StatusCode getGraph( const std::string& name, TGraph*& ) const override;
94 
96 
99 
109  StatusCode getShared( const std::string& name, LockedHandle<TH1>& ) const override;
111  StatusCode getShared( const std::string& name, LockedHandle<TH2>& ) const override;
113  StatusCode getShared( const std::string& name, LockedHandle<TH3>& ) const override;
115  StatusCode getShared( const std::string& name, LockedHandle<TGraph>& ) const override;
116 
118 
121 
123  StatusCode deReg( const std::string& name ) override;
125  StatusCode deReg( TObject* obj ) override;
126 
128  StatusCode merge( const std::string& id ) override;
130  StatusCode merge( TObject* ) override;
131 
133  bool exists( const std::string& name ) const override;
134 
136 
139 
140  std::vector<std::string> getHists() const override;
141  std::vector<std::string> getTrees() const override;
142  std::vector<std::string> getGraphs() const override;
143 
144  StatusCode getTHists( TDirectory* td, TList&, bool recurse = false ) const override;
145  StatusCode getTHists( const std::string& name, TList&, bool recurse = false ) const override;
146  StatusCode getTHists( TDirectory* td, TList& tl, bool recurse = false, bool reg = false ) override;
147  StatusCode getTHists( const std::string& name, TList& tl, bool recurse = false, bool reg = false ) override;
148 
149  StatusCode getTTrees( TDirectory* td, TList&, bool recurse = false ) const override;
150  StatusCode getTTrees( const std::string& name, TList&, bool recurse = false ) const override;
151  StatusCode getTTrees( TDirectory* td, TList& tl, bool recurse = false, bool reg = false ) override;
152  StatusCode getTTrees( const std::string& name, TList& tl, bool recurse = false, bool reg = false ) override;
153 
155 
156 public:
157  // Methods from other interfaces
158  // From IIncidentListener
159  void handle( const Incident& ) override;
160 
161  // From IIoComponent
162  StatusCode io_reinit() override;
163 
164 private:
167 
170  {
171  public:
172  GlobalDirectoryRestore( THistSvcMutex_t& mut );
174 
175  private:
176  TDirectory* m_gDirectory;
177  TFile* m_gFile;
180  };
181 
184 
186  Mode charToMode( const char typ )
187  {
188  switch ( typ ) {
189  case 'O':
190  return READ;
191  case 'A':
192  return APPEND;
193  case 'R':
194  return UPDATE;
195  case 'S':
196  return SHARE;
197  default:
198  return INVALID;
199  }
200  }
201 
203  struct THistID {
204  std::string id{""};
205  bool temp{true};
206  TObject* obj{nullptr};
207  TFile* file{nullptr};
208  Mode mode{INVALID};
209  histMut_t* mutex{nullptr};
210  bool shared{false};
211 
212  THistID() = default;
213  THistID( const THistID& rhs ) = default;
214  THistID( std::string& i, bool& t, TObject* o, TFile* f ) : id( i ), temp( t ), obj( o ), file( f ) {}
215  THistID( std::string& i, bool& t, TObject* o, TFile* f, Mode m )
216  : id( i ), temp( t ), obj( o ), file( f ), mode( m )
217  {
218  }
219 
220  void reset()
221  {
222  id = "";
223  temp = true;
224  obj = nullptr;
225  file = nullptr;
226  mode = INVALID;
227  mutex = nullptr;
228  shared = false;
229  }
230 
231  bool operator<( THistID const& rhs ) const { return ( obj < rhs.obj ); }
232 
233  friend std::ostream& operator<<( std::ostream& ost, const THistID& hid )
234  {
235  ost << "id: " << hid.id << " t: " << hid.temp << " s: " << hid.shared << " M: " << hid.mode << " m: " << hid.mutex
236  << " o: " << hid.obj << " " << hid.obj->IsA()->GetName();
237  return ost;
238  }
239  };
240 
243 
245 
249 
253 
254  // containers for fast lookups
255  // same uid for all elements in vec
257  // all THistIDs
259  // uid: /stream/name -> vhid
261  // name -> vhid
264 
265  hlist_t m_hlist;
266  uidMap_t m_uids;
267  idMap_t m_ids;
268 
269  // Container holding all TObjects and vhid*s
270  objMap_t m_tobjs;
271 
274  streamMap m_fileStreams; // fileName->streams
275 
276  // stream->filename of shared files
278 
280 
283 
284  template <typename T>
285  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared );
286  template <typename T>
287  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared, THistID*& hid );
288  template <typename T>
289  T* getHist_i( const std::string& name, const size_t& ind = 0, bool quiet = false ) const;
290  template <typename T>
291  T* readHist_i( const std::string& name ) const;
292 
293  template <typename T>
295  template <typename T>
296  LockedHandle<T> getShared_i( const std::string& name ) const;
297 
299 
302 
303  template <typename T>
304  T* readHist( const std::string& name ) const;
305  TTree* readTree( const std::string& name ) const;
306 
308  void updateFiles();
310  StatusCode connect( const std::string& );
311  TDirectory* changeDir( const THistSvc::THistID& hid ) const;
313  void removeDoubleSlash( std::string& ) const;
314 
315  void MergeRootFile( TDirectory*, TDirectory* );
316 
317  bool findStream( const std::string& name, std::string& root, std::string& rem, TFile*& file ) const;
318  void parseString( const std::string& id, std::string& root, std::string& rem ) const;
319 
321  void setupInputFile();
322 
324  void setupOutputFile();
325 
327  void copyFileLayout( TDirectory*, TDirectory* );
328 
329  size_t findHistID( const std::string& id, const THistID*& hid, const size_t& index = 0 ) const;
330 
331  void dump() const;
332 
334  StatusCode merge( const THistID& );
336  StatusCode merge( vhid_t* );
337 
340 
342 
345 
346  Gaudi::Property<int> m_autoSave{this, "AutoSave", 0};
347  Gaudi::Property<int> m_autoFlush{this, "AutoFlush", 0};
348  Gaudi::Property<bool> m_print{this, "PrintAll", false};
349  Gaudi::Property<int> m_maxFileSize{this, "MaxFileSize", 10240, "maximum file size in MB. if exceeded,"
350  " will cause an abort. -1 to never check."};
351  Gaudi::Property<int> m_compressionLevel{this, "CompressionLevel", 1, [this]( auto& ) {
352  this->warning()
353  << "\"CompressionLevel\" Property has been deprecated. "
354  << "Set it via the \"CL=\" parameter in the \"Output\" Property"
355  << endmsg;
356  }};
359 
361 
362  IIncidentSvc* p_incSvc = nullptr;
363  IFileMgr* p_fileMgr = nullptr;
364 
365  bool m_signaledStop = false;
366  bool m_delayConnect = false;
367  bool m_okToConnect = false;
368 
370 
371  mutable THistSvcMutex_t m_svcMut;
372 };
373 
374 // Include template implementation
375 #include "THistSvc.icc"
376 
377 #endif // GAUDISVC_THISTSVC_H
GlobalDirectoryRestore(THistSvcMutex_t &mut)
Definition: THistSvc.cpp:1253
Helper struct that bundles the histogram ID with a mutex, TFile and TObject*.
Definition: THistSvc.h:203
bool m_delayConnect
Definition: THistSvc.h:366
void MergeRootFile(TDirectory *, TDirectory *)
Definition: THistSvc.cpp:1612
StatusCode getGraph(const std::string &name, TGraph *&) const override
Return TGraph with given name.
Definition: THistSvc.cpp:470
StatusCode initialize() override
Definition: THistSvc.cpp:58
StatusCode deReg(const std::string &name) override
Deregister object with given name and give up ownership (without deletion!)
Definition: THistSvc.cpp:560
THistID(std::string &i, bool &t, TObject *o, TFile *f, Mode m)
Definition: THistSvc.h:215
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:288
LockedHandle< T > regShared_i(const std::string &id, std::unique_ptr< T > hist)
Definition: THistSvc.icc:276
TTree * readTree(const std::string &name) const
Definition: THistSvc.cpp:1275
std::string m_curstream
Definition: THistSvc.h:369
Implementation of property with value of concrete type.
Definition: Property.h:383
bool m_signaledStop
Definition: THistSvc.h:365
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:480
std::vector< std::string > getGraphs() const override
Definition: THistSvc.cpp:687
std::mutex histMut_t
Definition: THistSvc.h:166
bool exists(const std::string &name) const override
Check if object with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:667
IIncidentSvc * p_incSvc
Definition: THistSvc.h:362
histMut_t * mutex
Definition: THistSvc.h:209
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1670
std::lock_guard< THistSvcMutex_t > m_lock
Definition: THistSvc.h:179
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:696
StatusCode writeObjectsToFile()
Definition: THistSvc.cpp:1359
LockedHandle< T > getShared_i(const std::string &name) const
Definition: THistSvc.icc:311
T * readHist_i(const std::string &name) const
Definition: THistSvc.icc:219
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
Definition: THistSvc.cpp:645
StatusCode rootOpenAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1943
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
Definition: THistSvc.cpp:1778
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1563
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:358
STL class.
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: THistSvc.h:357
objMap_t m_tobjs
Definition: THistSvc.h:270
std::vector< std::string > getHists() const override
Definition: THistSvc.cpp:669
std::vector< std::string > m_Wstream
Definition: THistSvc.h:244
STL class.
#define FILEMGR_CALLBACK_ARGS
Definition: IFileMgr.h:295
void setupOutputFile()
call-back method to handle output stream property
Definition: THistSvc.cpp:1753
hlist_t m_hlist
Definition: THistSvc.h:265
idMap_t m_ids
Definition: THistSvc.h:267
void setupInputFile()
call-back method to handle input stream property
Definition: THistSvc.cpp:1724
Gaudi::Property< int > m_autoSave
Definition: THistSvc.h:346
std::string id
Definition: THistSvc.h:204
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1588
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Gaudi::Property< std::vector< std::string > > m_inputfile
Definition: THistSvc.h:358
Mode charToMode(const char typ)
Convert a char to a Mode enum.
Definition: THistSvc.h:186
std::vector< std::string > m_Rstream
Definition: THistSvc.h:244
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
constexpr double m
Definition: SystemOfUnits.h:94
bool m_okToConnect
Definition: THistSvc.h:367
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
Gaudi::Property< bool > m_print
Definition: THistSvc.h:348
std::unordered_map< TObject *, std::pair< vhid_t *, size_t > > objMap_t
Definition: THistSvc.h:263
void handle(const Incident &) override
Definition: THistSvc.cpp:1104
Gaudi::Property< int > m_maxFileSize
Definition: THistSvc.h:349
void dump() const
Definition: THistSvc.cpp:1857
std::vector< THistID > vhid_t
Definition: THistSvc.h:256
std::multimap< std::string, std::string > streamMap
Definition: THistSvc.h:273
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
Definition: THistSvc.cpp:421
StatusCode rootOpenErrAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1968
Gaudi::Property< int > m_compressionLevel
Definition: THistSvc.h:351
std::unordered_multimap< std::string, vhid_t * > idMap_t
Definition: THistSvc.h:262
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1709
THistID(std::string &i, bool &t, TObject *o, TFile *f)
Definition: THistSvc.h:214
StatusCode finalize() override
Definition: THistSvc.cpp:175
std::list< vhid_t * > hlist_t
Definition: THistSvc.h:258
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
bool operator<(THistID const &rhs) const
Definition: THistSvc.h:231
StatusCode regHist(const std::string &name) override
Register a new ROOT histogram TH*X with a name.
Definition: THistSvc.cpp:332
std::recursive_mutex THistSvcMutex_t
Definition: THistSvc.h:165
Mode
Enumerating all possible file access modes.
Definition: THistSvc.h:183
T * getHist_i(const std::string &name, const size_t &ind=0, bool quiet=false) const
Definition: THistSvc.icc:176
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:907
TObject * obj
Definition: THistSvc.h:206
friend std::ostream & operator<<(std::ostream &ost, const THistID &hid)
Definition: THistSvc.h:233
STL class.
StatusCode reinitialize() override
Definition: THistSvc.cpp:168
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
Base class for all Incidents (computing events).
Definition: Incident.h:17
uidMap_t m_uids
Definition: THistSvc.h:266
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:272
StatusCode connect(const std::string &)
Definition: THistSvc.cpp:1385
IFileMgr * p_fileMgr
Definition: THistSvc.h:363
std::set< std::string > m_alreadyConnectedOutFiles
list of already connected files.
Definition: THistSvc.h:252
StatusCode regTree(const std::string &name) override
Register a new TTree with a given name.
Definition: THistSvc.cpp:388
std::map< std::string, std::string > m_sharedFiles
Definition: THistSvc.h:277
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1811
std::vector< std::string > getTrees() const override
Definition: THistSvc.cpp:678
StatusCode getShared(const std::string &name, LockedHandle< TH1 > &) const override
Retrieve shared object with given name as TH1 through LockedHandle.
Definition: THistSvc.cpp:520
std::set< std::string > m_alreadyConnectedInFiles
list of already connected files.
Definition: THistSvc.h:248
StatusCode regGraph(const std::string &name) override
Register a new TGraph with a given name.
Definition: THistSvc.cpp:431
Gaudi::Property< int > m_autoFlush
Definition: THistSvc.h:347
STL class.
std::unordered_map< std::string, vhid_t * > uidMap_t
Definition: THistSvc.h:260
StatusCode io_reinit() override
callback method to reinitialize the internal state of the component for I/O purposes (e...
Definition: THistSvc.cpp:1150
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:371
Helper class that manages ROOts global directory and file.
Definition: THistSvc.h:169
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
streamMap m_fileStreams
Definition: THistSvc.h:274
T * readHist(const std::string &name) const
Definition: THistSvc.cpp:1270
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1605
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
Definition: THistSvc.cpp:1277