The Gaudi Framework  v32r2 (46d42edc)
THistSvc Class Reference

#include <src/THistSvc/THistSvc.h>

Inheritance diagram for THistSvc:
Collaboration diagram for THistSvc:

Classes

class  GlobalDirectoryRestore
 Helper class that manages ROOts global directory and file. More...
 
struct  THistID
 Helper struct that bundles the histogram ID with a mutex, TFile and TObject*. More...
 

Public Member Functions

StatusCode initialize () override
 
StatusCode reinitialize () override
 
StatusCode finalize () override
 
void handle (const Incident &) override
 
StatusCode io_reinit () override
 callback method to reinitialize the internal state of the component for I/O purposes (e.g. More...
 
Functions to manage ROOT histograms of any kind
StatusCode regHist (const std::string &name) override
 Register a new ROOT histogram TH*X with a name. More...
 
StatusCode regHist (const std::string &name, std::unique_ptr< TH1 > hist) override
 Register an existing ROOT histogram TH*X with name and moved unique_ptr. More...
 
StatusCode regHist (const std::string &name, std::unique_ptr< TH1 > hist, TH1 *hist_ptr) override
 Register an existing ROOT histogram TH*X with name and moved unique_ptr. More...
 
StatusCode regHist (const std::string &name, TH1 *) override
 
StatusCode getHist (const std::string &name, TH1 *&, size_t index=0) const override
 Return histogram with given name as TH1*, THistSvcMT still owns object. More...
 
StatusCode getHist (const std::string &name, TH2 *&, size_t index=0) const override
 Return histogram with given name as TH2*, THistSvcMT still owns object. More...
 
StatusCode getHist (const std::string &name, TH3 *&, size_t index=0) const override
 Return histogram with given name as TH3*, THistSvcMT still owns object. More...
 
Functions to manage TTrees
StatusCode regTree (const std::string &name) override
 Register a new TTree with a given name. More...
 
StatusCode regTree (const std::string &name, std::unique_ptr< TTree >) override
 Register an existing TTree with a given name and moved unique_ptr. More...
 
StatusCode regTree (const std::string &name, TTree *) override
 
StatusCode getTree (const std::string &name, TTree *&) const override
 Return TTree with given name. More...
 
Functions to manage TGraphs
StatusCode regGraph (const std::string &name) override
 Register a new TGraph with a given name. More...
 
StatusCode regGraph (const std::string &name, std::unique_ptr< TGraph >) override
 Register an existing TGraph with a given name and moved unique_ptr. More...
 
virtual StatusCode regGraph (const std::string &name, TGraph *) override
 
StatusCode getGraph (const std::string &name, TGraph *&) const override
 Return TGraph with given name. More...
 
Functions managing shared objects
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. More...
 
StatusCode regShared (const std::string &name, std::unique_ptr< TH2 >, LockedHandle< TH2 > &) override
 Register shared object of type TH2 and return LockedHandle for that object. More...
 
StatusCode regShared (const std::string &name, std::unique_ptr< TH3 >, LockedHandle< TH3 > &) override
 Register shared object of type TH3 and return LockedHandle for that object. More...
 
StatusCode regShared (const std::string &name, std::unique_ptr< TGraph >, LockedHandle< TGraph > &) override
 Register shared object of type TGraph and return LockedHandle for that object. More...
 
StatusCode getShared (const std::string &name, LockedHandle< TH1 > &) const override
 Retrieve shared object with given name as TH1 through LockedHandle. More...
 
StatusCode getShared (const std::string &name, LockedHandle< TH2 > &) const override
 Retrieve shared object with given name as TH2 through LockedHandle. More...
 
StatusCode getShared (const std::string &name, LockedHandle< TH3 > &) const override
 Retrieve shared object with given name as TH3 through LockedHandle. More...
 
StatusCode getShared (const std::string &name, LockedHandle< TGraph > &) const override
 Retrieve shared object with given name as TGraph through LockedHandle. More...
 
Functions that work on any TObject in the THistSvcMT
StatusCode deReg (const std::string &name) override
 Deregister object with given name and give up ownership (without deletion!) More...
 
StatusCode deReg (TObject *obj) override
 Deregister obejct identified by TObject* and give up ownership (without deletion!) More...
 
StatusCode merge (const std::string &id) override
 Merge all clones for object with a given id. More...
 
StatusCode merge (TObject *) override
 Merge all clones for given TObject*. More...
 
bool exists (const std::string &name) const override
 Check if object with given name is managed by THistSvcMT. More...
 
Functions returning lists of all histograms, trees and graphs
std::vector< std::stringgetHists () const override
 
std::vector< std::stringgetTrees () const override
 
std::vector< std::stringgetGraphs () const override
 
StatusCode getTHists (TDirectory *td, TList &, bool recurse=false) const override
 
StatusCode getTHists (const std::string &name, TList &, bool recurse=false) const override
 
StatusCode getTHists (TDirectory *td, TList &tl, bool recurse=false, bool reg=false) override
 
StatusCode getTHists (const std::string &name, TList &tl, bool recurse=false, bool reg=false) override
 
StatusCode getTTrees (TDirectory *td, TList &, bool recurse=false) const override
 
StatusCode getTTrees (const std::string &name, TList &, bool recurse=false) const override
 
StatusCode getTTrees (TDirectory *td, TList &tl, bool recurse=false, bool reg=false) override
 
StatusCode getTTrees (const std::string &name, TList &tl, bool recurse=false, bool reg=false) override
 
- Public Member Functions inherited from extends< Service, ITHistSvc, IIncidentListener, IIoComponent >
void * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast. More...
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface. More...
 
std::vector< std::stringgetInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
- Public Member Functions inherited from Service
const std::stringname () const override
 Retrieve name of the service. More...
 
StatusCode configure () override
 
StatusCode initialize () override
 
StatusCode start () override
 
StatusCode stop () override
 
StatusCode finalize () override
 
StatusCode terminate () override
 
Gaudi::StateMachine::State FSMState () const override
 
Gaudi::StateMachine::State targetFSMState () const override
 
StatusCode reinitialize () override
 
StatusCode restart () override
 
StatusCode sysInitialize () override
 Initialize Service. More...
 
StatusCode sysStart () override
 Initialize Service. More...
 
StatusCode sysStop () override
 Initialize Service. More...
 
StatusCode sysFinalize () override
 Finalize Service. More...
 
StatusCode sysReinitialize () override
 Re-initialize the Service. More...
 
StatusCode sysRestart () override
 Re-initialize the Service. More...
 
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor. More...
 
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator. More...
 
StatusCode setProperties ()
 Method for setting declared properties to the values specified for the job. More...
 
template<class T >
StatusCode service (const std::string &name, const T *&psvc, bool createIf=true) const
 Access a service by name, creating it if it doesn't already exist. More...
 
template<class T >
StatusCode service (const std::string &name, T *&psvc, bool createIf=true) const
 
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
 
template<class T >
StatusCode service (const std::string &svcType, const std::string &svcName, T *&psvc) const
 Access a service by name and type, creating it if it doesn't already exist. More...
 
template<class T >
StatusCode declareTool (ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
 Declare used tool. More...
 
SmartIF< IAuditorSvc > & auditorSvc () const
 The standard auditor service.May not be invoked before sysInitialize() has been invoked. More...
 
- Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 PropertyHolder ()=default
 
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, TYPE &value, const std::string &doc="none")
 Helper to wrap a regular data member and use it as a regular property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, Gaudi::Property< TYPE, VERIFIER, HANDLERS > &prop, const std::string &doc="none")
 Declare a PropertyBase instance setting name and documentation. More...
 
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property. More...
 
StatusCode setProperty (const Gaudi::Details::PropertyBase &p) override
 set the property form another property More...
 
StatusCode setProperty (const std::string &s) override
 set the property from the formatted string More...
 
StatusCode setProperty (const std::string &n, const std::string &v) override
 set the property from name and the value More...
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value More...
 
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property More...
 
const Gaudi::Details::PropertyBasegetProperty (const std::string &name) const override
 get the property by name More...
 
StatusCode getProperty (const std::string &n, std::string &v) const override
 convert the property to the string More...
 
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties More...
 
bool hasProperty (const std::string &name) const override
 Return true if we have a property with the given name. More...
 
 PropertyHolder (const PropertyHolder &)=delete
 
PropertyHolderoperator= (const PropertyHolder &)=delete
 
- Public Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream) More...
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream More...
 
MSG::Level outputLevel () const
 Backward compatibility function for getting the output level. More...
 
- Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor. More...
 
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service. More...
 
MsgStreammsgStream () const
 Return an uninitialized MsgStream. More...
 
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts. More...
 
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS) More...
 
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL) More...
 
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING) More...
 
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO) More...
 
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG) More...
 
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE) More...
 
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO) More...
 

Private Types

enum  Mode {
  READ, WRITE, UPDATE, APPEND,
  SHARE, INVALID
}
 Enumerating all possible file access modes. More...
 
typedef std::recursive_mutex THistSvcMutex_t
 
typedef std::mutex histMut_t
 

Private Member Functions

Templated helper functions to register and retrieve Histograms and TObjects
template<typename T >
StatusCode regHist_i (std::unique_ptr< T > hist, const std::string &name, bool shared)
 
template<typename T >
StatusCode regHist_i (std::unique_ptr< T > hist, const std::string &name, bool shared, THistID *&hid)
 
template<typename T >
T * getHist_i (const std::string &name, const size_t &ind=0, bool quiet=false) const
 
template<typename T >
T * readHist_i (const std::string &name) const
 
template<typename T >
LockedHandle< T > regShared_i (const std::string &id, std::unique_ptr< T > hist)
 
template<typename T >
LockedHandle< T > getShared_i (const std::string &name) const
 
Collection of private helper methods
template<typename T >
T * readHist (const std::string &name) const
 
TTree * readTree (const std::string &name) const
 
void updateFiles ()
 Handle case where TTree grows beyond TTree::fgMaxTreeSize. More...
 
StatusCode writeObjectsToFile ()
 
StatusCode connect (const std::string &)
 
TDirectory * changeDir (const THistSvc::THistID &hid) const
 
std::string stripDirectoryName (std::string &dir) const
 
void removeDoubleSlash (std::string &) const
 
void MergeRootFile (TDirectory *, TDirectory *)
 
bool findStream (const std::string &name, std::string &root, std::string &rem, TFile *&file) const
 
void parseString (const std::string &id, std::string &root, std::string &rem) const
 
void setupInputFile ()
 call-back method to handle input stream property More...
 
void setupOutputFile ()
 call-back method to handle output stream property More...
 
void copyFileLayout (TDirectory *, TDirectory *)
 helper function to recursively copy the layout of a TFile into a new TFile More...
 
size_t findHistID (const std::string &id, const THistID *&hid, const size_t &index=0) const
 
void dump () const
 
StatusCode merge (const THistID &)
 Helper method to merge THistID objects. More...
 
StatusCode merge (vhid_t *)
 Helper method to merge vectors of THistID. More...
 
StatusCode rootOpenAction (FILEMGR_CALLBACK_ARGS)
 
StatusCode rootOpenErrAction (FILEMGR_CALLBACK_ARGS)
 

Static Private Member Functions

static Mode charToMode (const char typ)
 Convert a char to a Mode enum. More...
 

Private Attributes

IIncidentSvcp_incSvc = nullptr
 
IFileMgrp_fileMgr = nullptr
 
bool m_signaledStop = false
 
bool m_delayConnect = false
 
bool m_okToConnect = false
 
std::string m_curstream
 
THistSvcMutex_t m_svcMut
 
Gaudi properties
Gaudi::Property< int > m_autoSave {this, "AutoSave", 0}
 
Gaudi::Property< int > m_autoFlush {this, "AutoFlush", 0}
 
Gaudi::Property< bool > m_print {this, "PrintAll", false}
 
Gaudi::Property< int > m_maxFileSize
 
Gaudi::Property< int > m_compressionLevel
 
Gaudi::Property< std::vector< std::string > > m_outputfile {this, "Output", {}, &THistSvc::setupOutputFile}
 
Gaudi::Property< std::vector< std::string > > m_inputfile {this, "Input", {}, &THistSvc::setupInputFile}
 

Container definitions

typedef std::vector< THistIDvhid_t
 
typedef std::list< vhid_t * > hlist_t
 
typedef std::unordered_map< std::string, vhid_t * > uidMap_t
 
typedef std::unordered_multimap< std::string, vhid_t * > idMap_t
 
typedef std::unordered_map< TObject *, std::pair< vhid_t *, size_t > > objMap_t
 
typedef std::multimap< std::string, std::stringstreamMap
 
std::vector< std::stringm_Rstream
 
std::vector< std::stringm_Wstream
 
std::set< std::stringm_alreadyConnectedInFiles
 list of already connected files. More...
 
std::set< std::stringm_alreadyConnectedOutFiles
 list of already connected files. More...
 
hlist_t m_hlist
 
uidMap_t m_uids
 
idMap_t m_ids
 
objMap_t m_tobjs
 
std::map< std::string, std::pair< TFile *, Mode > > m_files
 
streamMap m_fileStreams
 
std::map< std::string, std::stringm_sharedFiles
 

Additional Inherited Members

- Public Types inherited from extends< Service, ITHistSvc, IIncidentListener, IIoComponent >
using base_class = extends
 Typedef to this class. More...
 
using extend_interfaces_base = extend_interfaces< Interfaces... >
 Typedef to the base of this class. More...
 
- Public Types inherited from Service
using Factory = Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)>
 
- Public Types inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
using PropertyHolderImpl = PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 Typedef used to refer to this class from derived classes, as in. More...
 
- Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
using base_class = CommonMessaging
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids... >::type
 take union of the ext_iids of all Interfaces... More...
 
- Protected Member Functions inherited from Service
 ~Service () override
 Standard Destructor. More...
 
int outputLevel () const
 get the Service's output level More...
 
- Protected Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
Gaudi::Details::PropertyBaseproperty (const std::string &name) const
 
- Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level setUpMessaging () const
 Set up local caches. More...
 
MSG::Level resetMessaging ()
 Reinitialize internal states. More...
 
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream. More...
 
- Protected Attributes inherited from Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state. More...
 
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state. More...
 
Gaudi::Property< int > m_outputLevel {this, "OutputLevel", MSG::NIL, "output level"}
 
Gaudi::Property< bool > m_auditInit {this, "AuditServices", false, " unused"}
 
Gaudi::Property< bool > m_auditorInitialize {this, "AuditInitialize", false, "trigger auditor on initialize()"}
 
Gaudi::Property< bool > m_auditorStart {this, "AuditStart", false, "trigger auditor on start()"}
 
Gaudi::Property< bool > m_auditorStop {this, "AuditStop", false, "trigger auditor on stop()"}
 
Gaudi::Property< bool > m_auditorFinalize {this, "AuditFinalize", false, "trigger auditor on finalize()"}
 
Gaudi::Property< bool > m_auditorReinitialize {this, "AuditReinitialize", false, "trigger auditor on reinitialize()"}
 
Gaudi::Property< bool > m_auditorRestart {this, "AuditRestart", false, "trigger auditor on restart()"}
 
SmartIF< IAuditorSvcm_pAuditorSvc
 Auditor Service. More...
 

Detailed Description

Definition at line 29 of file THistSvc.h.

Member Typedef Documentation

◆ histMut_t

Definition at line 165 of file THistSvc.h.

◆ hlist_t

typedef std::list<vhid_t*> THistSvc::hlist_t
private

Definition at line 251 of file THistSvc.h.

◆ idMap_t

Definition at line 255 of file THistSvc.h.

◆ objMap_t

typedef std::unordered_map<TObject*, std::pair<vhid_t*, size_t> > THistSvc::objMap_t
private

Definition at line 256 of file THistSvc.h.

◆ streamMap

Definition at line 266 of file THistSvc.h.

◆ THistSvcMutex_t

