The Gaudi Framework  v30r2 (9eca68f7)
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 
240 
241  uidXMap m_uidsX;
242  idXMap m_idsX;
243 
244  // containers for fast lookups
245  // same uid for all elements in vec
247  // all THistIDs
249  // uid: /stream/name -> vhid
251  // name -> vhid
254 
255  hlist_t m_hlist;
256  uidMap_t m_uids;
257  idMap_t m_ids;
258 
259  // Container holding all TObjects and vhid*s
260  objMap_t m_tobjs;
261 
263  streamMap m_fileStreams; // fileName->streams
264 
265  // stream->filename of shared files
267 
269 
272 
273  template <typename T>
274  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared );
275  template <typename T>
276  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared, THistID*& hid );
277  template <typename T>
278  T* getHist_i( const std::string& name, const size_t& ind = 0, bool quiet = false ) const;
279  template <typename T>
280  T* readHist_i( const std::string& name ) const;
281 
282  template <typename T>
284  template <typename T>
285  LockedHandle<T> getShared_i( const std::string& name ) const;
286 
288 
291 
292  template <typename T>
293  T* readHist( const std::string& name ) const;
294  TTree* readTree( const std::string& name ) const;
295 
297  void updateFiles();
299  StatusCode connect( const std::string& );
300  TDirectory* changeDir( const THistSvc::THistID& hid ) const;
302  void removeDoubleSlash( std::string& ) const;
303 
304  void MergeRootFile( TDirectory*, TDirectory* );
305 
306  bool findStream( const std::string& name, std::string& root, std::string& rem, TFile*& file ) const;
307  void parseString( const std::string& id, std::string& root, std::string& rem ) const;
308 
310  void setupInputFile( Gaudi::Details::PropertyBase& inputfile );
311 
313  void setupOutputFile( Gaudi::Details::PropertyBase& outputfile );
314 
316 
318  void copyFileLayout( TDirectory*, TDirectory* );
319 
320  size_t findHistID( const std::string& id, const THistID*& hid, const size_t& index = 0 ) const;
321 
322  void dump() const;
323 
325  StatusCode merge( const THistID& );
327  StatusCode merge( vhid_t* );
328 
331 
333 
336 
337  Gaudi::Property<int> m_autoSave{this, "AutoSave", 0};
338  Gaudi::Property<int> m_autoFlush{this, "AutoFlush", 0};
339  Gaudi::Property<bool> m_print{this, "PrintAll", false};
340  Gaudi::Property<int> m_maxFileSize{this, "MaxFileSize", 10240, "maximum file size in MB. if exceeded,"
341  " will cause an abort. -1 to never check."};
342  Gaudi::Property<int> m_compressionLevel{this, "CompressionLevel", 1};
345 
347 
348  IIncidentSvc* p_incSvc = nullptr;
349  IFileMgr* p_fileMgr = nullptr;
350 
351  bool m_signaledStop = false;
352  bool m_delayConnect = false;
353  bool m_okToConnect = false;
354 
356 
357  mutable THistSvcMutex_t m_svcMut;
358 };
359 
360 // Include template implementation
361 #include "THistSvc.icc"
362 
363 #endif // GAUDISVC_THISTSVC_H
GlobalDirectoryRestore(THistSvcMutex_t &mut)
Definition: THistSvc.cpp:1263
Helper struct that bundles the histogram ID with a mutex, TFile and TObject*.
Definition: THistSvc.h:186
bool m_delayConnect
Definition: THistSvc.h:352
void MergeRootFile(TDirectory *, TDirectory *)
Definition: THistSvc.cpp:1629
StatusCode getGraph(const std::string &name, TGraph *&) const override
Return TGraph with given name.
Definition: THistSvc.cpp:481
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:571
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:1285
std::string m_curstream
Definition: THistSvc.h:355
Implementation of property with value of concrete type.
Definition: Property.h:381
bool m_signaledStop
Definition: THistSvc.h:351
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:491
std::vector< std::string > getGraphs() const override
Definition: THistSvc.cpp:696
uidXMap m_uidsX
Definition: THistSvc.h:241
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:676
IIncidentSvc * p_incSvc
Definition: THistSvc.h:348
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:1687
std::lock_guard< THistSvcMutex_t > m_lock
Definition: THistSvc.h:179
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:705
StatusCode writeObjectsToFile()
Definition: THistSvc.cpp:1365
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:654
std::map< std::string, THistID > uidXMap
Definition: THistSvc.h:237
StatusCode rootOpenAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1969
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
Definition: THistSvc.cpp:1804
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1580
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:369
void setupCompressionLevel(Gaudi::Details::PropertyBase &cmp)
Definition: THistSvc.cpp:1797
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: THistSvc.h:343
objMap_t m_tobjs
Definition: THistSvc.h:260
std::vector< std::string > getHists() const override
Definition: THistSvc.cpp:678
std::vector< std::string > m_Wstream
Definition: THistSvc.h:227
STL class.
#define FILEMGR_CALLBACK_ARGS
Definition: IFileMgr.h:293
hlist_t m_hlist
Definition: THistSvc.h:255
idMap_t m_ids
Definition: THistSvc.h:257
Gaudi::Property< int > m_autoSave
Definition: THistSvc.h:337
std::string id
Definition: THistSvc.h:187
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1605
idXMap m_idsX
Definition: THistSvc.h:242
Gaudi::Property< std::vector< std::string > > m_inputfile
Definition: THistSvc.h:344
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:353
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
Gaudi::Property< bool > m_print
Definition: THistSvc.h:339
std::unordered_map< TObject *, std::pair< vhid_t *, size_t > > objMap_t
Definition: THistSvc.h:253
void handle(const Incident &) override
Definition: THistSvc.cpp:1113
Gaudi::Property< int > m_maxFileSize
Definition: THistSvc.h:340
void dump() const
Definition: THistSvc.cpp:1883
std::vector< THistID > vhid_t
Definition: THistSvc.h:246
std::multimap< std::string, std::string > streamMap
Definition: THistSvc.h:239
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
Definition: THistSvc.cpp:432
StatusCode rootOpenErrAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1994
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
Gaudi::Property< int > m_compressionLevel
Definition: THistSvc.h:342
std::unordered_multimap< std::string, vhid_t * > idMap_t
Definition: THistSvc.h:252
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1726
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:248
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:343
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:916
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:256
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:262
StatusCode connect(const std::string &)
Definition: THistSvc.cpp:1391
IFileMgr * p_fileMgr
Definition: THistSvc.h:349
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:399
std::map< std::string, std::string > m_sharedFiles
Definition: THistSvc.h:266
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1837
std::multimap< std::string, THistID > idXMap
Definition: THistSvc.h:238
std::vector< std::string > getTrees() const override
Definition: THistSvc.cpp:687
StatusCode getShared(const std::string &name, LockedHandle< TH1 > &) const override
Retrieve shared object with given name as TH1 through LockedHandle.
Definition: THistSvc.cpp:531
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:1741
void setupOutputFile(Gaudi::Details::PropertyBase &outputfile)
call-back method to handle output stream property
Definition: THistSvc.cpp:1771
StatusCode regGraph(const std::string &name) override
Register a new TGraph with a given name.
Definition: THistSvc.cpp:442
Gaudi::Property< int > m_autoFlush
Definition: THistSvc.h:338
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:250
StatusCode io_reinit() override
callback method to reinitialize the internal state of the component for I/O purposes (e...
Definition: THistSvc.cpp:1160
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:357
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:263
T * readHist(const std::string &name) const
Definition: THistSvc.cpp:1280
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1622
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
Definition: THistSvc.cpp:1287