GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4TreeStore.cxx
Go to the documentation of this file.
1 // $Id$
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4TreeStore.h"
15 
16 #include "TFile.h"
17 #include "TTree.h"
18 #include "TBranch.h"
19 
20 #include "TGo4Log.h"
21 #include "TGo4EventElement.h"
22 #include "TGo4Parameter.h"
23 #include "TGo4TreeStoreParameter.h"
24 #include "TGo4MainTree.h"
25 
26 const char *TGo4TreeStore::fgcFILESUF = ".root";
27 
29  TGo4EventElement *event,
30  Int_t splitlevel,
31  Int_t bufsize,
32  const char *filename,
33  Int_t compression) :
34  TGo4EventStore(name),
35  fxEvent(event), fiSplit(splitlevel), fiBufsize(bufsize)
36 {
37  GO4TRACE((15,"TGo4TreeStore::TGo4TreeStore(const char*,...)", __LINE__, __FILE__));
38 
39  TString buffer = TString::Format("%s.", name); // for branches containing the same event structure
41  if(fxEvent) {
42  fcEventClass = fxEvent->ClassName();
44  fxBranch = fxTree->GetBranch(buffer.Data());
45  if(fxBranch) {
46  // tree already had branch of our name, check it
47  TGo4Log::Debug(" TreeStore: Found existing branch %s, reset it ", name);
48  fxBranch->Reset();
49  } else {
50  // no such branch existing, create a new one
51  fxBranch = fxTree->Branch(buffer, fcEventClass.Data(), &fxEvent, fiBufsize, fiSplit);
52  TGo4Log::Debug(" TreeStore: Created new branch %s ", name);
53  } // if(fxBranch)
54  if(filename) {
55  buffer = TString::Format("%s%s", filename, fgcFILESUF);
56  fxFile = TFile::Open(buffer.Data(), "UPDATE", "Go4 tree store", compression);
57  fxBranch->SetFile(fxFile);
58  TGo4Log::Debug(" TreeStore: Set branch to file %s ", filename);
59  TGo4Log::Info("TGo4TreeStore: Open file tree %s UPDATE", buffer.Data());
60  } else {
61  fxFile = nullptr;
62  } // if(filename)
63  }
64  else
65  {
66  TGo4Log::Debug(" !!! TreeStore creation ERROR: no event structure !!! ");
67  } // if(fxEvent)
68 }
69 
71 : TGo4EventStore("dummy"),fxEvent(event), fiSplit(par->fiSplit), fiBufsize(par->fiBufsize)
72 {
73  GO4TRACE((15,"TGo4TreeStore::TGo4TreeStore(const char*,...)", __LINE__, __FILE__));
74 
75  SetName(par->GetName());
76  TString buffer = TString::Format("%s.", par->GetName());
78  if(fxEvent) {
79  fcEventClass = fxEvent->ClassName();
80  fxTree = fxSingletonTree->GetTree();
81  fxBranch = fxTree->GetBranch(buffer.Data());
82  if(fxBranch) {
83  // tree already had branch of our name, check it
84  TGo4Log::Debug(" TreeStore: Found existing branch %s, reset it ", buffer.Data());
85  fxBranch->Reset();
86  } else {
87  // no such branch existing, create a new one
88  fxBranch = fxTree->Branch(buffer.Data(), fcEventClass.Data(), &fxEvent, fiBufsize, fiSplit);
89  TGo4Log::Debug(" TreeStore: Created new branch %s ", buffer.Data());
90  }
91  if(!par->fxBranchFile.IsNull()) {
92  buffer = TString::Format("%s%s", par->fxBranchFile.Data(), fgcFILESUF);
93  fxFile = TFile::Open(buffer.Data(), "UPDATE", "Go4 tree store", par->fiCompression);
94  fxBranch->SetFile(fxFile);
95  TGo4Log::Debug(" TreeStore: Set branch to file %s ", buffer.Data());
96  TGo4Log::Info("TGo4TreeStore: Open file tree %s UPDATE", buffer.Data());
97  }
98  else
99  {
100  fxFile = nullptr;
101  } // if(filename)
102 
103  }
104  else
105  {
106  TGo4Log::Debug(" !!! TreeStore creation ERROR: no event structure !!! ");
107  } // if(fxEvent)
108 }
109 
110 TGo4TreeStore::TGo4TreeStore() : TGo4EventStore("Go4 Default Tree Store"), fiSplit(0), fiBufsize(0)
111 {
112  GO4TRACE((15, "TGo4TreeStore::TGo4TreeStore()", __LINE__, __FILE__));
113  // public default ctor for streamer
114 }
115 
117 {
118  GO4TRACE((15,"TGo4TreeStore::~TGo4TreeStore()", __LINE__, __FILE__));
119  //delete fxFile; // closes File <- done by maintree dtor
120 }
121 
123 {
124  GO4TRACE((12,"TGo4TreeStore::Store(TGo4EventElement *)", __LINE__, __FILE__));
125 
126  Int_t rev = -1;
127  // check if new event is of the right class
128  if(fcEventClass == event->ClassName()) {
129  // event class is matching, we fill it into our tree
130  fxEvent = event;
131  fxBranch->SetAddress(&fxEvent);
132  fxBranch->Fill();
133  rev = 0;
134  } else {
135  // new event does not match the old ones, we ignore it
136  TGo4Log::Debug(" !!! TreeStore::Store ERROR: this event does not match !!! ");
137  fxEvent->Clear();
138  fxEvent->SetValid(kFALSE);
139  fxBranch->SetAddress(&fxEvent);
140  fxBranch->Fill(); // we fill dummy event for consistency
141  rev = 1;
142  }
143  //fxBranch->Write();
144  return rev;
145 }
146 
147 
149 {
150  GO4TRACE((12,"TGo4TreeStore::Store(TGo4Parameter*)", __LINE__, __FILE__));
151  if(cali) {
152  TString oldname = cali->GetName();
153  cali->SetName(TString::Format("%s_%d", oldname.Data(), fxSingletonTree->GetCurrentIndex()).Data());
154  fxSingletonTree->fxFile->cd(); // we are main tree's friend
155  cali->Write(nullptr, TObject::kOverwrite);
156  cali->SetName(oldname.Data());
157  }
158 
159  return 0;
160 }
161 
163 {
164  GO4TRACE((12,"TGo4TreeStore::SetCompression(Int_t)", __LINE__, __FILE__));
165  fxFile->SetCompressionLevel(comp);
166 }
static TGo4MainTree * Instance()
Int_t Store(TGo4EventElement *event) override
void SetValid(Bool_t on)
static void Info(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:294
TGo4MainTree * fxSingletonTree
Definition: TGo4TreeStore.h:81
TFile * fxFile
Definition: TGo4TreeStore.h:76
TTree * fxTree
Definition: TGo4TreeStore.h:84
static void Debug(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:281
TFile * fxFile
Definition: TGo4MainTree.h:93
Int_t GetCurrentIndex() const
Definition: TGo4MainTree.h:66
void SetCompression(Int_t comp)
static const char * fgcFILESUF
Definition: TGo4TreeStore.h:70
TString fcEventClass
Definition: TGo4TreeStore.h:96
TTree * GetTree() const
Definition: TGo4MainTree.h:39
void Clear(Option_t *opt="") override
TBranch * fxBranch
Definition: TGo4TreeStore.h:87
#define GO4TRACE(X)
Definition: TGo4Log.h:25
TGo4EventElement * fxEvent
Definition: TGo4TreeStore.h:90
virtual ~TGo4TreeStore()