Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4CompositeEvent.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE 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 "TGo4CompositeEvent.h"
00017 
00018 #include "Riostream.h"
00019 
00020 #include "snprintf.h"
00021 #include "TTree.h"
00022 #include "TROOT.h"
00023 #include "TObjArray.h"
00024 
00025 R__EXTERN TTree *gTree;
00026 
00027 TGo4CompositeEvent::TGo4CompositeEvent() :
00028    TGo4EventElement(),
00029    fNElements(0),
00030    fEventElements(0)
00031 {
00032 }
00033 
00034 TGo4CompositeEvent::TGo4CompositeEvent(const char*aName, const char* aTitle, Short_t aBaseCat) :
00035    TGo4EventElement(aName,aTitle, aBaseCat),
00036    fNElements(0),
00037    fEventElements(0)
00038 {
00039    fEventElements = new TObjArray(12);
00040 }
00041 
00042 TGo4CompositeEvent::~TGo4CompositeEvent()
00043 {
00044    //cout << "Start TGo4CompositeEvent::~TGo4CompositeEvent(void)"<< this << endl;
00045    if(fEventElements!=0) {
00046         fEventElements->Sort();
00047 
00048          if(!fEventElements->IsEmpty()){
00049             fEventElements->Delete();
00050              //cout << "TGo4CompositeEvent::~TGo4CompositeEvent(void) " <<this << " "  <<fEventElements->IsEmpty() << endl;
00051          }
00052       delete fEventElements;
00053 
00054    }
00055     //cout << " End  TGo4CompositeEvent::~TGo4CompositeEvent(void)"<< this <<endl;
00056 }
00057 
00058 
00059 void TGo4CompositeEvent::makeBranch(TBranch *parent)
00060 {
00061 
00062   TGo4EventElement  **par;
00063   char buffer[256];
00064   TBranch *b=NULL;
00065   Int_t i=0;
00066 
00067   TObjArray &vector=*fEventElements;
00068   for(i=0;i<=fEventElements->GetLast();i++) {
00069     par=(TGo4EventElement **)&(vector[i]);
00070     if ((*par)!=NULL) {
00071       snprintf(buffer,255,"%s.",(*par)->GetName());
00072       b=gTree->TTree::Branch(buffer, (*par)->ClassName(),par,4000,99);
00073       (*par)->makeBranch(b);
00074     }
00075   }
00076   TGo4EventElement::makeBranch( parent );
00077 }
00078 
00079 
00080 Int_t  TGo4CompositeEvent::activateBranch( TBranch *branch, Int_t splitLevel, Int_t init){
00081 
00082   // synchronize with TTree for Composite Event
00083   if ( branch ){
00084     TString cad=branch->GetName();
00085     TTree *tree = branch->GetTree();
00086 
00087     TGo4CompositeEvent *dump=this;
00088     tree->SetBranchStatus(cad.Data(),1);
00089     tree->SetBranchAddress(cad.Data(), &dump);
00090     cad+="*";
00091     tree->SetBranchStatus(cad.Data(),1);
00092 
00093     TGo4EventElement::activateBranch(branch,splitLevel);
00094 
00095     if (fDebug)
00096     printf("##### TGo4CompositeEvent::activateBranch called from obj:%s \n",this->GetName());
00097 
00098     TObjArray *br = tree->GetListOfBranches();
00099     TBranch*  b=NULL;
00100     TClass*  cl=NULL;
00101     char* obj=NULL;
00102 
00103     Int_t i=init;
00104 
00105     if (fDebug)
00106     printf("-I-TGo4CompositeEvent::activateBranch from obj:%s bname%s Elements :%i  index:%i\n",
00107              this->GetName(),cad.Data(), fNElements,init);
00108 
00109     int max = init+fNElements;
00110     while ( i < init+fNElements ){
00111       i++;
00112       TGo4EventElement *par = NULL;
00113       b = (TBranch*) br->At(i);
00114       if(b){
00115 
00116    if (fDebug)
00117         printf("-I TGo4CompositeEvent::activateBranch from obj:%s (while) bname:%s cname:%s init:%i elem:%i \n",
00118           this->GetName(), b->GetName(), b->GetClassName(), init, fNElements);
00119 
00120    cl=gROOT->GetClass(b->GetClassName());
00121    obj= (char*) cl->New();
00122    par= (TGo4EventElement*) obj;
00123    if(par) {
00124        Int_t offset=par->activateBranch(b, splitLevel,i);
00125        if (fDebug)
00126        printf("-I activate from obj:%s elems:%i index:%i adding:%s\n",
00127          this->GetName(), max , i, par->GetName());
00128 
00129        addEventElement(par,1);
00130        i+=offset;
00131        fNElements+=offset;
00132        if (fDebug)
00133        printf("-I from obj:%s activate indexing after offset:%i index:%i elems:%i\n",
00134          this->GetName(),
00135          offset,i,max);
00136    }
00137       }
00138 
00139     } 
00140     Clear();
00141   }
00142 
00143   if (fDebug)
00144   printf("-I activate return value from obj:%s offset:%i \n", this->GetName(),
00145      fNElements);
00146   return fNElements;
00147 
00148 }
00149 
00150 
00151 
00152 
00153 void  TGo4CompositeEvent::synchronizeWithTree( TTree *tree, Int_t splitLevel){
00154   // synchronize with TTree for composite Event
00155   // FIXME !
00156   // should <test> the differents splitlevel?
00157 
00158 
00159   if (tree==0) return;
00160 
00161   if (fEventElements==0)
00162      fEventElements = new TObjArray(12);
00163 
00164     TGo4CompositeEvent *dump=this;
00165 
00166     //-- Initialize the global Branch structure
00167     TBranch* b=NULL;
00168     TObjArray *br = tree->GetListOfBranches();
00169     b=(TBranch*) br->At(0);
00170     TString cad=b->GetName();
00171 
00172     tree->SetBranchStatus(cad.Data(),1);
00173     tree->SetBranchAddress(cad.Data(), &dump);
00174     cad+="*";
00175     tree->SetBranchStatus(cad.Data(),1);
00176 
00177     // Initialize underlying  TTree
00178     TGo4EventElement::activateBranch(b,splitLevel);
00179     //--
00180     TClass* cl=NULL;
00181     char* obj=NULL;
00182 
00183     if(fDebug)
00184             printf("-I- TGo4CompositeEvent::synchronize(tree): Elements :%i Id:%i branch:%s \n", fNElements ,fIdentifier,cad.Data());
00185 
00186     //-- Rebuild the Elements array
00187     Int_t i=0;
00188     while( i<fNElements ){
00189      i++;
00190       TGo4EventElement *par = NULL;
00191       b = (TBranch*) br->At(i);
00192       if(b){
00193 
00194        if (fDebug)
00195     printf("-I TGo4CompositeEvent::synchronize(tree) bname:%s cname:%s Elem:%i\n",
00196                           b->GetName(), b->GetClassName(), fNElements);
00197 
00198    cl=gROOT->GetClass(b->GetClassName());
00199    obj= (char*) cl->New();
00200    par= (TGo4EventElement*) obj;
00201    if(par) {
00202      Int_t offset=par->activateBranch(b, splitLevel,i);
00203      if (fDebug)
00204            printf("-I synchronize index:%i from:%s adding:%s\n",
00205           i, this->GetName(), par->GetName());
00206      addEventElement(par,1);
00207      i+=offset;
00208           fNElements+=offset;
00209 
00210      if (fDebug)
00211           printf("-I synchronize indexing after offset:%i index:%i elem:%i \n",
00212          offset,i,fNElements);
00213    }
00214 
00215       }
00216 
00217     } 
00218      Clear();
00219      //-- Resynchronize all branches with the TTree (needed once)
00220      //TGo4LockGuard Glob2;
00221      tree->GetEntry(0);
00222 }
00223 
00224 
00225 
00226 
00227 void TGo4CompositeEvent::Clear(Option_t *) {
00228   //Clears the data in the event (i.e. clears the internal buffers...)
00229 
00230      TIter next(fEventElements);
00231      TGo4EventElement *ev;
00232      while ( (ev=(TGo4EventElement *)next())!=NULL) {
00233               ev->Clear();
00234      }
00235 }
00236 
00237 
00238 
00239 void TGo4CompositeEvent::clearAll(Int_t level) {
00240   // Clears the data in the event and the event structure (list of subevents...)
00241   // FIXME
00242   // Levels are to be defined
00243 
00244   if (level==0) {
00245     // if (fEventElements) fEventElements->Delete();
00246   } else {
00247     TIter next(fEventElements);
00248      TGo4EventElement *ev;
00249     while ((ev=( TGo4EventElement *)next())!=NULL) {
00250       ev->clearAll(level);
00251     }
00252   }
00253 }
00254 
00255 
00256 void TGo4CompositeEvent::addEventElement( TGo4EventElement* aElement, Int_t reading){
00257   // if Identifiers  are needed for fast retrieval of elements
00258   // one should use:
00259   // >>> fEventElements->AddAtAndExpand(aElement,aElement->getId());
00260   // Note: When reading from file, adding elements should not be
00261   // incremented
00262 
00263  if (getEventElement(aElement->GetName(),1)){
00264       printf("-E-1 <TGo4CompositeEvent::addEventElement> object:%s already in structure => not added ! \n",
00265                    aElement->GetName());
00266       return;
00267    }
00268 
00269   if ( aElement->getId() < 0 ) {
00270     printf("-E-2 <TGo4CompositeEvent::addEventElement> object:%s with invalid Id:%i   => not added ! \n",
00271              aElement->GetName(), aElement->getId());
00272     return;
00273   }
00274 
00275   if ( getEventElement( aElement->getId() ) ){
00276       printf("-E-3 <TGo4CompositeEvent::addEventElement> object:%s with Id:%i already used => not added ! \n",
00277         aElement->GetName(), aElement->getId());
00278   }
00279 
00280   if (fDebug)
00281   printf("-I adding element in :%s :%p at location:%i \n",GetName(),aElement, aElement->getId());
00282   fEventElements->AddAtAndExpand (aElement, aElement->getId());
00283   if( reading == 0 ) fNElements++;
00284 
00285 }
00286 
00287 
00288 TGo4EventElement* TGo4CompositeEvent::getEventElement(Int_t idx) {
00289   // Returns a pointer to the partial event with number idx.
00290 
00291   if ( idx > fEventElements->GetLast() ) return NULL;
00292   return ( ( TGo4EventElement*) fEventElements->At(idx) );
00293 }
00294 
00295 
00296 TGo4EventElement* TGo4CompositeEvent::getEventElement(const char* name,Int_t final) {
00297 
00298   TIter next(fEventElements);
00299   TGo4EventElement *ev=NULL,*inter=NULL;
00300   while ((ev=( TGo4EventElement *)next())!=NULL) {
00301       if(strcmp(name,ev->GetName())==0) return ev;
00302       inter=NULL;
00303       if (ev->isComposed()) {
00304          inter= ((TGo4CompositeEvent*) ev)->getEventElement(name,1);
00305          if (inter !=0 ) return inter;
00306       }
00307   }
00308   if(final==0)
00309      printf("-E- TGo4CompositeEvent => Element:%s not found in Composite:%s \n",name,this->GetName());
00310   return NULL;
00311 }
00312 
00313 void TGo4CompositeEvent::deactivate(){
00314    //
00315 
00316   TIter next(fEventElements);
00317   TGo4EventElement *ev=NULL;
00318 
00319   while ((ev=( TGo4EventElement *)next())!=NULL) {
00320    ev->deactivate();
00321   }
00322 }
00323 
00324 void TGo4CompositeEvent::activate(){
00325       //
00326   TIter next(fEventElements);
00327   TGo4EventElement *ev=NULL;
00328 
00329   while ((ev=( TGo4EventElement *)next())!=NULL) {
00330    ev->activate();
00331   }
00332 }
00333 
00334 
00335 
00336 
00337 TObjArray* TGo4CompositeEvent::getListOfComposites(Bool_t toplevel){
00338    //
00339   TObjArray *comp = new TObjArray(12);
00340   TObjArray *dump = NULL;
00341   TIter next(fEventElements);
00342   TGo4EventElement *ev,*ev2;
00343   //-- Add top level composite
00344   if (toplevel) {
00345     comp->Add(this);
00346     // this->Dump();
00347   }
00348 
00349   ev=NULL;
00350   while ((ev=( TGo4EventElement *)next())!=NULL) {
00351      if ( ev->isComposed() ) {
00352       comp->Add( ev );
00353       dump = ((TGo4CompositeEvent*) ev)->getListOfComposites(kFALSE);
00354       TIter next2( dump);
00355       ev2=NULL;
00356        while( (ev2=( TGo4EventElement *)next2())!=NULL) {
00357    comp->Add( ev2 );
00358       }
00359     }
00360   }
00361   return comp;
00362 }
00363 
00364 
00365 TGo4EventElement& TGo4CompositeEvent::operator[]( Int_t i )
00366 {
00367    return *dynamic_cast<TGo4EventElement*> ((*fEventElements)[i]);
00368 }
00369 
00370 void TGo4CompositeEvent::Streamer(TBuffer &R__b)
00371 {
00372   // Stream an object of class TGo4CompositeEvent.
00373   //  char buffer[255];
00374    //
00375   if (R__b.IsReading()) {
00376     // printf("TGo4CompositeEvent::Streamer reading \n");
00377     Version_t R__v = R__b.ReadVersion(); if (R__v) { }
00378     TGo4EventElement::Streamer(R__b);
00379     R__b >> fEventElements;
00380     R__b >> fNElements;
00381 
00382   } else {
00383     //   printf("TGo4CompositeEvent::Streamer writing \n");
00384    TGo4EventElement::Streamer(R__b);
00385     R__b << fEventElements;
00386     R__b << fNElements;
00387   }
00388 }
00389 
00390 
00391 
00392 
00393 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:07 2008 for Go4-v3.04-1 by  doxygen 1.4.2