18 #include "boost/algorithm/string/case_conv.hpp"
22 #include "TDirectory.h"
59 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
60 OutputIterator transform_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
61 UnaryPredicate pred ) {
62 while ( first != last ) {
63 if ( pred( *first ) ) *result++ = op( *first );
69 constexpr
struct select1st_t {
70 template <
typename T,
typename S>
85 error() <<
"initializing service" <<
endmsg;
94 error() <<
"Caught: " << err <<
endmsg;
101 error() <<
"Caught: " << err <<
endmsg;
107 static TROOT
root(
"root",
"ROOT I/O" );
114 error() <<
"unable to get the IncidentSvc" <<
endmsg;
121 error() <<
"unable to get the FileMgr" <<
endmsg;
124 debug() <<
"got the FileMgr" <<
endmsg;
131 error() <<
"unable to register ROOT file open action with FileMgr" <<
endmsg;
137 error() <<
"unable to register ROOT file open Error action with FileMgr" <<
endmsg;
150 if (
service(
"IoComponentMgr", iomgr,
true ).isFailure() ) {
151 error() <<
"unable to get the IoComponentMgr" <<
endmsg;
155 error() <<
"could not register with the I/O component manager !" <<
endmsg;
158 bool all_good =
true;
160 for (
const auto& reg :
m_files ) {
161 const std::string& fname = reg.second.first->GetName();
165 warning() <<
"could not register file [" << fname <<
"] with the I/O component manager..." <<
endmsg;
168 info() <<
"registered file [" << fname <<
"]... [ok]" <<
endmsg;
172 error() <<
"problem while registering input/output files with "
173 <<
"the I/O component manager !" <<
endmsg;
182 error() <<
"Could not access/load the dictionary for TTree!" <<
endmsg;
186 if ( st.
isFailure() ) { fatal() <<
"Unable to initialize THistSvc" <<
endmsg; }
193 warning() <<
"reinitialize not implemented" <<
endmsg;
202 debug() <<
"THistSvc::finalize" <<
endmsg;
208 for (
const auto& itr : sorted_uids ) {
209 THistID& thid = itr.second->at( 0 );
210 TObject* tobj = thid.
obj;
213 if ( tobj && tobj->IsA()->InheritsFrom(
"TTree" ) ) {
214 TTree* tree =
dynamic_cast<TTree*
>( tobj );
215 if ( tree->GetDirectory() != 0 ) { dirname = tree->GetDirectory()->GetPath(); }
216 }
else if ( tobj && tobj->IsA()->InheritsFrom(
"TGraph" ) ) {
218 dirname = thid.
file->GetPath();
222 if ( id2.
find(
"/" ) == 0 ) { id2.
erase( 0, 1 ); }
227 }
else if ( tobj && tobj->IsA()->InheritsFrom(
"TH1" ) ) {
228 TH1* th =
dynamic_cast<TH1*
>( tobj );
229 if ( th ==
nullptr ) {
230 error() <<
"Couldn't dcast: " << itr.first <<
endmsg;
232 if ( th->GetDirectory() != 0 ) { dirname = th->GetDirectory()->GetPath(); }
234 }
else if ( !tobj ) {
235 warning() << itr.first <<
" has NULL TObject ptr" <<
endmsg;
237 debug() <<
"finalize: " << thid <<
endmsg;
244 if (
m_print ) { info() <<
"Listing contents of ROOT files: " <<
endmsg; }
247 if (
std::find( deleted_files.
begin(), deleted_files.
end(), itr.second.first ) == deleted_files.
end() ) {
248 deleted_files.
push_back( itr.second.first );
252 debug() <<
"finalizing stream/file " << itr.first <<
":" << itr.second.first->GetName() <<
endmsg;
263 info() <<
"==> File: " << itr.second.first->GetName() <<
" stream: " << itr.first <<
endmsg;
265 itr.second.first->Print(
"base" );
273 if (
service(
"IncidentSvc", pIncidentSvc ).isFailure() ) {
274 error() <<
"Unable to get the IncidentSvc" <<
endmsg;
278 if ( itr.second.second ==
SHARE ) {
280 void* vfile =
nullptr;
285 error() <<
"unable to open Final Output File: \"" <<
m_sharedFiles[itr.first] <<
"\" for merging" <<
endmsg;
289 TFile* outputfile = (TFile*)vfile;
298 error() <<
"unable to open temporary file: \"" << tmpfn <<
endmsg;
302 TFile* inputfile = (TFile*)vfile;
304 outputfile->SetCompressionLevel( inputfile->GetCompressionLevel() );
316 delete itr.second.first;
329 if ( obj.second.second == 0 ) {
330 delete obj.second.first;
351 if ( hist_ptr !=
nullptr ) { hist_ptr = hist.
get(); }
361 hist = getHist_i<TH1>(
id, ind );
362 if ( hist !=
nullptr ) {
370 hist = getHist_i<TH2>(
id, ind );
371 if ( hist !=
nullptr ) {
379 hist = getHist_i<TH3>(
id, ind );
380 if ( hist !=
nullptr ) {
414 tree = getHist_i<TTree>(
id );
415 if ( tree !=
nullptr ) {
428 if ( strcmp(
graph->GetName(),
"Graph" ) == 0 ) {
430 std::string::size_type i = id2.
rfind(
"/" );
431 if ( i != std::string::npos ) { id2.
erase( 0, i + 1 ); }
433 info() <<
"setting name of TGraph id: \"" <<
id <<
"\" to \"" << id2 <<
"\" since it is unset" <<
endmsg;
442 if ( strcmp(
graph->GetName(),
"Graph" ) == 0 ) {
444 std::string::size_type i = id2.
rfind(
"/" );
445 if ( i != std::string::npos ) { id2.
erase( 0, i + 1 ); }
447 info() <<
"setting name of TGraph id: \"" <<
id <<
"\" to \"" << id2 <<
"\" since it is unset" <<
endmsg;
455 graph = getHist_i<TGraph>(
id );
456 if (
graph !=
nullptr ) {
478 eff = getHist_i<TEfficiency>(
id );
479 if ( eff !=
nullptr ) {
487 lh = regShared_i<TH1>(
id,
std::move( hist ) );
496 lh = regShared_i<TH2>(
id,
std::move( hist ) );
505 lh = regShared_i<TH3>(
id,
std::move( hist ) );
524 lh = regShared_i<TEfficiency>(
id,
std::move( eff ) );
533 lh = getShared_i<TH1>(
name );
542 lh = getShared_i<TH2>(
name );
551 lh = getShared_i<TH3>(
name );
560 lh = getShared_i<TGraph>(
name );
569 lh = getShared_i<TEfficiency>(
name );
580 error() <<
"Problem deregistering id \"" <<
id <<
"\": not found in registry" <<
endmsg;
585 debug() <<
"will deregister " << vh->
size() <<
" elements of id \"" <<
id <<
"\"" <<
endmsg;
588 size_t vh_size = vh->
size();
589 while ( vh_size-- ) {
592 error() <<
"Problems deRegistering " << vh->
size() <<
" element of id \"" <<
id <<
"\"" <<
endmsg;
603 vhid_t* vhid = obj_itr->second.first;
604 THistID hid = obj_itr->second.first->at( obj_itr->second.second );
608 error() <<
"Problems deregistering TObject \"" << obj->GetName() <<
"\" with id \"" << hid.
id
609 <<
"\": not in uidMap" <<
endmsg;
613 if ( vhid->
size() == 1 ) {
615 debug() <<
"vhid for " << hid.
id <<
" is empty. deleting" <<
endmsg;
622 [&]( idMap_t::const_reference i ) { return i.second->at( 0 ).obj == obj; } );
623 if ( id_itr == mitr.second ) {
624 error() <<
"Problems deregistering TObject \"" << obj->GetName() <<
"\" with id \"" << hid.
id
625 <<
"\": not in idMap" <<
endmsg;
631 error() <<
"Problems deregistering TObject \"" << obj->GetName() <<
"\" with id \"" << hid.
id
632 <<
"\": not in hlist" <<
endmsg;
636 vhid->
erase( vhid->
begin() + obj_itr->second.second );
645 }
else if ( vhid->
size() > 1 ) {
647 vhid->
erase( vhid->
begin() + obj_itr->second.second );
651 error() <<
"Deregistration failed unexpectedly. (bug in THistSvc?)" <<
endmsg;
655 error() <<
"Cannot unregister TObject \"" << obj->GetName() <<
"\": not known to THistSvc" <<
endmsg;
663 error() <<
"merge: id \"" <<
name <<
"\" not found" <<
endmsg;
667 return merge( itr->second );
673 return merge( itr->second.first );
675 error() <<
"merge: unknown object " << obj <<
endmsg;
685 return ( getHist_i<TEfficiency>(
name, 0,
true ) !=
nullptr );
689 return ( getHist_i<TGraph>(
name, 0,
true ) !=
nullptr );
698 []( uidMap_t::const_reference i ) {
return i.second->at( 0 ).obj->IsA()->InheritsFrom(
"TH1" ); } );
706 []( uidMap_t::const_reference i ) {
return i.second->at( 0 ).obj->IsA()->InheritsFrom(
"TTree" ); } );
714 []( uidMap_t::const_reference i ) {
return i.second->at( 0 ).obj->IsA()->InheritsFrom(
"TGraph" ); } );
723 []( uidMap_t::const_reference i ) {
return i.second->at( 0 ).obj->IsA()->InheritsFrom(
"TEfficiency" ); } );
730 gErrorIgnoreLevel = kBreak;
733 error() <<
"getTHists: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
738 debug() <<
"getTHists: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
741 TIter nextkey( td->GetListOfKeys() );
742 while ( TKey*
key = (TKey*)nextkey() ) {
745 TObject* obj =
key->ReadObj();
746 if ( obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
748 }
else if ( obj != 0 && obj->IsA()->InheritsFrom(
"TH1" ) ) {
751 }
else if ( obj != 0 ) {
759 nextkey = td->GetListOfKeys();
760 while ( TKey*
key = (TKey*)nextkey() ) {
761 TObject* obj =
key->ReadObj();
762 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
763 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
776 gErrorIgnoreLevel = kBreak;
785 r2 = itr->second.first->GetName();
790 debug() <<
"getTHists: \"" << dir <<
"\" looks like a stream name."
791 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
794 if ( gDirectory->cd( r2.
c_str() ) ) {
807 if ( !gDirectory->cd( dir.
c_str() ) ) {
808 error() <<
"getTHists: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
820 gErrorIgnoreLevel = kBreak;
823 error() <<
"getTHists: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
828 debug() <<
"getTHists: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
831 TIter nextkey( td->GetListOfKeys() );
832 while ( TKey*
key = (TKey*)nextkey() ) {
835 TObject* obj =
key->ReadObj();
836 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
838 }
else if ( obj && obj->IsA()->InheritsFrom(
"TH1" ) ) {
847 id =
id +
"/" +
key->GetName();
849 id =
id + dir +
"/" +
key->GetName();
866 nextkey = td->GetListOfKeys();
867 while ( TKey*
key = (TKey*)nextkey() ) {
868 TObject* obj =
key->ReadObj();
869 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
870 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
882 gErrorIgnoreLevel = kBreak;
891 r2 = itr->second.first->GetName();
896 debug() <<
"getTHists: \"" << dir <<
"\" looks like a stream name."
897 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
900 if ( gDirectory->cd( r2.
c_str() ) ) {
902 sc =
getTHists( gDirectory, tl, rcs, reg );
911 if ( !gDirectory->cd( dir.
c_str() ) ) {
912 error() <<
"getTHists: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
916 warning() <<
"Unable to register histograms automatically "
917 <<
"without a valid stream name" <<
endmsg;
920 sc =
getTHists( gDirectory, tl, rcs, reg );
929 gErrorIgnoreLevel = kBreak;
932 error() <<
"getTTrees: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
937 debug() <<
"getTHists: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
940 TIter nextkey( td->GetListOfKeys() );
941 while ( TKey*
key = (TKey*)nextkey() ) {
944 TObject* obj =
key->ReadObj();
945 if ( obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
947 }
else if ( obj != 0 && obj->IsA()->InheritsFrom(
"TTree" ) ) {
950 }
else if ( obj != 0 ) {
958 nextkey = td->GetListOfKeys();
959 while ( TKey*
key = (TKey*)nextkey() ) {
960 TObject* obj =
key->ReadObj();
961 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
962 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
974 gErrorIgnoreLevel = kBreak;
983 r2 = itr->second.first->GetName();
988 debug() <<
"getTTrees: \"" << dir <<
"\" looks like a stream name."
989 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
992 if ( gDirectory->cd( r2.
c_str() ) ) {
return getTTrees( gDirectory, tl, rcs ); }
998 if ( !gDirectory->cd( dir.
c_str() ) ) {
999 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() ) {
1023 auto&
log = debug();
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 );
1072 gErrorIgnoreLevel = kBreak;
1081 r2 = itr->second.first->GetName();
1086 debug() <<
"getTTrees: \"" << dir <<
"\" looks like a stream name."
1087 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
1090 if ( gDirectory->cd( r2.
c_str() ) ) {
1091 return getTTrees( gDirectory, tl, rcs, reg );
1099 if ( !gDirectory->cd( dir.
c_str() ) ) {
1100 error() <<
"getTTrees: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
1104 return getTTrees( gDirectory, tl, rcs, reg );
1110 gErrorIgnoreLevel = kBreak;
1113 error() <<
"getTEfficiencies: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
1118 debug() <<
"getTEfficiencies: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys"
1122 TIter nextkey( td->GetListOfKeys() );
1123 while ( TKey*
key = (TKey*)nextkey() ) {
1124 auto&
log = debug();
1126 TObject* obj =
key->ReadObj();
1127 if ( obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1129 }
else if ( obj != 0 && obj->IsA()->InheritsFrom(
"TEfficiency" ) ) {
1132 }
else if ( obj != 0 ) {
1140 nextkey = td->GetListOfKeys();
1141 while ( TKey*
key = (TKey*)nextkey() ) {
1142 TObject* obj =
key->ReadObj();
1143 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1144 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
1157 gErrorIgnoreLevel = kBreak;
1166 r2 = itr->second.first->GetName();
1171 debug() <<
"getTEfficiencies: \"" << dir <<
"\" looks like a stream name."
1172 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
1175 if ( gDirectory->cd( r2.
c_str() ) ) {
1188 if ( !gDirectory->cd( dir.
c_str() ) ) {
1189 error() <<
"getTEfficiencies: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
1201 gErrorIgnoreLevel = kBreak;
1204 error() <<
"getTEfficiencies: No such TDirectory \"" << td->GetPath() <<
"\"" <<
endmsg;
1209 debug() <<
"getTEfficiencies: \"" << td->GetPath() <<
"\": found " << td->GetListOfKeys()->GetSize() <<
" keys"
1213 TIter nextkey( td->GetListOfKeys() );
1214 while ( TKey*
key = (TKey*)nextkey() ) {
1215 auto&
log = debug();
1217 TObject* obj =
key->ReadObj();
1218 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1220 }
else if ( obj && obj->IsA()->InheritsFrom(
"TEfficiency" ) ) {
1229 id =
id +
"/" +
key->GetName();
1231 id =
id + dir +
"/" +
key->GetName();
1248 nextkey = td->GetListOfKeys();
1249 while ( TKey*
key = (TKey*)nextkey() ) {
1250 TObject* obj =
key->ReadObj();
1251 if ( obj && obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1252 TDirectory* tt =
dynamic_cast<TDirectory*
>( obj );
1264 gErrorIgnoreLevel = kBreak;
1273 r2 = itr->second.first->GetName();
1278 debug() <<
"getTEfficiencies: \"" << dir <<
"\" looks like a stream name."
1279 <<
" associated TFile: \"" << itr->second.first->GetName() <<
"\"" <<
endmsg;
1282 if ( gDirectory->cd( r2.
c_str() ) ) {
1293 if ( !gDirectory->cd( dir.
c_str() ) ) {
1294 error() <<
"getTEfficiencies: No such TDirectory/stream \"" << dir <<
"\"" <<
endmsg;
1298 warning() <<
"Unable to register histograms automatically "
1299 <<
"without a valid stream name" <<
endmsg;
1317 Long64_t mfs_warn = mfs * 95 / 100;
1321 for (
const auto& f :
m_files ) {
1322 TFile* tf = f.second.first;
1326 debug() <<
"stream: " << f.first <<
" name: " << tf->GetName() <<
" size: " << tf->GetSize() <<
endmsg;
1331 if ( tf->GetSize() > mfs ) {
1335 fatal() <<
"file \"" << tf->GetName() <<
"\" associated with stream \"" << f.first
1339 if (
service(
"ApplicationMgr",
evt,
true ).isSuccess() ) {
1340 evt->stopRun().ignore( );
1345 }
else if ( tf->GetSize() > mfs_warn ) {
1346 warning() <<
"file \"" << tf->GetName() <<
"\" associated with stream \"" << f.first
1356 bool all_good =
true;
1363 if (
service(
"IoComponentMgr", iomgr,
true ).isFailure() ) {
1364 error() <<
"could not retrieve I/O component manager !" <<
endmsg;
1371 gErrorIgnoreLevel = kFatal;
1373 for (
auto& ifile :
m_files ) {
1374 TFile* f = ifile.second.first;
1377 debug() <<
"file [" << fname <<
"] mode: [" << f->GetOption() <<
"] r:" << f->GetFileBytesRead()
1378 <<
" w:" << f->GetFileBytesWritten() <<
" cnt:" << f->GetFileCounter() <<
endmsg;
1381 if ( ifile.second.second ==
READ ) {
1386 if ( !iomgr->
io_retrieve(
this, fname ).isSuccess() ) {
1387 error() <<
"could not retrieve new name for [" << fname <<
"] !!" <<
endmsg;
1390 }
else if ( fname.
empty() ) {
1398 Option_t*
opts = f->GetOption();
1401 error() <<
"unable to open file \"" << fname <<
"\" for writing" <<
endmsg;
1404 TFile* newfile = (TFile*)vf;
1405 newfile->SetOption(
opts );
1409 ifile.second.first = newfile;
1413 for (
auto& uid :
m_uids ) {
1414 for (
auto& hid : *uid.second ) {
1415 if ( hid.file != f )
continue;
1416 TDirectory* olddir = this->
changeDir( hid );
1419 TDirectory* newdir = this->
changeDir( hid );
1420 TClass* cl = hid.obj->IsA();
1424 if ( cl->InheritsFrom(
"TTree" ) ) {
1425 dynamic_cast<TTree&
>( *hid.obj ).SetDirectory( newdir );
1426 dynamic_cast<TTree&
>( *hid.obj ).Reset();
1427 }
else if ( cl->InheritsFrom(
"TH1" ) ) {
1428 dynamic_cast<TH1&
>( *hid.obj ).SetDirectory( newdir );
1429 dynamic_cast<TH1&
>( *hid.obj ).Reset();
1430 }
else if ( cl->InheritsFrom(
"TEfficiency" ) ) {
1431 dynamic_cast<TEfficiency&
>( *hid.obj ).SetDirectory( newdir );
1432 }
else if ( cl->InheritsFrom(
"TGraph" ) ) {
1433 olddir->Remove( hid.obj );
1434 newdir->Append( hid.obj );
1436 error() <<
"id: \"" << hid.id <<
"\" is not a inheriting from a class "
1437 <<
"we know how to handle (received [" << cl->GetName() <<
"], "
1438 <<
"expected [TTree, TH1, TGraph or TEfficiency]) !" <<
endmsg <<
"attaching to current dir ["
1439 << newdir->GetPath() <<
"] "
1440 <<
"nonetheless..." <<
endmsg;
1441 olddir->Remove( hid.obj );
1442 newdir->Append( hid.obj );
1446 f->ReOpen(
"READ" );
1463 gDirectory = m_gDirectory;
1465 gErrorIgnoreLevel = m_gErrorIgnoreLevel;
1470 template <
typename T>
1472 return dynamic_cast<T*
>( readHist_i<T>(
id ) );
1486 for (
auto& hid : *( uitr->second ) ) {
1489 verbose() <<
" update: " << uitr->first <<
" " << hid.id <<
" " << hid.mode <<
endmsg;
1491 TObject* to = hid.obj;
1492 TFile* oldFile = hid.file;
1496 warning() << uitr->first <<
": TObject == 0" <<
endmsg;
1497 }
else if ( hid.temp || hid.mode ==
READ ) {
1503 }
else if ( to->IsA()->InheritsFrom(
m_ttreeClass ) ) {
1504 TTree* tr =
dynamic_cast<TTree*
>( to );
1505 TFile* newFile = tr->GetCurrentFile();
1507 if ( oldFile != newFile ) {
1510 TFile* dummy =
nullptr;
1511 findStream( hid.id, streamName, rem, dummy );
1514 if ( itr.second.first == oldFile ) { itr.second.first = newFile; }
1517 for (
auto uitr2 = uitr; uitr2 !=
m_uids.
end(); ++uitr2 ) {
1518 for (
auto& hid2 : *( uitr2->second ) ) {
1519 if ( hid2.file == oldFile ) { hid2.file = newFile; }
1524 [&]( streamMap::const_reference
s ) {
return s.second == streamName; } );
1529 debug() <<
"migrating uid: " << hid.id <<
" stream: " << streamName <<
" oldFile: " << oldFileName
1530 <<
" newFile: " << newFileName <<
endmsg;
1534 if ( !oldFileName.
empty() ) {
1539 debug() <<
"changing filename \"" << i->first <<
"\" to \"" << newFileName <<
"\" for stream \""
1540 << i->second <<
"\"" <<
endmsg;
1548 error() <<
"Problems updating fileStreams with new file name" <<
endmsg;
1560 auto mode = i.second.second;
1561 auto file = i.second.first;
1562 if ( mode == WRITE || mode == UPDATE || mode == SHARE ) {
1563 file->Write(
"", TObject::kOverwrite );
1564 }
else if ( mode ==
APPEND ) {
1570 debug() <<
"THistSvc::writeObjectsToFile()::List of Files connected in ROOT " <<
endmsg;
1571 TSeqCollection* filelist = gROOT->GetListOfFiles();
1572 for (
int ii = 0; ii < filelist->GetEntries(); ii++ ) {
1573 debug() <<
"THistSvc::writeObjectsToFile()::List of Files connected in ROOT: \"" << filelist->At( ii )->GetName()
1582 auto loc = ident.
find(
" " );
1590 if ( loc != std::string::npos ) {
1592 for (
auto attrib : Parser( ident.
substr( loc + 1 ) ) ) {
1593 auto TAG = boost::algorithm::to_upper_copy( attrib.tag );
1594 auto VAL = boost::algorithm::to_upper_copy( attrib.value );
1596 if ( TAG ==
"FILE" || TAG ==
"DATAFILE" ) {
1599 }
else if ( TAG ==
"OPT" ) {
1600 if ( VAL ==
"APPEND" || VAL ==
"UPDATE" ) {
1602 }
else if ( VAL ==
"CREATE" || VAL ==
"NEW" || VAL ==
"WRITE" ) {
1604 }
else if ( VAL ==
"RECREATE" ) {
1606 }
else if ( VAL ==
"SHARE" ) {
1608 }
else if ( VAL ==
"OLD" || VAL ==
"READ" ) {
1611 error() <<
"Unknown OPT: \"" << attrib.value <<
"\"" <<
endmsg;
1614 }
else if ( TAG ==
"TYP" ) {
1616 }
else if ( TAG ==
"CL" ) {
1619 props.emplace_back( attrib.tag, attrib.value );
1624 if (
stream ==
"temp" ) {
1625 error() <<
"in JobOption \"" << ident <<
"\": stream name \"temp\" reserved." <<
endmsg;
1629 if ( db_typ !=
"ROOT" ) {
1630 error() <<
"in JobOption \"" << ident <<
"\": technology type \"" << db_typ <<
"\" not supported." <<
endmsg;
1635 error() <<
"in JobOption \"" << ident <<
"\":\n stream \"" <<
stream <<
"\" already connected to file: \""
1642 error() <<
"No OPT= specified or unknown access mode in: " << ident <<
endmsg;
1650 const std::string& oldstream = fitr.first->second;
1652 const auto& f_info =
m_files[oldstream];
1654 if ( newMode != f_info.second ) {
1655 error() <<
"in JobOption \"" << ident <<
"\":\n file \"" <<
filename <<
"\" already opened by stream: \""
1656 << oldstream <<
"\" with different access mode." <<
endmsg;
1659 TFile* f2 = f_info.first;
1662 debug() <<
"Connecting stream: \"" <<
stream <<
"\" to previously opened TFile: \"" <<
filename <<
"\""
1669 if (
service(
"IncidentSvc",
pi ).isFailure() ) {
1670 error() <<
"Unable to get the IncidentSvc" <<
endmsg;
1682 error() <<
"Unable to open ROOT file " <<
filename <<
" for reading" <<
endmsg;
1696 error() <<
"Unable to open ROOT file " <<
filename <<
" for writing" <<
endmsg;
1706 error() <<
"unable to open file \"" <<
filename <<
"\" for appending" <<
endmsg;
1717 static int ishared = 0;
1722 debug() <<
"Creating temp file \"" <<
filename <<
"\" and realfilename=" << realfilename <<
endmsg;
1729 error() <<
"Unable to open ROOT file " <<
filename <<
" for writing" <<
endmsg;
1740 error() <<
"Unable to open ROOT file " <<
filename <<
" for appending" <<
endmsg;
1751 debug() <<
"Opening TFile \"" <<
filename <<
"\" stream: \"" <<
stream <<
"\" mode: \"" << typ <<
"\""
1752 <<
" comp level: " << cl <<
endmsg;
1760 TFile* file = hid.
file;
1773 if ( !gDirectory->GetKey( dir.
c_str() ) ) { gDirectory->mkdir( dir.
c_str() ); }
1774 gDirectory->cd( dir.
c_str() );
1781 std::string::size_type i = dir.
find(
"/" );
1783 if ( i == std::string::npos )
return {};
1797 while (
id.find(
"//" ) != std::string::npos ) {
id.replace(
id.find(
"//" ), 2,
"/" ); }
1802 TString
path( (
char*)strstr( target->GetPath(),
":" ) );
1803 path.Remove( 0, 2 );
1806 TDirectory* current_sourcedir = gDirectory;
1809 TList* lkeys = current_sourcedir->GetListOfKeys();
1810 int nkeys = lkeys->GetEntries();
1811 TKey*
key =
nullptr;
1812 for (
int jj = 0; jj < nkeys; jj++ ) {
1813 key = (TKey*)lkeys->At( jj );
1816 TObject* obj = source->Get( pathnameinsource.
c_str() );
1819 if ( obj->IsA()->InheritsFrom(
"TDirectory" ) ) {
1825 TDirectory* newtargetdir = target->mkdir( obj->GetName(), obj->GetTitle() );
1829 }
else if ( obj->IsA()->InheritsFrom(
"TTree" ) ) {
1831 TTree* mytree =
dynamic_cast<TTree*
>( obj );
1832 int nentries = (int)mytree->GetEntries();
1833 mytree->SetBranchStatus(
"*", 1 );
1837 mytree->CloneTree();
1841 obj->Write(
key->GetName() );
1848 auto pos =
id.find(
"/" );
1850 if ( pos == std::string::npos ) {
1854 }
else if ( pos != 0 ) {
1861 auto pos2 =
id.
find(
"/", pos + 1 );
1863 if ( pos2 == std::string::npos ) {
1865 error() <<
"badly formed Hist/Tree id: \"" <<
id <<
"\"" <<
endmsg;
1871 if (
stream ==
"temp" ) {
1878 if ( !file ) { warning() <<
"no stream \"" <<
stream <<
"\" associated with id: \"" <<
id <<
"\"" <<
endmsg; }
1884 auto pos =
id.find(
"/" );
1886 if ( pos == std::string::npos ) {
1889 }
else if ( pos == 0 ) {
1892 root =
id.substr( 0, pos );
1893 rem =
id.
substr( pos + 1 );
1899 debug() <<
"Delaying connection of Input Files until Initialize"
1904 debug() <<
"Now connecting of Input Files" <<
endmsg;
1910 if (
connect( itr ).isFailure() ) {
1923 debug() <<
"Delaying connection of Output Files until Initialize"
1930 if (
connect( itr ).isFailure() ) {
1943 debug() <<
"copyFileLayout() to destination path: " << destination->GetPath() <<
endmsg;
1947 TString
path( (
char*)strstr( destination->GetPath(),
":" ) );
1948 path.Remove( 0, 2 );
1951 TDirectory* current_source_dir = gDirectory;
1954 TList* key_list = current_source_dir->GetListOfKeys();
1955 int n = key_list->GetEntries();
1956 for (
int j = 0; j <
n; ++j ) {
1957 TKey* k = (TKey*)key_list->At( j );
1959 TObject* o = source->Get( source_pathname.
c_str() );
1961 if ( o && o->IsA()->InheritsFrom(
"TDirectory" ) ) {
1965 TDirectory* destination_dir = destination->mkdir( o->GetName(), o->GetTitle() );
1966 if ( destination_dir ==
nullptr ) destination_dir = destination->GetDirectory( o->GetName() );
1981 if ( idr.
find(
"/" ) == 0 ) {
1989 if ( index >= itr->second->size() ) {
1990 error() <<
"no index " << index <<
" found for Hist " << idr <<
endmsg;
1993 hid = &( itr->second->at( index ) );
1999 if ( mitr.first == mitr.second ) {
2002 }
else if ( distance( mitr.first, mitr.second ) == 1 ) {
2004 if ( index >= mitr.first->second->size() ) {
2005 error() <<
"no index " << index <<
" found for Hist " << idr <<
endmsg;
2008 hid = &( mitr.first->second->at( 0 ) );
2012 hid = &( mitr.first->second->at( 0 ) );
2013 return distance( mitr.first, mitr.second );
2022 ost <<
"m_hlist: size: " <<
m_hlist.
size() <<
"\n";
2024 ost <<
" - " << vh->at( 0 ) <<
" :: [" << vh <<
"] " << vh->size() <<
" {";
2025 for (
auto& e : *vh ) {
2027 ost <<
"[" << o <<
"]";
2035 for (
auto& e :
m_uids ) { ost <<
" - " << e.first <<
" [" << e.second <<
"]" <<
std::endl; }
2040 for (
auto& e :
m_ids ) { ost <<
" - " << e.first <<
" [" << e.second <<
"]" <<
std::endl; }
2046 TObject* o = e.first;
2047 THistID& i = e.second.first->at( e.second.second );
2048 ost <<
" - " << o <<
" -> " << i <<
std::endl;
2051 debug() <<
"dumping THistSvc contents\n" << ost.
str() <<
endmsg;
2058 if ( vh->
size() == 1 ) {
2059 debug() <<
"merge: id: \"" <<
name <<
"\" is size 1. nothing to do" <<
endmsg;
2063 if ( !vh->
at( 0 ).obj->IsA()->InheritsFrom(
"TH1" ) ) {
2064 error() <<
"merge: id \"" <<
name <<
"\" is not a THn. Cannot merge" <<
endmsg;
2068 TList*
l =
new TList();
2069 for (
size_t i = 1; i < vh->
size(); ++i ) {
2070 debug() <<
"merge: id: \"" <<
name <<
"\" (" << vh->
at( i ).obj <<
") adding index " << i <<
endmsg;
2071 l->Add( vh->
at( i ).obj );
2074 TH1* t0 =
dynamic_cast<TH1*
>( vh->
at( 0 ).obj );
2076 error() <<
"merge: could not dcast " <<
name <<
"(" << t0 <<
") index " << 0 <<
" to TH1" <<
endmsg;
2080 Long64_t
n = t0->Merge(
l );
2082 debug() <<
"merge: id: \"" <<
name <<
"\" merged " <<
n <<
" entries" <<
endmsg;
2084 for (
size_t i = 1; i < vh->
size(); ++i ) {
2085 TH1* th =
dynamic_cast<TH1*
>( vh->
at( i ).obj );
2087 debug() <<
"clearing index " << i <<
"(" << th <<
")" <<
endmsg;
2088 th->SetDirectory(
nullptr );
2091 error() <<
"merge: could not dcast " <<
name <<
" index " << i <<
" to TH1" <<
endmsg;