The Gaudi Framework  v30r3 (a5ef0a68)
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  THistSvc( const std::string& name, ISvcLocator* svc );
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  struct THistID {
187  std::string id{""};
188  bool temp{true};
189  TObject* obj{nullptr};
190  TFile* file{nullptr};
191  Mode mode{INVALID};
192  histMut_t* mutex{nullptr};
193  bool shared{false};
194 
195  THistID() = default;
196  THistID( const THistID& rhs ) = default;
197  THistID( std::string& i, bool& t, TObject* o, TFile* f ) : id( i ), temp( t ), obj( o ), file( f ) {}
198  THistID( std::string& i, bool& t, TObject* o, TFile* f, Mode m )
199  : id( i ), temp( t ), obj( o ), file( f ), mode( m )
200  {
201  }
202 
203  void reset()
204  {
205  id = "";
206  temp = true;
207  obj = nullptr;
208  file = nullptr;
209  mode = INVALID;
210  mutex = nullptr;
211  shared = false;
212  }
213 
214  bool operator<( THistID const& rhs ) const { return ( obj < rhs.obj ); }
215 
216  friend std::ostream& operator<<( std::ostream& ost, const THistID& hid )
217  {
218  ost << "id: " << hid.id << " t: " << hid.temp << " s: " << hid.shared << " M: " << hid.mode << " m: " << hid.mutex
219  << " o: " << hid.obj << " " << hid.obj->IsA()->GetName();
220  return ost;
221  }
222  };
223 
226 
228 
232 
236 
237  // containers for fast lookups
238  // same uid for all elements in vec
240  // all THistIDs
242  // uid: /stream/name -> vhid
244  // name -> vhid
247 
248  hlist_t m_hlist;
249  uidMap_t m_uids;
250  idMap_t m_ids;
251 
252  // Container holding all TObjects and vhid*s
253  objMap_t m_tobjs;
254 
257  streamMap m_fileStreams; // fileName->streams
258 
259  // stream->filename of shared files
261 
263 
266 
267  template <typename T>
268  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared );
269  template <typename T>
270  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared, THistID*& hid );
271  template <typename T>
272  T* getHist_i( const std::string& name, const size_t& ind = 0, bool quiet = false ) const;
273  template <typename T>
274  T* readHist_i( const std::string& name ) const;
275 
276  template <typename T>
278  template <typename T>
279  LockedHandle<T> getShared_i( const std::string& name ) const;
280 
282 
285 
286  template <typename T>
287  T* readHist( const std::string& name ) const;
288  TTree* readTree( const std::string& name ) const;
289 
291  void updateFiles();
293  StatusCode connect( const std::string& );
294  TDirectory* changeDir( const THistSvc::THistID& hid ) const;
296  void removeDoubleSlash( std::string& ) const;
297 
298  void MergeRootFile( TDirectory*, TDirectory* );
299 
300  bool findStream( const std::string& name, std::string& root, std::string& rem, TFile*& file ) const;
301  void parseString( const std::string& id, std::string& root, std::string& rem ) const;
302 
304  void setupInputFile( Gaudi::Details::PropertyBase& inputfile );
305 
307  void setupOutputFile( Gaudi::Details::PropertyBase& outputfile );
308 
310 
312  void copyFileLayout( TDirectory*, TDirectory* );
313 
314  size_t findHistID( const std::string& id, const THistID*& hid, const size_t& index = 0 ) const;
315 
316  void dump() const;
317 
319  StatusCode merge( const THistID& );
321  StatusCode merge( vhid_t* );
322 
325 
327 
330 
331  Gaudi::Property<int> m_autoSave{this, "AutoSave", 0};
332  Gaudi::Property<int> m_autoFlush{this, "AutoFlush", 0};
333  Gaudi::Property<bool> m_print{this, "PrintAll", false};
334  Gaudi::Property<int> m_maxFileSize{this, "MaxFileSize", 10240, "maximum file size in MB. if exceeded,"
335  " will cause an abort. -1 to never check."};
336  Gaudi::Property<int> m_compressionLevel{this, "CompressionLevel", 1};
339 
341 
342  IIncidentSvc* p_incSvc = nullptr;
343  IFileMgr* p_fileMgr = nullptr;
344 
345  bool m_signaledStop = false;
346  bool m_delayConnect = false;
347  bool m_okToConnect = false;
348 
350 
351  mutable THistSvcMutex_t m_svcMut;
352 };
353 
354 // Include template implementation
355 #include "THistSvc.icc"
356 
357 #endif // GAUDISVC_THISTSVC_H
GlobalDirectoryRestore(THistSvcMutex_t &mut)
Definition: THistSvc.cpp:1264
Helper struct that bundles the histogram ID with a mutex, TFile and TObject*.
Definition: THistSvc.h:186
bool m_delayConnect
Definition: THistSvc.h:346
void MergeRootFile(TDirectory *, TDirectory *)
Definition: THistSvc.cpp:1634
StatusCode getGraph(const std::string &name, TGraph *&) const override
Return TGraph with given name.
Definition: THistSvc.cpp:482
StatusCode initialize() override
Definition: THistSvc.cpp:65
StatusCode deReg(const std::string &name) override
Deregister object with given name and give up ownership (without deletion!)
Definition: THistSvc.cpp:572
THistID(std::string &i, bool &t, TObject *o, TFile *f, Mode m)
Definition: THistSvc.h:198
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
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:272
TTree * readTree(const std::string &name) const
Definition: THistSvc.cpp:1286
std::string m_curstream
Definition: THistSvc.h:349
Implementation of property with value of concrete type.
Definition: Property.h:381
bool m_signaledStop
Definition: THistSvc.h:345
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:492
std::vector< std::string > getGraphs() const override
Definition: THistSvc.cpp:697
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:677
IIncidentSvc * p_incSvc
Definition: THistSvc.h:342
histMut_t * mutex
Definition: THistSvc.h:192
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1692
std::lock_guard< THistSvcMutex_t > m_lock
Definition: THistSvc.h:179
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:706
StatusCode writeObjectsToFile()
Definition: THistSvc.cpp:1370
LockedHandle< T > getShared_i(const std::string &name) const
Definition: THistSvc.icc:307
T * readHist_i(const std::string &name) const
Definition: THistSvc.icc:215
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
Definition: THistSvc.cpp:655
StatusCode rootOpenAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1974
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
Definition: THistSvc.cpp:1809
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1585
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:370
void setupCompressionLevel(Gaudi::Details::PropertyBase &cmp)
Definition: THistSvc.cpp:1802
STL class.
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: THistSvc.h:337
objMap_t m_tobjs
Definition: THistSvc.h:253
std::vector< std::string > getHists() const override
Definition: THistSvc.cpp:679
std::vector< std::string > m_Wstream
Definition: THistSvc.h:227
STL class.
#define FILEMGR_CALLBACK_ARGS
Definition: IFileMgr.h:295
hlist_t m_hlist
Definition: THistSvc.h:248
idMap_t m_ids
Definition: THistSvc.h:250
Gaudi::Property< int > m_autoSave
Definition: THistSvc.h:331
std::string id
Definition: THistSvc.h:187
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1610
Gaudi::Property< std::vector< std::string > > m_inputfile
Definition: THistSvc.h:338
std::vector< std::string > m_Rstream
Definition: THistSvc.h:227
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:347
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
Gaudi::Property< bool > m_print
Definition: THistSvc.h:333
std::unordered_map< TObject *, std::pair< vhid_t *, size_t > > objMap_t
Definition: THistSvc.h:246
void handle(const Incident &) override
Definition: THistSvc.cpp:1114
Gaudi::Property< int > m_maxFileSize
Definition: THistSvc.h:334
void dump() const
Definition: THistSvc.cpp:1888
std::vector< THistID > vhid_t
Definition: THistSvc.h:239
std::multimap< std::string, std::string > streamMap
Definition: THistSvc.h:256
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
Definition: THistSvc.cpp:433
StatusCode rootOpenErrAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1999
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
Gaudi::Property< int > m_compressionLevel
Definition: THistSvc.h:336
std::unordered_multimap< std::string, vhid_t * > idMap_t
Definition: THistSvc.h:245
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1731
THistID(std::string &i, bool &t, TObject *o, TFile *f)
Definition: THistSvc.h:197
StatusCode finalize() override
Definition: THistSvc.cpp:187
std::list< vhid_t * > hlist_t
Definition: THistSvc.h:241
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:214
StatusCode regHist(const std::string &name) override
Register a new ROOT histogram TH*X with a name.
Definition: THistSvc.cpp:344
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:172
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:917
TObject * obj
Definition: THistSvc.h:189
friend std::ostream & operator<<(std::ostream &ost, const THistID &hid)
Definition: THistSvc.h:216
STL class.
StatusCode reinitialize() override
Definition: THistSvc.cpp:180
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:249
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:255
StatusCode connect(const std::string &)
Definition: THistSvc.cpp:1396
IFileMgr * p_fileMgr
Definition: THistSvc.h:343
std::set< std::string > m_alreadyConnectedOutFiles
list of already connected files.
Definition: THistSvc.h:235
StatusCode regTree(const std::string &name) override
Register a new TTree with a given name.
Definition: THistSvc.cpp:400
std::map< std::string, std::string > m_sharedFiles
Definition: THistSvc.h:260
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1842
std::vector< std::string > getTrees() const override
Definition: THistSvc.cpp:688
StatusCode getShared(const std::string &name, LockedHandle< TH1 > &) const override
Retrieve shared object with given name as TH1 through LockedHandle.
Definition: THistSvc.cpp:532
std::set< std::string > m_alreadyConnectedInFiles
list of already connected files.
Definition: THistSvc.h:231
void setupInputFile(Gaudi::Details::PropertyBase &inputfile)
call-back method to handle input stream property
Definition: THistSvc.cpp:1746
void setupOutputFile(Gaudi::Details::PropertyBase &outputfile)
call-back method to handle output stream property
Definition: THistSvc.cpp:1776
StatusCode regGraph(const std::string &name) override
Register a new TGraph with a given name.
Definition: THistSvc.cpp:443
Gaudi::Property< int > m_autoFlush
Definition: THistSvc.h:332
THistSvc(const std::string &name, ISvcLocator *svc)
Definition: THistSvc.cpp:58
STL class.
std::unordered_map< std::string, vhid_t * > uidMap_t
Definition: THistSvc.h:243
StatusCode io_reinit() override
callback method to reinitialize the internal state of the component for I/O purposes (e...
Definition: THistSvc.cpp:1161
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:351
Helper class that manages ROOts global directory and file.
Definition: THistSvc.h:169
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
streamMap m_fileStreams
Definition: THistSvc.h:257
T * readHist(const std::string &name) const
Definition: THistSvc.cpp:1281
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1627
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
Definition: THistSvc.cpp:1288