10 #include <Compression.h>
11 #include "TChainElement.h"
12 #include "TBranchElement.h"
13 #include "TFriendElement.h"
15 #include "TVirtualIndex.h"
32 dirname=
"same_dir_or_same_file_as_main_tree";
41 if (t3friend)
delete t3friend;
43 if (t3output)
delete t3output;
48 if (dirname.compare(
"same_dir_or_same_file_as_main_tree")==0)
return true;
57 printf(
"%s-%s was already existing (%p)... Substituting it (with %p)!\n",
ptr->ClassName(),
ptr->GetName(),
ptr,
this);
71 printf(
"Object myChain (%p) created as singleton...\n",
ptr);
80 for (
int ii=0; ii<(int)(
sections.size()); ii++)
95 #define _FRIENDSNUM 11
96 const char _names[
_FRIENDSNUM][256]={
"MainTree",
"TRDPlus",
"TRDKPlus",
"EcalPlus",
"BetaPlus",
"BetaHPlus",
"RichPlus",
"TrackerPlus",
"StatusTree",
"RTI",
"TRDQtPlus"};
101 s0->
bname=
"MainTree";
111 printf(
"--- Disabling TRDQtPlus section since now is not produced by default\n");
112 printf(
" it could be enabled simply calling myChain::EnableSection(\"TRDQtPlus\") ---\n");
129 s0->
bname=
"will_be_read_from_tree";
131 s0->
t3friend =
new TChain(Form(
"AMSRootNtuplized_%s", Name));
133 if (DirName) s0->
dirname=DirName;
141 for (
int ii=1; ii<(int)(
sections.size()); ii++) {
143 AddFriend(
sections[ii]->t3friend, Form(
"%s = AMSRootNtuplized_%s",
sections[ii]->name.c_str(),
sections[ii]->name.c_str()));
147 TIter next((TCollection*)GetListOfFriends());
148 while (TObject *obj = next()) {
150 printf(
"Present Active friend %s\n", obj->GetName());
163 TString path = u.GetFile();
164 if( not path.BeginsWith(
"/castor/") )
166 TString cmd =
"stager_qry -M "+path+
" | grep -qw STAGED";
167 int ret = system(cmd);
179 TString opt = u.GetOptions();
if(!opt.IsNull())opt.Prepend(
"?");
184 bool iscastor = fn.BeginsWith(
"/castor/")
185 or fn.BeginsWith(
"root://castorpublic")
186 or fn.BeginsWith(
"castor://");
187 if( iscastor and opt.IsNull() ){
188 opt =
"?svcClass=amsuser";
191 if( fn.BeginsWith(
"/eos/") ){
192 u.SetProtocol(
"root");
193 u.SetHost(
"eosams.cern.ch");
200 fn = u.GetProtocol()==TString(
"file") ? u.GetFile() : u.GetUrl();
209 ShowError(kError+1, __func__,
"Can not load castor file %s: not STAGED", fn.Data());
212 int oldgil = gErrorIgnoreLevel;gErrorIgnoreLevel = kWarning+1;
214 gErrorIgnoreLevel = oldgil;
215 TIter it( c.GetListOfFiles() );
218 while( (po = it()) ){
220 list.push_back( po->GetTitle()+opt );
237 Int_t gErrorIgnoreLevel_default = gErrorIgnoreLevel;
242 TString tname = name;
247 vector<TString> flist;
250 Error(__func__,
"Failed to add %s to the chain", name);
254 for(
int i=0; i<ret; ++i ){
255 Add( flist[i], nentries );
263 printf(
"MainTree AMSRootNtuplized |%s\n", name);
264 sections[0]->ntrees+=TChain::Add(name, nentries);
276 SetBranchAddress(
"fmyEvent", &
me);
298 for (
int ii=1; ii<(int)(
sections.size()); ii++) {
300 TString stringa(name);
303 TString filename = basename(uname);
304 TString dname = dirname(uname);
306 stringa.ReplaceAll(
"MainTree",
sections[ii]->name.c_str());
307 filename.ReplaceAll(
"MainTree",
sections[ii]->name.c_str());
310 stringa=
sections[ii]->dirname; stringa+=filename;
313 if (GetTree() && GetTree()->GetCurrentFile()) {
314 TString stringa2(GetTree()->GetCurrentFile()->GetName());
315 stringa2.ReplaceAll(
"MainTree",
sections[ii]->name.c_str());
317 TString file2beadded;
318 gErrorIgnoreLevel = 5000;
319 TFile *ffriend=TFile::Open(stringa2.Data());
320 gErrorIgnoreLevel = gErrorIgnoreLevel_default;
326 TString shortname =
sections[ii]->t3friend->GetName();
327 shortname.ReplaceAll(
"MSRootNtupli",
"...");
328 printf(
" Friend %-19s|%s\n", shortname.Data(), stringa.Data());
329 file2beadded=stringa;
332 printf(
"File %s has not been found\n", stringa2.Data());
333 printf(
" --> assuming tree friend chain %s on the same file as the MainTree (%s)...\n",
sections[ii]->t3friend->GetName(), name);
337 gErrorIgnoreLevel_default = gErrorIgnoreLevel;
338 gErrorIgnoreLevel = 5000;
339 TFile* f=TFile::Open(file2beadded);
340 gErrorIgnoreLevel = gErrorIgnoreLevel_default;
344 if (f && f->Get(
sections[ii]->t3friend->GetName())) {
351 printf(
"The tree %s has NOT been found on file %s. Disabling the %s section...\n",
sections[ii]->t3friend->GetName(), f?f->GetName():
"",
sections[ii]->name.c_str());
353 RemoveFriend(
sections[ii]->t3friend);
383 static std::vector<myTrTrackPlus*>* vmtp = &(
me->
vmtp);
384 static std::vector<myTrdTrackPlus*>* vmup = &(
me->
vmup);
385 static std::vector<myTrdK*>* vmuktp = &(
me->
vmuktp);
386 static std::vector<myTrdK*>* vmukup = &(
me->
vmukup);
387 static std::vector<myTrdQtFromTrTrack*>* vmuqtp = &(
me->
vmuqtp);
388 static std::vector<myBetaPlus*>* vmbp = &(
me->
vmbp);
389 static std::vector<myBetaHPlus*>* vmbhp = &(
me->
vmbhp);
390 static std::vector<myEcalShowerPlus*>* vmep = &(
me->
vmep);
391 static std::vector<myRichRingPlus*>* vmrp = &(
me->
vmrp);
393 for (
int ii=1; ii<(int)(
sections.size()); ii++){
395 printf(
"Setting branch addresses for %s...\n",
sections[ii]->name.c_str());
398 TIter nn(
sections[ii]->t3friend->GetListOfBranches());
403 while (TBranchElement* bb=dynamic_cast<TBranchElement*>(nn())) {
404 TString bn = bb->GetName();
423 if (
sections[ii]->name ==
"TRDPlus"){
424 if (bn==
"vmup") SetBranchAddress(bn, &vmup);
426 else if (
sections[ii]->name ==
"TRDKPlus"){
427 if (bn==
"vmuktp") SetBranchAddress(bn, &vmuktp);
428 else if (bn==
"vmukup") SetBranchAddress(bn, &vmukup);
430 else if (
sections[ii]->name ==
"TRDQtPlus"){
431 if (bn==
"vmuqtp") SetBranchAddress(bn, &vmuqtp);
433 else if(
sections[ii]->name ==
"EcalPlus"){
434 if (bn==
"vmep") SetBranchAddress(bn, &vmep);
436 else if(
sections[ii]->name ==
"BetaPlus"){
437 if (bn==
"vmbp") SetBranchAddress(bn.Data(), &vmbp);
439 else if(
sections[ii]->name ==
"BetaHPlus"){
440 if (bn==
"vmbhp")
sections[ii]->t3friend->SetBranchAddress(bn, &vmbhp);
442 else if(
sections[ii]->name ==
"RichPlus"){
443 if (bn==
"vmrp")
sections[ii]->t3friend->SetBranchAddress(bn, &vmrp);
445 else if(
sections[ii]->name ==
"TrackerPlus"){
446 if (bn==
"vmtp")
sections[ii]->t3friend->SetBranchAddress(bn, &vmtp);
448 else if(
sections[ii]->name ==
"StatusTree"){
449 if (bn==
"ms")
sections[ii]->t3friend->SetBranchAddress(bn, &(
me->
ms));
451 else if(
sections[ii]->name ==
"RTI"){
452 if (bn==
"RTI")
sections[ii]->t3friend->SetBranchAddress(bn, &(
me->
RTI));
454 else if(
sections[ii]->name.find(
"User1")!=std::string::npos){
476 if (bn.Contains(
"User"))
sections[ii]->t3friend->SetBranchAddress(bn, (
void**)(&(
me->
us1)));
479 printf(
" Section %s unknown\n",
sections[ii]->name.c_str());
484 printf(
" Section %s is not Active...\n",
sections[ii]->name.c_str());
486 printf(
" Section %s has not the TTree friend...\n",
sections[ii]->name.c_str());
504 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
506 printf(
"TChain (%s) with %lld entries (%d trees)\n",
sections[ii]->t3friend->GetName(),
sections[ii]->t3friend->GetEntries(),
sections[ii]->ntrees);
515 printf(
"List of Branches:------------\n");
516 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
518 printf(
"----Tree %s:\n",
sections[ii]->t3friend->GetName());
519 TObjArray *branchElements=
sections[ii]->t3friend->GetListOfBranches();
520 TIter next((TCollection*)branchElements);
522 while (( brEl=(TBranch*)next() )) {
523 printf(
"There's branch %s (%s, on file %s)...\n", brEl->GetName(), brEl->GetTitle(), brEl->GetFileName());
528 printf(
"List of Aliases:------------\n");
529 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
531 printf(
"----Tree %s:\n",
sections[ii]->t3friend->GetName());
532 TIter next((TCollection*)
sections[ii]->t3friend->GetListOfAliases());
533 while (TObject *obj = next()) {
535 printf(
"There's alias %s\n", obj->GetName());
541 printf(
"List of Clones:------------\n");
542 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
544 printf(
"----Tree %s:\n",
sections[ii]->t3friend->GetName());
545 TIter next((TCollection*)
sections[ii]->t3friend->GetListOfClones());
546 while (TObject *obj = next()) {
548 printf(
"There's clone %s\n", obj->GetName());
554 printf(
"List of Files:------------\n");
555 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
557 printf(
"----Tree %s:\n",
sections[ii]->t3friend->GetName());
558 TObjArray *branchElements=
sections[ii]->t3friend->GetListOfFiles();
559 TIter next((TCollection*)branchElements);
561 while (( ff=(TFile*)next() )) {
562 printf(
"There's file %s (%s)...\n", ff->GetName(), ff->GetTitle());
567 printf(
"List of Friends:------------\n");
568 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
570 printf(
"----Tree %s:\n",
sections[ii]->t3friend->GetName());
571 TIter next((TCollection*)
sections[ii]->t3friend->GetListOfFriends());
572 while (TObject *obj = next()) {
574 printf(
"There's friend %s\n", obj->GetName());
580 printf(
"List of UserInfo:------------\n");
581 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
583 printf(
"----Tree %s:\n",
sections[ii]->t3friend->GetName());
584 TIter next((TCollection*)
sections[ii]->t3friend->GetUserInfo());
585 while (TObject *obj = next()) {
587 printf(
"There's info %s\n", obj->GetName());
598 if (GetTree() && GetTree()->GetUserInfo()) {
599 for (
int ii=0; ii<GetTree()->GetUserInfo()->GetEntries(); ii++) {
600 if (GetTree()->GetUserInfo()->At(ii))
601 GetTree()->GetUserInfo()->At(ii)->Dump();
610 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
612 TIter next((TCollection*)
sections[ii]->t3output->GetListOfFriends());
613 while (TObject *obj = next()) {
616 sections[ii]->t3output->RemoveFriend((TTree*)obj);
622 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
623 if(!
sections[ii]->t3output)
continue;
624 bool isRTI= (strstr(
sections[ii]->t3output->GetName(),
"RTI")!=0);
625 printf(
"Building index for TTree %s...\n",
sections[ii]->t3output->GetName());
626 if (ii!=0 && !isRTI) {
627 sections[ii]->t3output->BuildIndex(
"Run",
"Event");
630 sections[ii]->t3output->BuildIndex(
"Time");
637 for (
int ii=0; ii<(int)(
filesoutput.size()); ii++) {
646 static unsigned int last_writtenRTI_time=UInt_t(-1);
647 for (
int ii=0; ii<(int)(
sections.size()); ii++) {
648 if(!
sections[ii]->t3output)
continue;
649 bool isRTI= (strstr(
sections[ii]->t3output->GetName(),
"RTI")!=0);
652 if (!isRTI)
sections[ii]->t3output->Fill();
666 int complevel=ROOT::CompressionSettings(ROOT::kLZMA, 2);
675 printf(
"Created files %s for output...\n",
filesoutput[0]->GetName());
679 sections[0]->t3output->AutoSave(
"SaveSelf,Overwrite");
681 for (
int ii=1; ii<(int)(
sections.size()); ii++) {
689 printf(
"Created files %s for output...\n",
filesoutput[ii]->GetName());
693 sections[ii]->t3output->AutoSave(
"SaveSelf,Overwrite");
702 ((TTree*)_InputTree)->GetEvent(0);
703 TTree* _InputTreeChecked=((TChain*)_InputTree)->GetTree();
711 TDirectory* Prev = gDirectory;
714 printf(
" myChain::CloneTree: The input tree pointer is empty\n");
719 ((TTree*)_InputTree)->GetEvent(0);
720 _OutputTree=_InputTree->CloneTree(0);
722 _OutputTree->SetDirectory(ff);
725 if (Prev) Prev->cd();
732 char* filename =
new char[1024];
733 sprintf(filename,
"dummyname");
736 printf(
"myChain::ExtractFileNameWithoutExtension: The input tree pointer is empty\n");
741 if (((TTree*)_InputTree)->GetEntries() != 0) {
742 ((TTree*)_InputTree)->GetEvent(0);
743 rootfile = ((TTree*)_InputTree)->GetCurrentFile();
747 printf(
"The passed TTree/TChain doesn't contain any event! Did you already make TChain::Add()?!\n");
751 TString s(rootfile->GetName());
753 TObjArray* array = s.Tokenize(
"/");
754 for (
int ii=0; ii<array->GetEntries(); ii++) {
755 TString temp = ((TObjString*)array->At(ii))->GetString();
757 if (temp.Contains(
".root")) striped=temp;
761 striped.ReplaceAll(
".root",
"");
763 sprintf(filename,
"%s", striped.Data());
774 FILE* listfile = fopen(fname,
"r");
777 while (!feof(listfile)){
778 fscanf(listfile,
"%s\n", rname);
783 cerr <<
"myChain::AddFromFile-E- Error opening file '" << fname <<
"';";
794 for (
int ii=1;ii<(int)(
sections.size());ii++)
795 if(
sections[ii]->name.compare(nn)==0) {
797 printf(
"Section %s activated with branchname \"%s\"\n",
sections[ii]->name.c_str(),
sections[ii]->bname.c_str());
800 printf(
"Section %s NOT FOUND!!!\n",Name);
808 for (
int ii=1;ii<(int)(
sections.size());ii++)
809 if(
sections[ii]->name.compare(nn)==0) {
812 printf(
"Section %s disabled\n",
sections[ii]->name.c_str());
815 printf(
"Section %s NOT FOUND!!!\n",Name);
822 string nn(SectionName);
823 for (
int ii=1;ii<(int)(
sections.size());ii++)
824 if(
sections[ii]->name.compare(nn)==0) {
826 printf(
"SearchDir set to %s for Section %s with branchname \"%s\"\n", DirName,
sections[ii]->name.c_str(),
sections[ii]->bname.c_str());
829 printf(
"Section %s NOT FOUND!!!\n",SectionName);
836 for (
int ii=0;ii<(int)(
sections.size());ii++)
837 printf(
"Name: %15s BranchName: %25s Active: %d SearchDir: %20s\n",
sections[ii]->name.c_str(),
sections[ii]->bname.c_str(), (int)(
sections[ii]->Active),
sections[ii]->dirname.c_str());
844 for (
int ii=0;ii<(int)(
sections.size());ii++)
845 if(
sections[ii]->name.compare(nn)==0)
854 for (
int ii=0;ii<(int)(
sections.size());ii++)
855 if(
sections[ii]->name.compare(nn)==0)
864 for (
int ii=0;ii<(int)(
sections.size());ii++)
874 GetEntry(entry,getall);
890 TTree* t3main =
sections[0]->t3friend->GetTree();
891 if (t3main->GetTreeIndex()) {
892 printf(
"MainTree tree HAS a the index. Neglecting it...\n");
895 t3main->SetTreeIndex(0);
898 TTree* tch_main =
sections[0]->t3friend;
899 if (tch_main->GetTreeIndex()) {
900 printf(
"MainTree (chain) tree HAS a the index. Neglecting it...\n");
903 tch_main->SetTreeIndex(0);
907 for (
int ii=1; ii<(int)(
sections.size()); ii++){
913 TTree* t3_friend =
sections[ii]->t3friend->GetTree();
915 if (!t3_friend->GetTreeIndex()) {
916 printf(
"%s tree has NOT the index. This should not happen...\n",
sections[ii]->name.c_str());
917 printf(
"Building index for TTree %s...\n",
sections[ii]->t3friend->GetName());
919 if(
sections[ii]->name.compare(
"RTI")==0){
920 t3_friend->BuildIndex(
"Time");
923 t3_friend->BuildIndex(
"Run",
"Event");
930 sections[ii]->t3friend->SetTreeIndex(t3_friend->GetTreeIndex());
935 TVirtualIndex * treeIndex=0;
936 if (
sections[ii]->t3friend->GetTreeIndex()) {
937 treeIndex =
sections[ii]->t3friend->GetTreeIndex();
940 sections[ii]->t3friend->LoadTree(0);
941 if (
sections[ii]->t3friend->GetTree())
942 treeIndex =
sections[ii]->t3friend->GetTree()->GetTreeIndex();
945 TString major = treeIndex->GetMajorName();
946 TString minor = treeIndex->GetMinorName();
947 Info(__func__,
"building index for %s as %s, %s",
sections[ii]->name.c_str(), major.Data(), minor.Data() );
948 sections[ii]->t3friend->BuildIndex(major, minor);
951 Warning(__func__,
"Can not setup chain index for section %s: original tree index not present (how did you reach this point?!)",
sections[ii]->name.c_str());
961 for (
int ii=1; ii<(int)(
sections.size()); ii++){
964 sections[ii]->t3friend->GetEntry(entry);
971 return TChain::LoadTree(entry);
974 Long64_t
myChain::Draw(
const char* varexp,
const TCut& selection, Option_t* option, Long64_t nentries, Long64_t firstentry){
980 Long64_t ret = TChain::Draw(varexp, selection, option, nentries, firstentry);
987 Long64_t
myChain::Draw(
const char* varexp,
const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry){
993 Long64_t ret = TChain::Draw(varexp, selection, option, nentries, firstentry);