Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4DynamicList/TGo4TreeHistogramEntry.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4TreeHistogramEntry.h"
00017 
00018 #include <iostream.h>
00019 
00020 #include "TH1.h"
00021 #include "TTree.h"
00022 
00023 #include "Go4Log/TGo4Log.h"
00024 #include "Go4Analysis/TGo4AnalysisImp.h"
00025 #include "TGo4TreeHistogramEntryStatus.h"
00026 #include "TGo4DynamicListException.h"
00027 
00028 const Text_t TGo4TreeHistogramEntry::fgcENTRYSUF[]="-DL";
00029 
00030 TGo4TreeHistogramEntry::TGo4TreeHistogramEntry(const Text_t* histogramname,
00031                                                 TTree* tree,
00032                                                 const Text_t* varexp,
00033                                                 const TCut& cut)
00034 : TGo4DynamicEntry(histogramname), fxTree(tree),
00035    fxHistogramName(histogramname), fxVarexp(varexp), fxSelection(cut),
00036    fbNewHistogram(kTRUE), fiLastEvent(0)
00037 {
00038    TRACE((15,"TGo4TreeHistogramEntry::TGo4TreeHistogramEntry(const Text_t*, TTree*, const Text_t*, TCut)",__LINE__, __FILE__));
00039 
00040    Text_t buffer[2048];
00041 
00042    snprintf(buffer,2048,"%s>>+%s",varexp,histogramname);
00043    fxDrawexp=buffer;
00044    if(fxTree)
00045       fxTreeName=fxTree->GetName();
00046    snprintf(buffer,2048,"%s%s",histogramname,fgcENTRYSUF);
00047    SetName(buffer);
00048    EnableProcessing(kTRUE);
00049 }
00050 
00051 TGo4TreeHistogramEntry::TGo4TreeHistogramEntry(const Text_t* histogramname,
00052                                                 const Text_t* treename,
00053                                                 const Text_t* varexp,
00054                                                 const TCut& cut)
00055 : TGo4DynamicEntry(histogramname), fxTree(0),
00056    fxHistogramName(histogramname), fxTreeName(treename), fxVarexp(varexp), fxSelection(cut),
00057    fbNewHistogram(kTRUE), fiLastEvent(0)
00058 {
00059    TRACE((15,"TGo4TreeHistogramEntry::TGo4TreeHistogramEntry(const Text_t*, const Text_t*, const Text_t*, TCut)",__LINE__, __FILE__));
00060    Text_t buffer[2048];
00061    snprintf(buffer,2047,"%s>>+%s",varexp,histogramname);
00062    fxDrawexp=buffer;
00063    snprintf(buffer,2047,"%s%s",histogramname,fgcENTRYSUF);
00064    SetName(buffer); // histogram name is different from entryname!
00065    EnableProcessing(kTRUE);
00066 }
00067 
00068 
00069 TGo4TreeHistogramEntry::TGo4TreeHistogramEntry()
00070 : TGo4DynamicEntry("Go4TreeHistogram1"), fxTree(0),
00071    fxHistogramName(0), fxVarexp(0), fxSelection(0),
00072    fbNewHistogram(kTRUE), fiLastEvent(0)
00073 {
00074    TRACE((15,"TGo4TreeHistogramEntry::TGo4TreeHistogramEntry()",__LINE__, __FILE__));
00075    fxDrawexp="x>>+Go4TreeHistogram";
00076    fxTreeName="Go4Tree";
00077    EnableProcessing(kTRUE);
00078 }
00079 
00080 
00081 TGo4TreeHistogramEntry::~TGo4TreeHistogramEntry()
00082 {
00083 TRACE((15,"TGo4TreeHistogramEntry::~TGo4TreeHistogramEntry()",__LINE__, __FILE__));
00084 
00085 }
00086 
00087 
00088 TGo4DynamicEntryStatus * TGo4TreeHistogramEntry::CreateStatus()
00089 {
00090 TRACE((12,"TGo4TreeHistogramEntry::CreateStatus()",__LINE__, __FILE__));
00091    TGo4TreeHistogramEntryStatus* state = new TGo4TreeHistogramEntryStatus( GetName() );
00092    UpdateStatus(state);
00093    return state;
00094 }
00095 
00096 
00097 void TGo4TreeHistogramEntry::UpdateStatus(TGo4DynamicEntryStatus * state)
00098 {
00099    TRACE((12,"TGo4TreeHistogramEntry::UpdateStatus(TGo4DynamicEntryStatus*)",__LINE__, __FILE__));
00100    TGo4DynamicEntry::UpdateStatus(state); // fill superclass attributes
00101    TGo4TreeHistogramEntryStatus* thstate= dynamic_cast<TGo4TreeHistogramEntryStatus*> (state);
00102    if (thstate)
00103       {
00104          thstate->SetHistogramName(fxHistogramName.Data());
00105          thstate->SetTreeName(fxTreeName);
00106          thstate->SetVarexp(fxVarexp);
00107          thstate->SetSelection(fxSelection);
00108          if(TGo4Analysis::Exists()) // on analysis side
00109                thstate->SetDynListInterval(TGo4Analysis::Instance()->GetDynListInterval());
00110          else // gui reading entry from file
00111                thstate->SetDynListInterval(2000);
00112       }
00113    else
00114       {
00115          // dynamic cast failed! we have a different status class (never come here...)
00116       throw TGo4DynamicListException(this,
00117                "Dynamic Tree Histogram Entry %s failed to UpdateStatus: wrong status type !!!",
00118                      GetName());
00119       }
00120 
00121 }
00122 
00123 void TGo4TreeHistogramEntry::Reset()
00124 {
00125 TRACE((12,"TGo4TreeHistogramEntry::Reset()",__LINE__, __FILE__));
00126   TGo4DynamicEntry::Reset();
00127   fiLastEvent=0;
00128   fxTree=0;
00129   fbNewHistogram=kTRUE;
00130   //EnableProcessing(kTRUE); // this can be done by gui editor now!
00131 }
00132 
00133 void TGo4TreeHistogramEntry::SetStatus(TGo4DynamicEntryStatus * state)
00134 {
00135 TRACE((12,"TGo4TreeHistogramEntry::SetStatus(TGo4DynamicEntryStatus*)",__LINE__, __FILE__));
00136    //
00137    if(state==0) return;
00138    if(state->InheritsFrom("TGo4TreeHistogramEntryStatus"))
00139       {
00140          TGo4DynamicEntry::SetStatus(state);
00141          TGo4TreeHistogramEntryStatus* hstate= dynamic_cast<TGo4TreeHistogramEntryStatus*>(state);
00142          fxHistogramName=hstate->GetHistogramName();
00143          fxTreeName=hstate->GetTreeName();
00144          fxVarexp=hstate->GetVarexp();
00145          fxSelection=hstate->GetSelection();
00146          Text_t buffer[2048];
00147          snprintf(buffer,2048,"%s>>+%s",
00148             fxVarexp.Data(),
00149             fxHistogramName.Data()); // compose the draw expression
00150          fxDrawexp=buffer;
00151          //Reset(); // baseclass SetStatus will call Reset()
00152       }
00153    else
00154       {
00155          throw TGo4DynamicListException(this,
00156                "Dynamic Tree Histogram Entry %s failed to SetStatus: wrong status type !!!",
00157                      GetName());
00158       }
00159 }
00160 
00161 
00162 
00163 Int_t TGo4TreeHistogramEntry::Process()
00164 {
00165 TRACE((12,"TGo4TreeHistogramEntry::Process(Int_t)",__LINE__, __FILE__));
00166    if(!fbProcessEntry)
00167       return 0;
00168    if(TGo4Analysis::Instance()->CheckDynListInterval())
00169          return ( ProcessTree(TGo4Analysis::Instance()->GetDynListInterval()) );
00170 return 0;
00171 }
00172 
00173 
00174 
00175 
00176 Int_t TGo4TreeHistogramEntry::ProcessTree(Int_t times)
00177 {
00178 TRACE((12,"TGo4TreeHistogramEntry::ProcessTree(Int_t)",__LINE__, __FILE__));
00179 //cout <<"TGo4TreeHistogramEntry::ProcessTree("<<times<<")" << endl;
00180 //
00181    // if there is no tree, we will search for it
00182    if(fxTree==0)
00183       {
00184           fxTree=TGo4Analysis::Instance()->GetTree( fxTreeName.Data() );
00185           if (fxTree==0)
00186                   throw TGo4DynamicListException(this,
00187                      "Tree Histogram Entry: !!! Could not find Tree %s ",
00188                      fxTreeName.Data());  // tree not found, exception skips this entry
00189 //         else
00190 //            cout <<"****** ******* found tree "<< fxTree->Data() << endl;
00191       }
00192    else { }
00193    Int_t rev=-2;
00194     //cout << "\t -- init times: "<< times<< endl;
00195    if(times<=0) times=1;
00196    Int_t lastentrynumber=static_cast<Int_t> (fxTree->GetEntries());
00197    if(fxTree->TestBit(TGo4Status::kGo4BackStoreReset))
00198       {
00199          //cout <<"ProcessTree found reset backstore #"<<counter << endl;
00200          // this tree belongs to backstore and was reset since last process
00201          fiLastEvent=0; // start again with first entry
00202          //fxTree->ResetBit(TGo4Status::kGo4BackStoreReset);           
00203          // do not reset, because next backstore entry on this tree will fail then!
00204       }
00205    if(fiLastEvent==lastentrynumber)
00206       {
00207          //cout << "\t -- treeentries: "<< lastentrynumber<< endl;
00208          throw TGo4DynamicListException(this,
00209                      "Tree Histogram Entry: %s Reached end of tree %s. Draw() is stopped. ",
00210                      GetName(), fxTreeName.Data());   
00211       }
00212    Int_t startindex= lastentrynumber -times;
00213    //cout << "\t -- init startindex: "<< startindex<< endl;
00214    if(startindex<fiLastEvent)
00215       {
00216          // we have to check if some of the events were not filled in the tree,
00217          // in this case we take all from the last processed event to the current:
00218          if(startindex>0)
00219             {
00220                // normal case in continuous processed tree
00221                startindex=fiLastEvent;
00222             }
00223             else
00224             {
00225                // this can happen when tree was Reset() in the meantime
00226                // e.g. tree of TGo4BackStore, then fiLastEvent is wrong
00227                startindex=0;
00228             }
00229          times=lastentrynumber-startindex;
00230       }
00231    else
00232       {
00233          // this happens when dynamic list entry started after the tree filling has begun
00234          // switch this on for cint tree processing: 
00235          startindex=fiLastEvent;
00236          //times=lastentrynumber-startindex;
00237          // <- this would cause to process all events in tree since the last time this
00238          // dynamic entry was active. might be dangerous though, not useful...
00239       }
00240 //   cout << "\t -- startindex: "<< startindex<< endl;
00241 //   cout << "\t -- times: "<< times<< endl;
00242 //   cout << "\t -- treeentries: "<< lastentrynumber<< endl;
00243 //   cout << "\t -- varexp: "<< fxVarexp.Data() << endl;
00244 //   cout << "\t -- selection: "<< fxSelection.GetTitle() << endl;
00245    fiLastEvent=startindex+times; // for cint mode
00246    if(fiLastEvent>lastentrynumber)
00247       {
00248          cout <<"Passing over end of tree - NEVER COME HERE!!!" << endl;
00249          fiLastEvent=lastentrynumber;
00250          times=fiLastEvent-startindex;
00251          cout << "\t -- startindex: "<< startindex<< endl;
00252          cout << "\t -- times: "<< times<< endl;
00253          cout << "\t -- treeentries: "<< lastentrynumber<< endl;
00254       }
00255    TDirectory* savdir=gDirectory;
00256    gROOT->cd(); // be sure to be in the top directory when processing
00257    if(fbNewHistogram)
00258       {
00259         // first call of Process: look for the histogram to fill
00260         TH1* histo;
00261         histo=TGo4Analysis::Instance()->GetHistogram( fxHistogramName.Data());
00262         if(histo==0)
00263           {
00264            // histogram not yet in histogram folder:
00265            // we create it by Draw and then search for it in root framework
00266            rev=fxTree->Draw(fxDrawexp.Data(),fxSelection,"goff",times,startindex);
00267            TObject* ob= gROOT->FindObject( fxHistogramName.Data() );
00268            histo= dynamic_cast<TH1*> (ob);
00269            if(histo!=0)
00270               {
00271 //                 cout << " found in gROOT new histogram "<< fxHistogramName.Data() << endl;
00272                  TGo4Analysis::Instance()->AddHistogram(histo); // we put the new histogram into our histo folder!
00273                  histo->SetBit(TGo4Status::kGo4CanDelete);
00274                  fbNewHistogram=kFALSE;
00275               }
00276            else
00277               {
00278 //                 cout << " no histogram "<< fxHistogramName.Data() << endl;
00279               }   // if(histo==0)
00280           }
00281         else
00282            {
00283 //               cout << " >>>found in folder new histogram  "<< histo->GetName() << endl;
00284 //               cout << " >>>histogram directory  "<< histo->GetDirectory()->GetName() << endl;
00285 //               TDirectory* treedir= fxTree->GetDirectory();
00286 //               // histo->SetDirectory(treedir); // histogram must be in the same dir as the tree!
00287 //               if(treedir)
00288 //                cout << " >>>tree directory  "<< treedir->GetName() << endl;
00290 //               cout << " >>>gDirectory before draw"<< gDirectory->GetName() << endl;
00291                rev=fxTree->Draw(fxDrawexp.Data(),fxSelection,"goff",times,startindex);
00292                fbNewHistogram=kFALSE;
00293            } // if(histo!=0)
00294       }
00295    else
00296       {
00297          // histogram should be there, normal operation
00298          rev=fxTree->Draw(fxDrawexp.Data(),fxSelection,"goff",times,startindex);
00299       } // if(fbNewHistogram)
00300       savdir->cd();
00301       return rev; // returns number of selected events, or -1 for TTree error
00302 }
00303 ClassImp(TGo4TreeHistogramEntry)
00304 
00305 
00306 
00307 
00308 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:55:52 2005 for Go4-v2.10-5 by doxygen1.2.15