00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4CompositeEvent.h"
00015
00016 #include "TTree.h"
00017 #include "TROOT.h"
00018 #include "TObjArray.h"
00019
00020 #include "TGo4Log.h"
00021
00022
00023
00024 TGo4CompositeEvent::TGo4CompositeEvent() :
00025 TGo4EventElement(),
00026 fNElements(0),
00027 fEventElements(0),
00028 fMaxIndex(0)
00029 {
00030 }
00031
00032 TGo4CompositeEvent::TGo4CompositeEvent(const char*aName, const char* aTitle, Short_t aBaseCat) :
00033 TGo4EventElement(aName,aTitle, aBaseCat),
00034 fNElements(0),
00035 fEventElements(0),
00036 fMaxIndex(0)
00037 {
00038 }
00039
00040 TGo4CompositeEvent::~TGo4CompositeEvent()
00041 {
00042 if(fEventElements!=0) {
00043
00044 fEventElements->Delete();
00045
00046 delete fEventElements;
00047
00048 fEventElements = 0;
00049 }
00050 }
00051
00052 TGo4EventElement* TGo4CompositeEvent::GetChild(const char* name)
00053 {
00054 TGo4EventElement* res = TGo4EventElement::GetChild(name);
00055
00056 if ((res==0) && (fEventElements!=0))
00057 res = dynamic_cast<TGo4EventElement*> (fEventElements->FindObject(name));
00058
00059 return res;
00060 }
00061
00062
00063 void TGo4CompositeEvent::makeBranch(TBranch *parent)
00064 {
00065 if (fEventElements!=0)
00066 for(Int_t i=0; i<=fEventElements->GetLast();i++) {
00067 TGo4EventElement** par = (TGo4EventElement**) &((*fEventElements)[i]);
00068 if (par && *par) {
00069 TBranch *b = parent->GetTree()->TTree::Branch(Form("%s.",(*par)->GetName()), (*par)->ClassName(),par,4000,99);
00070 (*par)->makeBranch(b);
00071 }
00072 }
00073
00074 TGo4EventElement::makeBranch(parent);
00075 }
00076
00077
00078 Int_t TGo4CompositeEvent::activateBranch(TBranch *branch, Int_t init, TGo4EventElement** var_ptr)
00079 {
00080
00081 TGo4EventElement::activateBranch(branch, init, var_ptr);
00082
00083 if (branch==0) return fNElements;
00084
00085
00086 branch->GetEntry(0);
00087
00088 if (fDebug)
00089 TGo4Log::Debug("##### TGo4CompositeEvent::activateBranch called from obj:%s",this->GetName());
00090
00091 TTree* tree = branch->GetTree();
00092 TObjArray *br = tree->GetListOfBranches();
00093
00094 Int_t i = init;
00095 Int_t all_branches = fNElements;
00096
00097
00098 ProvideArray();
00099
00100
00101 if (fDebug)
00102 TGo4Log::Debug("-I-TGo4CompositeEvent::activateBranch from obj:%s bname:%s Elements:%d index:%d",
00103 GetName(), branch->GetName(), fNElements, init);
00104
00105 while (i < init + all_branches) {
00106 i++;
00107 TBranch* b = (TBranch*) br->At(i);
00108 Bool_t readentry = kFALSE;
00109 if (b==0) continue;
00110
00111 TString sub = b->GetName();
00112 sub.Remove(sub.Length()-1);
00113 TGo4EventElement* par = getEventElement(sub.Data());
00114
00115 if (fDebug)
00116 TGo4Log::Debug("-I TGo4CompositeEvent::activateBranch use subbranch %s", b->GetName());
00117
00118 if (par==0) {
00119
00120 TClass* cl = gROOT->GetClass(b->GetClassName());
00121 if (cl==0) {
00122 TGo4Log::Debug("-I class %s cannot be reconstructed", b->GetClassName());
00123 continue;
00124 }
00125
00126 par = (TGo4EventElement*) cl->New();
00127
00128 par->SetName(sub.Data());
00129 if(fDebug)
00130 TGo4Log::Debug("-I Created new instance of class %s, name:%s", cl->GetName(), par->GetName());
00131
00132
00133 if (par==0) {
00134 TGo4Log::Error("-I class %s instance cannot be created", b->GetClassName());
00135 continue;
00136 }
00137
00138 readentry = !par->isComposed();
00139 }
00140
00141
00142
00143 Int_t offset = par->activateBranch(b, i, &par);
00144
00145 if (fDebug)
00146 TGo4Log::Debug("-I activate from obj:%s elems:%d index:%d adding:%s",
00147 this->GetName(), init+fNElements, i, par->GetName());
00148
00149
00150 if (readentry) b->GetEntry(0);
00151
00152 if (fDebug)
00153 TGo4Log::Debug("Add branch %s event %s offset %d", b->GetName(), par->GetName(), offset);
00154
00155 if (addEventElement(par, kTRUE)) {
00156 TGo4EventElement** par_ptr = (TGo4EventElement**) &((*fEventElements)[par->getId()]);
00157 tree->SetBranchAddress(b->GetName(), par_ptr);
00158 }
00159
00160 i+=offset;
00161 all_branches+=offset;
00162 if (fDebug)
00163 TGo4Log::Debug("-I from obj:%s activate indexing after offset:%d index:%d max:%d",
00164 this->GetName(), offset, i, init+all_branches);
00165 }
00166
00167
00168
00169
00170 if (fDebug)
00171 TGo4Log::Debug("-I activate return value from obj:%s offset:%i", GetName(), all_branches);
00172
00173 return all_branches;
00174 }
00175
00176 void TGo4CompositeEvent::Clear(Option_t *opt)
00177 {
00178
00179
00180 TGo4EventElement::Clear(opt);
00181
00182 TIter next(fEventElements);
00183 TGo4EventElement *ev;
00184 while ( (ev=(TGo4EventElement *)next())!=0)
00185 ev->Clear(opt);
00186 }
00187
00188 Bool_t TGo4CompositeEvent::addEventElement(TGo4EventElement* aElement, Bool_t reading)
00189 {
00190
00191
00192
00193
00194
00195
00196
00197 if (reading && fEventElements && fEventElements->FindObject(aElement)) return kTRUE;
00198
00199 if (getEventElement(aElement->GetName(),1)) {
00200 TGo4Log::Error("<TGo4CompositeEvent::addEventElement> object:%s already in structure => not added !",
00201 aElement->GetName());
00202 return kFALSE;
00203 }
00204
00205 if ( aElement->getId() < 0 ) {
00206 TGo4Log::Error("<TGo4CompositeEvent::addEventElement> object:%s with invalid Id:%d => not added !",
00207 aElement->GetName(), aElement->getId());
00208 return kFALSE;
00209 }
00210
00211 if (getEventElement(aElement->getId()) != 0) {
00212 TGo4Log::Error("<TGo4CompositeEvent::addEventElement> object:%s with Id:%d already used => not added !",
00213 aElement->GetName(), aElement->getId());
00214 return kFALSE;
00215 }
00216
00217 ProvideArray();
00218
00219 if (fDebug)
00220 TGo4Log::Debug("-I adding element in :%s :%p of id:%i",GetName(),aElement, aElement->getId());
00221
00222 fEventElements->AddAtAndExpand(aElement,aElement->getId());
00223 if(!reading) fNElements++;
00224 if(aElement->getId()>fMaxIndex) fMaxIndex=aElement->getId();
00225 if (fDebug)
00226 TGo4Log::Debug("-I fNElements:%d fMaxIndex:%d",fNElements, fMaxIndex);
00227
00228 return kTRUE;
00229 }
00230
00231
00232 TGo4EventElement* TGo4CompositeEvent::getEventElement(Int_t idx)
00233 {
00234
00235
00236 if ((fEventElements==0) || (idx<0) || (idx > fEventElements->GetLast())) return NULL;
00237 return ( TGo4EventElement*) fEventElements->At(idx);
00238 }
00239
00240
00241
00242 TGo4EventElement* TGo4CompositeEvent::getEventElement(const char* name, Int_t final)
00243 {
00244 TIter next(fEventElements);
00245 TGo4EventElement *ev(0);
00246 while ((ev=( TGo4EventElement *)next())!=0) {
00247 if(strcmp(name,ev->GetName())==0) return ev;
00248 if (ev->isComposed()) {
00249 TGo4EventElement* inter= ((TGo4CompositeEvent*) ev)->getEventElement(name,1);
00250 if (inter !=0) return inter;
00251 }
00252 }
00253 if(final==0)
00254 TGo4Log::Debug("TGo4CompositeEvent => Element:%s not found in Composite:%s", name, GetName());
00255 return NULL;
00256 }
00257
00258 void TGo4CompositeEvent::deactivate()
00259 {
00260 TGo4EventElement::deactivate();
00261
00262 TIter next(fEventElements);
00263 TGo4EventElement *ev(0);
00264
00265 while ((ev=( TGo4EventElement *)next())!=0)
00266 ev->deactivate();
00267 }
00268
00269 void TGo4CompositeEvent::activate()
00270 {
00271 TGo4EventElement::activate();
00272
00273 TIter next(fEventElements);
00274 TGo4EventElement *ev(0);
00275
00276 while ((ev=( TGo4EventElement *)next())!=0)
00277 ev->activate();
00278 }
00279
00280
00281 TObjArray* TGo4CompositeEvent::getListOfComposites(Bool_t toplevel)
00282 {
00283 TObjArray *comp = new TObjArray(12);
00284 if (toplevel) comp->Add(this);
00285
00286 TIter next(fEventElements);
00287
00288 TGo4EventElement *ev(0);
00289 while ((ev=( TGo4EventElement *)next())!=0) {
00290 if ( ev->isComposed() ) {
00291 comp->Add( ev );
00292 TObjArray* dump = ((TGo4CompositeEvent*) ev)->getListOfComposites(kFALSE);
00293
00294 comp->AddAll(dump);
00295
00296
00297 delete dump;
00298 }
00299 }
00300 return comp;
00301 }
00302
00303 TGo4EventElement& TGo4CompositeEvent::operator[]( Int_t i )
00304 {
00305 if ((fEventElements==0) || (i<0) || (i>fEventElements->GetLast())) {
00306 TGo4Log::Error("Wrong index %d in TGo4CompositeEvent::operator[]", i);
00307 return *this;
00308 }
00309
00310 return * ((TGo4EventElement*) (fEventElements->At(i)));
00311 }
00312
00313
00314 void TGo4CompositeEvent::ProvideArray()
00315 {
00316 if (fEventElements==0) {
00317 Int_t size = fMaxIndex+1;
00318 fEventElements = new TObjArray(size);
00319 if (fDebug)
00320 TGo4Log::Debug("-I creating TObjArray of size %i",size);
00321 }
00322 if(fMaxIndex+1 >fEventElements->GetSize()){
00323 fEventElements->Expand(fMaxIndex+1);
00324 if (fDebug)
00325 TGo4Log::Debug("-I Expanded component array to size %i",fEventElements->GetSize());
00326 }
00327 }