Definition at line 164 of file THistSvc.h.

◆ uidMap_t

Definition at line 253 of file THistSvc.h.

◆ vhid_t

Definition at line 249 of file THistSvc.h.

Member Enumeration Documentation

◆ Mode

enum THistSvc::Mode
private

Enumerating all possible file access modes.

Enumerator
READ 
WRITE 
UPDATE 
APPEND 
SHARE 
INVALID 

Definition at line 181 of file THistSvc.h.

Member Function Documentation

◆ changeDir()

TDirectory * THistSvc::changeDir ( const THistSvc::THistID hid) const
private

Definition at line 1469 of file THistSvc.cpp.

1469  {
1470  std::string uid = hid.id;
1471  TFile* file = hid.file;
1472  std::string stream, fdir, bdir, dir, id;
1473 
1474  if ( file ) {
1475  file->cd( "/" );
1476  } else {
1477  gROOT->cd();
1478  }
1479 
1480  fdir = uid;
1481  bdir = stripDirectoryName( fdir );
1482 
1483  while ( ( dir = stripDirectoryName( fdir ) ) != "" ) {
1484  if ( !gDirectory->GetKey( dir.c_str() ) ) { gDirectory->mkdir( dir.c_str() ); }
1485  gDirectory->cd( dir.c_str() );
1486  }
1487 
1488  return gDirectory;
1489 }
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1491
STL class.
std::string id
Definition: THistSvc.h:201
T c_str(T... args)

◆ charToMode()

static Mode THistSvc::charToMode ( const char  typ)
inlinestaticprivate

Convert a char to a Mode enum.

Definition at line 184 of file THistSvc.h.

184  {
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  }

◆ connect()

StatusCode THistSvc::connect ( const std::string ident)
private

Definition at line 1292 of file THistSvc.cpp.

