The Gaudi Framework  master (37c0b60a)
RDirectoryCnv.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // Include files
15 #include <GaudiKernel/IRegistry.h>
16 #include <GaudiKernel/MsgStream.h>
17 #include <GaudiKernel/NTuple.h>
18 
19 #include <optional>
20 
21 #include "RDirectoryCnv.h"
22 
23 // Root files
24 #include <TDirectory.h>
25 #include <TFile.h>
26 #include <TH1.h>
27 #include <TH2.h>
28 #include <TH3.h>
29 #include <TKey.h>
30 #include <TObject.h>
31 #include <TProfile.h>
32 #include <TProfile2D.h>
33 #include <TTree.h>
34 
35 namespace {
36  auto maybe_stol = []( const std::string& s ) -> std::optional<int> {
37  auto pos = s.find_first_of( "0123456789+-" );
38  if ( pos == std::string::npos ) return std::nullopt;
39  return std::stol( s.substr( pos ) );
40  };
41 } // namespace
42 
44 
45 //-----------------------------------------------------------------------------
46 StatusCode RootHistCnv::RDirectoryCnv::createObj( IOpaqueAddress* /* pAddress */, DataObject*& refpObject )
47 //-----------------------------------------------------------------------------
48 {
49  refpObject = new NTuple::Directory();
50  return StatusCode::SUCCESS;
51 }
52 
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
56 {
57  const std::string& loc = pObject->registry()->identifier();
58  if ( createDirectory( loc ).isSuccess() ) {
59  setDirectory( loc );
60  setDiskDirectory( loc );
61  // return createAddress(pObject, pObject->registry()->name(), refpAddress);
62  return createAddress( pObject, gDirectory, nullptr, refpAddress );
63  }
64  refpAddress = nullptr;
65  return StatusCode::FAILURE;
66 }
67 
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
71 {
72  const std::string& loc = pObject->registry()->identifier();
73  if ( createDirectory( loc ).isSuccess() ) {
74  setDirectory( loc );
75  return StatusCode::SUCCESS;
76  }
77  return StatusCode::FAILURE;
78 }
79 
80 //-----------------------------------------------------------------------------
82  MsgStream log( msgSvc(), "RDirectoryCnv" );
83  IRegistry* pReg = pObj->registry();
84  std::string full = pReg->identifier();
85  const std::string& fname = pAddr->par()[0];
86 
87  TFile* tf = nullptr;
88  findTFile( full, tf ).ignore();
89 
90  // cd to TFile:
91  setDirectory( full );
92  TIter nextkey( gDirectory->GetListOfKeys() );
93  while ( TKey* key = (TKey*)nextkey() ) {
94  IOpaqueAddress* pA = nullptr;
95  TObject* obj = key->ReadObj();
96  std::string title = obj->GetTitle();
97  std::string sid = obj->GetName();
98  std::string f2 = full + "/" + sid;
99  int idh = maybe_stol( sid ).value_or( 0 );
100  // introduced by Grigori Rybkine
101  std::string clname = key->GetClassName();
102  std::string clnm = clname.substr( 0, 3 );
103  TClass* isa = obj->IsA();
104  if ( isa->InheritsFrom( "TTree" ) ) {
105  createAddress( full, CLID_ColumnWiseTuple, idh, obj, pA ).ignore();
106  TTree* tree = (TTree*)obj;
107  tree->Print();
108  log << MSG::DEBUG << "Reg CWNT \"" << obj->GetTitle() << "\" as " << f2 << endmsg;
109  title = "/" + sid;
110  } else if ( isa->InheritsFrom( "TDirectory" ) ) {
111  createAddress( full, CLID_NTupleDirectory, title, obj, pA ).ignore();
112  } else if ( isa == TProfile::Class() ) {
113  createAddress( full, CLID_ProfileH, idh, obj, pA ).ignore();
114  title = sid;
115  } else if ( isa == TProfile2D::Class() ) {
116  createAddress( full, CLID_ProfileH2, idh, obj, pA ).ignore();
117  title = sid;
118  } else if ( isa == TH1C::Class() ) {
119  createAddress( full, CLID_H1D, idh, obj, pA ).ignore();
120  title = sid;
121  } else if ( isa == TH1S::Class() ) {
122  createAddress( full, CLID_H1D, idh, obj, pA ).ignore();
123  title = sid;
124  } else if ( isa == TH1I::Class() ) {
125  createAddress( full, CLID_H1D, idh, obj, pA ).ignore();
126  title = sid;
127  } else if ( isa == TH1F::Class() ) {
128  createAddress( full, CLID_H1D, idh, obj, pA ).ignore();
129  title = sid;
130  } else if ( isa == TH1D::Class() ) {
131  createAddress( full, CLID_H1D, idh, obj, pA ).ignore();
132  title = sid;
133  } else if ( isa == TH2C::Class() ) {
134  createAddress( full, CLID_H2D, idh, obj, pA ).ignore();
135  title = sid;
136  } else if ( isa == TH2S::Class() ) {
137  createAddress( full, CLID_H2D, idh, obj, pA ).ignore();
138  title = sid;
139  } else if ( isa == TH2I::Class() ) {
140  createAddress( full, CLID_H2D, idh, obj, pA ).ignore();
141  title = sid;
142  } else if ( isa == TH2F::Class() ) {
143  createAddress( full, CLID_H2D, idh, obj, pA ).ignore();
144  title = sid;
145  } else if ( isa == TH2D::Class() ) {
146  createAddress( full, CLID_H2D, idh, obj, pA ).ignore();
147  title = sid;
148  } else if ( isa == TH3C::Class() ) {
149  createAddress( full, CLID_H3D, idh, obj, pA ).ignore();
150  title = sid;
151  } else if ( isa == TH3S::Class() ) {
152  createAddress( full, CLID_H3D, idh, obj, pA ).ignore();
153  title = sid;
154  } else if ( isa == TH3I::Class() ) {
155  createAddress( full, CLID_H3D, idh, obj, pA ).ignore();
156  title = sid;
157  } else if ( isa == TH3F::Class() ) {
158  createAddress( full, CLID_H3D, idh, obj, pA ).ignore();
159  title = sid;
160  } else if ( isa == TH3D::Class() ) {
161  createAddress( full, CLID_H3D, idh, obj, pA ).ignore();
162  title = sid;
163  } else {
164  log << MSG::ERROR << "Encountered an unknown object with key: " << obj->GetName() << " in ROOT file " << fname
165  << endmsg;
166  return StatusCode::FAILURE;
167  }
168  if ( pA ) {
169  StatusCode sc = dataManager()->registerAddress( pReg, title, pA );
170  if ( !sc.isSuccess() ) {
171  log << MSG::ERROR << "Failed to register address for " << full << endmsg;
172  return sc;
173  }
174  log << MSG::VERBOSE << "Created address for " << clnm << "'" << title << "' in " << full << endmsg;
175  }
176  }
177  return StatusCode::SUCCESS;
178 }
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
std::string
STL class.
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
Gaudi.Configuration.log
log
Definition: Configuration.py:28
RootHistCnv::RDirectoryCnv::fillObjRefs
StatusCode fillObjRefs(IOpaqueAddress *pAddr, DataObject *refpObj) override
Update the transient object from the other representation.
Definition: RDirectoryCnv.cpp:81
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
NTuple::Directory
Small class representing an N tuple directory in the transient store.
Definition: NTuple.h:914
gaudirun.s
string s
Definition: gaudirun.py:346
IOpaqueAddress
Definition: IOpaqueAddress.h:33
DECLARE_CONVERTER
#define DECLARE_CONVERTER(x)
Definition: Converter.h:163
RootHistCnv::RDirectoryCnv
Definition: RDirectoryCnv.h:26
IRegistry
Definition: IRegistry.h:32
IDataProviderSvc.h
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:80
RootHistCnv::RConverter::setDirectory
void setDirectory(const std::string &loc)
Definition: RConverter.cpp:118
std::stol
T stol(T... args)
RootHistCnv
Definition: DirectoryCnv.h:27
StatusCode
Definition: StatusCode.h:65
RDirectoryCnv.h
IOpaqueAddress.h
NTuple.h
RootHistCnv::RConverter::createAddress
StatusCode createAddress(DataObject *pObject, TDirectory *pDir, TObject *pTObject, IOpaqueAddress *&refpAddr)
Create address of the transient object according to the requested representation.
Definition: RConverter.cpp:165
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
IRegistry.h
MsgStream
Definition: MsgStream.h:33
RootHistCnv::RDirectoryCnv::updateRep
StatusCode updateRep(IOpaqueAddress *pAddr, DataObject *pObject) override
Convert the transient object to the requested representation.
Definition: RDirectoryCnv.cpp:69
std::string::substr
T substr(T... args)
RootHistCnv::RConverter::setDiskDirectory
void setDiskDirectory(const std::string &loc)
Definition: RConverter.cpp:151
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
RootHistCnv::RConverter::createDirectory
StatusCode createDirectory(const std::string &loc)
Definition: RConverter.cpp:34
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
DataObject
Definition: DataObject.h:36
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ProduceConsume.key
key
Definition: ProduceConsume.py:84
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:78
IDataManagerSvc.h
MsgStream.h
RootHistCnv::RDirectoryCnv::createRep
StatusCode createRep(DataObject *pObj, IOpaqueAddress *&refpAddr) override
Convert the transient object to the requested representation.
Definition: RDirectoryCnv.cpp:54