4 #pragma warning(disable:2259)
23 #include "TDirectory.h"
47 :
base_class(name, svc), m_log(msgSvc(), name ), signaledStop(false) {
53 "maximum file size in MB. if exceeded, will cause an abort");
85 cur != props->
end(); cur++) {
86 if ( (*cur)->name() ==
"OutputLevel" ) {
111 <<
"Caught: " << err <<
endmsg;
119 <<
"Caught: " << err <<
endmsg;
125 static TROOT
root(
"root",
"ROOT I/O");
135 if (
service(
"IncidentSvc", p_incSvc,
true).isFailure()) {
139 p_incSvc->
addListener(
this,
"EndEvent", 100,
true);
144 if (
service(
"IoComponentMgr", iomgr,
true).isFailure()) {
151 <<
"could not register with the I/O component manager !"
155 bool all_good =
true;
158 for ( Registry_t::const_iterator
163 const std::string fname = ireg->second.first->GetName();
170 << fname <<
"] with the I/O component manager..." <<
endmsg;
179 <<
"problem while registering input/output files with "
180 <<
"the I/O component manager !" <<
endmsg;
224 TObject* to = uitr->second.obj;
227 if (to && to->IsA()->InheritsFrom(
"TTree")) {
228 TTree* tr =
dynamic_cast<TTree*
>(to);
229 if (tr->GetDirectory() != 0) {
230 dirname = tr->GetDirectory()->GetPath();
232 }
else if (to && to->IsA()->InheritsFrom(
"TGraph")) {
233 if (!uitr->second.temp) {
234 dirname = uitr->second.file->GetPath();
235 string id2(uitr->second.id);
236 id2.erase(0,id2.find(
"/",1));
237 id2.erase(id2.rfind(
"/"), id2.length());
238 if (id2.find(
"/") == 0) {
245 }
else if (to && to->IsA()->InheritsFrom(
"TH1")) {
246 TH1* th =
dynamic_cast<TH1*
>(to);
250 if (th->GetDirectory() != 0) {
251 dirname = th->GetDirectory()->GetPath();
260 << uitr->second.temp <<
" dir: " << dirname
278 if (
find(deleted_files.
begin(), deleted_files.
end(), itr->second.first) ==
279 deleted_files.
end()) {
280 deleted_files.
push_back(itr->second.first);
285 << itr->second.first->GetName()
300 m_log <<
"==> File: " << itr->second.first->GetName()
301 <<
" stream: " << itr->first <<
endmsg;
303 itr->second.first->Print(
"base");
306 string tmpfn=itr->second.first->GetName();
308 itr->second.first->Close();
311 if (
service(
"IncidentSvc",pi).isFailure()) {
316 if (itr->second.second==
SHARE) {
323 outputfile =
new TFile(
m_sharedFiles[itr->first].c_str(),
"UPDATE");
327 <<
" -> file probably corrupt." <<
endmsg;
334 <<
"\" for append: probably corrupt" <<
endmsg;
349 << tmpfn.c_str() <<
endmsg;
350 inputfile=
new TFile(tmpfn.c_str(),
"READ");
354 <<
" -> file probably corrupt." <<
endmsg;
359 <<
"\" for append: probably corrupt" <<
endmsg;
363 outputfile->SetCompressionLevel( inputfile->GetCompressionLevel() );
377 delete itr->second.first;
405 cout <<
"-> " << dir->GetPath() <<
" "
406 << dir->GetListOfKeys()->GetSize() <<
endl;
409 TIter nextkey(dir->GetList());
410 while (TKey *key = (TKey*)nextkey()) {
412 TObject *obj = key->ReadObj();
413 if (obj == 0) {
cout << key->GetName() <<
" obj==0"<<
endl;
continue; }
415 cout <<
" Key: " << key->GetName() <<
" "
416 <<
" tit: " << obj->GetTitle() <<
" "
417 <<
" (" << key->GetClassName() <<
")" <<
endl;
421 nextkey = dir->GetListOfKeys();
422 while (TKey *key = (TKey*)nextkey()) {
424 TObject *obj = key->ReadObj();
425 if (obj == 0) {
cout << key->GetName() <<
" obj==0"<<
endl;
continue; }
426 if (obj->IsA()->InheritsFrom(
"TDirectory")) {
427 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
442 gErrorIgnoreLevel = kBreak;
445 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory \"" << td->GetPath()
451 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
452 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
454 TIter nextkey(td->GetListOfKeys());
455 while (TKey *key = (TKey*)nextkey()) {
458 TObject *obj = key->ReadObj();
459 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
461 m_log <<
" (" << obj->IsA()->GetName() <<
")";
462 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TH1")) {
464 m_log <<
" (" << obj->IsA()->GetName() <<
")";
466 }
else if (obj != 0) {
468 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
476 nextkey = td->GetListOfKeys();
477 while (TKey *key = (TKey*)nextkey()) {
478 TObject *obj = key->ReadObj();
479 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
480 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
498 gErrorIgnoreLevel = kBreak;
507 r2 = itr->second.first->GetName();
513 <<
"\" looks like a stream name." <<
" associated TFile: \""
514 << itr->second.first->GetName() <<
"\"" <<
endmsg;
516 if (gDirectory->cd(r2.c_str())) {
529 m_log <<
MSG::DEBUG <<
"getTHists: stream \"" << stream <<
"\" not found"
533 if (!gDirectory->cd(dir.c_str())) {
534 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory/stream \"" << dir
550 gErrorIgnoreLevel = kBreak;
554 << td->GetPath() <<
"\"" <<
endmsg;
559 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
560 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
562 TIter nextkey(td->GetListOfKeys());
563 while (TKey *key = (TKey*)nextkey()) {
566 TObject *obj = key->ReadObj();
567 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
569 m_log <<
" (" << obj->IsA()->GetName() <<
")";
570 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TTree")) {
572 m_log <<
" (" << obj->IsA()->GetName() <<
")";
574 }
else if (obj != 0) {
576 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
583 nextkey = td->GetListOfKeys();
584 while (TKey *key = (TKey*)nextkey()) {
585 TObject *obj = key->ReadObj();
586 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
587 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
603 gErrorIgnoreLevel = kBreak;
612 r2 = itr->second.first->GetName();
618 <<
"\" looks like a stream name." <<
" associated TFile: \""
619 << itr->second.first->GetName() <<
"\"" <<
endmsg;
621 if (gDirectory->cd(r2.c_str())) {
631 m_log <<
MSG::DEBUG <<
"getTTrees: stream \"" << stream <<
"\" not found"
635 if (!gDirectory->cd(dir.c_str())) {
636 m_log <<
MSG::ERROR <<
"getTTrees: No such TDirectory/stream \"" << dir
656 gErrorIgnoreLevel = kBreak;
659 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory \"" << td->GetPath()
665 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
666 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
668 TIter nextkey(td->GetListOfKeys());
669 while (TKey *key = (TKey*)nextkey()) {
672 TObject *obj = key->ReadObj();
673 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
675 m_log <<
" (" << obj->IsA()->GetName() <<
")";
676 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TH1")) {
678 m_log <<
" (" << obj->IsA()->GetName() <<
")";
681 string dir = td->GetPath();
682 string fil = td->GetFile()->GetName();
683 dir.erase(0,fil.length()+1);
686 id =
id +
"/" + key->GetName();
688 id =
id + dir +
"/" + key->GetName();
692 m_log <<
" reg as \"" <<
id <<
"\"";
696 m_log <<
" already registered";
699 }
else if (obj != 0) {
701 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
709 nextkey = td->GetListOfKeys();
710 while (TKey *key = (TKey*)nextkey()) {
711 TObject *obj = key->ReadObj();
712 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
713 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
730 gErrorIgnoreLevel = kBreak;
739 r2 = itr->second.first->GetName();
745 <<
"\" looks like a stream name." <<
" associated TFile: \""
746 << itr->second.first->GetName() <<
"\"" <<
endmsg;
748 if (gDirectory->cd(r2.c_str())) {
761 m_log <<
MSG::DEBUG <<
"getTHists: stream \"" << stream <<
"\" not found"
765 if (!gDirectory->cd(dir.c_str())) {
766 m_log <<
MSG::ERROR <<
"getTHists: No such TDirectory/stream \"" << dir
772 <<
"without a valid stream name" <<
endmsg;
788 gErrorIgnoreLevel = kBreak;
792 << td->GetPath() <<
"\"" <<
endmsg;
797 m_log <<
MSG::DEBUG <<
"getTHists: \"" << td->GetPath() <<
"\": found "
798 << td->GetListOfKeys()->GetSize() <<
" keys" <<
endmsg;
800 TIter nextkey(td->GetListOfKeys());
801 while (TKey *key = (TKey*)nextkey()) {
804 TObject *obj = key->ReadObj();
805 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
807 m_log <<
" (" << obj->IsA()->GetName() <<
")";
808 }
else if (obj != 0 && obj->IsA()->InheritsFrom(
"TTree")) {
810 m_log <<
" (" << obj->IsA()->GetName() <<
")";
813 string dir = td->GetPath();
814 string fil = td->GetFile()->GetName();
815 dir.erase(0,fil.length()+1);
818 id =
id +
"/" + key->GetName();
820 id =
id + dir +
"/" + key->GetName();
824 m_log <<
" reg as \"" <<
id <<
"\"";
828 m_log <<
" already registered";
831 }
else if (obj != 0) {
833 m_log <<
" [" << obj->IsA()->GetName() <<
"]";
841 nextkey = td->GetListOfKeys();
842 while (TKey *key = (TKey*)nextkey()) {
843 TObject *obj = key->ReadObj();
844 if (obj != 0 && obj->IsA()->InheritsFrom(
"TDirectory")) {
845 TDirectory *tt =
dynamic_cast<TDirectory*
>(obj);
862 gErrorIgnoreLevel = kBreak;
871 r2 = itr->second.first->GetName();
877 <<
"\" looks like a stream name." <<
" associated TFile: \""
878 << itr->second.first->GetName() <<
"\"" <<
endmsg;
880 if (gDirectory->cd(r2.c_str())) {
890 m_log <<
MSG::DEBUG <<
"getTTrees: stream \"" << stream <<
"\" not found"
894 if (!gDirectory->cd(dir.c_str())) {
895 m_log <<
MSG::ERROR <<
"getTTrees: No such TDirectory/stream \"" << dir
919 <<
"\" with id \"" << hid.
id <<
"\"" <<
endmsg;
933 <<
"\" with id \"" << hid.
id <<
"\"" <<
endmsg;
936 for (itr3 = mitr.
first; itr3 != mitr.
second; ++itr3) {
937 if (itr3->second.obj == obj) {
945 <<
"\" with id \"" << hid.
id <<
"\"" <<
endmsg;
956 m_log <<
MSG::ERROR <<
"Cannot unregister TObject \"" << obj->GetName()
957 <<
"\": not known to THistSvc" <<
endmsg;
976 TObject* obj = itr->second.obj;
1045 if ( strcmp(hist->GetName(),
"Graph") == 0 ) {
1049 if (i != string::npos) {
1053 m_log <<
MSG::INFO <<
"setting name of TGraph id: \"" <<
id <<
"\" to \""
1054 << id2 <<
"\" since it is unset" <<
endmsg;
1055 hist->SetName(id2.c_str());
1094 if (tid.
obj->IsA()->InheritsFrom(
"TH1")) {
1122 if (tid.
obj->IsA()->InheritsFrom(
"TTree")) {
1150 if (tid.
obj->IsA()->InheritsFrom(
"TGraph")) {
1191 TFile*&
file)
const {
1195 if (pos == string::npos) {
1198 }
else if (pos != 0) {
1205 if (pos2 == string::npos) {
1215 if (stream ==
"temp") {
1222 file = itr->second.first;
1226 <<
"\" associated with id: \"" <<
id <<
"\""
1240 if (pos == string::npos) {
1249 root =
id.substr(0,pos);
1250 rem =
id.substr(pos+1,
id.length());
1262 <<
"Set it via the \"CL=\" parameter in the \"Output\" Property"
1347 << uitr->second.id <<
endmsg;
1349 TObject* to = uitr->second.obj;
1350 TFile* oldFile = uitr->second.file;
1353 }
else if (to->IsA()->InheritsFrom(
"TTree")) {
1354 TTree* tr =
dynamic_cast<TTree*
>(to);
1355 TFile* newFile = tr->GetCurrentFile();
1357 if (oldFile != newFile) {
1361 findStream(uitr->second.id, streamName, rem, dummy);
1365 if (itr->second.first == oldFile) {
1366 itr->second.first = newFile;
1372 for (; uitr2 !=
m_uids.
end(); ++uitr2) {
1373 if (uitr2->second.file == oldFile) {
1374 uitr2->second.file = newFile;
1380 if (sitr->second == streamName) {
1381 oldFileName = sitr->first;
1390 <<
" stream: " << streamName
1391 <<
" oldFile: " << oldFileName
1392 <<
" newFile: " << newFileName
1397 if (oldFileName !=
"") {
1403 <<
"\" to \"" << newFileName <<
"\" for stream \""
1404 << sitr->second <<
"\"" <<
endmsg;
1413 <<
"Problems updating fileStreams with new file name" <<
endmsg;
1432 if (itr->second.second ==
WRITE || itr->second.second ==
UPDATE
1433 ||itr->second.second==
SHARE) {
1434 itr->second.first->Write(
"",TObject::kOverwrite);
1435 }
else if (itr->second.second ==
APPEND) {
1436 itr->second.first->Write(
"");
1442 m_log <<
MSG::DEBUG <<
"THistSvc::write()::List of Files connected in ROOT "
1445 TSeqCollection *filelist=gROOT->GetListOfFiles();
1446 for (
int ii=0; ii<filelist->GetEntries(); ii++) {
1449 <<
"THistSvc::write()::List of Files connected in ROOT: \""
1450 << filelist->At(ii)->GetName()<<
"\""<<
endmsg;
1465 string stream = ident.substr(0,loc);
1469 string val,VAL,TAG,filename,db_typ(
"ROOT");
1472 tok.
analyse(ident.substr(loc+1,ident.length()),
" ",
"",
"",
"=",
"'",
"'");
1483 if (TAG ==
"FILE" || TAG ==
"DATAFILE") {
1486 }
else if ( TAG ==
"OPT" ) {
1487 if ( VAL ==
"APPEND" || VAL ==
"UPDATE" ) {
1489 }
else if ( VAL ==
"CREATE" || VAL ==
"NEW" || VAL ==
"WRITE" ) {
1491 }
else if ( VAL ==
"RECREATE" ) {
1493 }
else if (VAL ==
"SHARE") {
1495 }
else if ( VAL ==
"OLD" || VAL ==
"READ" ) {
1502 }
else if (TAG ==
"TYP") {
1503 db_typ = (*i).value();
1504 }
else if (TAG ==
"CL") {
1505 cl = atoi(val.c_str());
1507 props.
push_back( Prop((*i).tag(), (*i).value()));
1512 if (stream ==
"temp") {
1514 <<
"\": stream name \"temp\" reserved."
1519 if (db_typ !=
"ROOT") {
1521 <<
"\": technology type \"" << db_typ <<
"\" not supported."
1529 <<
"\":\n stream \"" << stream <<
"\" already connected to file: \""
1530 <<
m_files[stream].first->GetName() <<
"\""
1538 }
else if (typ ==
'N') {
1540 }
else if (typ ==
'A') {
1542 }
else if (typ ==
'R') {
1544 }
else if (typ ==
'S') {
1548 m_log <<
MSG::ERROR <<
"No OPT= specified or unknown access mode in: "
1562 if (newMode != f_info.
second) {
1564 <<
"\":\n file \"" << filename <<
"\" already opened by stream: \""
1565 << oldstream <<
"\" with different access mode."
1569 TFile *f2 = f_info.
first;
1573 <<
"\" to previously opened TFile: \"" << filename <<
"\""
1581 if (
service(
"IncidentSvc",pi).isFailure()) {
1591 f = TFile::Open(filename.c_str(),
"READ");
1595 <<
" -> file probably corrupt." <<
endmsg;
1601 <<
"\": probably corrupt" <<
endmsg;
1609 <<
"\": file does not exist" <<
endmsg;
1623 f = TFile::Open(filename.c_str(),
"NEW",stream.c_str(),cl);
1625 m_log <<
MSG::ERROR <<
"Unable to create new output file \"" << filename
1626 <<
"\" for writing: file already exists" <<
endmsg;
1640 f = TFile::Open(filename.c_str(),
"UPDATE",stream.c_str(),cl);
1644 <<
" -> file probably corrupt." <<
endmsg;
1650 <<
"\" for append: probably corrupt" <<
endmsg;
1658 <<
"\" for appending" <<
endmsg;
1671 static int ishared = 0;
1673 string realfilename=filename;
1675 filename =
string(
"tmp_THistSvc_")+out.str()+
string(
".root");
1679 <<
"\" and realfilename="<<realfilename <<
endmsg;
1683 f = TFile::Open(filename.c_str(),
"NEW",stream.c_str(),cl);
1687 <<
" -> file probably corrupt." <<
endmsg;
1693 <<
"\" for append: probably corrupt" <<
endmsg;
1701 <<
"\" for appending" <<
endmsg;
1713 f = TFile::Open(filename.c_str(),
"RECREATE",stream.c_str(),cl);
1717 <<
" -> file probably corrupt." <<
endmsg;
1723 <<
"\" for update: probably corrupt" <<
endmsg;
1731 <<
"\" for updating" <<
endmsg;
1745 m_log <<
MSG::DEBUG <<
"Opening TFile \"" << filename <<
"\" stream: \""
1746 << stream <<
"\" mode: \"" << typ <<
"\"" <<
" comp level: " << cl
1757 string uid = hid.
id;
1759 string stream, fdir, bdir, dir, id;
1770 while ( (dir =
dirname(fdir)) !=
"") {
1771 if (! gDirectory->GetKey(dir.c_str())) {
1772 gDirectory->mkdir(dir.c_str());
1774 gDirectory->cd(dir.c_str());
1789 if (i == string::npos) {
1798 string root = dir.substr(0,i);
1810 m_ge = gErrorIgnoreLevel;
1816 gErrorIgnoreLevel = m_ge;
1824 while (
id.
find(
"//") != std::string::npos) {
1825 id.replace(
id.
find(
"//"),2,
"/");
1836 TString
path( (
char*)
strstr(target->GetPath(),
":") );
1840 TDirectory *current_sourcedir = gDirectory;
1843 TList *lkeys=current_sourcedir->GetListOfKeys();
1844 int nkeys=lkeys->GetEntries();
1846 for (
int jj=0; jj<nkeys; jj++) {
1847 key=(TKey*) lkeys->At(jj);
1848 string pathnameinsource=current_sourcedir->GetPath()+
string(
"/")+key->GetName();
1853 TObject *obj=source->Get(pathnameinsource.c_str());
1855 if (obj->IsA()->InheritsFrom(
"TDirectory") ) {
1864 TDirectory *newtargetdir =
1865 target->mkdir(obj->GetName(), obj->GetTitle() );
1869 }
else if (obj->IsA()->InheritsFrom(
"TTree")) {
1872 TTree *mytree=
dynamic_cast<TTree*
>(obj);
1873 int nentries=(
int) mytree->GetEntries();
1874 mytree->SetBranchStatus(
"*",1);
1886 mytree->CloneTree();
1894 obj->Write(key->GetName() );
1924 Long64_t mfs_warn = mfs * 95 / 100;
1930 TFile* tf = itr->second.first;
1935 << tf->GetName() <<
" size: " << tf->GetSize()
1940 if (tf->GetSize() > mfs) {
1945 <<
"\" associated with stream \"" << itr->first
1946 <<
"\" has exceeded the max file size of "
1951 if (
service(
"ApplicationMgr", evt,
true).isSuccess()) {
1957 }
else if (tf->GetSize() > mfs_warn) {
1959 <<
"\" associated with stream \"" << itr->first
1960 <<
"\" is at 95% of its maximum allowable file size of "
1976 <<
"copyFileLayout() to dst path: " << dst->GetPath () <<
endmsg;
1979 TString
path ((
char*)
strstr (dst->GetPath(),
":"));
1983 TDirectory *cur_src_dir = gDirectory;
1986 TList *key_list = cur_src_dir->GetListOfKeys ();
1987 int n = key_list->GetEntries ();
1988 for (
int j = 0; j < n; ++j ) {
1989 TKey *k = (TKey*)key_list->At (j);
1990 const std::string src_pathname = cur_src_dir->GetPath()
1993 TObject *o=src->Get (src_pathname.c_str());
1995 if (o != NULL && o->IsA()->InheritsFrom (
"TDirectory")) {
2000 TDirectory * dst_dir = dst->mkdir (o->GetName(), o->GetTitle());
2014 bool all_good =
true;
2022 if (
service(
"IoComponentMgr", iomgr,
true).isFailure()) {
2031 gErrorIgnoreLevel = kFatal;
2036 ifile != iend; ++ifile) {
2037 TFile *f = ifile->second.first;
2041 << f->GetOption() <<
"] r:"
2042 << f->GetFileBytesRead()
2043 <<
" w:" << f->GetFileBytesWritten()
2044 <<
" cnt:" << f->GetFileCounter()
2047 if ( ifile->second.second ==
READ ) {
2050 <<
" TFile opened in READ mode: not reassigning names" <<
endmsg;
2064 TFile *newfile = TFile::Open (fname.c_str(), f->GetOption());
2075 if ( hid.
file != f ) {
2078 TDirectory *olddir = this->
changeDir (hid);
2080 ifile->second.first = newfile;
2082 TDirectory *newdir = this->
changeDir (hid);
2083 TClass *cl = hid.
obj->IsA();
2087 if (cl->InheritsFrom (
"TTree")) {
2088 dynamic_cast<TTree*
> (hid.
obj)->SetDirectory (newdir);
2089 dynamic_cast<TTree*
> (hid.
obj)->Reset();
2091 else if (cl->InheritsFrom (
"TH1")) {
2092 dynamic_cast<TH1*
> (hid.
obj)->SetDirectory (newdir);
2093 dynamic_cast<TH1*
> (hid.
obj)->Reset();
2095 else if (cl->InheritsFrom (
"TGraph")) {
2096 olddir->Remove (hid.
obj);
2097 newdir->Append (hid.
obj);
2100 <<
"id: \"" << hid.
id <<
"\" is not a inheriting from a class "
2101 <<
"we know how to handle (received [" << cl->GetName()
2102 <<
"], " <<
"expected [TTree, TH1 or TGraph]) !"
2104 <<
"attaching to current dir [" << newdir->GetPath() <<
"] "
2105 <<
"nonetheless..." <<
endmsg;
2106 olddir->Remove (hid.
obj);
2107 newdir->Append (hid.
obj);