4 #pragma warning(disable:2259)
21 #include "boost/bind.hpp"
26 #include "TDirectory.h"
50 :
base_class(name, svc), m_log(msgSvc(), name ), signaledStop(false),
51 m_delayConnect(false),m_okToConnect(false),
52 p_incSvc(0), p_fileMgr(0) {
58 "maximum file size in MB. if exceeded, will cause an abort. -1 to never check.");
86 cur != props->
end(); cur++) {
87 if ( (*cur)->name() ==
"OutputLevel" ) {
112 <<
"Caught: " << err <<
endmsg;
120 <<
"Caught: " << err <<
endmsg;
126 static TROOT
root(
"root",
"ROOT I/O");
154 <<
"unable to register ROOT file open action with FileMgr"
160 <<
"unable to register ROOT file open Error action with FileMgr"
180 if (
service(
"IoComponentMgr", iomgr,
true).isFailure()) {
187 <<
"could not register with the I/O component manager !"
191 bool all_good =
true;
194 for ( Registry_t::const_iterator
199 const std::string fname = ireg->second.first->GetName();
206 << fname <<
"] with the I/O component manager..." <<
endmsg;
215 <<
"problem while registering input/output files with "
216 <<
"the I/O component manager !" <<
endmsg;
260 TObject* to = uitr->second.obj;
263 if (to && to->IsA()->InheritsFrom(
"TTree")) {
264 TTree* tr =
dynamic_cast<TTree*
>(to);
265 if (tr->GetDirectory() != 0) {
266 dirname = tr->GetDirectory()->GetPath();
268 }
else if (to && to->IsA()->InheritsFrom(
"TGraph")) {
269 if (!uitr->second.temp) {
270 dirname = uitr->second.file->GetPath();
271 string id2(uitr->second.id);
272 id2.erase(0,id2.find(
"/",1));
273 id2.erase(id2.rfind(
"/"), id2.length());
274 if (id2.find(
"/") == 0) {
281 }
else if (to && to->IsA()->InheritsFrom(
"TH1")) {
282 TH1* th =
dynamic_cast<TH1*
>(to);
286 if (th->GetDirectory() != 0) {
287 dirname = th->GetDirectory()->GetPath();
296 << uitr->second.temp <<
" dir: " << dirname
314 if (
find(deleted_files.
begin(), deleted_files.
end(), itr->second.first) ==
315 deleted_files.
end()) {
316 deleted_files.
push_back(itr->second.first);
321 << itr->second.first->GetName()
336 m_log <<
"==> File: " << itr->second.first->GetName()
337 <<
" stream: " << itr->first <<
endmsg;
339 itr->second.first->Print(
"base");
342 string tmpfn=itr->second.first->GetName();
347 if (
service(
"IncidentSvc",pi).isFailure()) {
352 if (itr->second.second==
SHARE) {
366 TFile *outputfile = (TFile*) vf;
382 TFile *inputfile = (TFile*) vf;
384 outputfile->SetCompressionLevel( inputfile->GetCompressionLevel() );
398 delete itr->second.first;
426 cout <<
"-> " << dir->GetPath() <<
" "
427 << dir->GetListOfKeys()->GetSize() <<
endl;
430 TIter nextkey(dir->GetList());
431 while (TKey *key = (TKey*)nextkey()) {
433 TObject *obj = key->ReadObj();
434 if (obj == 0) {
cout << key->GetName() <<
" obj==0"<<
endl;
continue; }
436 cout <<
" Key: " << key->GetName() <<
" "
437 <<
" tit: " << obj->GetTitle() <<
" "
438 <<
" (" << key->GetClassName() <<
")" <<
endl;
442 nextkey = dir->GetListOfKeys();
443 while (TKey *key = (TKey*)nextkey()) {
445 TObject *obj = key->ReadObj();
446 if (obj == 0) {
cout << key->GetName() <<
" obj==0"<<
endl;
continue; }
447 if (obj->IsA()->InheritsFrom(
"TDirectory")) {
448 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
463 gErrorIgnoreLevel = kBreak;
466 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory \"" << td->GetPath()
472 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
473 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
475 TIter nextkey(td->GetListOfKeys());
476 while (TKey *key = (TKey*)nextkey()) {
479 TObject *obj = key->ReadObj();
480 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
482 m_log <<
" (" << obj->IsA()->GetName() <<
")";
483 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TH1")) {
485 m_log <<
" (" << obj->IsA()->GetName() <<
")";
487 }
else if (obj != 0) {
489 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
497 nextkey = td->GetListOfKeys();
498 while (TKey *key = (TKey*)nextkey()) {
499 TObject *obj = key->ReadObj();
500 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
501 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
519 gErrorIgnoreLevel = kBreak;
528 r2 = itr->second.first->GetName();
534 <<
"\" looks like a stream name." <<
" associated TFile: \""
535 << itr->second.first->GetName() <<
"\"" <<
endmsg;
537 if (gDirectory->cd(r2.c_str())) {
550 m_log <<
MSG::DEBUG <<
"getTHists: stream \"" << stream <<
"\" not found"
554 if (!gDirectory->cd(dir.c_str())) {
555 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory/stream \"" << dir
571 gErrorIgnoreLevel = kBreak;
575 << td->GetPath() <<
"\"" <<
endmsg;
580 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
581 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
583 TIter nextkey(td->GetListOfKeys());
584 while (TKey *key = (TKey*)nextkey()) {
587 TObject *obj = key->ReadObj();
588 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
590 m_log <<
" (" << obj->IsA()->GetName() <<
")";
591 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TTree")) {
593 m_log <<
" (" << obj->IsA()->GetName() <<
")";
595 }
else if (obj != 0) {
597 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
604 nextkey = td->GetListOfKeys();
605 while (TKey *key = (TKey*)nextkey()) {
606 TObject *obj = key->ReadObj();
607 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
608 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
624 gErrorIgnoreLevel = kBreak;
633 r2 = itr->second.first->GetName();
639 <<
"\" looks like a stream name." <<
" associated TFile: \""
640 << itr->second.first->GetName() <<
"\"" <<
endmsg;
642 if (gDirectory->cd(r2.c_str())) {
652 m_log <<
MSG::DEBUG <<
"getTTrees: stream \"" << stream <<
"\" not found"
656 if (!gDirectory->cd(dir.c_str())) {
657 m_log <<
MSG::ERROR <<
"getTTrees: No such TDirectory/stream \"" << dir
677 gErrorIgnoreLevel = kBreak;
680 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory \"" << td->GetPath()
686 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
687 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
689 TIter nextkey(td->GetListOfKeys());
690 while (TKey *key = (TKey*)nextkey()) {
693 TObject *obj = key->ReadObj();
694 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
696 m_log <<
" (" << obj->IsA()->GetName() <<
")";
697 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TH1")) {
699 m_log <<
" (" << obj->IsA()->GetName() <<
")";
702 string dir = td->GetPath();
703 string fil = td->GetFile()->GetName();
704 dir.erase(0,fil.length()+1);
707 id =
id +
"/" + key->GetName();
709 id =
id + dir +
"/" + key->GetName();
713 m_log <<
" reg as \"" <<
id <<
"\"";
717 m_log <<
" already registered";
720 }
else if (obj != 0) {
722 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
730 nextkey = td->GetListOfKeys();
731 while (TKey *key = (TKey*)nextkey()) {
732 TObject *obj = key->ReadObj();
733 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
734 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
751 gErrorIgnoreLevel = kBreak;
760 r2 = itr->second.first->GetName();
766 <<
"\" looks like a stream name." <<
" associated TFile: \""
767 << itr->second.first->GetName() <<
"\"" <<
endmsg;
769 if (gDirectory->cd(r2.c_str())) {
782 m_log <<
MSG::DEBUG <<
"getTHists: stream \"" << stream <<
"\" not found"
786 if (!gDirectory->cd(dir.c_str())) {
787 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory/stream \"" << dir
793 <<
"without a valid stream name" <<
endmsg;
809 gErrorIgnoreLevel = kBreak;
813 << td->GetPath() <<
"\"" <<
endmsg;
818 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
819 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
821 TIter nextkey(td->GetListOfKeys());
822 while (TKey *key = (TKey*)nextkey()) {
825 TObject *obj = key->ReadObj();
826 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
828 m_log <<
" (" << obj->IsA()->GetName() <<
")";
829 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TTree")) {
831 m_log <<
" (" << obj->IsA()->GetName() <<
")";
834 string dir = td->GetPath();
835 string fil = td->GetFile()->GetName();
836 dir.erase(0,fil.length()+1);
839 id =
id +
"/" + key->GetName();
841 id =
id + dir +
"/" + key->GetName();
845 m_log <<
" reg as \"" <<
id <<
"\"";
849 m_log <<
" already registered";
852 }
else if (obj != 0) {
854 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
862 nextkey = td->GetListOfKeys();
863 while (TKey *key = (TKey*)nextkey()) {
864 TObject *obj = key->ReadObj();
865 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
866 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
883 gErrorIgnoreLevel = kBreak;
892 r2 = itr->second.first->GetName();
898 <<
"\" looks like a stream name." <<
" associated TFile: \""
899 << itr->second.first->GetName() <<
"\"" <<
endmsg;
901 if (gDirectory->cd(r2.c_str())) {
911 m_log <<
MSG::DEBUG <<
"getTTrees: stream \"" << stream <<
"\" not found"
915 if (!gDirectory->cd(dir.c_str())) {
916 m_log <<
MSG::ERROR <<
"getTTrees: No such TDirectory/stream \"" << dir
940 <<
"\" with id \"" << hid.
id <<
"\"" <<
endmsg;
954 <<
"\" with id \"" << hid.
id <<
"\"" <<
endmsg;
957 for (itr3 = mitr.
first; itr3 != mitr.
second; ++itr3) {
958 if (itr3->second.obj == obj) {
966 <<
"\" with id \"" << hid.
id <<
"\"" <<
endmsg;
977 m_log <<
MSG::ERROR <<
"Cannot unregister TObject \"" << obj->GetName()
978 <<
"\": not known to THistSvc" <<
endmsg;
997 TObject* obj = itr->second.obj;
1066 if ( strcmp(hist->GetName(),
"Graph") == 0 ) {
1070 if (i != string::npos) {
1074 m_log <<
MSG::INFO <<
"setting name of TGraph id: \"" <<
id <<
"\" to \""
1075 << id2 <<
"\" since it is unset" <<
endmsg;
1076 hist->SetName(id2.c_str());
1115 if (tid.
obj->IsA()->InheritsFrom(
"TH1")) {
1143 if (tid.
obj->IsA()->InheritsFrom(
"TTree")) {
1171 if (tid.
obj->IsA()->InheritsFrom(
"TGraph")) {
1212 TFile*&
file)
const {
1216 if (pos == string::npos) {
1219 }
else if (pos != 0) {
1226 if (pos2 == string::npos) {
1236 if (stream ==
"temp") {
1243 file = itr->second.first;
1247 <<
"\" associated with id: \"" <<
id <<
"\""
1261 if (pos == string::npos) {
1270 root =
id.substr(0,pos);
1271 rem =
id.substr(pos+1,
id.length());
1283 <<
"Set it via the \"CL=\" parameter in the \"Output\" Property"
1297 m_log <<
MSG::DEBUG <<
"Delaying connection of Input Files until Initialize"
1341 m_log <<
MSG::DEBUG <<
"Delaying connection of Input Files until Initialize"
1392 << uitr->second.id <<
" " << uitr->second.mode <<
endmsg;
1394 TObject* to = uitr->second.obj;
1395 TFile* oldFile = uitr->second.file;
1398 }
else if ( uitr->second.temp || uitr->second.mode ==
READ ) {
1406 }
else if (to->IsA()->InheritsFrom(
"TTree")) {
1407 TTree* tr =
dynamic_cast<TTree*
>(to);
1408 TFile* newFile = tr->GetCurrentFile();
1410 if (oldFile != newFile) {
1414 findStream(uitr->second.id, streamName, rem, dummy);
1418 if (itr->second.first == oldFile) {
1419 itr->second.first = newFile;
1425 for (; uitr2 !=
m_uids.
end(); ++uitr2) {
1426 if (uitr2->second.file == oldFile) {
1427 uitr2->second.file = newFile;
1433 if (sitr->second == streamName) {
1434 oldFileName = sitr->first;
1443 <<
" stream: " << streamName
1444 <<
" oldFile: " << oldFileName
1445 <<
" newFile: " << newFileName
1450 if (oldFileName !=
"") {
1456 <<
"\" to \"" << newFileName <<
"\" for stream \""
1457 << sitr->second <<
"\"" <<
endmsg;
1466 <<
"Problems updating fileStreams with new file name" <<
endmsg;
1485 if (itr->second.second ==
WRITE || itr->second.second ==
UPDATE
1486 ||itr->second.second==
SHARE) {
1487 itr->second.first->Write(
"",TObject::kOverwrite);
1488 }
else if (itr->second.second ==
APPEND) {
1489 itr->second.first->Write(
"");
1495 m_log <<
MSG::DEBUG <<
"THistSvc::write()::List of Files connected in ROOT "
1498 TSeqCollection *filelist=gROOT->GetListOfFiles();
1499 for (
int ii=0; ii<filelist->GetEntries(); ii++) {
1502 <<
"THistSvc::write()::List of Files connected in ROOT: \""
1503 << filelist->At(ii)->GetName()<<
"\""<<
endmsg;
1518 string stream = ident.substr(0,loc);
1522 string val,VAL,TAG,filename,db_typ(
"ROOT");
1525 tok.
analyse(ident.substr(loc+1,ident.length()),
" ",
"",
"",
"=",
"'",
"'");
1536 if (TAG ==
"FILE" || TAG ==
"DATAFILE") {
1539 }
else if ( TAG ==
"OPT" ) {
1540 if ( VAL ==
"APPEND" || VAL ==
"UPDATE" ) {
1542 }
else if ( VAL ==
"CREATE" || VAL ==
"NEW" || VAL ==
"WRITE" ) {
1544 }
else if ( VAL ==
"RECREATE" ) {
1546 }
else if (VAL ==
"SHARE") {
1548 }
else if ( VAL ==
"OLD" || VAL ==
"READ" ) {
1555 }
else if (TAG ==
"TYP") {
1556 db_typ = (*i).value();
1557 }
else if (TAG ==
"CL") {
1558 cl = atoi(val.c_str());
1560 props.
push_back( Prop((*i).tag(), (*i).value()));
1565 if (stream ==
"temp") {
1567 <<
"\": stream name \"temp\" reserved."
1572 if (db_typ !=
"ROOT") {
1574 <<
"\": technology type \"" << db_typ <<
"\" not supported."
1582 <<
"\":\n stream \"" << stream <<
"\" already connected to file: \""
1583 <<
m_files[stream].first->GetName() <<
"\""
1591 }
else if (typ ==
'N') {
1593 }
else if (typ ==
'A') {
1595 }
else if (typ ==
'R') {
1597 }
else if (typ ==
'S') {
1601 m_log <<
MSG::ERROR <<
"No OPT= specified or unknown access mode in: "
1615 if (newMode != f_info.
second) {
1617 <<
"\":\n file \"" << filename <<
"\" already opened by stream: \""
1618 << oldstream <<
"\" with different access mode."
1622 TFile *f2 = f_info.
first;
1626 <<
"\" to previously opened TFile: \"" << filename <<
"\""
1634 if (
service(
"IncidentSvc",pi).isFailure()) {
1648 m_log <<
"Unable to open ROOT file " << filename <<
" for reading"
1668 m_log <<
"Unable to open ROOT file " << filename <<
" for writing"
1682 <<
"\" for appending" <<
endmsg;
1694 static int ishared = 0;
1696 string realfilename=filename;
1698 filename =
string(
"tmp_THistSvc_")+out.str()+
string(
".root");
1702 <<
"\" and realfilename="<<realfilename <<
endmsg;
1710 m_log <<
"Unable to open ROOT file " << filename <<
" for writing"
1724 m_log <<
"Unable to open ROOT file " << filename <<
" for appending"
1737 m_log <<
MSG::DEBUG <<
"Opening TFile \"" << filename <<
"\" stream: \""
1738 << stream <<
"\" mode: \"" << typ <<
"\"" <<
" comp level: " << cl
1749 string uid = hid.
id;
1751 string stream, fdir, bdir, dir, id;
1762 while ( (dir =
dirname(fdir)) !=
"") {
1763 if (! gDirectory->GetKey(dir.c_str())) {
1764 gDirectory->mkdir(dir.c_str());
1766 gDirectory->cd(dir.c_str());
1781 if (i == string::npos) {
1790 string root = dir.substr(0,i);
1802 m_ge = gErrorIgnoreLevel;
1808 gErrorIgnoreLevel = m_ge;
1816 while (
id.
find(
"//") != std::string::npos) {
1817 id.replace(
id.
find(
"//"),2,
"/");
1828 TString
path( (
char*)
strstr(target->GetPath(),
":") );
1832 TDirectory *current_sourcedir = gDirectory;
1835 TList *lkeys=current_sourcedir->GetListOfKeys();
1836 int nkeys=lkeys->GetEntries();
1838 for (
int jj=0; jj<nkeys; jj++) {
1839 key=(TKey*) lkeys->At(jj);
1840 string pathnameinsource=current_sourcedir->GetPath()+
string(
"/")+key->GetName();
1845 TObject *obj=source->Get(pathnameinsource.c_str());
1848 if (obj->IsA()->InheritsFrom(
"TDirectory") ) {
1857 TDirectory *newtargetdir =
1858 target->mkdir(obj->GetName(), obj->GetTitle() );
1862 }
else if (obj->IsA()->InheritsFrom(
"TTree")) {
1865 TTree *mytree=
dynamic_cast<TTree*
>(obj);
1866 int nentries=(
int) mytree->GetEntries();
1867 mytree->SetBranchStatus(
"*",1);
1879 mytree->CloneTree();
1887 obj->Write(key->GetName() );
1922 Long64_t mfs_warn = mfs * 95 / 100;
1928 TFile* tf = itr->second.first;
1933 << tf->GetName() <<
" size: " << tf->GetSize()
1938 if (tf->GetSize() > mfs) {
1943 <<
"\" associated with stream \"" << itr->first
1944 <<
"\" has exceeded the max file size of "
1949 if (
service(
"ApplicationMgr", evt,
true).isSuccess()) {
1955 }
else if (tf->GetSize() > mfs_warn) {
1957 <<
"\" associated with stream \"" << itr->first
1958 <<
"\" is at 95% of its maximum allowable file size of "
1974 <<
"copyFileLayout() to dst path: " << dst->GetPath () <<
endmsg;
1977 TString
path ((
char*)
strstr (dst->GetPath(),
":"));
1981 TDirectory *cur_src_dir = gDirectory;
1984 TList *key_list = cur_src_dir->GetListOfKeys ();
1985 int n = key_list->GetEntries ();
1986 for (
int j = 0; j < n; ++j ) {
1987 TKey *k = (TKey*)key_list->At (j);
1988 const std::string src_pathname = cur_src_dir->GetPath()
1991 TObject *o=src->Get (src_pathname.c_str());
1993 if (o != NULL && o->IsA()->InheritsFrom (
"TDirectory")) {
1998 TDirectory * dst_dir = dst->mkdir (o->GetName(), o->GetTitle());
2012 bool all_good =
true;
2020 if (
service(
"IoComponentMgr", iomgr,
true).isFailure()) {
2029 gErrorIgnoreLevel = kFatal;
2034 ifile != iend; ++ifile) {
2035 TFile *f = ifile->second.first;
2039 << f->GetOption() <<
"] r:"
2040 << f->GetFileBytesRead()
2041 <<
" w:" << f->GetFileBytesWritten()
2042 <<
" cnt:" << f->GetFileCounter()
2045 if ( ifile->second.second ==
READ ) {
2048 <<
" TFile opened in READ mode: not reassigning names" <<
endmsg;
2065 Option_t *
opts = f->GetOption();
2069 <<
"\" for writing" <<
endmsg;
2072 TFile *newfile = (TFile*) vf;
2073 newfile->SetOption(opts);
2078 ifile->second.first = newfile;
2087 if ( hid.
file != f ) {
2090 TDirectory *olddir = this->
changeDir (hid);
2093 TDirectory *newdir = this->
changeDir (hid);
2094 TClass *cl = hid.
obj->IsA();
2098 if (cl->InheritsFrom (
"TTree")) {
2099 dynamic_cast<TTree*
> (hid.
obj)->SetDirectory (newdir);
2100 dynamic_cast<TTree*
> (hid.
obj)->Reset();
2102 else if (cl->InheritsFrom (
"TH1")) {
2103 dynamic_cast<TH1*
> (hid.
obj)->SetDirectory (newdir);
2104 dynamic_cast<TH1*
> (hid.
obj)->Reset();
2106 else if (cl->InheritsFrom (
"TGraph")) {
2107 olddir->Remove (hid.
obj);
2108 newdir->Append (hid.
obj);
2111 <<
"id: \"" << hid.
id <<
"\" is not a inheriting from a class "
2112 <<
"we know how to handle (received [" << cl->GetName()
2113 <<
"], " <<
"expected [TTree, TH1 or TGraph]) !"
2115 <<
"attaching to current dir [" << newdir->GetPath() <<
"] "
2116 <<
"nonetheless..." <<
endmsg;
2117 olddir->Remove (hid.
obj);
2118 newdir->Append (hid.
obj);
2140 if (fa->
desc() !=
"HIST") {
2172 if (fa->
desc() !=
"HIST") {