The Gaudi Framework  v30r3 (a5ef0a68)
THistSvc.icc
Go to the documentation of this file.
1 #ifndef GAUDISVC_THISTSVC_ICC
2 #define GAUDISVC_THISTSVC_ICC
3 
4 #include <map>
5 #include <string>
6 
7 #include "TFile.h"
8 #include "TObject.h"
9 
10 #ifndef GAUDIKERNEL_MSGSTREAM_H
11 #include "GaudiKernel/MsgStream.h"
12 #endif
13 
14 #include "GaudiKernel/System.h"
15 
16 template <typename T>
18 {
19  THistID* hid = nullptr ;
20  return regHist_i( std::move(hist), id, shared, hid );
21 }
22 
23 template <typename T>
24 StatusCode THistSvc::regHist_i( std::unique_ptr<T> hist_unique, const std::string& id, bool shared, THistID*& phid )
25 {
27  phid = nullptr;
28 
29  // It is sad that we lose propper memory management here
30  T* hist = nullptr;
31  if( hist_unique.get() != nullptr ) {
32  hist = hist_unique.release();
33  }
34  debug() << "regHist_i obj: " << hist << " id: " << id << " s: " << shared << endmsg;
35 
36  std::string idr( id );
37  removeDoubleSlash( idr );
38 
39  if ( idr.find( "/" ) == idr.length() ) {
40  error() << "Badly formed identifier \"" << idr << "\": "
41  << "Must not end with a /" << endmsg;
42  delete hist;
43  return StatusCode::FAILURE;
44  }
45 
46  TFile* f = nullptr;
47  std::string stream, name;
48  if ( !findStream( idr, stream, name, f ) ) {
49  error() << "Could not register id: \"" << idr << "\"" << endmsg;
50  delete hist;
51  return StatusCode::FAILURE;
52  }
53 
54  std::string uid = "/" + stream + "/" + name;
55 
56  uidMap_t::iterator uitr = m_uids.find( uid );
57  bool exists( false );
58  if ( uitr != m_uids.end() ) {
59  exists = true;
60  TObject* t1 = uitr->second->at( 0 ).obj;
61  if ( hist->Compare( t1 ) != 0 ) {
62  error() << "previously registered object with identifier \"" << uid << "\" does not compare to this one"
63  << endmsg;
64  delete hist;
65  return StatusCode::FAILURE;
66  } else {
67  debug() << "previously registered id \"" << uid << "\": num " << uitr->second->size() << endmsg;
68  }
69  }
70 
71  bool temp = false;
72  if ( !f ) {
73  temp = true;
74  if ( msgLevel( MSG::DEBUG ) ) {
75  debug() << "Historgram with id \"" << idr << "\" is temporary" << endmsg;
76  }
77  }
78 
79  TObject* to = nullptr;
80  THistID hid;
81  // check to see if this hist is to be read in;
82  if ( !temp && m_files.find( stream )->second.second == READ ) {
83  if ( hist != 0 ) {
84  warning() << "Registering id: \"" << idr << "\" with non zero pointer!" << endmsg;
85  }
86 
87  hist = readHist_i<T>( idr );
88  if ( hist == nullptr ) {
89  error() << "Unable to read in hist" << endmsg;
90  delete hist;
91  return StatusCode::FAILURE;
92  }
93  to = dynamic_cast<TObject*>( hist );
94  hid = THistID( uid, temp, to, f, m_files.find( stream )->second.second );
95  } else if ( !hist ) {
96  error() << "Unable to read in hist with id: \"" << idr << "\"" << endmsg;
97  delete hist;
98  return StatusCode::FAILURE;
99  } else {
100  to = dynamic_cast<TObject*>( hist );
101  if ( to == nullptr ) {
102  error() << "Could not dcast to TObject. id: \"" << idr << "\"" << endmsg;
103  delete hist;
104  return StatusCode::FAILURE;
105  }
106 
107  auto oitr = m_tobjs.find( to );
108  if ( oitr != m_tobjs.end() ) {
109  error() << "already registered id: \"" << idr << "\" with identifier \""
110  << oitr->second.first->at( oitr->second.second ).id << "\"" << endmsg;
111  delete hist;
112  return StatusCode::FAILURE;
113  }
114 
115  hid = THistID( uid, temp, to, f, m_files.find( stream )->second.second );
116  hid.shared = shared;
117  TDirectory* dir = changeDir( hid );
118 
119  if ( dynamic_cast<TTree*>( hist ) ) {
120  dynamic_cast<TTree*>( hist )->SetDirectory( dir );
121  } else if ( dynamic_cast<TH1*>( hist) ) {
122  dynamic_cast<TH1*>( hist )->SetDirectory( dir );
123  } else if ( dynamic_cast<TGraph*>( hist ) ) {
124  dir->Append( hist );
125  } else {
126  error() << "id: \"" << idr << "\" is not a TH, TTree, or TGraph. Attaching it to current dir." << endmsg;
127  dir->Append( hist );
128  }
129  }
130 
131  std::string fname;
132  if ( !f ) {
133  fname = "none";
134  } else {
135  fname = f->GetName();
136  }
137 
138  debug() << "Registering" << ( shared ? " shared " : " " ) << System::typeinfoName( typeid( *hist ) ) << " title: \""
139  << hist->GetTitle() << "\" id: \"" << uid
140  << "\" dir: "
141  // << hist->GetDirectory()->GetPath() << " "
142  << changeDir( hid )->GetPath() << " file: " << fname << endmsg;
143 
144  // create a mutex for all shared histograms
145  if ( shared ) {
146  hid.mutex = new histMut_t;
147  }
148 
149  if ( exists ) {
150  vhid_t* vi = uitr->second;
151  vi->push_back( hid );
152  phid = &( vi->back() );
153 
154  m_tobjs.emplace( to, std::pair<vhid_t*, size_t>( vi, vi->size() - 1 ) );
155  } else {
156  vhid_t* vi = new vhid_t{hid};
157  m_hlist.emplace( m_hlist.end(), vi );
158 
159  phid = &( vi->back() );
160  m_uids.emplace( uid, vi );
161  m_ids.emplace( name, vi );
162 
164  }
165 
166  debug() << "regHist_i THistID: " << hid << endmsg;
167 
168  return StatusCode::SUCCESS;
169 }
170 
171 template <typename T>
172 T* THistSvc::getHist_i( const std::string& id, const size_t& ind, bool quiet ) const
173 {
174  // id starts with "/": unique
175 
176  GlobalDirectoryRestore restore( m_svcMut );
177 
178  T* hist = nullptr;
179  const THistID* hid = nullptr;
180  size_t num = findHistID( id, hid, ind );
181  if ( num == 0 ) {
182  // no matches found
183  if ( !quiet ) {
184  error() << "could not locate Hist with id \"" << id << "\"" << endmsg;
185  }
186  return nullptr;
187  } else if ( num > 1 ) {
188  if ( !quiet ) {
189  // return failure if trying to GET a single hist
190  error() << "Multiple matches with id \"" << id << "\"."
191  << " Further specifications required." << endmsg;
192  return nullptr;
193  } else {
194  info() << "Found multiple matches with id \"" << id << "\"" << endmsg;
195  // return first match if just INQUIRING (i.e. != nullptr)
196  hist = dynamic_cast<T*>( hid->obj );
197  if ( hist == nullptr ) {
198  error() << "dcast failed, Hist id: \"" << id << "\"" << endmsg;
199  return nullptr;
200  }
201  }
202  } else {
203  hist = dynamic_cast<T*>( hid->obj );
204  if ( hist == nullptr ) {
205  error() << "dcast failed, Hist id: \"" << id << "\"" << endmsg;
206  return nullptr;
207  }
208  verbose() << "found unique Hist title: \"" << hist->GetTitle() << "\" id: \"" << id << "\"" << endmsg;
209  }
210 
211  return hist;
212 }
213 
214 template <typename T>
215 T* THistSvc::readHist_i( const std::string& id ) const
216 {
217  GlobalDirectoryRestore restore( m_svcMut );
218 
219  std::string idr( id );
220  removeDoubleSlash( idr );
221 
222  std::string stream, rem, dir, fdir, bdir, fdir2;
223  TFile* file = nullptr;
224 
225  if ( !findStream( idr, stream, rem, file ) ) {
226  return nullptr;
227  }
228 
229  if ( !file ) {
230  error() << "no associated file found" << endmsg;
231  return nullptr;
232  }
233 
234  file->cd( "/" );
235 
236  fdir = idr;
237  bdir = stripDirectoryName( fdir );
238  fdir2 = fdir;
239  while ( ( dir = stripDirectoryName( fdir ) ) != "" ) {
240  if ( !gDirectory->GetKey( dir.c_str() ) ) {
241  error() << "Directory \"" << fdir2 << "\" doesnt exist in " << file->GetName() << endmsg;
242  return nullptr;
243  }
244  gDirectory->cd( dir.c_str() );
245  }
246 
247  TObject* to = nullptr;
248  gDirectory->GetObject( fdir.c_str(), to );
249 
250  if ( !to ) {
251  error() << "Could not get obj \"" << fdir << "\" in " << gDirectory->GetPath() << endmsg;
252  return nullptr;
253  }
254 
255  T* hist = dynamic_cast<T*>( to );
256  if ( hist == nullptr ) {
257  error() << "Could not convert \"" << idr << "\" to a " << System::typeinfoName( typeid( *hist ) ) << " as is a "
258  << to->IsA()->GetName() << endmsg;
259  return nullptr;
260  }
261 
262  if ( msgLevel( MSG::DEBUG ) ) {
263  debug() << "Read in " << hist->IsA()->GetName() << " \"" << hist->GetName() << "\" from file " << file->GetName()
264  << endmsg;
265  hist->Print();
266  }
267 
268  return hist;
269 }
270 
271 template <typename T>
273 {
274  LockedHandle<T> lh( nullptr, nullptr );
275  const THistID* hid = nullptr;
276  if ( findHistID( id, hid ) == 0 ) {
277  T* phist = hist.get();
278  THistID* phid = nullptr;
279  if ( regHist_i( std::move(hist), id, true, phid ).isSuccess() ) {
280  lh.set( phist, phid->mutex );
281 
282  } else {
283  error() << "regSharedHist: unable to register shared hist with id \"" << id << "\"" << endmsg;
284  }
285  } else {
286  if ( !hid->shared ) {
287  error() << "regSharedHist: previously register Hist with id \"" << id << "\" was not marked shared" << endmsg;
288  }
289 
290  if ( hist->Compare( hid->obj ) != 0 ) {
291  error() << "regSharedHist: Histogram " << id << " does not compare with " << hid << endmsg;
292  } else {
293  T* phist = dynamic_cast<T*>( hid->obj );
294  if ( phist == 0 ) {
295  error() << "regSharedHist: unable to dcast retrieved shared hist \"" << id << "\" of type "
296  << hid->obj->IsA()->GetName() << " to requested type " << System::typeinfoName( typeid( T ) ) << endmsg;
297  } else {
298  lh.set( phist, hid->mutex );
299  delete hist.release();
300  }
301  }
302  }
303  return lh;
304 }
305 
306 template <typename T>
308 {
309  GlobalDirectoryRestore restore( m_svcMut );
310 
311  const THistID* hid = nullptr;
312  size_t i = findHistID( name, hid );
313 
314  LockedHandle<T> hist( nullptr, nullptr );
315 
316  if ( i == 1 ) {
317  if ( !hid->shared ) {
318  error() << "getSharedHist: found Hist with id \"" << name << "\", but it's not marked as shared" << endmsg;
319  return hist;
320  }
321  T* h1 = dynamic_cast<T*>( hid->obj );
322  hist = LockedHandle<T>( h1, hid->mutex );
323 
324  debug() << "getSharedHist: found THistID: " << *hid << endmsg;
325  } else if ( i == 0 ) {
326  error() << "no histograms matching id \"" << name << "\" found" << endmsg;
327  } else {
328  info() << "multiple matches for id \"" << name << "\" found [" << i << "], probably from different streams"
329  << endmsg;
330  }
331  return hist;
332 }
333 
334 #endif // GAUDISVC_THISTSVC_ICC
Helper struct that bundles the histogram ID with a mutex, TFile and TObject*.
Definition: THistSvc.h:186
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
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
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
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
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
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1585
objMap_t m_tobjs
Definition: THistSvc.h:253
T release(T...args)
STL class.
T at(T...args)
hlist_t m_hlist
Definition: THistSvc.h:248
idMap_t m_ids
Definition: THistSvc.h:250
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1610
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
void set(T *ptr, MutexType *mut)
Definition: LockedHandle.h:36
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
T move(T...args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
T * getHist_i(const std::string &name, const size_t &ind=0, bool quiet=false) const
Definition: THistSvc.icc:172
T get(T...args)
T find(T...args)
T length(T...args)
TObject * obj
Definition: THistSvc.h:189
STL class.
STL class.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T c_str(T...args)
uidMap_t m_uids
Definition: THistSvc.h:249
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:255
T emplace(T...args)
T back(T...args)
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1842
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:351
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
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1627
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)