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