Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 public:
31  using extends::extends;
32 
33  StatusCode initialize() override;
34  StatusCode reinitialize() override;
35  StatusCode finalize() override;
36 
37 public:
38  // Methods from ITHistSvc
41 
43  StatusCode regHist( const std::string& name ) override;
47  StatusCode regHist( const std::string& name, std::unique_ptr<TH1> hist ) override;
52  StatusCode regHist( const std::string& name, std::unique_ptr<TH1> hist, TH1* hist_ptr ) override;
55  StatusCode regHist( const std::string& name, TH1* ) override;
56 
58  StatusCode getHist( const std::string& name, TH1*&, size_t index = 0 ) const override;
60  StatusCode getHist( const std::string& name, TH2*&, size_t index = 0 ) const override;
62  StatusCode getHist( const std::string& name, TH3*&, size_t index = 0 ) const override;
63 
65 
68 
70  StatusCode regTree( const std::string& name ) override;
72  StatusCode regTree( const std::string& name, std::unique_ptr<TTree> ) override;
75  StatusCode regTree( const std::string& name, TTree* ) override;
77  StatusCode getTree( const std::string& name, TTree*& ) const override;
78 
80 
83 
85  StatusCode regGraph( const std::string& name ) override;
87  StatusCode regGraph( const std::string& name, std::unique_ptr<TGraph> ) override;
90  virtual StatusCode regGraph( const std::string& name, TGraph* ) override;
92  StatusCode getGraph( const std::string& name, TGraph*& ) const override;
93 
95 
98 
108  StatusCode getShared( const std::string& name, LockedHandle<TH1>& ) const override;
110  StatusCode getShared( const std::string& name, LockedHandle<TH2>& ) const override;
112  StatusCode getShared( const std::string& name, LockedHandle<TH3>& ) const override;
114  StatusCode getShared( const std::string& name, LockedHandle<TGraph>& ) const override;
115 
117 
120 
122  StatusCode deReg( const std::string& name ) override;
124  StatusCode deReg( TObject* obj ) override;
125 
127  StatusCode merge( const std::string& id ) override;
129  StatusCode merge( TObject* ) override;
130 
132  bool exists( const std::string& name ) const override;
133 
135 
138 
139  std::vector<std::string> getHists() const override;
140  std::vector<std::string> getTrees() const override;
141  std::vector<std::string> getGraphs() const override;
142 
143  StatusCode getTHists( TDirectory* td, TList&, bool recurse = false ) const override;
144  StatusCode getTHists( const std::string& name, TList&, bool recurse = false ) const override;
145  StatusCode getTHists( TDirectory* td, TList& tl, bool recurse = false, bool reg = false ) override;
146  StatusCode getTHists( const std::string& name, TList& tl, bool recurse = false, bool reg = false ) override;
147 
148  StatusCode getTTrees( TDirectory* td, TList&, bool recurse = false ) const override;
149  StatusCode getTTrees( const std::string& name, TList&, bool recurse = false ) const override;
150  StatusCode getTTrees( TDirectory* td, TList& tl, bool recurse = false, bool reg = false ) override;
151  StatusCode getTTrees( const std::string& name, TList& tl, bool recurse = false, bool reg = false ) override;
152 
154 
155 public:
156  // Methods from other interfaces
157  // From IIncidentListener
158  void handle( const Incident& ) override;
159 
160  // From IIoComponent
161  StatusCode io_reinit() override;
162 
163 private:
166 
169  public:
170  GlobalDirectoryRestore( THistSvcMutex_t& mut );
172 
173  private:
174  TDirectory* m_gDirectory;
175  TFile* m_gFile;
178  };
179 
182 
184  static Mode charToMode( const char typ ) {
185  switch ( typ ) {
186  case 'O':
187  return READ;
188  case 'A':
189  return APPEND;
190  case 'R':
191  return UPDATE;
192  case 'S':
193  return SHARE;
194  default:
195  return INVALID;
196  }
197  }
198 
200  struct THistID {
201  std::string id{""};
202  bool temp{true};
203  TObject* obj{nullptr};
204  TFile* file{nullptr};
205  Mode mode{INVALID};
206  histMut_t* mutex{nullptr};
207  bool shared{false};
208 
209  THistID() = default;
210  THistID( const THistID& rhs ) = default;
211  THistID( std::string& i, bool& t, TObject* o, TFile* f ) : id( i ), temp( t ), obj( o ), file( f ) {}
212  THistID( std::string& i, bool& t, TObject* o, TFile* f, Mode m )
213  : id( i ), temp( t ), obj( o ), file( f ), mode( m ) {}
214 
215  void reset() {
216  id = "";
217  temp = true;
218  obj = nullptr;
219  file = nullptr;
220  mode = INVALID;
221  mutex = nullptr;
222  shared = false;
223  }
224 
225  bool operator<( THistID const& rhs ) const { return ( obj < rhs.obj ); }
226 
227  friend std::ostream& operator<<( std::ostream& ost, const THistID& hid ) {
228  ost << "id: " << hid.id << " t: " << hid.temp << " s: " << hid.shared << " M: " << hid.mode << " m: " << hid.mutex
229  << " o: " << hid.obj << " " << hid.obj->IsA()->GetName();
230  return ost;
231  }
232  };
233 
236 
238 
242 
246 
247  // containers for fast lookups
248  // same uid for all elements in vec
250  // all THistIDs
252  // uid: /stream/name -> vhid
254  // name -> vhid
257 
258  hlist_t m_hlist;
259  uidMap_t m_uids;
260  idMap_t m_ids;
261 
262  // Container holding all TObjects and vhid*s
263  objMap_t m_tobjs;
264 
267  streamMap m_fileStreams; // fileName->streams
268 
269  // stream->filename of shared files
271 
273 
276 
277  template <typename T>
278  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared );
279  template <typename T>
280  StatusCode regHist_i( std::unique_ptr<T> hist, const std::string& name, bool shared, THistID*& hid );
281  template <typename T>
282  T* getHist_i( const std::string& name, const size_t& ind = 0, bool quiet = false ) const;
283  template <typename T>
284  T* readHist_i( const std::string& name ) const;
285 
286  template <typename T>
288  template <typename T>
289  LockedHandle<T> getShared_i( const std::string& name ) const;
290 
292 
295 
296  template <typename T>
297  T* readHist( const std::string& name ) const;
298  TTree* readTree( const std::string& name ) const;
299 
301  void updateFiles();
303  StatusCode connect( const std::string& );
304  TDirectory* changeDir( const THistSvc::THistID& hid ) const;
306  void removeDoubleSlash( std::string& ) const;
307 
308  void MergeRootFile( TDirectory*, TDirectory* );
309 
310  bool findStream( const std::string& name, std::string& root, std::string& rem, TFile*& file ) const;
311  void parseString( const std::string& id, std::string& root, std::string& rem ) const;
312 
314  void setupInputFile();
315 
317  void setupOutputFile();
318 
320  void copyFileLayout( TDirectory*, TDirectory* );
321 
322  size_t findHistID( const std::string& id, const THistID*& hid, const size_t& index = 0 ) const;
323 
324  void dump() const;
325 
327  StatusCode merge( const THistID& );
329  StatusCode merge( vhid_t* );
330 
333 
335 
338 
339  Gaudi::Property<int> m_autoSave{this, "AutoSave", 0};
340  Gaudi::Property<int> m_autoFlush{this, "AutoFlush", 0};
341  Gaudi::Property<bool> m_print{this, "PrintAll", false};
342  Gaudi::Property<int> m_maxFileSize{this, "MaxFileSize", 10240,
343  "maximum file size in MB. if exceeded,"
344  " will cause an abort. -1 to never check."};
345  Gaudi::Property<int> m_compressionLevel{this, "CompressionLevel", 1, [this]( auto& ) {
346  this->warning()
347  << "\"CompressionLevel\" Property has been deprecated. "
348  << "Set it via the \"CL=\" parameter in the \"Output\" Property"
349  << endmsg;
350  }};
353 
355 
356  IIncidentSvc* p_incSvc = nullptr;
357  IFileMgr* p_fileMgr = nullptr;
358 
359  bool m_signaledStop = false;
360  bool m_delayConnect = false;
361  bool m_okToConnect = false;
362 
364 
365  mutable THistSvcMutex_t m_svcMut;
366 };
367 
368 // Include template implementation
369 #include "THistSvc.icc"
370 
371 #endif // GAUDISVC_THISTSVC_H
GlobalDirectoryRestore(THistSvcMutex_t &mut)
Definition: THistSvc.cpp:1153
Helper struct that bundles the histogram ID with a mutex, TFile and TObject*.
Definition: THistSvc.h:200
bool m_delayConnect
Definition: THistSvc.h:360
void MergeRootFile(TDirectory *, TDirectory *)
Definition: THistSvc.cpp:1495
StatusCode getGraph(const std::string &name, TGraph *&) const override
Return TGraph with given name.
Definition: THistSvc.cpp:420
StatusCode initialize() override
Definition: THistSvc.cpp:55
StatusCode deReg(const std::string &name) override
Deregister object with given name and give up ownership (without deletion!)
Definition: THistSvc.cpp:501
THistID(std::string &i, bool &t, TObject *o, TFile *f, Mode m)
Definition: THistSvc.h:212
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
LockedHandle< T > regShared_i(const std::string &id, std::unique_ptr< T > hist)
Definition: THistSvc.icc:258
TTree * readTree(const std::string &name) const
Definition: THistSvc.cpp:1172
std::string m_curstream
Definition: THistSvc.h:363
Implementation of property with value of concrete type.
Definition: Property.h:352
bool m_signaledStop
Definition: THistSvc.h:359
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:429
std::vector< std::string > getGraphs() const override
Definition: THistSvc.cpp:622
std::mutex histMut_t
Definition: THistSvc.h:165
bool exists(const std::string &name) const override
Check if object with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:604
IIncidentSvc * p_incSvc
Definition: THistSvc.h:356
histMut_t * mutex
Definition: THistSvc.h:206
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1542
std::lock_guard< THistSvcMutex_t > m_lock
Definition: THistSvc.h:177
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:630
StatusCode writeObjectsToFile()
Definition: THistSvc.cpp:1251
LockedHandle< T > getShared_i(const std::string &name) const
Definition: THistSvc.icc:292
T * readHist_i(const std::string &name) const
Definition: THistSvc.icc:204
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
Definition: THistSvc.cpp:584
StatusCode rootOpenAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1791
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
Definition: THistSvc.cpp:1636
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1453
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:326
STL class.
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: THistSvc.h:351
objMap_t m_tobjs
Definition: THistSvc.h:263
std::vector< std::string > getHists() const override
Definition: THistSvc.cpp:606
std::vector< std::string > m_Wstream
Definition: THistSvc.h:237
STL class.
#define FILEMGR_CALLBACK_ARGS
Definition: IFileMgr.h:277
void setupOutputFile()
call-back method to handle output stream property
Definition: THistSvc.cpp:1616
hlist_t m_hlist
Definition: THistSvc.h:258
idMap_t m_ids
Definition: THistSvc.h:260
void setupInputFile()
call-back method to handle input stream property
Definition: THistSvc.cpp:1592
Gaudi::Property< int > m_autoSave
Definition: THistSvc.h:339
std::string id
Definition: THistSvc.h:201
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1475
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Gaudi::Property< std::vector< std::string > > m_inputfile
Definition: THistSvc.h:352
std::vector< std::string > m_Rstream
Definition: THistSvc.h:237
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
constexpr double m
Definition: SystemOfUnits.h:92
bool m_okToConnect
Definition: THistSvc.h:361
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
Gaudi::Property< bool > m_print
Definition: THistSvc.h:341
std::unordered_map< TObject *, std::pair< vhid_t *, size_t > > objMap_t
Definition: THistSvc.h:256
void handle(const Incident &) override
Definition: THistSvc.cpp:1012
Gaudi::Property< int > m_maxFileSize
Definition: THistSvc.h:342
void dump() const
Definition: THistSvc.cpp:1711
std::vector< THistID > vhid_t
Definition: THistSvc.h:249
std::multimap< std::string, std::string > streamMap
Definition: THistSvc.h:266
static Mode charToMode(const char typ)
Convert a char to a Mode enum.
Definition: THistSvc.h:184
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
Definition: THistSvc.cpp:379
StatusCode rootOpenErrAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1813
Gaudi::Property< int > m_compressionLevel
Definition: THistSvc.h:345
std::unordered_multimap< std::string, vhid_t * > idMap_t
Definition: THistSvc.h:255
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1578
THistID(std::string &i, bool &t, TObject *o, TFile *f)
Definition: THistSvc.h:211
StatusCode finalize() override
Definition: THistSvc.cpp:166
std::list< vhid_t * > hlist_t
Definition: THistSvc.h:251
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:225
StatusCode regHist(const std::string &name) override
Register a new ROOT histogram TH*X with a name.
Definition: THistSvc.cpp:306
std::recursive_mutex THistSvcMutex_t
Definition: THistSvc.h:164
Mode
Enumerating all possible file access modes.
Definition: THistSvc.h:181
T * getHist_i(const std::string &name, const size_t &ind=0, bool quiet=false) const
Definition: THistSvc.icc:164
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:829
TObject * obj
Definition: THistSvc.h:203
friend std::ostream & operator<<(std::ostream &ost, const THistID &hid)
Definition: THistSvc.h:227
STL class.
StatusCode reinitialize() override
Definition: THistSvc.cpp:160
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:259
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
StatusCode connect(const std::string &)
Definition: THistSvc.cpp:1276
IFileMgr * p_fileMgr
Definition: THistSvc.h:357
std::set< std::string > m_alreadyConnectedOutFiles
list of already connected files.
Definition: THistSvc.h:245
StatusCode regTree(const std::string &name) override
Register a new TTree with a given name.
Definition: THistSvc.cpp:353
std::map< std::string, std::string > m_sharedFiles
Definition: THistSvc.h:270
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1666
std::vector< std::string > getTrees() const override
Definition: THistSvc.cpp:614
StatusCode getShared(const std::string &name, LockedHandle< TH1 > &) const override
Retrieve shared object with given name as TH1 through LockedHandle.
Definition: THistSvc.cpp:465
std::set< std::string > m_alreadyConnectedInFiles
list of already connected files.
Definition: THistSvc.h:241
StatusCode regGraph(const std::string &name) override
Register a new TGraph with a given name.
Definition: THistSvc.cpp:388
Gaudi::Property< int > m_autoFlush
Definition: THistSvc.h:340
STL class.
std::unordered_map< std::string, vhid_t * > uidMap_t
Definition: THistSvc.h:253
StatusCode io_reinit() override
callback method to reinitialize the internal state of the component for I/O purposes (e...
Definition: THistSvc.cpp:1057
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
Helper class that manages ROOts global directory and file.
Definition: THistSvc.h:168
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
streamMap m_fileStreams
Definition: THistSvc.h:267
T * readHist(const std::string &name) const
Definition: THistSvc.cpp:1168
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1491
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
Definition: THistSvc.cpp:1174