1292  {
1293  auto loc = ident.find( " " );
1294  std::string stream = ident.substr( 0, loc );
1295  char typ( 0 );
1297  std::vector<Prop> props;
1298  std::string filename, db_typ( "ROOT" );
1299  int cl( 1 );
1300 
1301  if ( loc != std::string::npos ) {
1302  using Parser = Gaudi::Utils::AttribStringParser;
1303  for ( auto attrib : Parser( ident.substr( loc + 1 ) ) ) {
1304  auto TAG = boost::algorithm::to_upper_copy( attrib.tag );
1305  auto VAL = boost::algorithm::to_upper_copy( attrib.value );
1306 
1307  if ( TAG == "FILE" || TAG == "DATAFILE" ) {
1308  filename = attrib.value;
1310  } else if ( TAG == "OPT" ) {
1311  if ( VAL == "APPEND" || VAL == "UPDATE" ) {
1312  typ = 'A';
1313  } else if ( VAL == "CREATE" || VAL == "NEW" || VAL == "WRITE" ) {
1314  typ = 'N';
1315  } else if ( VAL == "RECREATE" ) {
1316  typ = 'R';
1317  } else if ( VAL == "SHARE" ) {
1318  typ = 'S';
1319  } else if ( VAL == "OLD" || VAL == "READ" ) {
1320  typ = 'O';
1321  } else {
1322  error() << "Unknown OPT: \"" << attrib.value << "\"" << endmsg;
1323  typ = 0;
1324  }
1325  } else if ( TAG == "TYP" ) {
1326  db_typ = std::move( attrib.value );
1327  } else if ( TAG == "CL" ) {
1328  cl = std::stoi( attrib.value );
1329  } else {
1330  props.emplace_back( attrib.tag, attrib.value );
1331  }
1332  }
1333  }
1334 
1335  if ( stream == "temp" ) {
1336  error() << "in JobOption \"" << ident << "\": stream name \"temp\" reserved." << endmsg;
1337  return StatusCode::FAILURE;
1338  }
1339 
1340  if ( db_typ != "ROOT" ) {
1341  error() << "in JobOption \"" << ident << "\": technology type \"" << db_typ << "\" not supported." << endmsg;
1342  return StatusCode::FAILURE;
1343  }
1344 
1345  if ( m_files.find( stream ) != m_files.end() ) {
1346  error() << "in JobOption \"" << ident << "\":\n stream \"" << stream << "\" already connected to file: \""
1347  << m_files[stream].first->GetName() << "\"" << endmsg;
1348  return StatusCode::FAILURE;
1349  }
1350 
1351  const auto newMode = charToMode( typ );
1352  if ( newMode == THistSvc::INVALID ) {
1353  error() << "No OPT= specified or unknown access mode in: " << ident << endmsg;
1354  return StatusCode::FAILURE;
1355  }
1356 
1357  // Is this file already connected to another stream?
1358  if ( m_fileStreams.find( filename ) != m_fileStreams.end() ) {
1359  auto fitr = m_fileStreams.equal_range( filename );
1360 
1361  const std::string& oldstream = fitr.first->second;
1362 
1363  const auto& f_info = m_files[oldstream];
1364 
1365  if ( newMode != f_info.second ) {
1366  error() << "in JobOption \"" << ident << "\":\n file \"" << filename << "\" already opened by stream: \""
1367  << oldstream << "\" with different access mode." << endmsg;
1368  return StatusCode::FAILURE;
1369  } else {
1370  TFile* f2 = f_info.first;
1371  m_files[stream] = std::make_pair( f2, newMode );
1372  if ( msgLevel( MSG::DEBUG ) )
1373  debug() << "Connecting stream: \"" << stream << "\" to previously opened TFile: \"" << filename << "\""
1374  << endmsg;
1375  return StatusCode::SUCCESS;
1376  }
1377  }
1378 
1379  IIncidentSvc* pi = nullptr;
1380  if ( service( "IncidentSvc", pi ).isFailure() ) {
1381  error() << "Unable to get the IncidentSvc" << endmsg;
1382  return StatusCode::FAILURE;
1383  }
1384 
1385  void* vf = nullptr;
1386  TFile* f = nullptr;
1387 
1388  if ( newMode == THistSvc::READ ) {
1389  // old file
1390  int r = p_fileMgr->open( Io::ROOT, name(), filename, Io::READ, vf, "HIST" );
1391 
1392  if ( r != 0 ) {
1393  error() << "Unable to open ROOT file " << filename << " for reading" << endmsg;
1394  return StatusCode::FAILURE;
1395  }
1396 
1397  f = (TFile*)vf;
1398 
1399  // FIX ME!
1400  pi->fireIncident( FileIncident( name(), "BeginHistFile", filename ) );
1401 
1402  } else if ( newMode == THistSvc::WRITE ) {
1403  // new file. error if file exists
1404  int r = p_fileMgr->open( Io::ROOT, name(), filename, ( Io::WRITE | Io::CREATE | Io::EXCL ), vf, "HIST" );
1405 
1406  if ( r != 0 ) {
1407  error() << "Unable to open ROOT file " << filename << " for writing" << endmsg;
1408  return StatusCode::FAILURE;
1409  }
1410 
1411  f = (TFile*)vf;
1412 
1413  } else if ( newMode == THistSvc::APPEND ) {
1414  // update file
1415  int r = p_fileMgr->open( Io::ROOT, name(), filename, ( Io::WRITE | Io::APPEND ), vf, "HIST" );
1416  if ( r != 0 ) {
1417  error() << "unable to open file \"" << filename << "\" for appending" << endmsg;
1418  return StatusCode::FAILURE;
1419  }
1420 
1421  f = (TFile*)vf;
1422 
1423  } else if ( newMode == THistSvc::SHARE ) {
1424  // SHARE file type
1425  // For SHARE files, all data will be stored in a temp file and will be
1426  // merged into the target file in writeObjectsToFile() when finalize(),
1427  // this help to solve some confliction. e.g. with storegate
1428  static int ishared = 0;
1429  std::string realfilename = filename;
1430  filename = "tmp_THistSvc_" + std::to_string( ishared++ ) + ".root";
1431 
1432  if ( msgLevel( MSG::DEBUG ) ) {
1433  debug() << "Creating temp file \"" << filename << "\" and realfilename=" << realfilename << endmsg;
1434  }
1435  m_sharedFiles[stream] = realfilename;
1436 
1437  int r = p_fileMgr->open( Io::ROOT, name(), filename, ( Io::WRITE | Io::CREATE | Io::EXCL ), vf, "HIST" );
1438 
1439  if ( r != 0 ) {
1440  error() << "Unable to open ROOT file " << filename << " for writing" << endmsg;
1441  return StatusCode::FAILURE;
1442  }
1443 
1444  f = (TFile*)vf;
1445 
1446  } else if ( newMode == THistSvc::UPDATE ) {
1447  // update file
1448  int r = p_fileMgr->open( Io::ROOT, name(), filename, ( Io::WRITE | Io::CREATE ), vf, "HIST" );
1449 
1450  if ( r != 0 ) {
1451  error() << "Unable to open ROOT file " << filename << " for appending" << endmsg;
1452  return StatusCode::FAILURE;
1453  }
1454 
1455  f = (TFile*)vf;
1456  }
1457 
1458  m_files[stream] = std::make_pair( f, newMode );
1460 
1461  if ( msgLevel( MSG::DEBUG ) ) {
1462  debug() << "Opening TFile \"" << filename << "\" stream: \"" << stream << "\" mode: \"" << typ << "\""
1463  << " comp level: " << cl << endmsg;
1464  }
1465 
1466  return StatusCode::SUCCESS;
1467 }
Parse attribute strings allowing iteration over the various attributes.
This class is the FileIncident.
Definition: FileIncident.h:17
virtual Io::open_t open(const Io::IoTech &, const std::string &caller, const std::string &fname, const Io::IoFlags &, Io::Fd &, void *&, const std::string &desc, const bool shared=false)=0
T to_string(T... args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T end(T... args)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
static Mode charToMode(const char typ)
Convert a char to a Mode enum.
Definition: THistSvc.h:184
T make_pair(T... args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T move(T... args)
T insert(T... args)
T find(T... args)
STL class.
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1507
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
IFileMgr * p_fileMgr
Definition: THistSvc.h:357
T substr(T... args)
std::map< std::string, std::string > m_sharedFiles
Definition: THistSvc.h:270
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:83
T stoi(T... args)
T equal_range(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
constexpr double pi
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
streamMap m_fileStreams
Definition: THistSvc.h:267
T emplace_back(T... args)

◆ copyFileLayout()

void THistSvc::copyFileLayout ( TDirectory *  destination,
TDirectory *  source 
)
private

helper function to recursively copy the layout of a TFile into a new TFile

Definition at line 1652 of file THistSvc.cpp.

1652  {
1653  if ( msgLevel( MSG::DEBUG ) ) {
1654  debug() << "copyFileLayout() to destination path: " << destination->GetPath() << endmsg;
1655  }
1656 
1657  // strip out URLs
1658  TString path( (char*)strstr( destination->GetPath(), ":" ) );
1659  path.Remove( 0, 2 );
1660 
1661  source->cd( path );
1662  TDirectory* current_source_dir = gDirectory;
1663 
1664  // loop over all keys in this directory
1665  TList* key_list = current_source_dir->GetListOfKeys();
1666  int n = key_list->GetEntries();
1667  for ( int j = 0; j < n; ++j ) {
1668  TKey* k = (TKey*)key_list->At( j );
1669  const std::string source_pathname = current_source_dir->GetPath() + std::string( "/" ) + k->GetName();
1670  TObject* o = source->Get( source_pathname.c_str() );
1671 
1672  if ( o && o->IsA()->InheritsFrom( "TDirectory" ) ) {
1673  if ( msgLevel( MSG::VERBOSE ) ) { verbose() << " subdir [" << o->GetName() << "]..." << endmsg; }
1674  destination->cd();
1675  // Create directory if it does not exist yet
1676  TDirectory* destination_dir = destination->mkdir( o->GetName(), o->GetTitle() );
1677  if ( destination_dir == nullptr ) destination_dir = destination->GetDirectory( o->GetName() );
1678  copyFileLayout( destination_dir, source );
1679  }
1680  } // loop over keys
1681  return;
1682 }
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
Definition: THistSvc.cpp:1652
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T c_str(T... args)
T strstr(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ deReg() [1/2]

StatusCode THistSvc::deReg ( const std::string name)
override

Deregister object with given name and give up ownership (without deletion!)

Definition at line 514 of file THistSvc.cpp.

514  {
515  auto itr = m_uids.find( id );
516  if ( itr == m_uids.end() ) {
517  error() << "Problem deregistering id \"" << id << "\": not found in registry" << endmsg;
518  return StatusCode::FAILURE;
519  }
520 
521  vhid_t* vh = itr->second;
522  debug() << "will deregister " << vh->size() << " elements of id \"" << id << "\"" << endmsg;
524  // vh will get deleted in deReg once empty, so we cannot query the list size in the loop
525  size_t vh_size = vh->size();
526  while ( vh_size-- ) {
527  if ( deReg( vh->back().obj ).isFailure() ) {
528  sc = StatusCode::FAILURE;
529  error() << "Problems deRegistering " << vh->size() << " element of id \"" << id << "\"" << endmsg;
530  break;
531  }
532  }
533 
534  return sc;
535 }
StatusCode deReg(const std::string &name) override
Deregister object with given name and give up ownership (without deletion!)
Definition: THistSvc.cpp:514
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T end(T... args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
std::vector< THistID > vhid_t
Definition: THistSvc.h:249
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T find(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
constexpr static const auto FAILURE
Definition: StatusCode.h:86
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ deReg() [2/2]

StatusCode THistSvc::deReg ( TObject *  obj)
override

Deregister obejct identified by TObject* and give up ownership (without deletion!)

Definition at line 537 of file THistSvc.cpp.

537  {
538  objMap_t::iterator obj_itr = m_tobjs.find( obj );
539  if ( obj_itr != m_tobjs.end() ) {
540  vhid_t* vhid = obj_itr->second.first;
541  THistID hid = obj_itr->second.first->at( obj_itr->second.second );
542 
543  auto uid_itr = m_uids.find( hid.id );
544  if ( uid_itr == m_uids.end() ) {
545  error() << "Problems deregistering TObject \"" << obj->GetName() << "\" with id \"" << hid.id
546  << "\": not in uidMap" << endmsg;
547  return StatusCode::FAILURE;
548  }
549 
550  if ( vhid->size() == 1 ) {
551  // We are the last object, so we have to delete vhid properly
552  debug() << "vhid for " << hid.id << " is empty. deleting" << endmsg;
553 
554  std::string root, rem;
555  parseString( hid.id, root, rem );
556 
557  auto mitr = m_ids.equal_range( rem );
558  auto id_itr = std::find_if( mitr.first, mitr.second,
559  [&]( idMap_t::const_reference i ) { return i.second->at( 0 ).obj == obj; } );
560  if ( id_itr == mitr.second ) {
561  error() << "Problems deregistering TObject \"" << obj->GetName() << "\" with id \"" << hid.id
562  << "\": not in idMap" << endmsg;
563  return StatusCode::FAILURE;
564  }
565 
566  auto hlist_itr = std::find( m_hlist.begin(), m_hlist.end(), vhid );
567  if ( hlist_itr == m_hlist.end() ) {
568  error() << "Problems deregistering TObject \"" << obj->GetName() << "\" with id \"" << hid.id
569  << "\": not in hlist" << endmsg;
570  return StatusCode::FAILURE;
571  }
572 
573  vhid->erase( vhid->begin() + obj_itr->second.second );
574  m_tobjs.erase( obj_itr );
575 
576  m_uids.erase( uid_itr );
577  m_ids.erase( id_itr );
578  m_hlist.erase( hlist_itr );
579 
580  delete vhid;
581 
582  } else if ( vhid->size() > 1 ) {
583  m_tobjs.erase( obj_itr );
584  vhid->erase( vhid->begin() + obj_itr->second.second );
585 
586  // vector of THistID is still not empty (i.e. other instances with same name registered)
587  } else {
588  error() << "Deregistration failed unexpectedly. (bug in THistSvc?)" << endmsg;
589  }
590  return StatusCode::SUCCESS;
591  } else {
592  error() << "Cannot unregister TObject \"" << obj->GetName() << "\": not known to THistSvc" << endmsg;
593  return StatusCode::FAILURE;
594  }
595 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T end(T... args)
objMap_t m_tobjs
Definition: THistSvc.h:263
STL class.
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1594
hlist_t m_hlist
Definition: THistSvc.h:258
idMap_t m_ids
Definition: THistSvc.h:260
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T erase(T... args)
std::vector< THistID > vhid_t
Definition: THistSvc.h:249
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T find(T... args)
T begin(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
constexpr static const auto FAILURE
Definition: StatusCode.h:86
T equal_range(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ dump()

void THistSvc::dump ( ) const
private

Definition at line 1729 of file THistSvc.cpp.

1729  {
1730  std::ostringstream ost;
1731 
1732  // list< vector<THistID> >
1733  ost << "m_hlist: size: " << m_hlist.size() << "\n";
1734  for ( auto& vh : m_hlist ) {
1735  ost << " - " << vh->at( 0 ) << " :: [" << vh << "] " << vh->size() << " {";
1736  for ( auto& e : *vh ) {
1737  TObject* o = e.obj;
1738  ost << "[" << o << "]";
1739  }
1740  ost << "}\n";
1741  }
1742 
1743  // map uid -> vector<THistID>*
1744  ost << "\n"
1745  << "m_uids: " << m_uids.size() << "\n";
1746  for ( auto& e : m_uids ) { ost << " - " << e.first << " [" << e.second << "]" << std::endl; }
1747 
1748  // multimap id -> vector<THistID>*
1749  ost << "\n"
1750  << "m_ids: " << m_ids.size() << "\n";
1751  for ( auto& e : m_ids ) { ost << " - " << e.first << " [" << e.second << "]" << std::endl; }
1752 
1753  // map TObject* -> THistID*
1754  ost << "\n"
1755  << "m_tobjs: " << m_tobjs.size() << "\n";
1756  for ( auto& e : m_tobjs ) {
1757  TObject* o = e.first;
1758  THistID& i = e.second.first->at( e.second.second );
1759  ost << " - " << o << " -> " << i << std::endl;
1760  }
1761 
1762  debug() << "dumping THistSvc contents\n" << ost.str() << endmsg;
1763 }
T endl(T... args)
objMap_t m_tobjs
Definition: THistSvc.h:263
hlist_t m_hlist
Definition: THistSvc.h:258
idMap_t m_ids
Definition: THistSvc.h:260
T str(T... args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T size(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ exists()

bool THistSvc::exists ( const std::string name) const
override

Check if object with given name is managed by THistSvcMT.

Definition at line 617 of file THistSvc.cpp.

617 { return ( getHist_i<TH1>( name, 0, true ) != nullptr ); }
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274

◆ finalize()

StatusCode THistSvc::finalize ( )
override

Definition at line 179 of file THistSvc.cpp.

179  {
180  GlobalDirectoryRestore restore( m_svcMut );
181 
182  if ( msgLevel( MSG::DEBUG ) ) {
183  dump();
184  debug() << "THistSvc::finalize" << endmsg;
185  }
186 
187 #ifndef NDEBUG
188  if ( msgLevel( MSG::DEBUG ) ) {
190  for ( const auto& itr : sorted_uids ) {
191  THistID& thid = itr.second->at( 0 );
192  TObject* tobj = thid.obj;
193 
194  std::string dirname( "none" );
195  if ( tobj && tobj->IsA()->InheritsFrom( "TTree" ) ) {
196  TTree* tree = dynamic_cast<TTree*>( tobj );
197  if ( tree->GetDirectory() != 0 ) { dirname = tree->GetDirectory()->GetPath(); }
198  } else if ( tobj && tobj->IsA()->InheritsFrom( "TGraph" ) ) {
199  if ( !thid.temp ) {
200  dirname = thid.file->GetPath();
201  std::string id2( thid.id );
202  id2.erase( 0, id2.find( "/", 1 ) );
203  id2.erase( id2.rfind( "/" ), id2.length() );
204  if ( id2.find( "/" ) == 0 ) { id2.erase( 0, 1 ); }
205  dirname += id2;
206  } else {
207  dirname = "/tmp";
208  }
209  } else if ( tobj && tobj->IsA()->InheritsFrom( "TH1" ) ) {
210  TH1* th = dynamic_cast<TH1*>( tobj );
211  if ( th == nullptr ) {
212  error() << "Couldn't dcast: " << itr.first << endmsg;
213  } else {
214  if ( th->GetDirectory() != 0 ) { dirname = th->GetDirectory()->GetPath(); }
215  }
216  } else if ( !tobj ) {
217  warning() << itr.first << " has NULL TObject ptr" << endmsg;
218  }
219  debug() << "finalize: " << thid << endmsg;
220  }
221  }
222 #endif
223 
224  if ( writeObjectsToFile().isFailure() ) { error() << "problems writing histograms" << endmsg; }
225 
226  if ( m_print ) { info() << "Listing contents of ROOT files: " << endmsg; }
227  std::vector<TFile*> deleted_files;
228  for ( auto& itr : m_files ) {
229  if ( std::find( deleted_files.begin(), deleted_files.end(), itr.second.first ) == deleted_files.end() ) {
230  deleted_files.push_back( itr.second.first );
231 
232 #ifndef NDEBUG
233  if ( msgLevel( MSG::DEBUG ) ) {
234  debug() << "finalizing stream/file " << itr.first << ":" << itr.second.first->GetName() << endmsg;
235  }
236 #endif
237  } else {
238 #ifndef NDEBUG
239  if ( msgLevel( MSG::DEBUG ) ) { debug() << "already finalized stream " << itr.first << endmsg; }
240 #endif
241  continue;
242  }
243 
244  if ( m_print && msgLevel( MSG::INFO ) ) {
245  info() << "==> File: " << itr.second.first->GetName() << " stream: " << itr.first << endmsg;
246 
247  itr.second.first->Print( "base" );
248  }
249 
250  std::string tmpfn = itr.second.first->GetName();
251 
252  p_fileMgr->close( itr.second.first, name() );
253 
254  IIncidentSvc* pIncidentSvc = nullptr;
255  if ( service( "IncidentSvc", pIncidentSvc ).isFailure() ) {
256  error() << "Unable to get the IncidentSvc" << endmsg;
257  return StatusCode::FAILURE;
258  }
259 
260  if ( itr.second.second == SHARE ) {
261  // Merge File
262  void* vfile = nullptr;
263  int returncode =
264  p_fileMgr->open( Io::ROOT, name(), m_sharedFiles[itr.first], Io::WRITE | Io::APPEND, vfile, "HIST" );
265 
266  if ( returncode ) {
267  error() << "unable to open Final Output File: \"" << m_sharedFiles[itr.first] << "\" for merging" << endmsg;
268  return StatusCode::FAILURE;
269  }
270 
271  TFile* outputfile = (TFile*)vfile;
272  pIncidentSvc->fireIncident( FileIncident( name(), IncidentType::WroteToOutputFile, m_sharedFiles[itr.first] ) );
273 
274  if ( msgLevel( MSG::DEBUG ) ) { debug() << "THistSvc::writeObjectsToFile()::Merging Rootfile " << endmsg; }
275 
276  vfile = nullptr;
277  returncode = p_fileMgr->open( Io::ROOT, name(), tmpfn, Io::READ, vfile, "HIST" );
278 
279  if ( returncode ) {
280  error() << "unable to open temporary file: \"" << tmpfn << endmsg;
281  return StatusCode::FAILURE;
282  }
283 
284  TFile* inputfile = (TFile*)vfile;
285 
286  outputfile->SetCompressionLevel( inputfile->GetCompressionLevel() );
287 
288  MergeRootFile( outputfile, inputfile );
289 
290  outputfile->Write();
291  p_fileMgr->close( outputfile, name() );
292  p_fileMgr->close( inputfile, name() );
293 
294  if ( msgLevel( MSG::DEBUG ) ) { debug() << "Trying to remove temporary file \"" << tmpfn << "\"" << endmsg; }
295 
296  std::remove( tmpfn.c_str() );
297  }
298  delete itr.second.first;
299  }
300 
301  m_files.clear();
304  m_hlist.clear(); // vhid* is deleted in m_tobjs
305  m_uids.clear(); // vhid* is deleted in m_tobjs
306  m_ids.clear(); // vhid* is deleted in m_tobjs
307 
308  for ( auto& obj : m_tobjs ) {
309  // TObject*'s are already dealt with through root file i/o
310  delete obj.second.first; // delete vhid*
311  }
312  m_tobjs.clear();
313 
314  return Service::finalize();
315 }
void dump() const
Definition: THistSvc.cpp:1729
void MergeRootFile(TDirectory *, TDirectory *)
Definition: THistSvc.cpp:1511
This class is the FileIncident.
Definition: FileIncident.h:17
virtual Io::open_t open(const Io::IoTech &, const std::string &caller, const std::string &fname, const Io::IoFlags &, Io::Fd &, void *&, const std::string &desc, const bool shared=false)=0
StatusCode finalize() override
Definition: Service.cpp:164
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
StatusCode writeObjectsToFile()
Definition: THistSvc.cpp:1267
T end(T... args)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
T remove(T... args)
STL class.
objMap_t m_tobjs
Definition: THistSvc.h:263
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
hlist_t m_hlist
Definition: THistSvc.h:258
idMap_t m_ids
Definition: THistSvc.h:260
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
T push_back(T... args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
Gaudi::Property< bool > m_print
Definition: THistSvc.h:341
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
def end
Definition: IOTest.py:113
T clear(T... args)
T find(T... args)
STL class.
T begin(T... args)
T c_str(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
IFileMgr * p_fileMgr
Definition: THistSvc.h:357
std::map< std::string, std::string > m_sharedFiles
Definition: THistSvc.h:270
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:83
AttribStringParser::Iterator begin(const AttribStringParser &parser)
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
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
virtual Io::close_t close(const Io::Fd, const std::string &caller)=0

◆ findHistID()

size_t THistSvc::findHistID ( const std::string id,
const THistID *&  hid,
const size_t &  index = 0 
) const
private

Definition at line 1684 of file THistSvc.cpp.

1684  {
1685  GlobalDirectoryRestore restore( m_svcMut );
1686 
1687  std::string idr( id );
1688  removeDoubleSlash( idr );
1689 
1690  hid = 0;
1691 
1692  if ( idr.find( "/" ) == 0 ) {
1693  // fully specified name, starts with "/"
1694  auto itr = m_uids.find( idr );
1695  if ( itr == m_uids.end() ) {
1696  // no matches found
1697  return 0;
1698  } else {
1699  // one or more matches found (clones).
1700  if ( index >= itr->second->size() ) {
1701  error() << "no index " << index << " found for Hist " << idr << endmsg;
1702  return 0;
1703  }
1704  hid = &( itr->second->at( index ) );
1705  return 1;
1706  }
1707  } else {
1708  // name not fully specified.
1709  auto mitr = m_ids.equal_range( idr );
1710  if ( mitr.first == mitr.second ) {
1711  // not found
1712  return 0;
1713  } else if ( distance( mitr.first, mitr.second ) == 1 ) {
1714  // one found
1715  if ( index >= mitr.first->second->size() ) {
1716  error() << "no index " << index << " found for Hist " << idr << endmsg;
1717  return 0;
1718  }
1719  hid = &( mitr.first->second->at( 0 ) );
1720  return 1;
1721  } else {
1722  // multiple matches
1723  hid = &( mitr.first->second->at( 0 ) );
1724  return distance( mitr.first, mitr.second );
1725  }
1726  }
1727 }
T distance(T... args)
T end(T... args)
STL class.
idMap_t m_ids
Definition: THistSvc.h:260
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T find(T... args)
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1507
uidMap_t m_uids
Definition: THistSvc.h:259
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
T equal_range(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ findStream()

bool THistSvc::findStream ( const std::string name,
std::string root,
std::string rem,
TFile *&  file 
) const
private

Definition at line 1558 of file THistSvc.cpp.

1558  {
1559  auto pos = id.find( "/" );
1560 
1561  if ( pos == std::string::npos ) {
1562  // no "/" in id
1563  stream = "temp";
1564  rem = id;
1565  } else if ( pos != 0 ) {
1566  // id does not start with "/"
1567  stream = "temp";
1568  rem = id;
1569  } else {
1570  // id starts with "/"
1571 
1572  auto pos2 = id.find( "/", pos + 1 );
1573 
1574  if ( pos2 == std::string::npos ) {
1575  // need at least 2 "/" in format "/STREAM/name" or "/STREAM/dir/name"
1576  error() << "badly formed Hist/Tree id: \"" << id << "\"" << endmsg;
1577  return false;
1578  }
1579  parseString( id, stream, rem );
1580  }
1581 
1582  if ( stream == "temp" ) {
1583  file = nullptr;
1584  return true;
1585  }
1586 
1587  auto itr = m_files.find( stream );
1588  file = ( itr != m_files.end() ? itr->second.first : nullptr );
1589  if ( !file ) { warning() << "no stream \"" << stream << "\" associated with id: \"" << id << "\"" << endmsg; }
1590 
1591  return true;
1592 }
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
T end(T... args)
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1594
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T find(T... args)
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getGraph()

StatusCode THistSvc::getGraph ( const std::string name,
TGraph *&  graph 
) const
override

Return TGraph with given name.

Definition at line 433 of file THistSvc.cpp.

433  {
434  graph = getHist_i<TGraph>( id );
435  if ( graph != nullptr ) {
436  return StatusCode::SUCCESS;
437  } else {
438  return StatusCode::FAILURE;
439  }
440 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getGraphs()

std::vector< std::string > THistSvc::getGraphs ( ) const
override

Definition at line 635 of file THistSvc.cpp.

635  {
637  names.reserve( m_uids.size() );
638  transform_if( std::begin( m_uids ), std::end( m_uids ), std::back_inserter( names ), select1st,
639  []( uidMap_t::const_reference i ) { return i.second->at( 0 ).obj->IsA()->InheritsFrom( "TGraph" ); } );
640  return names;
641 }
T end(T... args)
T size(T... args)
T begin(T... args)
T back_inserter(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
T reserve(T... args)

◆ getHist() [1/3]

StatusCode THistSvc::getHist ( const std::string name,
TH1 *&  hist,
size_t  index = 0 
) const
override

Return histogram with given name as TH1*, THistSvcMT still owns object.

Definition at line 339 of file THistSvc.cpp.

339  {
340  hist = getHist_i<TH1>( id, ind );
341  if ( hist != nullptr ) {
342  return StatusCode::SUCCESS;
343  } else {
344  return StatusCode::FAILURE;
345  }
346 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getHist() [2/3]

StatusCode THistSvc::getHist ( const std::string name,
TH2 *&  hist,
size_t  index = 0 
) const
override

Return histogram with given name as TH2*, THistSvcMT still owns object.

Definition at line 348 of file THistSvc.cpp.

348  {
349  hist = getHist_i<TH2>( id, ind );
350  if ( hist != nullptr ) {
351  return StatusCode::SUCCESS;
352  } else {
353  return StatusCode::FAILURE;
354  }
355 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getHist() [3/3]

StatusCode THistSvc::getHist ( const std::string name,
TH3 *&  hist,
size_t  index = 0 
) const
override

Return histogram with given name as TH3*, THistSvcMT still owns object.

Definition at line 357 of file THistSvc.cpp.

357  {
358  hist = getHist_i<TH3>( id, ind );
359  if ( hist != nullptr ) {
360  return StatusCode::SUCCESS;
361  } else {
362  return StatusCode::FAILURE;
363  }
364 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getHist_i()

template<typename T >
T * THistSvc::getHist_i ( const std::string name,
const size_t &  ind = 0,
bool  quiet = false 
) const
private

Definition at line 164 of file THistSvc.icc.

164  {
165  // id starts with "/": unique
166 
167  GlobalDirectoryRestore restore( m_svcMut );
168 
169  T* hist = nullptr;
170  const THistID* hid = nullptr;
171  size_t num = findHistID( id, hid, ind );
172  if ( num == 0 ) {
173  // no matches found
174  if ( !quiet ) { error() << "could not locate Hist with id \"" << id << "\"" << endmsg; }
175  return nullptr;
176  } else if ( num > 1 ) {
177  if ( !quiet ) {
178  // return failure if trying to GET a single hist
179  error() << "Multiple matches with id \"" << id << "\"."
180  << " Further specifications required." << endmsg;
181  return nullptr;
182  } else {
183  info() << "Found multiple matches with id \"" << id << "\"" << endmsg;
184  // return first match if just INQUIRING (i.e. != nullptr)
185  hist = dynamic_cast<T*>( hid->obj );
186  if ( hist == nullptr ) {
187  error() << "dcast failed, Hist id: \"" << id << "\"" << endmsg;
188  return nullptr;
189  }
190  }
191  } else {
192  hist = dynamic_cast<T*>( hid->obj );
193  if ( hist == nullptr ) {
194  error() << "dcast failed, Hist id: \"" << id << "\"" << endmsg;
195  return nullptr;
196  }
197  verbose() << "found unique Hist title: \"" << hist->GetTitle() << "\" id: \"" << id << "\"" << endmsg;
198  }
199 
200  return hist;
201 }
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1684
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getHists()

std::vector< std::string > THistSvc::getHists ( ) const
override

Definition at line 619 of file THistSvc.cpp.

619  {
621  names.reserve( m_uids.size() );
622  transform_if( std::begin( m_uids ), std::end( m_uids ), std::back_inserter( names ), select1st,
623  []( uidMap_t::const_reference i ) { return i.second->at( 0 ).obj->IsA()->InheritsFrom( "TH1" ); } );
624  return names;
625 }
T end(T... args)
T size(T... args)
T begin(T... args)
T back_inserter(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
T reserve(T... args)

◆ getShared() [1/4]

StatusCode THistSvc::getShared ( const std::string name,
LockedHandle< TH1 > &  lh 
) const
override

Retrieve shared object with given name as TH1 through LockedHandle.

Definition at line 478 of file THistSvc.cpp.

478  {
479  lh = getShared_i<TH1>( name );
480  if ( lh ) {
481  return StatusCode::SUCCESS;
482  } else {
483  return StatusCode::FAILURE;
484  }
485 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getShared() [2/4]

StatusCode THistSvc::getShared ( const std::string name,
LockedHandle< TH2 > &  lh 
) const
override

Retrieve shared object with given name as TH2 through LockedHandle.

Definition at line 487 of file THistSvc.cpp.

487  {
488  lh = getShared_i<TH2>( name );
489  if ( lh ) {
490  return StatusCode::SUCCESS;
491  } else {
492  return StatusCode::FAILURE;
493  }
494 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getShared() [3/4]

StatusCode THistSvc::getShared ( const std::string name,
LockedHandle< TH3 > &  lh 
) const
override

Retrieve shared object with given name as TH3 through LockedHandle.

Definition at line 496 of file THistSvc.cpp.

496  {
497  lh = getShared_i<TH3>( name );
498  if ( lh ) {
499  return StatusCode::SUCCESS;
500  } else {
501  return StatusCode::FAILURE;
502  }
503 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getShared() [4/4]

StatusCode THistSvc::getShared ( const std::string name,
LockedHandle< TGraph > &  lh 
) const
override

Retrieve shared object with given name as TGraph through LockedHandle.

Definition at line 505 of file THistSvc.cpp.

505  {
506  lh = getShared_i<TGraph>( name );
507  if ( lh ) {
508  return StatusCode::SUCCESS;
509  } else {
510  return StatusCode::FAILURE;
511  }
512 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getShared_i()

template<typename T >
LockedHandle< T > THistSvc::getShared_i ( const std::string name) const
private

Definition at line 292 of file THistSvc.icc.

292  {
293  GlobalDirectoryRestore restore( m_svcMut );
294 
295  const THistID* hid = nullptr;
296  size_t i = findHistID( name, hid );
297 
298  LockedHandle<T> hist( nullptr, nullptr );
299 
300  if ( i == 1 ) {
301  if ( !hid->shared ) {
302  error() << "getSharedHist: found Hist with id \"" << name << "\", but it's not marked as shared" << endmsg;
303  return hist;
304  }
305  T* h1 = dynamic_cast<T*>( hid->obj );
306  hist = LockedHandle<T>( h1, hid->mutex );
307 
308  debug() << "getSharedHist: found THistID: " << *hid << endmsg;
309  } else if ( i == 0 ) {
310  error() << "no histograms matching id \"" << name << "\" found" << endmsg;
311  } else {
312  info() << "multiple matches for id \"" << name << "\" found [" << i << "], probably from different streams"
313  << endmsg;
314  }
315  return hist;
316 }
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1684
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTHists() [1/4]

StatusCode THistSvc::getTHists ( TDirectory *  td,
TList &  tl,
bool  recurse = false 
) const
override

Definition at line 643 of file THistSvc.cpp.

643  {
644  GlobalDirectoryRestore restore( m_svcMut );
645 
646  gErrorIgnoreLevel = kBreak;
647 
648  if ( !td->cd() ) {
649  error() << "getTHists: No such TDirectory \"" << td->GetPath() << "\"" << endmsg;
650  return StatusCode::FAILURE;
651  }
652 
653  if ( msgLevel( MSG::DEBUG ) ) {
654  debug() << "getTHists: \"" << td->GetPath() << "\": found " << td->GetListOfKeys()->GetSize() << " keys" << endmsg;
655  }
656 
657  TIter nextkey( td->GetListOfKeys() );
658  while ( TKey* key = (TKey*)nextkey() ) {
659  auto& log = debug();
660  if ( msgLevel( MSG::DEBUG ) ) log << " key: " << key->GetName();
661  TObject* obj = key->ReadObj();
662  if ( obj != 0 && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
663  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
664  } else if ( obj != 0 && obj->IsA()->InheritsFrom( "TH1" ) ) {
665  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
666  tl.Add( obj );
667  } else if ( obj != 0 ) {
668  if ( msgLevel( MSG::DEBUG ) ) log << " [" << obj->IsA()->GetName() << "]";
669  }
670  if ( msgLevel( MSG::DEBUG ) ) log << endmsg;
671  }
672 
673  // operate recursively
674  if ( rcs ) {
675  nextkey = td->GetListOfKeys();
676  while ( TKey* key = (TKey*)nextkey() ) {
677  TObject* obj = key->ReadObj();
678  if ( obj && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
679  TDirectory* tt = dynamic_cast<TDirectory*>( obj );
680  getTHists( tt, tl, rcs );
681  }
682  }
683  }
684 
685  return StatusCode::SUCCESS;
686 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:643
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
constexpr static const auto FAILURE
Definition: StatusCode.h:86
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTHists() [2/4]

StatusCode THistSvc::getTHists ( const std::string name,
TList &  tl,
bool  recurse = false 
) const
override

Definition at line 688 of file THistSvc.cpp.

688  {
689 
690  GlobalDirectoryRestore restore( m_svcMut );
691 
692  gErrorIgnoreLevel = kBreak;
693 
694  StatusCode sc;
695 
696  std::string stream, rem, r2;
697  parseString( dir, stream, rem );
698 
699  auto itr = m_files.find( stream );
700  if ( itr != m_files.end() ) {
701  r2 = itr->second.first->GetName();
702  r2 += ":/";
703  r2 += rem;
704 
705  if ( msgLevel( MSG::DEBUG ) ) {
706  debug() << "getTHists: \"" << dir << "\" looks like a stream name."
707  << " associated TFile: \"" << itr->second.first->GetName() << "\"" << endmsg;
708  }
709 
710  if ( gDirectory->cd( r2.c_str() ) ) {
711  m_curstream = stream;
712  sc = getTHists( gDirectory, tl, rcs );
713  m_curstream = "";
714  return sc;
715  } else {
716  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTHists: no such TDirectory \"" << r2 << "\"" << endmsg; }
717  }
718 
719  } else {
720  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTHists: stream \"" << stream << "\" not found" << endmsg; }
721  }
722 
723  if ( !gDirectory->cd( dir.c_str() ) ) {
724  error() << "getTHists: No such TDirectory/stream \"" << dir << "\"" << endmsg;
725  sc = StatusCode::FAILURE;
726  } else {
727  sc = getTHists( gDirectory, tl, rcs );
728  }
729 
730  return sc;
731 }
std::string m_curstream
Definition: THistSvc.h:363
T end(T... args)
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:643
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1594
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T find(T... args)
T c_str(T... args)
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTHists() [3/4]

StatusCode THistSvc::getTHists ( TDirectory *  td,
TList &  tl,
bool  recurse = false,
bool  reg = false 
)
override

Definition at line 733 of file THistSvc.cpp.

733  {
734  GlobalDirectoryRestore restore( m_svcMut );
735 
736  gErrorIgnoreLevel = kBreak;
737 
738  if ( !td->cd() ) {
739  error() << "getTHists: No such TDirectory \"" << td->GetPath() << "\"" << endmsg;
740  return StatusCode::FAILURE;
741  }
742 
743  if ( msgLevel( MSG::DEBUG ) ) {
744  debug() << "getTHists: \"" << td->GetPath() << "\": found " << td->GetListOfKeys()->GetSize() << " keys" << endmsg;
745  }
746 
747  TIter nextkey( td->GetListOfKeys() );
748  while ( TKey* key = (TKey*)nextkey() ) {
749  auto& log = debug();
750  if ( msgLevel( MSG::DEBUG ) ) log << " key: " << key->GetName();
751  TObject* obj = key->ReadObj();
752  if ( obj && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
753  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
754  } else if ( obj && obj->IsA()->InheritsFrom( "TH1" ) ) {
755  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
756  tl.Add( obj );
757  if ( reg && m_curstream != "" ) {
758  std::string dir = td->GetPath();
759  std::string fil = td->GetFile()->GetName();
760  dir.erase( 0, fil.length() + 1 );
761  std::string id = "/" + m_curstream;
762  if ( dir == "/" ) {
763  id = id + "/" + key->GetName();
764  } else {
765  id = id + dir + "/" + key->GetName();
766  }
767  if ( !exists( id ) ) {
768  if ( msgLevel( MSG::DEBUG ) ) log << " reg as \"" << id << "\"";
769  regHist( id ).ignore();
770  } else {
771  if ( msgLevel( MSG::DEBUG ) ) log << " already registered";
772  }
773  }
774  } else if ( obj ) {
775  if ( msgLevel( MSG::DEBUG ) ) log << " [" << obj->IsA()->GetName() << "]";
776  }
777  if ( msgLevel( MSG::DEBUG ) ) log << endmsg;
778  }
779 
780  // operate recursively
781  if ( rcs ) {
782  nextkey = td->GetListOfKeys();
783  while ( TKey* key = (TKey*)nextkey() ) {
784  TObject* obj = key->ReadObj();
785  if ( obj && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
786  TDirectory* tt = dynamic_cast<TDirectory*>( obj );
787  getTHists( tt, tl, rcs, reg );
788  }
789  }
790  }
791 
792  return StatusCode::SUCCESS;
793 }
std::string m_curstream
Definition: THistSvc.h:363
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:643
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T erase(T... args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
StatusCode regHist(const std::string &name) override
Register a new ROOT histogram TH*X with a name.
Definition: THistSvc.cpp:319
T length(T... args)
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
constexpr static const auto FAILURE
Definition: StatusCode.h:86
bool exists(const std::string &name) const override
Check if object with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:617
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTHists() [4/4]

StatusCode THistSvc::getTHists ( const std::string name,
TList &  tl,
bool  recurse = false,
bool  reg = false 
)
override

Definition at line 795 of file THistSvc.cpp.

795  {
796  GlobalDirectoryRestore restore( m_svcMut );
797 
798  gErrorIgnoreLevel = kBreak;
799 
800  StatusCode sc;
801 
802  std::string stream, rem, r2;
803  parseString( dir, stream, rem );
804 
805  auto itr = m_files.find( stream );
806  if ( itr != m_files.end() ) {
807  r2 = itr->second.first->GetName();
808  r2 += ":/";
809  r2 += rem;
810 
811  if ( msgLevel( MSG::DEBUG ) ) {
812  debug() << "getTHists: \"" << dir << "\" looks like a stream name."
813  << " associated TFile: \"" << itr->second.first->GetName() << "\"" << endmsg;
814  }
815 
816  if ( gDirectory->cd( r2.c_str() ) ) {
817  m_curstream = stream;
818  sc = getTHists( gDirectory, tl, rcs, reg );
819  m_curstream.clear();
820  return sc;
821  }
822  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTHists: no such TDirectory \"" << r2 << "\"" << endmsg; }
823  } else {
824  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTHists: stream \"" << stream << "\" not found" << endmsg; }
825  }
826 
827  if ( !gDirectory->cd( dir.c_str() ) ) {
828  error() << "getTHists: No such TDirectory/stream \"" << dir << "\"" << endmsg;
829  sc = StatusCode::FAILURE;
830  } else {
831  if ( reg ) {
832  warning() << "Unable to register histograms automatically "
833  << "without a valid stream name" << endmsg;
834  reg = false;
835  }
836  sc = getTHists( gDirectory, tl, rcs, reg );
837  }
838 
839  return sc;
840 }
std::string m_curstream
Definition: THistSvc.h:363
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
T end(T... args)
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:643
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1594
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T clear(T... args)
T find(T... args)
T c_str(T... args)
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTree()

StatusCode THistSvc::getTree ( const std::string name,
TTree *&  tree 
) const
override

Return TTree with given name.

Definition at line 392 of file THistSvc.cpp.

392  {
393  tree = getHist_i<TTree>( id );
394  if ( tree != nullptr ) {
395  return StatusCode::SUCCESS;
396  } else {
397  return StatusCode::FAILURE;
398  }
399 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ getTrees()

std::vector< std::string > THistSvc::getTrees ( ) const
override

Definition at line 627 of file THistSvc.cpp.

627  {
629  names.reserve( m_uids.size() );
630  transform_if( std::begin( m_uids ), std::end( m_uids ), std::back_inserter( names ), select1st,
631  []( uidMap_t::const_reference i ) { return i.second->at( 0 ).obj->IsA()->InheritsFrom( "TTree" ); } );
632  return names;
633 }
T end(T... args)
T size(T... args)
T begin(T... args)
T back_inserter(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
T reserve(T... args)

◆ getTTrees() [1/4]

StatusCode THistSvc::getTTrees ( TDirectory *  td,
TList &  tl,
bool  recurse = false 
) const
override

Definition at line 842 of file THistSvc.cpp.

842  {
843  GlobalDirectoryRestore restore( m_svcMut );
844 
845  gErrorIgnoreLevel = kBreak;
846 
847  if ( !td->cd() ) {
848  error() << "getTTrees: No such TDirectory \"" << td->GetPath() << "\"" << endmsg;
849  return StatusCode::FAILURE;
850  }
851 
852  if ( msgLevel( MSG::DEBUG ) ) {
853  debug() << "getTHists: \"" << td->GetPath() << "\": found " << td->GetListOfKeys()->GetSize() << " keys" << endmsg;
854  }
855 
856  TIter nextkey( td->GetListOfKeys() );
857  while ( TKey* key = (TKey*)nextkey() ) {
858  auto& log = debug();
859  if ( msgLevel( MSG::DEBUG ) ) log << " key: " << key->GetName();
860  TObject* obj = key->ReadObj();
861  if ( obj != 0 && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
862  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
863  } else if ( obj != 0 && obj->IsA()->InheritsFrom( "TTree" ) ) {
864  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
865  tl.Add( obj );
866  } else if ( obj != 0 ) {
867  if ( msgLevel( MSG::DEBUG ) ) log << " [" << obj->IsA()->GetName() << "]";
868  }
869  log << endmsg;
870  }
871 
872  // operate recursively
873  if ( rcs ) {
874  nextkey = td->GetListOfKeys();
875  while ( TKey* key = (TKey*)nextkey() ) {
876  TObject* obj = key->ReadObj();
877  if ( obj && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
878  TDirectory* tt = dynamic_cast<TDirectory*>( obj );
879  getTTrees( tt, tl, rcs );
880  }
881  }
882  }
883 
884  return StatusCode::SUCCESS;
885 }
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:842
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
constexpr static const auto FAILURE
Definition: StatusCode.h:86
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTTrees() [2/4]

StatusCode THistSvc::getTTrees ( const std::string name,
TList &  tl,
bool  recurse = false 
) const
override

Definition at line 887 of file THistSvc.cpp.

887  {
888  GlobalDirectoryRestore restore( m_svcMut );
889 
890  gErrorIgnoreLevel = kBreak;
891 
892  StatusCode sc;
893 
894  std::string stream, rem, r2;
895  parseString( dir, stream, rem );
896 
897  auto itr = m_files.find( stream );
898  if ( itr != m_files.end() ) {
899  r2 = itr->second.first->GetName();
900  r2 += ":/";
901  r2 += rem;
902 
903  if ( msgLevel( MSG::DEBUG ) ) {
904  debug() << "getTTrees: \"" << dir << "\" looks like a stream name."
905  << " associated TFile: \"" << itr->second.first->GetName() << "\"" << endmsg;
906  }
907 
908  if ( gDirectory->cd( r2.c_str() ) ) { return getTTrees( gDirectory, tl, rcs ); }
909  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTTrees: no such TDirectory \"" << r2 << "\"" << endmsg; }
910  } else {
911  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTTrees: stream \"" << stream << "\" not found" << endmsg; }
912  }
913 
914  if ( !gDirectory->cd( dir.c_str() ) ) {
915  error() << "getTTrees: No such TDirectory/stream \"" << dir << "\"" << endmsg;
916  sc = StatusCode::FAILURE;
917  } else {
918  sc = getTTrees( gDirectory, tl, rcs );
919  }
920  return sc;
921 }
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:842
T end(T... args)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1594
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T find(T... args)
T c_str(T... args)
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTTrees() [3/4]

StatusCode THistSvc::getTTrees ( TDirectory *  td,
TList &  tl,
bool  recurse = false,
bool  reg = false 
)
override

Definition at line 923 of file THistSvc.cpp.

923  {
924  GlobalDirectoryRestore restore( m_svcMut );
925 
926  gErrorIgnoreLevel = kBreak;
927 
928  if ( !td->cd() ) {
929  error() << "getTTrees: No such TDirectory \"" << td->GetPath() << "\"" << endmsg;
930  return StatusCode::FAILURE;
931  }
932 
933  if ( msgLevel( MSG::DEBUG ) ) {
934  debug() << "getTHists: \"" << td->GetPath() << "\": found " << td->GetListOfKeys()->GetSize() << " keys" << endmsg;
935  }
936 
937  TIter nextkey( td->GetListOfKeys() );
938  while ( TKey* key = (TKey*)nextkey() ) {
939  auto& log = debug();
940  if ( msgLevel( MSG::DEBUG ) ) log << " key: " << key->GetName();
941  TObject* obj = key->ReadObj();
942  if ( obj && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
943  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
944  } else if ( obj && obj->IsA()->InheritsFrom( "TTree" ) ) {
945  if ( msgLevel( MSG::DEBUG ) ) log << " (" << obj->IsA()->GetName() << ")";
946  tl.Add( obj );
947  if ( reg && m_curstream != "" ) {
948  std::string dir = td->GetPath();
949  std::string fil = td->GetFile()->GetName();
950  dir.erase( 0, fil.length() + 1 );
951  std::string id = "/" + m_curstream;
952  if ( dir == "/" ) {
953  id = id + "/" + key->GetName();
954  } else {
955  id = id + dir + "/" + key->GetName();
956  }
957  if ( !exists( id ) ) {
958  if ( msgLevel( MSG::DEBUG ) ) log << " reg as \"" << id << "\"";
959  regHist( id ).ignore();
960  } else {
961  if ( msgLevel( MSG::DEBUG ) ) log << " already registered";
962  }
963  }
964  } else if ( obj != 0 ) {
965  if ( msgLevel( MSG::DEBUG ) ) log << " [" << obj->IsA()->GetName() << "]";
966  }
967  if ( msgLevel( MSG::DEBUG ) ) log << endmsg;
968  }
969 
970  // operate recursively
971  if ( rcs ) {
972  nextkey = td->GetListOfKeys();
973  while ( TKey* key = (TKey*)nextkey() ) {
974  TObject* obj = key->ReadObj();
975  if ( obj && obj->IsA()->InheritsFrom( "TDirectory" ) ) {
976  TDirectory* tt = dynamic_cast<TDirectory*>( obj );
977  getTTrees( tt, tl, rcs, reg );
978  }
979  }
980  }
981 
982  return StatusCode::SUCCESS;
983 }
std::string m_curstream
Definition: THistSvc.h:363
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:842
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T erase(T... args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
StatusCode regHist(const std::string &name) override
Register a new ROOT histogram TH*X with a name.
Definition: THistSvc.cpp:319
T length(T... args)
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
constexpr static const auto FAILURE
Definition: StatusCode.h:86
bool exists(const std::string &name) const override
Check if object with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:617
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ getTTrees() [4/4]

StatusCode THistSvc::getTTrees ( const std::string name,
TList &  tl,
bool  recurse = false,
bool  reg = false 
)
override

Definition at line 985 of file THistSvc.cpp.

985  {
986  GlobalDirectoryRestore restore( m_svcMut );
987 
988  gErrorIgnoreLevel = kBreak;
989 
990  StatusCode sc;
991 
992  std::string stream, rem, r2;
993  parseString( dir, stream, rem );
994 
995  auto itr = m_files.find( stream );
996  if ( itr != m_files.end() ) {
997  r2 = itr->second.first->GetName();
998  r2 += ":/";
999  r2 += rem;
1000 
1001  if ( msgLevel( MSG::DEBUG ) ) {
1002  debug() << "getTTrees: \"" << dir << "\" looks like a stream name."
1003  << " associated TFile: \"" << itr->second.first->GetName() << "\"" << endmsg;
1004  }
1005 
1006  if ( gDirectory->cd( r2.c_str() ) ) {
1007  return getTTrees( gDirectory, tl, rcs, reg );
1008  } else {
1009  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTTrees: no such TDirectory \"" << r2 << "\"" << endmsg; }
1010  }
1011  } else {
1012  if ( msgLevel( MSG::DEBUG ) ) { debug() << "getTTrees: stream \"" << stream << "\" not found" << endmsg; }
1013  }
1014 
1015  if ( !gDirectory->cd( dir.c_str() ) ) {
1016  error() << "getTTrees: No such TDirectory/stream \"" << dir << "\"" << endmsg;
1017  return StatusCode::FAILURE;
1018  }
1019 
1020  return getTTrees( gDirectory, tl, rcs, reg );
1021 }
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
Definition: THistSvc.cpp:842
T end(T... args)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1594
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T find(T... args)
T c_str(T... args)
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ handle()

void THistSvc::handle ( const Incident )
override

Definition at line 1025 of file THistSvc.cpp.

1025  {
1026  if ( m_signaledStop ) return;
1027 
1028  if ( m_maxFileSize.value() == -1 ) return;
1029 
1030  // convert to bytes.
1031  Long64_t mfs = (Long64_t)m_maxFileSize.value() * (Long64_t)1048576;
1032  Long64_t mfs_warn = mfs * 95 / 100;
1033 
1034  updateFiles();
1035 
1036  for ( const auto& f : m_files ) {
1037  TFile* tf = f.second.first;
1038 
1039 #ifndef NDEBUG
1040  if ( msgLevel( MSG::DEBUG ) ) {
1041  debug() << "stream: " << f.first << " name: " << tf->GetName() << " size: " << tf->GetSize() << endmsg;
1042  }
1043 #endif
1044 
1045  // Signal job to terminate if output file is too large
1046  if ( tf->GetSize() > mfs ) {
1047 
1048  m_signaledStop = true;
1049 
1050  fatal() << "file \"" << tf->GetName() << "\" associated with stream \"" << f.first
1051  << "\" has exceeded the max file size of " << m_maxFileSize.value() << "MB. Terminating Job." << endmsg;
1052 
1053  IEventProcessor* evt = nullptr;
1054  if ( service( "ApplicationMgr", evt, true ).isSuccess() ) {
1055  evt->stopRun();
1056  evt->release();
1057  } else {
1058  abort();
1059  }
1060  } else if ( tf->GetSize() > mfs_warn ) {
1061  warning() << "file \"" << tf->GetName() << "\" associated with stream \"" << f.first
1062  << "\" is at 95% of its maximum allowable file size of " << m_maxFileSize.value() << "MB" << endmsg;
1063  }
1064  }
1065 }
bool m_signaledStop
Definition: THistSvc.h:359
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Gaudi::Property< int > m_maxFileSize
Definition: THistSvc.h:342
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
The IEventProcessor is the interface to process events.
T abort(T... args)
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:83
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
evt
Definition: IOTest.py:95
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
Definition: THistSvc.cpp:1190

◆ initialize()

StatusCode THistSvc::initialize ( )
override

Definition at line 68 of file THistSvc.cpp.

68  {
70 
72 
73  if ( status.isFailure() ) {
74  error() << "initializing service" << endmsg;
75  return status;
76  }
77 
79 
80  try {
82  } catch ( GaudiException& err ) {
83  error() << "Caught: " << err << endmsg;
85  }
86 
87  try {
89  } catch ( GaudiException& err ) {
90  error() << "Caught: " << err << endmsg;
92  }
93 
94  // Protect against multiple instances of TROOT
95  if ( !gROOT ) {
96  static TROOT root( "root", "ROOT I/O" );
97  // gDebug = 99;
98  } else {
99  if ( msgLevel( MSG::VERBOSE ) ) { verbose() << "ROOT already initialized, debug = " << gDebug << endmsg; }
100  }
101 
102  if ( service( "IncidentSvc", p_incSvc, true ).isFailure() ) {
103  error() << "unable to get the IncidentSvc" << endmsg;
104  st = StatusCode::FAILURE;
105  } else {
106  p_incSvc->addListener( this, "EndEvent", 100, true );
107  }
108 
109  if ( service( "FileMgr", p_fileMgr, true ).isFailure() ) {
110  error() << "unable to get the FileMgr" << endmsg;
111  st = StatusCode::FAILURE;
112  } else {
113  debug() << "got the FileMgr" << endmsg;
114  }
115 
116  // Register open/close callback actions
117  using namespace std::placeholders;
118  auto boa = [this]( const Io::FileAttr* fa, const std::string& caller ) { return this->rootOpenAction( fa, caller ); };
119  if ( p_fileMgr->regAction( boa, Io::OPEN, Io::ROOT ).isFailure() ) {
120  error() << "unable to register ROOT file open action with FileMgr" << endmsg;
121  }
122  auto bea = [this]( const Io::FileAttr* fa, const std::string& caller ) {
123  return this->rootOpenErrAction( fa, caller );
124  };
125  if ( p_fileMgr->regAction( bea, Io::OPEN_ERR, Io::ROOT ).isFailure() ) {
126  error() << "unable to register ROOT file open Error action with FileMgr" << endmsg;
127  }
128 
129  m_okToConnect = true;
130  if ( m_delayConnect ) {
131  if ( !m_inputfile.value().empty() ) setupInputFile();
132  if ( !m_outputfile.value().empty() ) setupOutputFile();
133  m_delayConnect = false;
134  }
137 
138  IIoComponentMgr* iomgr = nullptr;
139  if ( service( "IoComponentMgr", iomgr, true ).isFailure() ) {
140  error() << "unable to get the IoComponentMgr" << endmsg;
141  st = StatusCode::FAILURE;
142  } else {
143  if ( !iomgr->io_register( this ).isSuccess() ) {
144  error() << "could not register with the I/O component manager !" << endmsg;
145  st = StatusCode::FAILURE;
146  } else {
147  bool all_good = true;
148  // register input/output files...
149  for ( const auto& reg : m_files ) {
150  const std::string& fname = reg.second.first->GetName();
151  const IIoComponentMgr::IoMode::Type iomode =
153  if ( !iomgr->io_register( this, iomode, fname ).isSuccess() ) {
154  warning() << "could not register file [" << fname << "] with the I/O component manager..." << endmsg;
155  all_good = false;
156  } else {
157  info() << "registered file [" << fname << "]... [ok]" << endmsg;
158  }
159  }
160  if ( !all_good ) {
161  error() << "problem while registering input/output files with "
162  << "the I/O component manager !" << endmsg;
163  st = StatusCode::FAILURE;
164  }
165  }
166  }
167 
168  if ( st.isFailure() ) { fatal() << "Unable to initialize THistSvc" << endmsg; }
169 
170  return st;
171 }
bool m_delayConnect
Definition: THistSvc.h:360
StatusCode initialize() override
Definition: Service.cpp:60
Define general base for Gaudi exception.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
IIncidentSvc * p_incSvc
Definition: THistSvc.h:356
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
StatusCode rootOpenAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1809
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: THistSvc.h:351
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action &, const std::string &d="")=0
void setupOutputFile()
call-back method to handle output stream property
Definition: THistSvc.cpp:1632
void setupInputFile()
call-back method to handle input stream property
Definition: THistSvc.cpp:1608
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Gaudi::Property< std::vector< std::string > > m_inputfile
Definition: THistSvc.h:352
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
bool m_okToConnect
Definition: THistSvc.h:361
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
StatusCode rootOpenErrAction(FILEMGR_CALLBACK_ARGS)
Definition: THistSvc.cpp:1831
T clear(T... args)
bool isSuccess() const
Definition: StatusCode.h:267
virtual void addListener(IIncidentListener *lis, const std::string &type="", long priority=0, bool rethrow=false, bool singleShot=false)=0
Add listener.
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
virtual StatusCode io_register(IIoComponent *iocomponent)=0
: allow a IIoComponent to register itself with this manager so appropriate actions can be taken when ...
IFileMgr * p_fileMgr
Definition: THistSvc.h:357
std::set< std::string > m_alreadyConnectedOutFiles
list of already connected files.
Definition: THistSvc.h:245
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:83
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
bool isFailure() const
Definition: StatusCode.h:130
std::set< std::string > m_alreadyConnectedInFiles
list of already connected files.
Definition: THistSvc.h:241
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ io_reinit()

StatusCode THistSvc::io_reinit ( )
override

callback method to reinitialize the internal state of the component for I/O purposes (e.g.

upon fork(2))

Definition at line 1070 of file THistSvc.cpp.

1070  {
1071  bool all_good = true;
1072  if ( msgLevel( MSG::DEBUG ) ) { debug() << "reinitializing I/O..." << endmsg; }
1073 
1074  // retrieve the I/O component manager...
1075 
1076  IIoComponentMgr* iomgr = nullptr;
1077 
1078  if ( service( "IoComponentMgr", iomgr, true ).isFailure() ) {
1079  error() << "could not retrieve I/O component manager !" << endmsg;
1080  return StatusCode::FAILURE;
1081  }
1082 
1083  GlobalDirectoryRestore restore( m_svcMut );
1084  // to hide the expected errors upon closing the files whose
1085  // file descriptors have been swept under the rug...
1086  gErrorIgnoreLevel = kFatal;
1087 
1088  for ( auto& ifile : m_files ) {
1089  TFile* f = ifile.second.first;
1090  std::string fname = f->GetName();
1091  if ( msgLevel( MSG::DEBUG ) ) {
1092  debug() << "file [" << fname << "] mode: [" << f->GetOption() << "] r:" << f->GetFileBytesRead()
1093  << " w:" << f->GetFileBytesWritten() << " cnt:" << f->GetFileCounter() << endmsg;
1094  }
1095 
1096  if ( ifile.second.second == READ ) {
1097  if ( msgLevel( MSG::DEBUG ) ) { debug() << " TFile opened in READ mode: not reassigning names" << endmsg; }
1098  continue;
1099  }
1100 
1101  if ( !iomgr->io_retrieve( this, fname ).isSuccess() ) {
1102  error() << "could not retrieve new name for [" << fname << "] !!" << endmsg;
1103  all_good = false;
1104  continue;
1105  } else if ( fname.empty() ) {
1106  if ( msgLevel( MSG::DEBUG ) ) { debug() << "empty new name for [" << fname << "], skipping..." << endmsg; }
1107  continue;
1108  } else {
1109  if ( msgLevel( MSG::DEBUG ) ) { debug() << "got a new name [" << fname << "]..." << endmsg; }
1110  }
1111 
1112  void* vf = nullptr;
1113  Option_t* opts = f->GetOption();
1114  int r = p_fileMgr->open( Io::ROOT, name(), fname, Io::WRITE, vf, "HIST" );
1115  if ( r != 0 ) {
1116  error() << "unable to open file \"" << fname << "\" for writing" << endmsg;
1117  return StatusCode::FAILURE;
1118  }
1119  TFile* newfile = (TFile*)vf;
1120  newfile->SetOption( opts );
1121 
1122  if ( ifile.second.second != THistSvc::READ ) {
1123  copyFileLayout( newfile, f );
1124  ifile.second.first = newfile;
1125  }
1126 
1127  // loop over all uids and migrate them to the new file
1128  for ( auto& uid : m_uids ) {
1129  for ( auto& hid : *uid.second ) {
1130  if ( hid.file != f ) continue;
1131  TDirectory* olddir = this->changeDir( hid );
1132  hid.file = newfile;
1133  // side-effect: create needed directories...
1134  TDirectory* newdir = this->changeDir( hid );
1135  TClass* cl = hid.obj->IsA();
1136 
1137  // migrate the objects to the new file.
1138  // thanks to the object model of ROOT, it is super easy.
1139  if ( cl->InheritsFrom( "TTree" ) ) {
1140  dynamic_cast<TTree*>( hid.obj )->SetDirectory( newdir );
1141  dynamic_cast<TTree*>( hid.obj )->Reset();
1142  } else if ( cl->InheritsFrom( "TH1" ) ) {
1143  dynamic_cast<TH1*>( hid.obj )->SetDirectory( newdir );
1144  dynamic_cast<TH1*>( hid.obj )->Reset();
1145  } else if ( cl->InheritsFrom( "TGraph" ) ) {
1146  olddir->Remove( hid.obj );
1147  newdir->Append( hid.obj );
1148  } else {
1149  error() << "id: \"" << hid.id << "\" is not a inheriting from a class "
1150  << "we know how to handle (received [" << cl->GetName() << "], "
1151  << "expected [TTree, TH1 or TGraph]) !" << endmsg << "attaching to current dir [" << newdir->GetPath()
1152  << "] "
1153  << "nonetheless..." << endmsg;
1154  olddir->Remove( hid.obj );
1155  newdir->Append( hid.obj );
1156  }
1157  }
1158  }
1159  f->ReOpen( "READ" );
1160  p_fileMgr->close( f, name() );
1161  f = newfile;
1162  }
1163 
1164  return all_good ? StatusCode::SUCCESS : StatusCode::FAILURE;
1165 }
T empty(T... args)
virtual Io::open_t open(const Io::IoTech &, const std::string &caller, const std::string &fname, const Io::IoFlags &, Io::Fd &, void *&, const std::string &desc, const bool shared=false)=0
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
Definition: THistSvc.cpp:1652
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1469
virtual std::vector< std::string > io_retrieve(IIoComponent *iocomponent)=0
: retrieve all registered filenames for a given IIoComponent
uidMap_t m_uids
Definition: THistSvc.h:259
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
constexpr static const auto FAILURE
Definition: StatusCode.h:86
IFileMgr * p_fileMgr
Definition: THistSvc.h:357
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:83
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
virtual Io::close_t close(const Io::Fd, const std::string &caller)=0

◆ merge() [1/4]

StatusCode THistSvc::merge ( const std::string id)
override

Merge all clones for object with a given id.

Definition at line 597 of file THistSvc.cpp.

597  {
598  uidMap_t::iterator itr = m_uids.find( name );
599  if ( itr == m_uids.end() ) {
600  error() << "merge: id \"" << name << "\" not found" << endmsg;
601  return StatusCode::FAILURE;
602  }
603 
604  return merge( itr->second );
605 }
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
Definition: THistSvc.cpp:597
T end(T... args)
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T find(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
constexpr static const auto FAILURE
Definition: StatusCode.h:86
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ merge() [2/4]

StatusCode THistSvc::merge ( TObject *  obj)
override

Merge all clones for given TObject*.

Definition at line 607 of file THistSvc.cpp.

607  {
608  objMap_t::iterator itr = m_tobjs.find( obj );
609  if ( itr != m_tobjs.end() ) {
610  return merge( itr->second.first );
611  } else {
612  error() << "merge: unknown object " << obj << endmsg;
613  return StatusCode::FAILURE;
614  }
615 }
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
Definition: THistSvc.cpp:597
T end(T... args)
objMap_t m_tobjs
Definition: THistSvc.h:263
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T find(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ merge() [3/4]

StatusCode THistSvc::merge ( const THistID hid)
private

Helper method to merge THistID objects.

Definition at line 1765 of file THistSvc.cpp.

1765 { return merge( hid.id ); }
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
Definition: THistSvc.cpp:597

◆ merge() [4/4]

StatusCode THistSvc::merge ( vhid_t vh)
private

Helper method to merge vectors of THistID.

Definition at line 1767 of file THistSvc.cpp.

1767  {
1768  const std::string& name = vh->at( 0 ).id;
1769  if ( vh->size() == 1 ) {
1770  debug() << "merge: id: \"" << name << "\" is size 1. nothing to do" << endmsg;
1771  return StatusCode::SUCCESS;
1772  }
1773 
1774  if ( !vh->at( 0 ).obj->IsA()->InheritsFrom( "TH1" ) ) {
1775  error() << "merge: id \"" << name << "\" is not a THn. Cannot merge" << endmsg;
1776  return StatusCode::FAILURE;
1777  }
1778 
1779  TList* l = new TList();
1780  for ( size_t i = 1; i < vh->size(); ++i ) {
1781  debug() << "merge: id: \"" << name << "\" (" << vh->at( i ).obj << ") adding index " << i << endmsg;
1782  l->Add( vh->at( i ).obj );
1783  }
1784 
1785  TH1* t0 = dynamic_cast<TH1*>( vh->at( 0 ).obj );
1786  if ( t0 == 0 ) {
1787  error() << "merge: could not dcast " << name << "(" << t0 << ") index " << 0 << " to TH1" << endmsg;
1788  return StatusCode::FAILURE;
1789  }
1790 
1791  Long64_t n = t0->Merge( l );
1792 
1793  debug() << "merge: id: \"" << name << "\" merged " << n << " entries" << endmsg;
1794 
1795  for ( size_t i = 1; i < vh->size(); ++i ) {
1796  TH1* th = dynamic_cast<TH1*>( vh->at( i ).obj );
1797  if ( th != 0 ) {
1798  debug() << "clearing index " << i << "(" << th << ")" << endmsg;
1799  th->SetDirectory( nullptr );
1800  th->Reset();
1801  } else {
1802  error() << "merge: could not dcast " << name << " index " << i << " to TH1" << endmsg;
1803  return StatusCode::FAILURE;
1804  }
1805  }
1806  return StatusCode::SUCCESS;
1807 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
STL class.
T at(T... args)
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
dictionary l
Definition: gaudirun.py:523
constexpr static const auto FAILURE
Definition: StatusCode.h:86
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ MergeRootFile()

void THistSvc::MergeRootFile ( TDirectory *  target,
TDirectory *  source 
)
private

Definition at line 1511 of file THistSvc.cpp.

1511  {
1512  if ( msgLevel( MSG::DEBUG ) ) { debug() << "Target path: " << target->GetPath() << endmsg; }
1513  TString path( (char*)strstr( target->GetPath(), ":" ) );
1514  path.Remove( 0, 2 );
1515 
1516  source->cd( path );
1517  TDirectory* current_sourcedir = gDirectory;
1518 
1519  // loop over all keys in this directory
1520  TList* lkeys = current_sourcedir->GetListOfKeys();
1521  int nkeys = lkeys->GetEntries();
1522  TKey* key = nullptr;
1523  for ( int jj = 0; jj < nkeys; jj++ ) {
1524  key = (TKey*)lkeys->At( jj );
1525  std::string pathnameinsource = current_sourcedir->GetPath() + std::string( "/" ) + key->GetName();
1526  if ( msgLevel( MSG::DEBUG ) ) { debug() << "Reading Key:" << pathnameinsource << endmsg; }
1527  TObject* obj = source->Get( pathnameinsource.c_str() );
1528 
1529  if ( obj ) {
1530  if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
1531  // it's a subdirectory
1532  if ( msgLevel( MSG::DEBUG ) ) { debug() << "Found subdirectory " << obj->GetName() << endmsg; }
1533 
1534  // create a new subdir of same name and title in the target file
1535  target->cd();
1536  TDirectory* newtargetdir = target->mkdir( obj->GetName(), obj->GetTitle() );
1537 
1538  MergeRootFile( newtargetdir, source );
1539 
1540  } else if ( obj->IsA()->InheritsFrom( "TTree" ) ) {
1541  if ( msgLevel( MSG::DEBUG ) ) { debug() << "Found TTree " << obj->GetName() << endmsg; }
1542  TTree* mytree = dynamic_cast<TTree*>( obj );
1543  int nentries = (int)mytree->GetEntries();
1544  mytree->SetBranchStatus( "*", 1 );
1545 
1546  if ( msgLevel( MSG::DEBUG ) ) { debug() << "Dumping TTree " << nentries << " entries" << endmsg; }
1547  target->cd();
1548  mytree->CloneTree();
1549 
1550  } else {
1551  target->cd();
1552  obj->Write( key->GetName() );
1553  }
1554  }
1555  }
1556 }
void MergeRootFile(TDirectory *, TDirectory *)
Definition: THistSvc.cpp:1511
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T c_str(T... args)
T strstr(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ parseString()

void THistSvc::parseString ( const std::string id,
std::string root,
std::string rem 
) const
private

Definition at line 1594 of file THistSvc.cpp.

1594  {
1595  auto pos = id.find( "/" );
1596 
1597  if ( pos == std::string::npos ) {
1598  root.clear();
1599  rem = id;
1600  } else if ( pos == 0 ) {
1601  parseString( id.substr( 1 ), root, rem );
1602  } else {
1603  root = id.substr( 0, pos );
1604  rem = id.substr( pos + 1 );
1605  }
1606 }
void parseString(const std::string &id, std::string &root, std::string &rem) const
Definition: THistSvc.cpp:1594
T substr(T... args)

◆ readHist()

template<typename T >
T * THistSvc::readHist ( const std::string name) const
private

Definition at line 1184 of file THistSvc.cpp.

1184  {
1185  return dynamic_cast<T*>( readHist_i<T>( id ) );
1186 }

◆ readHist_i()

template<typename T >
T * THistSvc::readHist_i ( const std::string name) const
private

Definition at line 204 of file THistSvc.icc.

204  {
205  GlobalDirectoryRestore restore( m_svcMut );
206 
207  std::string idr( id );
208  removeDoubleSlash( idr );
209 
210  std::string stream, rem, dir, fdir, bdir, fdir2;
211  TFile* file = nullptr;
212 
213  if ( !findStream( idr, stream, rem, file ) ) { return nullptr; }
214 
215  if ( !file ) {
216  error() << "no associated file found" << endmsg;
217  return nullptr;
218  }
219 
220  file->cd( "/" );
221 
222  fdir = idr;
223  bdir = stripDirectoryName( fdir );
224  fdir2 = fdir;
225  while ( ( dir = stripDirectoryName( fdir ) ) != "" ) {
226  if ( !gDirectory->GetKey( dir.c_str() ) ) {
227  error() << "Directory \"" << fdir2 << "\" doesnt exist in " << file->GetName() << endmsg;
228  return nullptr;
229  }
230  gDirectory->cd( dir.c_str() );
231  }
232 
233  TObject* to = nullptr;
234  gDirectory->GetObject( fdir.c_str(), to );
235 
236  if ( !to ) {
237  error() << "Could not get obj \"" << fdir << "\" in " << gDirectory->GetPath() << endmsg;
238  return nullptr;
239  }
240 
241  T* hist = dynamic_cast<T*>( to );
242  if ( hist == nullptr ) {
243  error() << "Could not convert \"" << idr << "\" to a " << System::typeinfoName( typeid( *hist ) ) << " as is a "
244  << to->IsA()->GetName() << endmsg;
245  return nullptr;
246  }
247 
248  if ( msgLevel( MSG::DEBUG ) ) {
249  debug() << "Read in " << hist->IsA()->GetName() << " \"" << hist->GetName() << "\" from file " << file->GetName()
250  << endmsg;
251  hist->Print();
252  }
253 
254  return hist;
255 }
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1491
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1558
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1507
T c_str(T... args)
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ readTree()

TTree * THistSvc::readTree ( const std::string name) const
private

Definition at line 1188 of file THistSvc.cpp.

1188 { return dynamic_cast<TTree*>( readHist_i<TTree>( id ) ); }

◆ regGraph() [1/3]

StatusCode THistSvc::regGraph ( const std::string name)
override

Register a new TGraph with a given name.

Definition at line 401 of file THistSvc.cpp.

401  {
402  std::unique_ptr<TGraph> graph = std::make_unique<TGraph>();
403  return regHist_i( std::move( graph ), id, false );
404 }
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
STL class.

◆ regGraph() [2/3]

StatusCode THistSvc::regGraph ( const std::string name,
std::unique_ptr< TGraph >  graph 
)
override

Register an existing TGraph with a given name and moved unique_ptr.

Definition at line 406 of file THistSvc.cpp.

406  {
407  if ( strcmp( graph->GetName(), "Graph" ) == 0 ) {
408  std::string id2( id );
409  std::string::size_type i = id2.rfind( "/" );
410  if ( i != std::string::npos ) { id2.erase( 0, i + 1 ); }
411 
412  info() << "setting name of TGraph id: \"" << id << "\" to \"" << id2 << "\" since it is unset" << endmsg;
413  graph->SetName( id2.c_str() );
414  }
415 
416  return regHist_i( std::move( graph ), id, false );
417 }
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
STL class.
T strcmp(T... args)
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ regGraph() [3/3]

StatusCode THistSvc::regGraph ( const std::string name,
TGraph *  graph_ptr 
)
overridevirtual
Deprecated:
{Just kept for compatibiltiy to current ATLAS code.

Pleas use std::unique_ptrs instead!} Register a new TGraph with a given name and a raw pointer

Definition at line 419 of file THistSvc.cpp.

419  {
420  std::unique_ptr<TGraph> graph( graph_ptr );
421  if ( strcmp( graph->GetName(), "Graph" ) == 0 ) {
422  std::string id2( id );
423  std::string::size_type i = id2.rfind( "/" );
424  if ( i != std::string::npos ) { id2.erase( 0, i + 1 ); }
425 
426  info() << "setting name of TGraph id: \"" << id << "\" to \"" << id2 << "\" since it is unset" << endmsg;
427  graph->SetName( id2.c_str() );
428  }
429 
430  return regHist_i( std::move( graph ), id, false );
431 }
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
STL class.
T strcmp(T... args)
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
STL class.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ regHist() [1/4]

StatusCode THistSvc::regHist ( const std::string name)
override

Register a new ROOT histogram TH*X with a name.

Definition at line 319 of file THistSvc.cpp.

319  {
320  std::unique_ptr<TH1> hist = nullptr;
321  return regHist_i( std::move( hist ), id, false );
322 }
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
STL class.

◆ regHist() [2/4]

StatusCode THistSvc::regHist ( const std::string name,
std::unique_ptr< TH1 >  hist 
)
override

Register an existing ROOT histogram TH*X with name and moved unique_ptr.

Parameters
[in]namedefines the histogram id/name under which it is recorded
[in]histtransfers ownership of the histogram to the THistSvc

Definition at line 324 of file THistSvc.cpp.

324  {
325  return regHist_i( std::move( hist ), id, false );
326 }
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17

◆ regHist() [3/4]

StatusCode THistSvc::regHist ( const std::string name,
std::unique_ptr< TH1 >  hist,
TH1 *  hist_ptr 
)
override

Register an existing ROOT histogram TH*X with name and moved unique_ptr.

Parameters
[in]namedefines the histogram id/name under which it is recorded
[in]histtransfers ownership of the histogram to the THistSvc
[out]hist_ptrfor compatibility: return raw pointer to managed object to support common usage in Athena

Definition at line 328 of file THistSvc.cpp.

328  {
329  // This is only to support a common use case where the histogram is used after its registration
330  if ( hist_ptr != nullptr ) { hist_ptr = hist.get(); }
331  return regHist_i( std::move( hist ), id, false );
332 }
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
T get(T... args)

◆ regHist() [4/4]

StatusCode THistSvc::regHist ( const std::string name,
TH1 *  hist_ptr 
)
override
Deprecated:
{Just for compatibility purposes.

Ownership should be clearly managed.} Register an existing ROOT histogram TH*X with name and pointer

Definition at line 334 of file THistSvc.cpp.

334  {
335  std::unique_ptr<TH1> hist( hist_ptr );
336  return regHist_i( std::move( hist ), id, false );
337 }
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
STL class.

◆ regHist_i() [1/2]

template<typename T >
StatusCode THistSvc::regHist_i ( std::unique_ptr< T >  hist,
const std::string name,
bool  shared 
)
private

Definition at line 17 of file THistSvc.icc.

17  {
18  THistID* hid = nullptr;
19  return regHist_i( std::move( hist ), id, shared, hid );
20 }
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17

◆ regHist_i() [2/2]

template<typename T >
StatusCode THistSvc::regHist_i ( std::unique_ptr< T >  hist,
const std::string name,
bool  shared,
THistID *&  hid 
)
private

Definition at line 23 of file THistSvc.icc.

23  {
25  phid = nullptr;
26 
27  // It is sad that we lose propper memory management here
28  T* hist = nullptr;
29  if ( hist_unique.get() != nullptr ) { hist = hist_unique.release(); }
30  debug() << "regHist_i obj: " << hist << " id: " << id << " s: " << shared << endmsg;
31 
32  std::string idr( id );
33  removeDoubleSlash( idr );
34 
35  if ( idr.find( "/" ) == idr.length() ) {
36  error() << "Badly formed identifier \"" << idr << "\": "
37  << "Must not end with a /" << endmsg;
38  delete hist;
39  return StatusCode::FAILURE;
40  }
41 
42  TFile* f = nullptr;
43  std::string stream, name;
44  if ( !findStream( idr, stream, name, f ) ) {
45  error() << "Could not register id: \"" << idr << "\"" << endmsg;
46  delete hist;
47  return StatusCode::FAILURE;
48  }
49 
50  std::string uid = "/" + stream + "/" + name;
51 
52  uidMap_t::iterator uitr = m_uids.find( uid );
53  bool exists( false );
54  if ( uitr != m_uids.end() ) {
55  exists = true;
56  TObject* t1 = uitr->second->at( 0 ).obj;
57  if ( hist->Compare( t1 ) != 0 ) {
58  error() << "previously registered object with identifier \"" << uid << "\" does not compare to this one"
59  << endmsg;
60  delete hist;
61  return StatusCode::FAILURE;
62  } else {
63  debug() << "previously registered id \"" << uid << "\": num " << uitr->second->size() << endmsg;
64  }
65  }
66 
67  bool temp = false;
68  if ( !f ) {
69  temp = true;
70  if ( msgLevel( MSG::DEBUG ) ) { debug() << "Historgram with id \"" << idr << "\" is temporary" << endmsg; }
71  }
72 
73  TObject* to = nullptr;
74  THistID hid;
75  // check to see if this hist is to be read in;
76  if ( !temp && m_files.find( stream )->second.second == READ ) {
77  if ( hist != 0 ) { warning() << "Registering id: \"" << idr << "\" with non zero pointer!" << endmsg; }
78 
79  hist = readHist_i<T>( idr );
80  if ( hist == nullptr ) {
81  error() << "Unable to read in hist" << endmsg;
82  delete hist;
83  return StatusCode::FAILURE;
84  }
85  to = dynamic_cast<TObject*>( hist );
86  hid = THistID( uid, temp, to, f, m_files.find( stream )->second.second );
87  } else if ( !hist ) {
88  error() << "Unable to read in hist with id: \"" << idr << "\"" << endmsg;
89  delete hist;
90  return StatusCode::FAILURE;
91  } else {
92  to = dynamic_cast<TObject*>( hist );
93  if ( to == nullptr ) {
94  error() << "Could not dcast to TObject. id: \"" << idr << "\"" << endmsg;
95  delete hist;
96  return StatusCode::FAILURE;
97  }
98 
99  auto oitr = m_tobjs.find( to );
100  if ( oitr != m_tobjs.end() ) {
101  error() << "already registered id: \"" << idr << "\" with identifier \""
102  << oitr->second.first->at( oitr->second.second ).id << "\"" << endmsg;
103  delete hist;
104  return StatusCode::FAILURE;
105  }
106 
107  const auto findF = m_files.find( stream );
108  hid = ( findF != m_files.end() ? THistID( uid, temp, to, f, findF->second.second ) : THistID( uid, temp, to, f ) );
109 
110  hid.shared = shared;
111  TDirectory* dir = changeDir( hid );
112 
113  if ( dynamic_cast<TTree*>( hist ) ) {
114  dynamic_cast<TTree*>( hist )->SetDirectory( dir );
115  } else if ( dynamic_cast<TH1*>( hist ) ) {
116  dynamic_cast<TH1*>( hist )->SetDirectory( dir );
117  } else if ( dynamic_cast<TGraph*>( hist ) ) {
118  dir->Append( hist );
119  } else {
120  error() << "id: \"" << idr << "\" is not a TH, TTree, or TGraph. Attaching it to current dir." << endmsg;
121  dir->Append( hist );
122  }
123  }
124 
125  std::string fname;
126  if ( !f ) {
127  fname = "none";
128  } else {
129  fname = f->GetName();
130  }
131 
132  debug() << "Registering" << ( shared ? " shared " : " " ) << System::typeinfoName( typeid( *hist ) ) << " title: \""
133  << hist->GetTitle() << "\" id: \"" << uid
134  << "\" dir: "
135  // << hist->GetDirectory()->GetPath() << " "
136  << changeDir( hid )->GetPath() << " file: " << fname << endmsg;
137 
138  // create a mutex for all shared histograms
139  if ( shared ) { hid.mutex = new histMut_t; }
140 
141  if ( exists ) {
142  vhid_t* vi = uitr->second;
143  vi->push_back( hid );
144  phid = &( vi->back() );
145 
146  m_tobjs.emplace( to, std::pair<vhid_t*, size_t>( vi, vi->size() - 1 ) );
147  } else {
148  vhid_t* vi = new vhid_t{hid};
149  m_hlist.emplace( m_hlist.end(), vi );
150 
151  phid = &( vi->back() );
152  m_uids.emplace( uid, vi );
153  m_ids.emplace( name, vi );
154 
156  }
157 
158  debug() << "regHist_i THistID: " << hid << endmsg;
159 
160  return StatusCode::SUCCESS;
161 }
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
std::mutex histMut_t
Definition: THistSvc.h:165
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T end(T... args)
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1558
objMap_t m_tobjs
Definition: THistSvc.h:263
T release(T... args)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
hlist_t m_hlist
Definition: THistSvc.h:258
idMap_t m_ids
Definition: THistSvc.h:260
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
std::vector< THistID > vhid_t
Definition: THistSvc.h:249
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1469
T find(T... args)
T size(T... args)
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1507
uidMap_t m_uids
Definition: THistSvc.h:259
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
T emplace(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86
bool exists(const std::string &name) const override
Check if object with given name is managed by THistSvcMT.
Definition: THistSvc.cpp:617
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ regShared() [1/4]

StatusCode THistSvc::regShared ( const std::string name,
std::unique_ptr< TH1 >  hist,
LockedHandle< TH1 > &  lh 
)
override

Register shared object of type TH1 and return LockedHandle for that object.

Definition at line 442 of file THistSvc.cpp.

442  {
443  lh = regShared_i<TH1>( id, std::move( hist ) );
444  if ( lh ) {
445  return StatusCode::SUCCESS;
446  } else {
447  return StatusCode::FAILURE;
448  }
449 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T move(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ regShared() [2/4]

StatusCode THistSvc::regShared ( const std::string name,
std::unique_ptr< TH2 >  hist,
LockedHandle< TH2 > &  lh 
)
override

Register shared object of type TH2 and return LockedHandle for that object.

Definition at line 451 of file THistSvc.cpp.

451  {
452  lh = regShared_i<TH2>( id, std::move( hist ) );
453  if ( lh ) {
454  return StatusCode::SUCCESS;
455  } else {
456  return StatusCode::FAILURE;
457  }
458 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T move(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ regShared() [3/4]

StatusCode THistSvc::regShared ( const std::string name,
std::unique_ptr< TH3 >  hist,
LockedHandle< TH3 > &  lh 
)
override

Register shared object of type TH3 and return LockedHandle for that object.

Definition at line 460 of file THistSvc.cpp.

460  {
461  lh = regShared_i<TH3>( id, std::move( hist ) );
462  if ( lh ) {
463  return StatusCode::SUCCESS;
464  } else {
465  return StatusCode::FAILURE;
466  }
467 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T move(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ regShared() [4/4]

StatusCode THistSvc::regShared ( const std::string name,
std::unique_ptr< TGraph >  graph,
LockedHandle< TGraph > &  lh 
)
override

Register shared object of type TGraph and return LockedHandle for that object.

Definition at line 469 of file THistSvc.cpp.

469  {
470  lh = regShared_i<TGraph>( id, std::move( graph ) );
471  if ( lh ) {
472  return StatusCode::SUCCESS;
473  } else {
474  return StatusCode::FAILURE;
475  }
476 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T move(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86

◆ regShared_i()

template<typename T >
LockedHandle< T > THistSvc::regShared_i ( const std::string id,
std::unique_ptr< T >  hist 
)
private

Definition at line 258 of file THistSvc.icc.

258  {
259  LockedHandle<T> lh( nullptr, nullptr );
260  const THistID* hid = nullptr;
261  if ( findHistID( id, hid ) == 0 ) {
262  T* phist = hist.get();
263  THistID* phid = nullptr;
264  if ( regHist_i( std::move( hist ), id, true, phid ).isSuccess() ) {
265  lh.set( phist, phid->mutex );
266 
267  } else {
268  error() << "regSharedHist: unable to register shared hist with id \"" << id << "\"" << endmsg;
269  }
270  } else {
271  if ( !hid->shared ) {
272  error() << "regSharedHist: previously register Hist with id \"" << id << "\" was not marked shared" << endmsg;
273  }
274 
275  if ( hist->Compare( hid->obj ) != 0 ) {
276  error() << "regSharedHist: Histogram " << id << " does not compare with " << hid << endmsg;
277  } else {
278  T* phist = dynamic_cast<T*>( hid->obj );
279  if ( phist == 0 ) {
280  error() << "regSharedHist: unable to dcast retrieved shared hist \"" << id << "\" of type "
281  << hid->obj->IsA()->GetName() << " to requested type " << System::typeinfoName( typeid( T ) ) << endmsg;
282  } else {
283  lh.set( phist, hid->mutex );
284  delete hist.release();
285  }
286  }
287  }
288  return lh;
289 }
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
T release(T... args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
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
T get(T... args)
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
Definition: THistSvc.cpp:1684
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ regTree() [1/3]

StatusCode THistSvc::regTree ( const std::string name)
override

Register a new TTree with a given name.

Definition at line 366 of file THistSvc.cpp.

366  {
367  std::unique_ptr<TTree> tree = nullptr;
368  return regHist_i( std::move( tree ), id, false );
369 }
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
STL class.

◆ regTree() [2/3]

StatusCode THistSvc::regTree ( const std::string name,
std::unique_ptr< TTree >  tree 
)
override

Register an existing TTree with a given name and moved unique_ptr.

Definition at line 371 of file THistSvc.cpp.

371  {
372  StatusCode sc = regHist_i( std::move( tree ), id, false );
373  TTree* tr = nullptr;
374  if ( getTree( id, tr ).isSuccess() && sc.isSuccess() ) {
375  if ( m_autoSave != 0 ) { tr->SetAutoSave( m_autoSave ); }
376  tr->SetAutoFlush( m_autoFlush );
377  }
378  return sc;
379 }
Gaudi::Property< int > m_autoSave
Definition: THistSvc.h:339
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
Definition: THistSvc.cpp:392
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
bool isSuccess() const
Definition: StatusCode.h:267
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
Gaudi::Property< int > m_autoFlush
Definition: THistSvc.h:340

◆ regTree() [3/3]

StatusCode THistSvc::regTree ( const std::string name,
TTree *  tree_ptr 
)
override
Deprecated:
{Just kept for compatibiltiy to current ATLAS code.

Pleas use std::unique_ptrs instead!} Register a new TTree with a given name and a raw pointer

Definition at line 381 of file THistSvc.cpp.

381  {
382  std::unique_ptr<TTree> tree( tree_ptr );
383  StatusCode sc = regHist_i( std::move( tree ), id, false );
384  TTree* tr = nullptr;
385  if ( getTree( id, tr ).isSuccess() && sc.isSuccess() ) {
386  if ( m_autoSave != 0 ) { tr->SetAutoSave( m_autoSave ); }
387  tr->SetAutoFlush( m_autoFlush );
388  }
389  return sc;
390 }
Gaudi::Property< int > m_autoSave
Definition: THistSvc.h:339
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
Definition: THistSvc.cpp:392
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
bool isSuccess() const
Definition: StatusCode.h:267
T move(T... args)
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
Definition: THistSvc.icc:17
STL class.
Gaudi::Property< int > m_autoFlush
Definition: THistSvc.h:340

◆ reinitialize()

StatusCode THistSvc::reinitialize ( )
override

Definition at line 173 of file THistSvc.cpp.

173  {
174  GlobalDirectoryRestore restore( m_svcMut );
175  warning() << "reinitialize not implemented" << endmsg;
176  return StatusCode::SUCCESS;
177 }
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
THistSvcMutex_t m_svcMut
Definition: THistSvc.h:365
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ removeDoubleSlash()

void THistSvc::removeDoubleSlash ( std::string id) const
private

Definition at line 1507 of file THistSvc.cpp.

1507  {
1508  while ( id.find( "//" ) != std::string::npos ) { id.replace( id.find( "//" ), 2, "/" ); }
1509 }
T find(T... args)

◆ rootOpenAction()

StatusCode THistSvc::rootOpenAction ( FILEMGR_CALLBACK_ARGS  )
private

Definition at line 1809 of file THistSvc.cpp.

1809  {
1810  if ( fa->tech() != Io::ROOT ) {
1811  // This should never happen
1812  return StatusCode::SUCCESS;
1813  }
1814 
1815  if ( fa->desc() != "HIST" ) { return StatusCode::SUCCESS; }
1816 
1817  p_incSvc->fireIncident( FileIncident( caller, "OpenHistFile", fa->name() ) );
1818 
1819  if ( fa->flags().isRead() ) {
1820  p_incSvc->fireIncident( FileIncident( caller, "BeginHistFile", fa->name() ) );
1821  } else if ( fa->flags().isWrite() ) {
1822  p_incSvc->fireIncident( FileIncident( caller, IncidentType::BeginOutputFile, fa->name() ) );
1823  } else {
1824  // for Io::RW
1825  p_incSvc->fireIncident( FileIncident( caller, IncidentType::BeginOutputFile, fa->name() ) );
1826  }
1827 
1828  return StatusCode::SUCCESS;
1829 }
This class is the FileIncident.
Definition: FileIncident.h:17
IIncidentSvc * p_incSvc
Definition: THistSvc.h:356
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.

◆ rootOpenErrAction()

StatusCode THistSvc::rootOpenErrAction ( FILEMGR_CALLBACK_ARGS  )
private

Definition at line 1831 of file THistSvc.cpp.

1831  {
1832  if ( fa->tech() != Io::ROOT ) {
1833  // This should never happen
1834  return StatusCode::SUCCESS;
1835  }
1836 
1837  if ( fa->desc() != "HIST" ) { return StatusCode::SUCCESS; }
1838 
1839  if ( fa->flags().isRead() ) {
1840  p_incSvc->fireIncident( FileIncident( caller, IncidentType::FailInputFile, fa->name() ) );
1841  } else if ( fa->flags().isWrite() ) {
1842  p_incSvc->fireIncident( FileIncident( caller, IncidentType::FailOutputFile, fa->name() ) );
1843  } else {
1844  // for Io::RW
1845  p_incSvc->fireIncident( FileIncident( caller, "FailRWFile", fa->name() ) );
1846  }
1847 
1848  return StatusCode::SUCCESS;
1849 }
This class is the FileIncident.
Definition: FileIncident.h:17
IIncidentSvc * p_incSvc
Definition: THistSvc.h:356
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.

◆ setupInputFile()

void THistSvc::setupInputFile ( )
private

call-back method to handle input stream property

Definition at line 1608 of file THistSvc.cpp.

1608  {
1610  debug() << "Delaying connection of Input Files until Initialize"
1611  << ". now in " << FSMState() << endmsg;
1612 
1613  m_delayConnect = true;
1614  } else {
1615  debug() << "Now connecting of Input Files" << endmsg;
1616 
1618 
1619  for ( const auto& itr : m_inputfile.value() ) {
1620  if ( m_alreadyConnectedInFiles.end() != m_alreadyConnectedInFiles.find( itr ) ) { continue; }
1621  if ( connect( itr ).isFailure() ) {
1622  sc = StatusCode::FAILURE;
1623  } else {
1625  }
1626  }
1627 
1628  if ( !sc.isSuccess() ) { throw GaudiException( "Problem connecting inputfile !!", name(), StatusCode::FAILURE ); }
1629  }
1630 }
bool m_delayConnect
Definition: THistSvc.h:360
Define general base for Gaudi exception.
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:52
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T end(T... args)
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
Gaudi::Property< std::vector< std::string > > m_inputfile
Definition: THistSvc.h:352
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
bool m_okToConnect
Definition: THistSvc.h:361
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
bool isSuccess() const
Definition: StatusCode.h:267
T insert(T... args)
T find(T... args)
StatusCode connect(const std::string &)
Definition: THistSvc.cpp:1292
constexpr static const auto FAILURE
Definition: StatusCode.h:86
bool isFailure() const
Definition: StatusCode.h:130
std::set< std::string > m_alreadyConnectedInFiles
list of already connected files.
Definition: THistSvc.h:241
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ setupOutputFile()

void THistSvc::setupOutputFile ( )
private

call-back method to handle output stream property

Definition at line 1632 of file THistSvc.cpp.

1632  {
1634  debug() << "Delaying connection of Input Files until Initialize"
1635  << ". now in " << FSMState() << endmsg;
1636  m_delayConnect = true;
1637  } else {
1639  for ( const auto& itr : m_outputfile.value() ) {
1640  if ( m_alreadyConnectedOutFiles.end() != m_alreadyConnectedOutFiles.find( itr ) ) { continue; }
1641  if ( connect( itr ).isFailure() ) {
1642  sc = StatusCode::FAILURE;
1643  } else {
1645  }
1646  }
1647 
1648  if ( !sc.isSuccess() ) { throw GaudiException( "Problem connecting outputfile !!", name(), StatusCode::FAILURE ); }
1649  }
1650 }
bool m_delayConnect
Definition: THistSvc.h:360
Define general base for Gaudi exception.
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:52
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T end(T... args)
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: THistSvc.h:351
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
bool m_okToConnect
Definition: THistSvc.h:361
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
bool isSuccess() const
Definition: StatusCode.h:267
T insert(T... args)
T find(T... args)
StatusCode connect(const std::string &)
Definition: THistSvc.cpp:1292
constexpr static const auto FAILURE
Definition: StatusCode.h:86
std::set< std::string > m_alreadyConnectedOutFiles
list of already connected files.
Definition: THistSvc.h:245
bool isFailure() const
Definition: StatusCode.h:130
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192

◆ stripDirectoryName()

std::string THistSvc::stripDirectoryName ( std::string dir) const
private

Definition at line 1491 of file THistSvc.cpp.

1491  {
1492  std::string::size_type i = dir.find( "/" );
1493 
1494  if ( i == std::string::npos ) return {};
1495 
1496  if ( i == 0 ) {
1497  dir.erase( 0, 1 );
1498  return stripDirectoryName( dir );
1499  }
1500 
1501  std::string root = dir.substr( 0, i );
1502  dir.erase( 0, i );
1503 
1504  return root;
1505 }
std::string stripDirectoryName(std::string &dir) const
Definition: THistSvc.cpp:1491
STL class.
T erase(T... args)
T find(T... args)
T substr(T... args)

◆ updateFiles()

void THistSvc::updateFiles ( )
private

Handle case where TTree grows beyond TTree::fgMaxTreeSize.

Definition at line 1190 of file THistSvc.cpp.

1190  {
1191  // If TTrees grow beyond TTree::fgMaxTreeSize, a new file is
1192  // automatically created by root, and the old one closed. We
1193  // need to migrate all the UIDs over to show the correct file
1194  // pointer. This is ugly.
1195 
1196  if ( msgLevel( MSG::DEBUG ) ) debug() << "updateFiles()" << endmsg;
1197 
1198  for ( auto uitr = m_uids.begin(); uitr != m_uids.end(); ++uitr ) {
1199  for ( auto& hid : *( uitr->second ) ) {
1200 #ifndef NDEBUG
1201  if ( msgLevel( MSG::VERBOSE ) )
1202  verbose() << " update: " << uitr->first << " " << hid.id << " " << hid.mode << endmsg;
1203 #endif
1204  TObject* to = hid.obj;
1205  TFile* oldFile = hid.file;
1206  if ( !to ) {
1207  warning() << uitr->first << ": TObject == 0" << endmsg;
1208  } else if ( hid.temp || hid.mode == READ ) {
1209 // do nothing - no need to check how big the file is since we
1210 // are just reading it.
1211 #ifndef NDEBUG
1212  if ( msgLevel( MSG::VERBOSE ) ) verbose() << " skipping" << endmsg;
1213 #endif
1214  } else if ( to->IsA()->InheritsFrom( "TTree" ) ) {
1215  TTree* tr = dynamic_cast<TTree*>( to );
1216  TFile* newFile = tr->GetCurrentFile();
1217 
1218  if ( oldFile != newFile ) {
1219  std::string newFileName = newFile->GetName();
1220  std::string oldFileName, streamName, rem;
1221  TFile* dummy = nullptr;
1222  findStream( hid.id, streamName, rem, dummy );
1223 
1224  for ( auto& itr : m_files ) {
1225  if ( itr.second.first == oldFile ) { itr.second.first = newFile; }
1226  }
1227 
1228  for ( auto uitr2 = uitr; uitr2 != m_uids.end(); ++uitr2 ) {
1229  for ( auto& hid2 : *( uitr2->second ) ) {
1230  if ( hid2.file == oldFile ) { hid2.file = newFile; }
1231  }
1232  }
1233 
1235  [&]( streamMap::const_reference s ) { return s.second == streamName; } );
1236  if ( sitr != std::end( m_fileStreams ) ) oldFileName = sitr->first;
1237 
1238 #ifndef NDEBUG
1239  if ( msgLevel( MSG::DEBUG ) ) {
1240  debug() << "migrating uid: " << hid.id << " stream: " << streamName << " oldFile: " << oldFileName
1241  << " newFile: " << newFileName << endmsg;
1242  }
1243 #endif
1244 
1245  if ( !oldFileName.empty() ) {
1246  auto i = m_fileStreams.lower_bound( oldFileName );
1247  while ( i != std::end( m_fileStreams ) && i->first == oldFileName ) {
1248 #ifndef NDEBUG
1249  if ( msgLevel( MSG::DEBUG ) ) {
1250  debug() << "changing filename \"" << i->first << "\" to \"" << newFileName << "\" for stream \""
1251  << i->second << "\"" << endmsg;
1252  }
1253 #endif
1254  std::string nm = std::move( i->second );
1255  i = m_fileStreams.erase( i );
1256  m_fileStreams.emplace( newFileName, std::move( nm ) );
1257  }
1258  } else {
1259  error() << "Problems updating fileStreams with new file name" << endmsg;
1260  }
1261  }
1262  }
1263  }
1264  }
1265 }
T empty(T... args)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
T end(T... args)
T lower_bound(T... args)
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1558
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
T erase(T... args)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T move(T... args)
constexpr double nm
Definition: SystemOfUnits.h:81
T find_if(T... args)
T begin(T... args)
uidMap_t m_uids
Definition: THistSvc.h:259
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
T emplace(T... args)
string s
Definition: gaudirun.py:318
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
streamMap m_fileStreams
Definition: THistSvc.h:267

◆ writeObjectsToFile()

StatusCode THistSvc::writeObjectsToFile ( )
private

Definition at line 1267 of file THistSvc.cpp.

1267  {
1268  updateFiles();
1269 
1271  auto mode = i.second.second;
1272  auto file = i.second.first;
1273  if ( mode == WRITE || mode == UPDATE || mode == SHARE ) {
1274  file->Write( "", TObject::kOverwrite );
1275  } else if ( mode == APPEND ) {
1276  file->Write( "" );
1277  }
1278  } );
1279 
1280  if ( msgLevel( MSG::DEBUG ) ) {
1281  debug() << "THistSvc::writeObjectsToFile()::List of Files connected in ROOT " << endmsg;
1282  TSeqCollection* filelist = gROOT->GetListOfFiles();
1283  for ( int ii = 0; ii < filelist->GetEntries(); ii++ ) {
1284  debug() << "THistSvc::writeObjectsToFile()::List of Files connected in ROOT: \"" << filelist->At( ii )->GetName()
1285  << "\"" << endmsg;
1286  }
1287  }
1288 
1289  return StatusCode::SUCCESS;
1290 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
T end(T... args)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T begin(T... args)
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:265
T for_each(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
Definition: THistSvc.cpp:1190

Member Data Documentation

◆ m_alreadyConnectedInFiles

std::set<std::string> THistSvc::m_alreadyConnectedInFiles
private

list of already connected files.

This is to keep track of files registered by the setupInputFile callback method

Definition at line 241 of file THistSvc.h.

◆ m_alreadyConnectedOutFiles

std::set<std::string> THistSvc::m_alreadyConnectedOutFiles
private

list of already connected files.

This is to keep track of files registered by the setupOutputFile callback method

Definition at line 245 of file THistSvc.h.

◆ m_autoFlush

Gaudi::Property<int> THistSvc::m_autoFlush {this, "AutoFlush", 0}
private

Definition at line 340 of file THistSvc.h.

◆ m_autoSave

Gaudi::Property<int> THistSvc::m_autoSave {this, "AutoSave", 0}
private

Definition at line 339 of file THistSvc.h.

◆ m_compressionLevel

Gaudi::Property<int> THistSvc::m_compressionLevel
private
Initial value:
{this, "CompressionLevel", 1, [this]( auto& ) {
this->warning()
<< "\"CompressionLevel\" Property has been deprecated. "
<< "Set it via the \"CL=\" parameter in the \"Output\" Property"
<< endmsg;
}}

Definition at line 345 of file THistSvc.h.

◆ m_curstream

std::string THistSvc::m_curstream
mutableprivate

Definition at line 363 of file THistSvc.h.

◆ m_delayConnect

bool THistSvc::m_delayConnect = false
private

Definition at line 360 of file THistSvc.h.

◆ m_files

std::map<std::string, std::pair<TFile*, Mode> > THistSvc::m_files
private

Definition at line 265 of file THistSvc.h.

◆ m_fileStreams

streamMap THistSvc::m_fileStreams
private

Definition at line 267 of file THistSvc.h.

◆ m_hlist

hlist_t THistSvc::m_hlist
private

Definition at line 258 of file THistSvc.h.

◆ m_ids

idMap_t THistSvc::m_ids
private

Definition at line 260 of file THistSvc.h.

◆ m_inputfile

Gaudi::Property<std::vector<std::string> > THistSvc::m_inputfile {this, "Input", {}, &THistSvc::setupInputFile}
private

Definition at line 352 of file THistSvc.h.

◆ m_maxFileSize

Gaudi::Property<int> THistSvc::m_maxFileSize
private
Initial value:
{this, "MaxFileSize", 10240,
"maximum file size in MB. if exceeded,"
" will cause an abort. -1 to never check."}

Definition at line 342 of file THistSvc.h.

◆ m_okToConnect

bool THistSvc::m_okToConnect = false
private

Definition at line 361 of file THistSvc.h.

◆ m_outputfile

Gaudi::Property<std::vector<std::string> > THistSvc::m_outputfile {this, "Output", {}, &THistSvc::setupOutputFile}
private

Definition at line 351 of file THistSvc.h.

◆ m_print

Gaudi::Property<bool> THistSvc::m_print {this, "PrintAll", false}
private

Definition at line 341 of file THistSvc.h.

◆ m_Rstream

std::vector<std::string> THistSvc::m_Rstream
private

Definition at line 237 of file THistSvc.h.

◆ m_sharedFiles

std::map<std::string, std::string> THistSvc::m_sharedFiles
private

Definition at line 270 of file THistSvc.h.

◆ m_signaledStop

bool THistSvc::m_signaledStop = false
private

Definition at line 359 of file THistSvc.h.

◆ m_svcMut

THistSvcMutex_t THistSvc::m_svcMut
mutableprivate

Definition at line 365 of file THistSvc.h.

◆ m_tobjs

objMap_t THistSvc::m_tobjs
private

Definition at line 263 of file THistSvc.h.

◆ m_uids

uidMap_t THistSvc::m_uids
private

Definition at line 259 of file THistSvc.h.

◆ m_Wstream

std::vector<std::string> THistSvc::m_Wstream
private

Definition at line 237 of file THistSvc.h.

◆ p_fileMgr

IFileMgr* THistSvc::p_fileMgr = nullptr
private

Definition at line 357 of file THistSvc.h.

◆ p_incSvc

IIncidentSvc* THistSvc::p_incSvc = nullptr
private

Definition at line 356 of file THistSvc.h.


The documentation for this class was generated from the following files: