7 #include "boost/algorithm/string/case_conv.hpp" 10 #include "TDirectory.h" 36 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
37 OutputIterator transform_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
40 while ( first != last ) {
41 if ( pred( *first ) ) *result++ = op( *first );
47 constexpr
struct select1st_t {
48 template <
typename T,
typename S>
94 static TROOT
root(
"root",
"ROOT I/O" );
98 verbose() <<
"ROOT already initialized, debug = " << gDebug <<
endmsg;
103 error() <<
"unable to get the IncidentSvc" <<
endmsg;
120 error() <<
"unable to register ROOT file open action with FileMgr" <<
endmsg;
126 error() <<
"unable to register ROOT file open Error action with FileMgr" <<
endmsg;
144 if (
service(
"IoComponentMgr", iomgr,
true ).isFailure() ) {
145 error() <<
"unable to get the IoComponentMgr" <<
endmsg;
149 error() <<
"could not register with the I/O component manager !" <<
endmsg;
152 bool all_good =
true;
154 for (
const auto& reg :
m_files ) {
155 const std::string& fname = reg.second.first->GetName();
159 warning() <<
"could not register file [" << fname <<
"] with the I/O component manager..." <<
endmsg;
162 info() <<
"registered file [" << fname <<
"]... [ok]" <<
endmsg;
166 error() <<
"problem while registering input/output files with " 167 <<
"the I/O component manager !" <<
endmsg;
174 fatal() <<
"Unable to initialize THistSvc" <<
endmsg;
199 for (
const auto& itr : sorted_uids ) {
200 THistID& thid = itr.second->at( 0 );
201 TObject* tobj = thid.
obj;
204 if ( tobj && tobj->IsA()->InheritsFrom(
"TTree" ) ) {
205 TTree* tree =
dynamic_cast<TTree*
>( tobj );
206 if ( tree->GetDirectory() != 0 ) {
207 dirname = tree->GetDirectory()->GetPath();
209 }
else if ( tobj && tobj->IsA()->InheritsFrom(
"TGraph" ) ) {
211 dirname = thid.
file->GetPath();
215 if ( id2.
find(
"/" ) == 0 ) {
222 }
else if ( tobj && tobj->IsA()->InheritsFrom(
"TH1" ) ) {
223 TH1* th =
dynamic_cast<TH1*
>( tobj );
224 if ( th ==
nullptr ) {
225 error() <<
"Couldn't dcast: " << itr.first <<
endmsg;
227 if ( th->GetDirectory() != 0 ) {
228 dirname = th->GetDirectory()->GetPath();
231 }
else if ( !tobj ) {
240 error() <<
"problems writing histograms" <<
endmsg;
244 info() <<
"Listing contents of ROOT files: " <<
endmsg;
248 if (
std::find( deleted_files.
begin(), deleted_files.
end(), itr.second.first ) == deleted_files.
end() ) {
249 deleted_files.
push_back( itr.second.first );
253 debug() <<
"finalizing stream/file " << itr.first <<
":" << itr.second.first->GetName() <<
endmsg;
259 debug() <<
"already finalized stream " << itr.first <<
endmsg;
266 info() <<
"==> File: " << itr.second.first->GetName() <<
" stream: " << itr.first <<
endmsg;
268 itr.second.first->Print(
"base" );
276 if (
service(
"IncidentSvc", pIncidentSvc ).isFailure() ) {
277 error() <<
"Unable to get the IncidentSvc" <<
endmsg;
281 if ( itr.second.second ==
SHARE ) {
283 void* vfile =
nullptr;
292 TFile* outputfile = (TFile*)vfile;
296 debug() <<
"THistSvc::writeObjectsToFile()::Merging Rootfile " <<
endmsg;
303 error() <<
"unable to open temporary file: \"" << tmpfn <<
endmsg;
307 TFile* inputfile = (TFile*)vfile;
309 outputfile->SetCompressionLevel( inputfile->GetCompressionLevel() );
318 debug() <<
"Trying to remove temporary file \"" << tmpfn <<
"\"" <<
endmsg;
323 delete itr.second.first;
335 delete obj.second.first;
358 if ( hist_ptr !=
nullptr ) {
359 hist_ptr = hist.
get();
372 hist = getHist_i<TH1>( id, ind );
373 if ( hist !=
nullptr ) {
382 hist = getHist_i<TH2>( id, ind );
383 if ( hist !=
nullptr ) {
392 hist = getHist_i<TH3>( id, ind );
393 if ( hist !=
nullptr ) {
435 tree = getHist_i<TTree>( id );
436 if ( tree !=
nullptr ) {
451 if ( strcmp( graph->GetName(),
"Graph" ) == 0 ) {
453 std::string::size_type i = id2.
rfind(
"/" );
454 if ( i != std::string::npos ) {
455 id2.
erase( 0, i + 1 );
458 info() <<
"setting name of TGraph id: \"" <<
id <<
"\" to \"" << id2 <<
"\" since it is unset" <<
endmsg;
459 graph->SetName( id2.
c_str() );
468 if ( strcmp( graph->GetName(),
"Graph" ) == 0 ) {
470 std::string::size_type i = id2.
rfind(
"/" );
471 if ( i != std::string::npos ) {
472 id2.
erase( 0, i + 1 );
475 info() <<
"setting name of TGraph id: \"" <<
id <<
"\" to \"" << id2 <<
"\" since it is unset" <<
endmsg;
476 graph->SetName( id2.
c_str() );
484 graph = getHist_i<TGraph>( id );
485 if ( graph !=
nullptr ) {
494 lh = regShared_i<TH1>( id,
std::move( hist ) );
504 lh = regShared_i<TH2>( id,
std::move( hist ) );
514 lh = regShared_i<TH3>( id,
std::move( hist ) );
524 lh = regShared_i<TGraph>( id,
std::move( graph ) );
534 lh = getShared_i<TH1>(
name );
544 lh = getShared_i<TH2>(
name );
554 lh = getShared_i<TH3>(
name );
564 lh = getShared_i<TGraph>(
name );
576 error() <<
"Problem deregistering id \"" <<
id <<
"\": not found in registry" <<
endmsg;
581 debug() <<
"will deregister " << vh->
size() <<
" elements of id \"" <<
id <<
"\"" <<
endmsg;
583 while ( vh->
size() > 0 ) {
584 if (
deReg( vh->
back().obj ).isFailure() ) {
586 error() <<
"Problems deRegistering " << vh->
size() <<
" element of id \"" <<
id <<
"\"" <<
endmsg;
598 vhid_t* vhid = obj_itr->second.first;
599 THistID hid = obj_itr->second.first->at( obj_itr->second.second );
603 error() <<
"Problems deregistering TObject \"" << obj->GetName() <<
"\" with id \"" << hid.
id 604 <<
"\": not in uidMap" <<
endmsg;
608 if ( vhid->
size() == 1 ) {
610 debug() <<
"vhid for " << hid.
id <<
" is empty. deleting" <<
endmsg;
617 [&]( idMap_t::const_reference i ) { return i.second->at( 0 ).obj == obj; } );
618 if ( id_itr == mitr.second ) {
619 error() <<
"Problems deregistering TObject \"" << obj->GetName() <<
"\" with id \"" << hid.
id 620 <<
"\": not in idMap" <<
endmsg;
626 error() <<
"Problems deregistering TObject \"" << obj->GetName() <<
"\" with id \"" << hid.
id 627 <<
"\": not in hlist" <<
endmsg;
632 vhid->
erase( vhid->
begin() + obj_itr->second.second );
640 }
else if ( vhid->
size() > 1 ) {
642 vhid->
erase( vhid->
begin() + obj_itr->second.second );
646 error() <<
"Deregistration failed unexpectedly. (bug in THistSvc?)" <<
endmsg;
650 error() <<
"Cannot unregister TObject \"" << obj->GetName() <<
"\": not known to THistSvc" <<
endmsg;
659 error() <<
"merge: id \"" << name <<
"\" not found" <<
endmsg;
663 return merge( itr->second );
670 return merge( itr->second.first );
672 error() <<
"merge: unknown object " << obj <<
endmsg;
684 []( uidMap_t::const_reference i ) {
return i.second->at( 0 ).obj->IsA()->InheritsFrom(
"TH1" ); } );
693 []( uidMap_t::const_reference i ) {
return i.second->at( 0 ).obj->IsA()->InheritsFrom(
"TTree" ); } );
702 []( uidMap_t::const_reference i ) {
return i.second->at( 0 ).obj->IsA()->InheritsFrom(
"TGraph" ); } );
710 gErrorIgnoreLevel = kBreak;
713 error() <<
"getTHists: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
718 debug() <<
"getTHists: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
721 TIter nextkey( td->GetListOfKeys() );
722 while ( TKey* key = (TKey*)nextkey() ) {
725 TObject* obj = key->ReadObj();
726 if ( obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
728 }
else if ( obj != 0 && obj->IsA()->InheritsFrom(
"TH1" ) ) {
731 }
else if ( obj != 0 ) {
739 nextkey = td->GetListOfKeys();
740 while ( TKey* key = (TKey*)nextkey() ) {
741 TObject* obj = key->ReadObj();
742 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
743 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
757 gErrorIgnoreLevel = kBreak;
766 r2 = itr->second.first->GetName();
771 debug() <<
"getTHists: \"" << dir <<
"\" looks like a stream name." 772 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
775 if ( gDirectory->cd( r2.
c_str() ) ) {
782 debug() <<
"getTHists: no such TDirectory \"" << r2 <<
"\"" <<
endmsg;
788 debug() <<
"getTHists: stream \"" << stream <<
"\" not found" <<
endmsg;
792 if ( !gDirectory->cd( dir.
c_str() ) ) {
793 error() <<
"getTHists: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
806 gErrorIgnoreLevel = kBreak;
809 error() <<
"getTHists: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
814 debug() <<
"getTHists: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
817 TIter nextkey( td->GetListOfKeys() );
818 while ( TKey* key = (TKey*)nextkey() ) {
821 TObject* obj = key->ReadObj();
822 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
824 }
else if ( obj && obj->IsA()->InheritsFrom(
"TH1" ) ) {
833 id =
id +
"/" + key->GetName();
835 id =
id + dir +
"/" + key->GetName();
852 nextkey = td->GetListOfKeys();
853 while ( TKey* key = (TKey*)nextkey() ) {
854 TObject* obj = key->ReadObj();
855 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
856 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
869 gErrorIgnoreLevel = kBreak;
878 r2 = itr->second.first->GetName();
883 debug() <<
"getTHists: \"" << dir <<
"\" looks like a stream name." 884 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
887 if ( gDirectory->cd( r2.
c_str() ) ) {
889 sc =
getTHists( gDirectory, tl, rcs, reg );
894 debug() <<
"getTHists: no such TDirectory \"" << r2 <<
"\"" <<
endmsg;
898 debug() <<
"getTHists: stream \"" << stream <<
"\" not found" <<
endmsg;
902 if ( !gDirectory->cd( dir.
c_str() ) ) {
903 error() <<
"getTHists: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
907 warning() <<
"Unable to register histograms automatically " 908 <<
"without a valid stream name" <<
endmsg;
911 sc =
getTHists( gDirectory, tl, rcs, reg );
921 gErrorIgnoreLevel = kBreak;
924 error() <<
"getTTrees: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
929 debug() <<
"getTHists: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
932 TIter nextkey( td->GetListOfKeys() );
933 while ( TKey* key = (TKey*)nextkey() ) {
936 TObject* obj = key->ReadObj();
937 if ( obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
939 }
else if ( obj != 0 && obj->IsA()->InheritsFrom(
"TTree" ) ) {
942 }
else if ( obj != 0 ) {
950 nextkey = td->GetListOfKeys();
951 while ( TKey* key = (TKey*)nextkey() ) {
952 TObject* obj = key->ReadObj();
953 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
954 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
967 gErrorIgnoreLevel = kBreak;
976 r2 = itr->second.first->GetName();
981 debug() <<
"getTTrees: \"" << dir <<
"\" looks like a stream name." 982 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
985 if ( gDirectory->cd( r2.
c_str() ) ) {
989 debug() <<
"getTTrees: no such TDirectory \"" << r2 <<
"\"" <<
endmsg;
993 debug() <<
"getTTrees: stream \"" << stream <<
"\" not found" <<
endmsg;
997 if ( !gDirectory->cd( dir.
c_str() ) ) {
998 error() <<
"getTTrees: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
1010 gErrorIgnoreLevel = kBreak;
1013 error() <<
"getTTrees: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
1018 debug() <<
"getTHists: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
1021 TIter nextkey( td->GetListOfKeys() );
1022 while ( TKey* key = (TKey*)nextkey() ) {
1025 TObject* obj = key->ReadObj();
1026 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1028 }
else if ( obj && obj->IsA()->InheritsFrom(
"TTree" ) ) {
1037 id =
id +
"/" + key->GetName();
1039 id =
id + dir +
"/" + key->GetName();
1048 }
else if ( obj != 0 ) {
1056 nextkey = td->GetListOfKeys();
1057 while ( TKey* key = (TKey*)nextkey() ) {
1058 TObject* obj = key->ReadObj();
1059 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1060 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
1073 gErrorIgnoreLevel = kBreak;
1082 r2 = itr->second.first->GetName();
1087 debug() <<
"getTTrees: \"" << dir <<
"\" looks like a stream name." 1088 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
1091 if ( gDirectory->cd( r2.
c_str() ) ) {
1092 return getTTrees( gDirectory, tl, rcs, reg );
1095 debug() <<
"getTTrees: no such TDirectory \"" << r2 <<
"\"" <<
endmsg;
1100 debug() <<
"getTTrees: stream \"" << stream <<
"\" not found" <<
endmsg;
1104 if ( !gDirectory->cd( dir.
c_str() ) ) {
1105 error() <<
"getTTrees: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
1109 return getTTrees( gDirectory, tl, rcs, reg );
1121 Long64_t mfs = (Long64_t)
m_maxFileSize.value() * (Long64_t)1048576;
1122 Long64_t mfs_warn = mfs * 95 / 100;
1127 for (
const auto& f :
m_files ) {
1128 TFile* tf = f.second.first;
1132 debug() <<
"stream: " << f.first <<
" name: " << tf->GetName() <<
" size: " << tf->GetSize() <<
endmsg;
1137 if ( tf->GetSize() > mfs ) {
1141 fatal() <<
"file \"" << tf->GetName() <<
"\" associated with stream \"" << f.first
1142 <<
"\" has exceeded the max file size of " <<
m_maxFileSize.value() <<
"MB. Terminating Job." <<
endmsg;
1145 if (
service(
"ApplicationMgr", evt,
true ).isSuccess() ) {
1151 }
else if ( tf->GetSize() > mfs_warn ) {
1152 warning() <<
"file \"" << tf->GetName() <<
"\" associated with stream \"" << f.first
1153 <<
"\" is at 95% of its maximum allowable file size of " <<
m_maxFileSize.value() <<
"MB" <<
endmsg;
1163 bool all_good =
true;
1172 if (
service(
"IoComponentMgr", iomgr,
true ).isFailure() ) {
1173 error() <<
"could not retrieve I/O component manager !" <<
endmsg;
1180 gErrorIgnoreLevel = kFatal;
1182 for (
auto& ifile :
m_files ) {
1183 TFile* f = ifile.second.first;
1186 debug() <<
"file [" << fname <<
"] mode: [" << f->GetOption() <<
"] r:" << f->GetFileBytesRead()
1187 <<
" w:" << f->GetFileBytesWritten() <<
" cnt:" << f->GetFileCounter() <<
endmsg;
1190 if ( ifile.second.second ==
READ ) {
1192 debug() <<
" TFile opened in READ mode: not reassigning names" <<
endmsg;
1198 error() <<
"could not retrieve new name for [" << fname <<
"] !!" <<
endmsg;
1203 debug() <<
"got a new name [" << fname <<
"]..." <<
endmsg;
1208 Option_t*
opts = f->GetOption();
1211 error() <<
"unable to open file \"" << fname <<
"\" for writing" <<
endmsg;
1214 TFile* newfile = (TFile*)vf;
1215 newfile->SetOption( opts );
1219 ifile.second.first = newfile;
1223 for (
auto& uid :
m_uids ) {
1224 for (
auto& hid : *uid.second ) {
1225 if ( hid.file != f )
continue;
1226 TDirectory* olddir = this->
changeDir( hid );
1229 TDirectory* newdir = this->
changeDir( hid );
1230 TClass* cl = hid.obj->IsA();
1234 if ( cl->InheritsFrom(
"TTree" ) ) {
1235 dynamic_cast<TTree*
>( hid.obj )->SetDirectory( newdir );
1236 dynamic_cast<TTree*
>( hid.obj )->Reset();
1237 }
else if ( cl->InheritsFrom(
"TH1" ) ) {
1238 dynamic_cast<TH1*
>( hid.obj )->SetDirectory( newdir );
1239 dynamic_cast<TH1*
>( hid.obj )->Reset();
1240 }
else if ( cl->InheritsFrom(
"TGraph" ) ) {
1241 olddir->Remove( hid.obj );
1242 newdir->Append( hid.obj );
1244 error() <<
"id: \"" << hid.id <<
"\" is not a inheriting from a class " 1245 <<
"we know how to handle (received [" << cl->GetName() <<
"], " 1246 <<
"expected [TTree, TH1 or TGraph]) !" <<
endmsg <<
"attaching to current dir [" << newdir->GetPath()
1248 <<
"nonetheless..." <<
endmsg;
1249 olddir->Remove( hid.obj );
1250 newdir->Append( hid.obj );
1254 f->ReOpen(
"READ" );
1280 template <
typename T>
1283 return dynamic_cast<T*
>( readHist_i<T>( id ) );
1298 for (
auto& hid : *( uitr->second ) ) {
1301 verbose() <<
" update: " << uitr->first <<
" " << hid.id <<
" " << hid.mode <<
endmsg;
1303 TObject* to = hid.obj;
1304 TFile* oldFile = hid.file;
1307 }
else if ( hid.temp || hid.mode ==
READ ) {
1313 }
else if ( to->IsA()->InheritsFrom(
"TTree" ) ) {
1314 TTree* tr =
dynamic_cast<TTree*
>( to );
1315 TFile* newFile = tr->GetCurrentFile();
1317 if ( oldFile != newFile ) {
1320 TFile* dummy =
nullptr;
1321 findStream( hid.id, streamName, rem, dummy );
1324 if ( itr.second.first == oldFile ) {
1325 itr.second.first = newFile;
1329 for (
auto uitr2 = uitr; uitr2 !=
m_uids.
end(); ++uitr2 ) {
1330 for (
auto& hid2 : *( uitr2->second ) ) {
1331 if ( hid2.file == oldFile ) {
1332 hid2.file = newFile;
1338 [&]( streamMap::const_reference
s ) {
return s.second == streamName; } );
1343 debug() <<
"migrating uid: " << hid.id <<
" stream: " << streamName <<
" oldFile: " << oldFileName
1344 <<
" newFile: " << newFileName <<
endmsg;
1348 if ( !oldFileName.
empty() ) {
1353 debug() <<
"changing filename \"" << i->first <<
"\" to \"" << newFileName <<
"\" for stream \"" 1354 << i->second <<
"\"" <<
endmsg;
1362 error() <<
"Problems updating fileStreams with new file name" <<
endmsg;
1375 auto mode = i.second.second;
1376 auto file = i.second.first;
1378 file->Write(
"", TObject::kOverwrite );
1379 }
else if ( mode ==
APPEND ) {
1385 debug() <<
"THistSvc::writeObjectsToFile()::List of Files connected in ROOT " <<
endmsg;
1386 TSeqCollection* filelist = gROOT->GetListOfFiles();
1387 for (
int ii = 0; ii < filelist->GetEntries(); ii++ ) {
1388 debug() <<
"THistSvc::writeObjectsToFile()::List of Files connected in ROOT: \"" << filelist->At( ii )->GetName()
1398 auto loc = ident.
find(
" " );
1406 if ( loc != std::string::npos ) {
1408 for (
auto attrib : Parser( ident.
substr( loc + 1 ) ) ) {
1409 auto TAG = boost::algorithm::to_upper_copy( attrib.tag );
1410 auto VAL = boost::algorithm::to_upper_copy( attrib.value );
1412 if ( TAG ==
"FILE" || TAG ==
"DATAFILE" ) {
1413 filename = attrib.value;
1415 }
else if ( TAG ==
"OPT" ) {
1416 if ( VAL ==
"APPEND" || VAL ==
"UPDATE" ) {
1418 }
else if ( VAL ==
"CREATE" || VAL ==
"NEW" || VAL ==
"WRITE" ) {
1420 }
else if ( VAL ==
"RECREATE" ) {
1422 }
else if ( VAL ==
"SHARE" ) {
1424 }
else if ( VAL ==
"OLD" || VAL ==
"READ" ) {
1427 error() <<
"Unknown OPT: \"" << attrib.value <<
"\"" <<
endmsg;
1430 }
else if ( TAG ==
"TYP" ) {
1432 }
else if ( TAG ==
"CL" ) {
1440 if ( stream ==
"temp" ) {
1441 error() <<
"in JobOption \"" << ident <<
"\": stream name \"temp\" reserved." <<
endmsg;
1445 if ( db_typ !=
"ROOT" ) {
1446 error() <<
"in JobOption \"" << ident <<
"\": technology type \"" << db_typ <<
"\" not supported." <<
endmsg;
1451 error() <<
"in JobOption \"" << ident <<
"\":\n stream \"" << stream <<
"\" already connected to file: \"" 1459 }
else if ( typ ==
'N' ) {
1461 }
else if ( typ ==
'A' ) {
1463 }
else if ( typ ==
'R' ) {
1465 }
else if ( typ ==
'S' ) {
1469 error() <<
"No OPT= specified or unknown access mode in: " << ident <<
endmsg;
1477 const std::string& oldstream = fitr.first->second;
1479 const auto& f_info =
m_files[oldstream];
1481 if ( newMode != f_info.second ) {
1482 error() <<
"in JobOption \"" << ident <<
"\":\n file \"" << filename <<
"\" already opened by stream: \"" 1483 << oldstream <<
"\" with different access mode." <<
endmsg;
1486 TFile* f2 = f_info.first;
1489 debug() <<
"Connecting stream: \"" << stream <<
"\" to previously opened TFile: \"" << filename <<
"\"" 1496 if (
service(
"IncidentSvc", pi ).isFailure() ) {
1497 error() <<
"Unable to get the IncidentSvc" <<
endmsg;
1509 error() <<
"Unable to open ROOT file " << filename <<
" for reading" <<
endmsg;
1523 error() <<
"Unable to open ROOT file " << filename <<
" for writing" <<
endmsg;
1533 error() <<
"unable to open file \"" << filename <<
"\" for appending" <<
endmsg;
1544 static int ishared = 0;
1546 filename =
"tmp_THistSvc_" +
std::to_string( ishared++ ) +
".root";
1549 debug() <<
"Creating temp file \"" << filename <<
"\" and realfilename=" << realfilename <<
endmsg;
1556 error() <<
"Unable to open ROOT file " << filename <<
" for writing" <<
endmsg;
1567 error() <<
"Unable to open ROOT file " << filename <<
" for appending" <<
endmsg;
1578 debug() <<
"Opening TFile \"" << filename <<
"\" stream: \"" << stream <<
"\" mode: \"" << typ <<
"\"" 1579 <<
" comp level: " << cl <<
endmsg;
1588 TFile* file = hid.
file;
1601 if ( !gDirectory->GetKey( dir.
c_str() ) ) {
1602 gDirectory->mkdir( dir.
c_str() );
1604 gDirectory->cd( dir.
c_str() );
1612 std::string::size_type i = dir.
find(
"/" );
1614 if ( i == std::string::npos )
return {};
1629 while (
id.find(
"//" ) != std::string::npos ) {
1630 id.replace(
id.find(
"//" ), 2,
"/" );
1637 debug() <<
"Target path: " << target->GetPath() <<
endmsg;
1639 TString
path( (
char*)strstr( target->GetPath(),
":" ) );
1640 path.Remove( 0, 2 );
1643 TDirectory* current_sourcedir = gDirectory;
1646 TList* lkeys = current_sourcedir->GetListOfKeys();
1647 int nkeys = lkeys->GetEntries();
1648 TKey* key =
nullptr;
1649 for (
int jj = 0; jj < nkeys; jj++ ) {
1650 key = (TKey*)lkeys->At( jj );
1653 debug() <<
"Reading Key:" << pathnameinsource <<
endmsg;
1655 TObject* obj = source->Get( pathnameinsource.
c_str() );
1658 if ( obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1661 debug() <<
"Found subdirectory " << obj->GetName() <<
endmsg;
1666 TDirectory* newtargetdir = target->mkdir( obj->GetName(), obj->GetTitle() );
1670 }
else if ( obj->IsA()->InheritsFrom(
"TTree" ) ) {
1672 debug() <<
"Found TTree " << obj->GetName() <<
endmsg;
1674 TTree* mytree =
dynamic_cast<TTree*
>( obj );
1675 int nentries = (int)mytree->GetEntries();
1676 mytree->SetBranchStatus(
"*", 1 );
1679 debug() <<
"Dumping TTree " << nentries <<
" entries" <<
endmsg;
1682 mytree->CloneTree();
1686 obj->Write( key->GetName() );
1694 auto pos =
id.find(
"/" );
1696 if ( pos == std::string::npos ) {
1700 }
else if ( pos != 0 ) {
1707 auto pos2 =
id.
find(
"/", pos + 1 );
1709 if ( pos2 == std::string::npos ) {
1711 error() <<
"badly formed Hist/Tree id: \"" <<
id <<
"\"" <<
endmsg;
1717 if ( stream ==
"temp" ) {
1723 file = ( itr !=
m_files.
end() ? itr->second.first : nullptr );
1725 warning() <<
"no stream \"" << stream <<
"\" associated with id: \"" <<
id <<
"\"" <<
endmsg;
1733 auto pos =
id.find(
"/" );
1735 if ( pos == std::string::npos ) {
1738 }
else if ( pos == 0 ) {
1741 root =
id.
substr( 0, pos );
1742 rem =
id.
substr( pos + 1 );
1750 debug() <<
"Delaying connection of Input Files until Initialize" 1755 debug() <<
"Now connecting of Input Files" <<
endmsg;
1780 debug() <<
"Delaying connection of Input Files until Initialize" 1805 warning() <<
"\"CompressionLevel\" Property has been deprecated. " 1806 <<
"Set it via the \"CL=\" parameter in the \"Output\" Property" <<
endmsg;
1812 debug() <<
"copyFileLayout() to destination path: " << destination->GetPath() <<
endmsg;
1816 TString
path( (
char*)strstr( destination->GetPath(),
":" ) );
1817 path.Remove( 0, 2 );
1820 TDirectory* current_source_dir = gDirectory;
1823 TList* key_list = current_source_dir->GetListOfKeys();
1824 int n = key_list->GetEntries();
1825 for (
int j = 0; j <
n; ++j ) {
1826 TKey* k = (TKey*)key_list->At( j );
1828 TObject* o = source->Get( source_pathname.
c_str() );
1830 if ( o && o->IsA()->InheritsFrom(
"TDirectory" ) ) {
1832 verbose() <<
" subdir [" << o->GetName() <<
"]..." <<
endmsg;
1835 TDirectory* destination_dir = destination->mkdir( o->GetName(), o->GetTitle() );
1851 if ( idr.
find(
"/" ) == 0 ) {
1859 if ( index >= itr->second->size() ) {
1860 error() <<
"no index " << index <<
" found for Hist " << idr <<
endmsg;
1863 hid = &( itr->second->at( index ) );
1869 if ( mitr.first == mitr.second ) {
1872 }
else if ( distance( mitr.first, mitr.second ) == 1 ) {
1874 if ( index >= mitr.first->second->size() ) {
1875 error() <<
"no index " << index <<
" found for Hist " << idr <<
endmsg;
1878 hid = &( mitr.first->second->at( 0 ) );
1882 hid = &( mitr.first->second->at( 0 ) );
1883 return distance( mitr.first, mitr.second );
1893 ost <<
"m_hlist: size: " <<
m_hlist.
size() <<
"\n";
1895 ost <<
" - " << vh->at( 0 ) <<
" :: [" << vh <<
"] " << vh->size() <<
" {";
1896 for (
auto& e : *vh ) {
1898 ost <<
"[" << o <<
"]";
1906 for (
auto& e :
m_uids ) {
1907 ost <<
" - " << e.first <<
" [" << e.second <<
"]" <<
std::endl;
1913 for (
auto& e :
m_ids ) {
1914 ost <<
" - " << e.first <<
" [" << e.second <<
"]" <<
std::endl;
1921 TObject* o = e.first;
1922 THistID& i = e.second.first->at( e.second.second );
1923 ost <<
" - " << o <<
" -> " << i <<
std::endl;
1934 if ( vh->
size() == 1 ) {
1935 debug() <<
"merge: id: \"" << name <<
"\" is size 1. nothing to do" <<
endmsg;
1939 if ( !vh->
at( 0 ).obj->IsA()->InheritsFrom(
"TH1" ) ) {
1940 error() <<
"merge: id \"" << name <<
"\" is not a THn. Cannot merge" <<
endmsg;
1944 TList*
l =
new TList();
1945 for (
size_t i = 1; i < vh->
size(); ++i ) {
1946 debug() <<
"merge: id: \"" << name <<
"\" (" << vh->
at( i ).obj <<
") adding index " << i <<
endmsg;
1947 l->Add( vh->
at( i ).obj );
1950 TH1* t0 =
dynamic_cast<TH1*
>( vh->
at( 0 ).obj );
1952 error() <<
"merge: could not dcast " << name <<
"(" << t0 <<
") index " << 0 <<
" to TH1" <<
endmsg;
1956 Long64_t
n = t0->Merge( l );
1958 debug() <<
"merge: id: \"" << name <<
"\" merged " << n <<
" entries" <<
endmsg;
1960 for (
size_t i = 1; i < vh->
size(); ++i ) {
1961 TH1* th =
dynamic_cast<TH1*
>( vh->
at( i ).obj );
1963 debug() <<
"clearing index " << i <<
"(" << th <<
")" <<
endmsg;
1964 th->SetDirectory(
nullptr );
1967 error() <<
"merge: could not dcast " << name <<
" index " << i <<
" to TH1" <<
endmsg;
1981 if ( fa->
desc() !=
"HIST" ) {
2006 if ( fa->
desc() !=
"HIST" ) {
Parse attribute strings allowing iteration over the various attributes.
virtual StatusCode io_retrieve(IIoComponent *iocomponent, std::string &fname)=0
: retrieve the new filename for a given IIoComponent and
GlobalDirectoryRestore(THistSvcMutex_t &mut)
Helper struct that bundles the histogram ID with a mutex, TFile and TObject*.
constexpr static const auto FAILURE
StatusCode initialize() override
void MergeRootFile(TDirectory *, TDirectory *)
virtual StatusCode stopRun()=0
Schedule a stop of the current event processing.
StatusCode getGraph(const std::string &name, TGraph *&) const override
Return TGraph with given name.
This class is the FileIncident.
StatusCode initialize() override
StatusCode deReg(const std::string &name) override
Deregister object with given name and give up ownership (without deletion!)
Define general base for Gaudi exception.
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
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
const std::string & name() const override
Retrieve name of the service.
TTree * readTree(const std::string &name) const
StatusCode finalize() override
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
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.
std::vector< std::string > getGraphs() const override
This file contains the class definition for the FileIncident class.
bool exists(const std::string &name) const override
Check if object with given name is managed by THistSvcMT.
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
StatusCode getTHists(TDirectory *td, TList &, bool recurse=false) const override
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
StatusCode writeObjectsToFile()
StatusCode merge(const std::string &id) override
Merge all clones for object with a given id.
StatusCode rootOpenAction(FILEMGR_CALLBACK_ARGS)
void copyFileLayout(TDirectory *, TDirectory *)
helper function to recursively copy the layout of a TFile into a new TFile
TDirectory * changeDir(const THistSvc::THistID &hid) const
StatusCode getHist(const std::string &name, TH1 *&, size_t index=0) const override
Return histogram with given name as TH1*, THistSvcMT still owns object.
void setupCompressionLevel(Gaudi::Details::PropertyBase &cmp)
Gaudi::Property< std::vector< std::string > > m_outputfile
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
std::vector< std::string > getHists() const override
virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action &, const std::string &d="")=0
#define DECLARE_COMPONENT(type)
const std::string & name() const
Gaudi::Property< int > m_autoSave
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
std::string stripDirectoryName(std::string &dir) const
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
const std::string & desc() const
Gaudi::Property< std::vector< std::string > > m_inputfile
This class is used for returning status codes from appropriate routines.
Provides automatic lock/unlock access to a class upon deref of ptr.
Gaudi::Property< bool > m_print
void handle(const Incident &) override
Gaudi::Property< int > m_maxFileSize
StatusCode getTree(const std::string &name, TTree *&) const override
Return TTree with given name.
~GlobalDirectoryRestore()
StatusCode rootOpenErrAction(FILEMGR_CALLBACK_ARGS)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Gaudi::Property< int > m_compressionLevel
void parseString(const std::string &id, std::string &root, std::string &rem) const
StatusCode finalize() override
StatusCode regHist_i(std::unique_ptr< T > hist, const std::string &name, bool shared)
constexpr static const auto SUCCESS
StatusCode regHist(const std::string &name) override
Register a new ROOT histogram TH*X with a name.
Mode
Enumerating all possible file access modes.
StatusCode getTTrees(TDirectory *td, TList &, bool recurse=false) const override
virtual unsigned long release()=0
Release Interface instance.
StatusCode reinitialize() override
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T back_inserter(T...args)
const StatusCode & ignore() const
Ignore/check StatusCode.
Base class for all Incidents (computing events).
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
Gaudi::StateMachine::State FSMState() const override
StatusCode connect(const std::string &)
The IEventProcessor is the interface to process events.
virtual StatusCode io_register(IIoComponent *iocomponent)=0
: allow a IIoComponent to register itself with this manager so appropriate actions can be taken when ...
std::set< std::string > m_alreadyConnectedOutFiles
list of already connected files.
StatusCode regTree(const std::string &name) override
Register a new TTree with a given name.
std::map< std::string, std::string > m_sharedFiles
size_t findHistID(const std::string &id, const THistID *&hid, const size_t &index=0) const
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.
std::vector< std::string > getTrees() const override
StatusCode getShared(const std::string &name, LockedHandle< TH1 > &) const override
Retrieve shared object with given name as TH1 through LockedHandle.
AttribStringParser::Iterator begin(const AttribStringParser &parser)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
std::set< std::string > m_alreadyConnectedInFiles
list of already connected files.
void setupInputFile(Gaudi::Details::PropertyBase &inputfile)
call-back method to handle input stream property
void setupOutputFile(Gaudi::Details::PropertyBase &outputfile)
call-back method to handle output stream property
StatusCode regGraph(const std::string &name) override
Register a new TGraph with a given name.
Gaudi::Property< int > m_autoFlush
THistSvc(const std::string &name, ISvcLocator *svc)
StatusCode io_reinit() override
callback method to reinitialize the internal state of the component for I/O purposes (e...
Helper class that manages ROOts global directory and file.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
TDirectory * m_gDirectory
The interface implemented by the IncidentSvc service.
T * readHist(const std::string &name) const
void removeDoubleSlash(std::string &) const
void updateFiles()
Handle case where TTree grows beyond TTree::fgMaxTreeSize.
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
virtual Io::close_t close(const Io::Fd, const std::string &caller)=0