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

/Go4EventServer/TGo4FileStore.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 "TGo4FileStore.h"
00017 
00018 #include <iostream.h>
00019 
00020 #include "TFolder.h"
00021 
00022 #include "Go4Log/TGo4Log.h"
00023 #include "Go4Event/TGo4EventElement.h"
00024 #include "Go4Event/TGo4CompositeEvent.h"
00025 #include "Go4StatusBase/TGo4Parameter.h"
00026 #include "Go4ConditionsBase/TGo4Condition.h"
00027 #include "Go4Fit/TGo4Fitter.h"
00028 
00029 const Text_t TGo4FileStore::fgcTREESUF[] = "xTree";
00030 const Text_t TGo4FileStore::fgcFILESUF[] = ".root";
00031 const Text_t TGo4FileStore::fgcEVBRANCHNAME[] = "Go4EventBranch.";
00032 const Int_t TGo4FileStore::fgiFILESPLITSIZE = 1000000000;
00033 
00034 
00035 TGo4FileStore::TGo4FileStore()
00036 :TGo4EventStore("Go4 Default File Store"), fbBranchExists(kFALSE), fxEvent(0),
00037    fiSplit(1), fiBufsize(0), fiFillCount(0)
00038 {
00039    TRACE((15,"TGo4FileStore::TGo4FileStore()", __LINE__, __FILE__));
00040    // public default ctor for streamer
00041    fxFile=0;
00042    fxTree=0;
00043    TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
00044 
00045 }
00046 
00047 TGo4FileStore::TGo4FileStore(const char* name,
00048                              Int_t splitlevel,
00049                              Int_t compression,
00050                               Bool_t overwrite,
00051                               Int_t autosavesize,
00052                               Int_t bufsize)
00053 : TGo4EventStore(name),
00054    fbBranchExists(kFALSE), fxEvent(0),
00055    fiSplit(splitlevel), fiBufsize(bufsize), fiFillCount(0)
00056 {
00057    TRACE((15,"TGo4FileStore::TGo4FileStore(Text_t*,...)", __LINE__, __FILE__));
00058    TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
00059    Text_t buffer [TGo4EventStore::fguTXTLEN];
00060    snprintf(buffer, TGo4EventStore::fguTXTLEN -10 , name);
00061    if(!strstr(buffer,fgcFILESUF))
00062             strcat(buffer,fgcFILESUF);
00063    if(overwrite)
00064       {
00065          fxFile = new TFile(buffer, "RECREATE");
00066       }
00067    else
00068       {
00069          fxFile = new TFile(buffer, "UPDATE");
00070       }
00071    fxFile->SetCompressionLevel(compression);
00072 
00073    // strip any path information from treename:
00074    const char* lastname=name;
00075    const char* oldname=name;
00076    lastname=strstr(oldname,"/");
00077    while(lastname!=0)
00078       {
00079          oldname=lastname+1;
00080          lastname=strstr(oldname,"/");
00081       }
00082    snprintf(buffer, TGo4EventStore::fguTXTLEN -10 , oldname);
00083    strcat(buffer,fgcTREESUF);
00084    fxTree= dynamic_cast<TTree*> (fxFile->Get(buffer));
00085    if(fxTree)
00086       {
00087          TGo4Log::Debug(" Tree %s has been found in file %s ",buffer, fxFile->GetName());
00088          fiFillCount=(Int_t) (fxTree->GetEntries());
00089       }
00090    else
00091       {
00092          fxTree = new TTree(buffer, "Go4FileStore");
00093          fxTree->SetAutoSave(autosavesize);
00094          fxTree->Write();
00095          TGo4Log::Debug(" Tree %s has been created in file %s ",buffer, fxFile->GetName());
00096       }
00097 }
00098 
00099 
00100 TGo4FileStore::TGo4FileStore(TGo4FileStoreParameter* par)
00101 : TGo4EventStore("dummy"),
00102    fbBranchExists(kFALSE), fxEvent(0),
00103    fiSplit(par->fiSplit), fiBufsize(par->fiBufsize), fiFillCount(0)
00104 
00105 {
00106 TRACE((15,"TGo4FileStore::TGo4FileStore(TGo4FileStoreParameter* par)", __LINE__, __FILE__));
00107    TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
00108    Text_t buffer [TGo4EventStore::fguTXTLEN];
00109    snprintf(buffer,TGo4EventStore::fguTXTLEN -20,  par->GetName());
00110    SetName(buffer);
00111    if(!strstr(buffer,fgcFILESUF))
00112       strcat(buffer,fgcFILESUF);
00113    if(par->IsOverwriteMode())
00114       {
00115          fxFile = new TFile(buffer, "RECREATE");
00116       }
00117    else
00118       {
00119          fxFile = new TFile(buffer, "UPDATE");
00120       }
00121    fxFile->SetCompressionLevel(par->fiCompression);
00122 
00123      // strip any path information from treename (could be identical with filename!)
00124    const char* lastname = par->GetTitle();
00125    const char* oldname=lastname;
00126    lastname=strstr(oldname,"/");
00127    while(lastname!=0)
00128       {
00129          oldname=lastname+1;
00130          lastname=strstr(oldname,"/");
00131       }
00132    snprintf(buffer, TGo4EventStore::fguTXTLEN -10 , oldname);
00133    strcat(buffer,fgcTREESUF);
00134    fxTree= dynamic_cast<TTree*> (fxFile->Get(buffer));
00135    if(fxTree)
00136       {
00137          TGo4Log::Debug(" Tree %s has been found in file %s ",buffer, fxFile->GetName());
00138          fiFillCount= (Int_t) (fxTree->GetEntries());
00139       }
00140    else
00141       {
00142          fxTree = new TTree(buffer, "Go4FileStore");
00143          fxTree->SetAutoSave(par->fiAutosavesize);
00144          fxTree->Write();
00145          TGo4Log::Debug(" Tree %s has been created in file %s ",buffer, fxFile->GetName());
00146       }
00147 }
00148 
00149 
00150 TGo4FileStore::~TGo4FileStore()
00151 {
00152    TRACE((15,"TGo4FileStore::~TGo4FileStore()", __LINE__, __FILE__));
00153    if(fxFile)
00154       {
00155          fxFile=fxTree->GetCurrentFile(); // for file split after 1.8 Gb!
00156          fxFile->cd();
00157          fxTree->Write(0, TObject::kOverwrite);
00158          delete fxFile; // closes File, fxTree is removed from memory then
00159       }
00160    else
00161       { }
00162 }
00163 
00164 
00165 
00166 
00167 void TGo4FileStore::SetAutoSave(Int_t bytesinterval)
00168 {
00169    TRACE((15,"TGo4FileStore::SetAutoSave(Int_t)", __LINE__, __FILE__));
00170    fxTree->SetAutoSave(bytesinterval);
00171 }
00172 void TGo4FileStore::SetCompression(Int_t comp)
00173 {
00174    TRACE((12,"TGo4FileStore::SetCompression(Int_t)", __LINE__, __FILE__));
00175    fxFile->SetCompressionLevel(comp);
00176 }
00177 
00178 Int_t TGo4FileStore::Store(TGo4EventElement* event)
00179 {
00180    TRACE((12,"TGo4FileStore::Store(TGo4EventElement*)", __LINE__, __FILE__));
00181    Int_t rev=-1;
00182    TGo4EventElement* lastevent= fxEvent; // keep pointer to last event
00183    fxEvent=event; // address of next event into event pointer
00184    if(!fbBranchExists)
00185       {
00186          // first call of Store, create new branch
00187          //cout << "**********************Creating new branch! for "<<event->GetName()<< endl;
00188          if(fxEvent)
00189             {
00190                Text_t topbranchname [TGo4EventStore::fguTXTLEN];
00191                snprintf(topbranchname,TGo4EventStore::fguTXTLEN -20,"%s.", fxEvent->GetName());
00192                TBranch*   go4branch= fxTree->GetBranch(topbranchname);
00193                if(go4branch)
00194                   {
00195                      // tree already had branch of our name, check it
00196                      TGo4Log::Debug(" FileStore: Found existing branch %s , continue filling ", fgcEVBRANCHNAME);
00197                      // here we might check the classname of the stored events inbranch
00198                      go4branch->SetAddress(&fxEvent);
00199                      fbBranchExists=kTRUE;
00200                   }
00201                else
00202                   {
00203                      // no such branch existing, create a new one
00204                      TBranch *topbranch=
00205                      fxTree->Branch(topbranchname, fxEvent->ClassName(), &fxEvent, fiBufsize, fiSplit);
00206                      TGo4Log::Debug(" FileStore: Created new branch %s ", topbranchname);
00207                      fbBranchExists=kTRUE;
00208                      if (fxEvent->InheritsFrom("TGo4CompositeEvent"))
00209                       dynamic_cast<TGo4CompositeEvent*>  (fxEvent)->makeBranch(topbranch);
00210                   } // if(go4branch)
00211                lastevent=event; // this is our first event ever...
00212             }
00213          else
00214             {
00215                // this is an error....
00216                rev=1;
00217             }
00218       } // if(!fbEventBranchExists)
00219    else
00220       {
00221            // need not to create a branch, use existing one
00222       }
00223       // check if new event is same class as previous ones
00224 //   if(lastevent && !strcmp( event->ClassName(), lastevent->ClassName() ) )
00225 //      {
00226          // event class is matching, we fill it into our tree
00227          fxTree->Fill();
00228          fiFillCount++;
00229          rev=0;
00230 //      }
00231 //   else
00232 //      {
00233 //         // new event does not match the old ones, we ignore it
00234 //         fxEvent=lastevent; // restore old event pointer for next cycle
00235 //         rev=1;
00236 //      }
00237    return rev;
00238 }
00239 
00240 Int_t TGo4FileStore::Store(TGo4Parameter* cali)
00241 {
00242    WriteToStore(cali);
00243    return 0;
00244 }
00245 
00246 Int_t TGo4FileStore::Store(TGo4Condition* conny)
00247 {
00248    WriteToStore(conny);
00249    return 0;
00250 }
00251 
00252 Int_t TGo4FileStore::Store(TGo4Fitter* fitter)
00253 {
00254    WriteToStore(fitter);
00255    return 0;
00256 }
00257 
00258 Int_t TGo4FileStore::Store(TFolder* fold)
00259 {
00260    WriteToStore(fold);
00261    return 0;
00262 }
00263 
00264 
00265 
00266 void TGo4FileStore::WriteToStore(TNamed* ob)
00267 {
00268 if(ob)
00269    {
00270          TDirectory* dsav=gDirectory;
00271          Text_t buf[TGo4EventStore::fguTXTLEN];
00272          Text_t oldname[TGo4EventStore::fguTXTLEN];
00273          strncpy(oldname,ob->GetName(), TGo4EventStore::fguTXTLEN -1);
00274          snprintf(buf,
00275                   TGo4EventStore::fguTXTLEN-1,
00276                   "%s_%d" , oldname, fiFillCount);
00277          ob->SetName(buf);
00278          if(fxTree) fxFile=fxTree->GetCurrentFile();
00279          fxFile->cd();
00280          ob->Write(0, TObject::kOverwrite);
00281          ob->SetName(oldname);
00282          dsav->cd();
00283    }
00284 
00285 }
00286 
00287 
00288 ClassImp(TGo4FileStore)
00289 
00290 
00291 
00292 
00293 //----------------------------END OF GO4 SOURCE FILE ---------------------

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