GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ObjectManager.cxx
Go to the documentation of this file.
1 // $Id: TGo4ObjectManager.cxx 999 2013-07-25 11:58:59Z linev $
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 für 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 "TGo4ObjectManager.h"
15 
16 #include "TROOT.h"
17 #include "TSystem.h"
18 #include "TFile.h"
19 #include "TTree.h"
20 #include "TString.h"
21 #include "TTimer.h"
22 
23 #include "TGo4ObjectProxy.h"
24 #include "TGo4Iter.h"
25 #include "TGo4DirProxy.h"
26 #include "TGo4TreeProxy.h"
27 #include "TGo4FolderProxy.h"
28 #include "TGo4LinkProxy.h"
29 
30 class TGo4ObjManLink : public TObject {
31  public:
32  TGo4ObjManLink(TGo4Slot* source, TGo4Slot* target, Bool_t expandchilds) :
33  TObject(),
34  fxSource(source),
35  fxTarget(target),
36  fbExapndChilds(expandchilds)
37  {
38  }
39 
40  Bool_t CheckEventSource(const TGo4Slot* evtsource)
41  {
42  if (evtsource==fxSource) return kTRUE;
43  if (fbExapndChilds && evtsource->IsParent(fxSource)) return kTRUE;
44  return kFALSE;
45  }
46 
47  TGo4Slot* GetSource() const { return fxSource; }
48  TGo4Slot* GetTarget() const { return fxTarget; }
49  Bool_t DoChildsExpand() const { return fbExapndChilds; }
50 
51  protected:
54  Bool_t fbExapndChilds;
55 };
56 
57 class TGo4ObjManCleanup : public TObject {
58  public:
59  TGo4ObjManCleanup(TObject* obj, TGo4Slot* slot) :
60  TObject(),
61  fObject(obj),
62  fSlot(slot)
63  {
64  }
65  TObject* GetObject() const { return fObject; }
66  TGo4Slot* GetSlot() const { return fSlot; }
67  protected:
68  TObject* fObject;
70 };
71 
72 
73 // ********************************************************************
74 
76  TGo4Slot(),
77  fLinks(),
78  fCleanups()
79 {
80  gROOT->GetListOfCleanups()->Add(this);
81 }
82 
83 TGo4ObjectManager::TGo4ObjectManager(const char* name, const char* title) :
84  TGo4Slot(0, name, title),
85  fLinks(),
86  fCleanups()
87 {
88  gROOT->GetListOfCleanups()->Add(this);
89 }
90 
92 {
93  DeleteChilds();
94 
95  fLinks.Delete();
96 
97  fCleanups.Delete();
98 
99  gROOT->GetListOfCleanups()->Remove(this);
100 }
101 
102 void TGo4ObjectManager::ProduceFullName(TString& name, TGo4Slot* toparent)
103 {
104  name = "";
105 }
106 
108 {
109  return (TGo4ObjectManager*) this;
110 }
111 
112 void TGo4ObjectManager::MakeFolder(const char* pathname)
113 {
114  if ((pathname!=0) && (*pathname!=0))
115  GetSlot(pathname, kTRUE);
116 }
117 
118 TGo4Slot* TGo4ObjectManager::Add(const char* pathname, TObject* obj, Bool_t owner, Bool_t canrename)
119 {
120  if (obj==0) return 0;
121 
122  TGo4Slot* slot = MakeObjSlot(pathname, obj->GetName(), obj->ClassName());
123 
124  if (slot!=0) {
125  if (canrename && (strcmp(obj->GetName(),slot->GetName())!=0)) {
126  TNamed* n = dynamic_cast<TNamed*> (obj);
127  if (n!=0) n->SetName(slot->GetName());
128  }
129 
130  slot->SetProxy(new TGo4ObjectProxy(obj, owner));
131  }
132 
133  return slot;
134 }
135 
136 void TGo4ObjectManager::AddFile(const char* pathname, const char* filename)
137 {
138  AddDir(pathname, TFile::Open(filename), kTRUE, kTRUE);
139 }
140 
141 void TGo4ObjectManager::CloseFiles(const char* pathname)
142 {
143  TGo4Slot* slot = GetSlot(pathname);
144  if (slot==0) return;
145  for(int n=slot->NumChilds()-1;n>=0;n--) {
146  TGo4Slot* subslot = slot->GetChild(n);
147  TGo4DirProxy* dirproxy = dynamic_cast<TGo4DirProxy*> (subslot->GetProxy());
148  if (dirproxy!=0)
149  if (dirproxy->IsFile())
150  delete subslot;
151  }
152 }
153 
154 
155 void TGo4ObjectManager::AddDir(const char* pathname, TDirectory* dir, Bool_t owner, Bool_t readright)
156 {
157  if (dir==0) return;
158 
159  const char* name = (dir->InheritsFrom(TFile::Class())) ?
160  gSystem->BaseName(dir->GetName()) : dir->GetName();
161 
162  TGo4Slot* slot = MakeObjSlot(pathname, name, dir->ClassName());
163 
164  if (slot!=0)
165  slot->SetProxy(new TGo4DirProxy(dir, readright, owner));
166 }
167 
168 
169 void TGo4ObjectManager::AddTree(const char* pathname, TTree* tree, Bool_t owner)
170 {
171  if (tree==0) return;
172 
173  TGo4Slot* slot = MakeObjSlot(pathname, tree->GetName(), tree->ClassName());
174 
175  if (slot!=0)
176  slot->SetProxy(new TGo4TreeProxy(tree, owner));
177 }
178 
179 void TGo4ObjectManager::AddFolder(const char* pathname, TFolder* f, Bool_t owner)
180 {
181  if (f==0) return;
182 
183  TGo4Slot* slot = MakeObjSlot(pathname, f->GetName(), f->ClassName());
184  if (slot!=0)
185  slot->SetProxy(new TGo4FolderProxy(f, owner, ""));
186 }
187 
188 void TGo4ObjectManager::AddROOTFolder(const char* pathname, const char* foldername)
189 {
190  TFolder* f = TGo4FolderProxy::LocateROOTFolder(foldername);
191  if (f==0) return;
192 
193  TGo4Slot* slot = MakeObjSlot(pathname, f->GetName(), f->ClassName());
194 
195  if (slot!=0)
196  slot->SetProxy(new TGo4FolderProxy(f, kFALSE, foldername));
197 }
198 
199 void TGo4ObjectManager::AddROOTFolders(const char* pathname, Bool_t selected)
200 {
201  if (selected) {
202  TString name(pathname);
203  if (name.Length()>0) name+="/root";
204  else name="root";
205  TGo4Slot* slot = GetSlot(name, kTRUE);
206  if (slot==0) return;
207  slot->SetTitle("ROOT folders");
208  AddROOTFolder(name, "//root/Canvases");
209  AddROOTFolder(name, "//root/Functions");
210  AddROOTFolder(name, "//root/Tasks");
211  AddROOTFolder(name, "//root/Specials");
212  AddROOTFolder(name, "//root/ROOT Memory");
213  AddROOTFolder(name, "//root/ROOT Files");
214 // AddDir(name, gROOT, kFALSE);
215  } else
216  AddROOTFolder(pathname, "//root/");
217 }
218 
219 void TGo4ObjectManager::AddProxy(const char* pathname, TGo4Proxy* cont, const char* name, const char* title)
220 {
221  TGo4Slot* slot = MakeObjSlot(pathname, name, title);
222  if (slot!=0) slot->SetProxy(cont);
223  else delete cont;
224 }
225 
227 {
228  TGo4Slot* slot = GetSlot(name);
229  return slot==0 ? 0 : slot->GetProxy();
230 }
231 
232 
233 TGo4Slot* TGo4ObjectManager::MakeObjSlot(const char* foldername, const char* name, const char* title)
234 {
235  TGo4Slot* folder = GetSlot(foldername, kTRUE);
236  if (folder==0) return 0;
237  if (folder->FindChild(name)==0)
238  return new TGo4Slot(folder, name, title);
239 
240  TString extraname;
241  Int_t cycle = 1;
242 
243  do {
244  extraname.Form("%s_v%d", name, cycle++);
245  } while (folder->FindChild(extraname.Data())!=0);
246 
247  return new TGo4Slot(folder, extraname.Data(), title);
248 }
249 
250 
251 TGo4Slot* TGo4ObjectManager::AddLink(TGo4Slot* source, const char* pathname, const char* linkname, const char* linktitle)
252 {
253  if (source==0) return 0;
254 
255  TGo4Slot* slot = MakeObjSlot(pathname, linkname, linktitle);
256 
257 // std::cout << "TGo4ObjectManager::AddLink in " << pathname << " source = " << source->GetFullName() << std::endl;
258 
259  if (slot!=0)
260  slot->SetProxy(new TGo4LinkProxy(source));
261 
262  for(Int_t indx=fLinks.GetLast(); indx>=0; indx--) {
263  TGo4ObjManLink* link = (TGo4ObjManLink*) fLinks.At(indx);
264  if (link==0) continue;
265 
266  TString namesrc, nametgt;
267  link->GetSource()->ProduceFullName(namesrc);
268  link->GetTarget()->ProduceFullName(nametgt);
269 
270 // std::cout << "indx = " << indx
271 // << " source = " << namesrc << " target = " << nametgt << std::endl;
272  }
273 
274  return slot;
275 }
276 
277 
278 TGo4Slot* TGo4ObjectManager::AddLink(TGo4Slot* source, const char* pathname)
279 {
280 
281  if (source==0) return 0;
282 
283  TGo4Slot* slot = MakeObjSlot(pathname, source->GetName(), source->GetTitle());
284 
285 // std::cout << "Make link in path " << pathname << " with name " << source->GetName() << std::endl;
286 
287 // if (slot!=0) std::cout << " slot = " << slot->GetName() <<
288 // " parent = " << slot->GetParent()->GetName() << std::endl;
289 
290  if (slot!=0)
291  slot->SetProxy(new TGo4LinkProxy(source));
292 
293  return slot;
294 }
295 
296 TGo4Slot* TGo4ObjectManager::AddLink(const char* sourcename, const char* pathname)
297 {
298  return AddLink(GetSlot(sourcename), pathname);
299 }
300 
302 {
303  TGo4LinkProxy* linkcont = (link==0) ? 0 :
304  dynamic_cast<TGo4LinkProxy*> (link->GetProxy());
305 
306  return (linkcont!=0) ? linkcont->GetLink() : 0;
307 }
308 
309 
310 void TGo4ObjectManager::RegisterLink(TGo4Slot* source, TGo4Slot* target, Bool_t exapndchilds)
311 {
312  fLinks.Add(new TGo4ObjManLink(source, target, exapndchilds));
313 }
314 
316 {
317  RemoveFromLinks(target);
318 }
319 
321 {
322  Bool_t docompress = kFALSE;
323  for (Int_t n=0;n<=fLinks.GetLast();n++) {
324  TGo4ObjManLink* link = (TGo4ObjManLink*) fLinks[n];
325  if ((link->GetTarget()==slot) || (link->GetSource()==slot)) {
326  fLinks.Remove(link);
327  delete link;
328  docompress = kTRUE;
329  }
330  }
331  if (docompress)
332  fLinks.Compress();
333 }
334 
335 void TGo4ObjectManager::RetranslateEvent(TGo4Slot* source, Int_t id, void* param)
336 {
337  if (source==0) return;
338 
339  for(Int_t indx=fLinks.GetLast(); indx>=0; indx--) {
340  TGo4ObjManLink* link = (TGo4ObjManLink*) fLinks.At(indx);
341  if (link==0) continue;
342 
343  if (link->CheckEventSource(source)) {
344  TGo4Slot* target = link->GetTarget();
345 
346  if (gDebug>2)
347  Info("RetranslateEvent","src = %p %s tgt = %p %s id = %d", source, source->GetFullName().Data(), target, target->GetFullName().Data(), id);
348 
349  target->Event(source, id, param);
350  }
351  }
352 }
353 
354 void TGo4ObjectManager::Event(TGo4Slot* source, Int_t id, void* param)
355 {
356  if (gDebug>2)
357  Info("Event","src %s id %d", source->GetFullName().Data(), id);
358 
359  RetranslateEvent(source, id, param);
360 
361  if (id==evDelete) {
362  RemoveFromLinks(source);
363  UnregisterObject(0, (TGo4Slot*) source);
364  }
365 
366  TGo4Slot::Event(source, id, param);
367 }
368 
369 void TGo4ObjectManager::SaveDataToFile(TFile* f, Bool_t onlyobjs, TGo4Slot* startslot)
370 {
371  Bool_t usefile = (f!=0);
372 
373  TDirectory* olddir = gDirectory;
374 
375  TDirectory* curdir = f;
376 
377  if (startslot==0) startslot = this;
378 
379  TGo4Iter iter(startslot, kTRUE);
380 
381  bool isxml = (f!=0) && f->InheritsFrom("TXMLFile");
382 
383  while (iter.next()) {
384 
385  if (usefile && !isxml) {
386  Int_t levelchange = iter.levelchange();
387 
388  while ((levelchange++<0) && (curdir!=0)) {
389  curdir = dynamic_cast<TDirectory*> (curdir->GetMother());
390  }
391  if (curdir==0) break;
392 
393  if (iter.isfolder()) {
394  curdir = curdir->mkdir(iter.getname(),"subdirectory");
395  }
396  if (curdir==0) break;
397  }
398 
399  TGo4Slot* slot = iter.getslot();
400  if (slot!=0)
401  slot->SaveData(curdir, onlyobjs);
402  }
403 
404  if (olddir!=0) olddir->cd();
405 }
406 
408 {
409  Bool_t usefile = (f!=0);
410 
411  TDirectory* olddir = gDirectory;
412 
413  TDirectory* curdir = f;
414 
415  TGo4Iter iter(this, kTRUE);
416 
417  while (iter.next()) {
418  if (usefile) {
419  Int_t levelchange = iter.levelchange();
420  while ((levelchange++<0) && (curdir!=0))
421  curdir = dynamic_cast<TDirectory*> (curdir->GetMother());
422  if (curdir==0) break;
423  if (iter.isfolder())
424  curdir->GetObject(iter.getname(), curdir);
425  if (curdir==0) break;
426  }
427 
428  TGo4Slot* slot = iter.getslot();
429  if (slot!=0)
430  slot->ReadData(curdir);
431  }
432 
433  if (olddir!=0) olddir->cd();
434 }
435 
437 {
438  if (obj==0) return;
439  fCleanups.Add(new TGo4ObjManCleanup(obj, slot));
440  obj->SetBit(kMustCleanup);
441 }
442 
444 {
445  Bool_t compress = kFALSE;
446  for(int indx=fCleanups.GetLast();indx>=0;indx--) {
447  TGo4ObjManCleanup* entry = (TGo4ObjManCleanup*) fCleanups.At(indx);
448  if (entry->GetSlot()!=slot) continue;
449  if ((obj==0) || (entry->GetObject()==obj)) {
450  fCleanups.Remove(entry);
451  delete entry;
452  compress = kTRUE;
453  }
454  }
455  if (compress)
456  fCleanups.Compress();
457 }
458 
460 {
461  if ((obj==0) || (obj==this)) return;
462 
463  Bool_t compress = kFALSE;
464  for(int indx=fCleanups.GetLast();indx>=0;indx--) {
465  TGo4ObjManCleanup* entry = (TGo4ObjManCleanup*) fCleanups.At(indx);
466  if (entry==0) continue;
467  if (entry->GetObject()==obj) {
468  // first
469  fCleanups.Remove(entry);
470 
471  entry->GetSlot()->RecursiveRemove(obj);
472 // std::cout << "TGo4ObjectManager::RecursiveRemove " << entry->GetSlot()->GetFullName() << " " << obj->GetName() << std::endl;
473  entry->GetSlot()->ForwardEvent(entry->GetSlot(), evObjDeleted, obj);
474  delete entry;
475  compress = kTRUE;
476  }
477  }
478  if (compress)
479  fCleanups.Compress();
480 }
481 
483 {
484  TGo4Iter iter(this, kTRUE);
485  while (iter.next()) {
486 
487  printf("%*c%s\n", (iter.level()+1)*2, ' ', iter.getname());
488 
489 // if (iter.getslot()!=0)
490 // iter.getslot()->PrintPars((iter.level()+1)*2 + 3);
491  }
492 }
493 
495 {
496  TGo4Iter iter(this);
497  Int_t cnt = 0;
498  while (iter.next()) cnt++;
499  return cnt;
500 }
501 
502 void TGo4ObjectManager::DeleteSlot(const char* pathname)
503 {
504  TGo4Slot* slot = (TGo4Slot*) GetSlot(pathname);
505  if (slot!=0) delete slot;
506 }
507 
508 Int_t TGo4ObjectManager::RequestObject(const char* source, const char* targetslot, Int_t waittime_millisec)
509 // returns 0 when error
510 // 1 when object assigned immediately
511 // 2 when object will be obtained later
512 {
513  TGo4Slot* tgtslot = GetSlot(targetslot);
514  if (tgtslot==0) return 0;
515 
516  TGo4Access* proxy = ProvideSlotAccess(source);
517  if (proxy==0) return 0;
518 
519  TClass* cl = proxy->GetObjectClass();
520  if (cl==0) return 0;
521 
522  tgtslot->ResetAssignFlag();
523 
524  Int_t res = proxy->AssignObjectTo(this, targetslot);
525 
526  if (res<2) delete proxy;
527 
528  if ((res==2) && (waittime_millisec>0)) {
529 
530  gSystem->ProcessEvents();
531 
532  while ((tgtslot->GetAssignFlag()<0) && (waittime_millisec>0)) {
533  gSystem->Sleep(10);
534  waittime_millisec-=10;
535  gSystem->ProcessEvents();
536  }
537 
538  res = (tgtslot->GetAssignFlag() == (Int_t) kTRUE) ? 1 : 0;
539  }
540 
541  return res;
542 }
543 
544 Bool_t TGo4ObjectManager::AssignObject(const char* path, TObject* obj, Bool_t ownership)
545 {
546  Bool_t res = kFALSE;
547  TGo4Slot* tgtslot = GetSlot(path);
548  if (tgtslot!=0) res = tgtslot->AssignObject(obj, ownership);
549  else if (ownership) delete obj;
550  return res;
551 }
TGo4ObjManCleanup(TObject *obj, TGo4Slot *slot)
Int_t GetAssignFlag() const
Definition: TGo4Slot.h:102
void SaveData(TDirectory *dir, Bool_t onlyobjs=kFALSE)
Definition: TGo4Slot.cxx:430
void UnregisterObject(TObject *obj, TGo4Slot *slot)
TObject * GetObject() const
Int_t level() const
Definition: TGo4Iter.cxx:157
void SetProxy(TGo4Proxy *cont)
Definition: TGo4Slot.cxx:313
Int_t RequestObject(const char *source, const char *targetslot, Int_t waittime_millisec=0)
void AddFile(const char *pathname, const char *filename)
Bool_t isfolder()
Definition: TGo4Iter.cxx:109
void UnregisterLink(TGo4Slot *target)
TGo4Proxy * GetProxy() const
Definition: TGo4Slot.h:93
Bool_t IsFile() const
virtual void ProduceFullName(TString &name, TGo4Slot *toparent=0)
Definition: TGo4Slot.cxx:280
void MakeFolder(const char *pathname)
void AddROOTFolder(const char *pathname, const char *foldername)
TGo4Slot * getslot() const
Definition: TGo4Iter.cxx:167
void RegisterLink(TGo4Slot *source, TGo4Slot *target, Bool_t exapndchilds=kFALSE)
void ResetAssignFlag()
Definition: TGo4Slot.h:103
void DeleteChilds(const char *startedwith=0)
Definition: TGo4Slot.cxx:221
void RetranslateEvent(TGo4Slot *source, Int_t id, void *param=0)
TGo4Slot()
Definition: TGo4Slot.cxx:64
virtual TGo4ObjectManager * GetOM() const
void RegisterObjectWith(TObject *obj, TGo4Slot *slot)
virtual void Event(TGo4Slot *source, Int_t id, void *param=0)
Definition: TGo4Slot.cxx:575
void DeleteSlot(const char *pathname)
void AddFolder(const char *pathname, TFolder *folder, Bool_t owner=kFALSE)
virtual void ProduceFullName(TString &name, TGo4Slot *toparent=0)
static TFolder * LocateROOTFolder(const char *rootfolder)
virtual void RecursiveRemove(TObject *obj)
Definition: TGo4Slot.cxx:594
Bool_t IsParent(const TGo4Slot *slot) const
Definition: TGo4Slot.cxx:197
TGo4Slot * FindChild(const char *name)
Definition: TGo4Slot.cxx:262
TGo4Slot * MakeObjSlot(const char *foldername, const char *name=0, const char *title=0)
virtual TClass * GetObjectClass() const
Definition: TGo4Proxy.cxx:36
TGo4Slot * GetSlot(const char *name, Bool_t force=kFALSE)
Definition: TGo4Slot.cxx:471
void AddTree(const char *pathname, TTree *tree, Bool_t owner=kFALSE)
void SaveDataToFile(TFile *f, Bool_t onlyobjs=kFALSE, TGo4Slot *startslot=0)
void AddROOTFolders(const char *pathname, Bool_t selected=kTRUE)
virtual Bool_t AssignObject(const char *path, TObject *obj, Bool_t ownership)
TGo4Slot * GetSlot() const
void AddDir(const char *pathname, TDirectory *dir, Bool_t owner=kFALSE, Bool_t readright=kFALSE)
const char * getname()
Definition: TGo4Iter.cxx:115
virtual Int_t AssignObjectTo(TGo4ObjectManager *rcv, const char *path)
Definition: TGo4Proxy.cxx:46
void AddProxy(const char *pathname, TGo4Proxy *cont, const char *name, const char *title="title")
void ReadData(TDirectory *dir)
Definition: TGo4Slot.cxx:436
virtual void Event(TGo4Slot *source, Int_t id, void *param=0)
void CloseFiles(const char *pathname)
virtual void RecursiveRemove(TObject *obj)
TGo4Slot * GetLink() const
Definition: TGo4LinkProxy.h:45
void ForwardEvent(TGo4Slot *source, Int_t id, void *param=0)
Definition: TGo4Slot.cxx:585
Int_t levelchange() const
Definition: TGo4Iter.h:42
Bool_t AssignObject(TObject *obj, Bool_t owner)
Definition: TGo4Slot.cxx:361
Int_t NumChilds() const
Definition: TGo4Slot.h:76
TString GetFullName(TGo4Slot *toparent=0)
Definition: TGo4Slot.cxx:290
TGo4Slot * GetLinked(TGo4Slot *link)
TGo4Access * ProvideSlotAccess(const char *name)
Definition: TGo4Slot.cxx:413
TGo4Slot * GetChild(Int_t n) const
Definition: TGo4Slot.h:77
void RemoveFromLinks(const TGo4Slot *slot)
TObjArray fCleanups
list of links between slots
TGo4Slot * Add(const char *pathname, TObject *obj, Bool_t owner=kFALSE, Bool_t canrename=kFALSE)
void ReadDataFromFile(TFile *f)
TGo4Slot * AddLink(TGo4Slot *source, const char *pathname, const char *linkname, const char *linktitle)
Bool_t next(Bool_t goesinto=kTRUE)
Definition: TGo4Iter.cxx:45