GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4DirProxy.cxx
Go to the documentation of this file.
1 // $Id: TGo4DirProxy.cxx 1448 2015-05-27 15:55:26Z 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 "TGo4DirProxy.h"
15 
16 #include "TSystem.h"
17 #include "TDirectory.h"
18 #include "TKey.h"
19 #include "TROOT.h"
20 #include "TH1.h"
21 #include "TTree.h"
22 #include "TFile.h"
23 
24 #include "TGo4ObjectProxy.h"
25 #include "TGo4TreeProxy.h"
26 #include "TGo4CanvasProxy.h"
27 #include "TGo4HStackProxy.h"
28 #include "TGo4Slot.h"
29 
30 class TGo4KeyAccess : public TGo4Access {
31  public:
32  TGo4KeyAccess(TDirectory* dir, TKey* key) : TGo4Access(), fDir(dir), fKey(key) {}
33 
34  virtual Bool_t CanGetObject() const
35  { return kTRUE; }
36 
37  virtual Bool_t GetObject(TObject* &obj, Bool_t &owner) const
38  {
39  TClass* cl = gROOT->GetClass(fKey->GetClassName());
40  if ((cl!=0) && !cl->IsLoaded()) {
41  obj = 0;
42  owner = kFALSE;
43  return kFALSE;
44  }
45 
46  owner = kTRUE;
47  fDir->cd();
48  obj = fKey->ReadObj();
49  if (obj==0) return kFALSE;
50 
51  if (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(TTree::Class())) owner = kFALSE; else
52  if (obj->InheritsFrom(TCanvas::Class())) { fDir->Add(obj); owner = kFALSE; }
53  return kTRUE;
54  }
55 
56  virtual const char* GetObjectName() const
57  { return fKey->GetName(); }
58 
59  virtual const char* GetObjectClassName() const
60  { return fKey->GetClassName(); }
61 
62  private:
63  TDirectory* fDir;
64  TKey* fKey;
65 };
66 
67 // ************************************************************************
68 
70  protected:
71  TDirectory* fDir;
72  Bool_t fReadRight;
73  TIterator* fIter;
74  Bool_t fIsKeyIter;
75  TObject* fCurrent;
76  TString fNameBuf;
77 
78  public:
79  TGo4DirLevelIter(TDirectory* dir, Bool_t readright) :
80  TGo4LevelIter(),
81  fDir(dir),
82  fReadRight(readright),
83  fIter(0),
84  fIsKeyIter(kFALSE),
85  fCurrent(0),
86  fNameBuf()
87  {
88  fIter = fDir->GetListOfKeys()->MakeIterator();
89  fIsKeyIter = kTRUE;
90  }
91 
93  {
94  if (fIter!=0) delete fIter;
95  }
96 
97  virtual Bool_t next()
98  {
99  Bool_t donext = kTRUE;
100 
101  while (donext) {
102 
103  fCurrent = fIter->Next();
104 
105  if (fCurrent==0) {
106  if(fIsKeyIter) {
107  delete fIter;
108  fIter = fDir->GetList()->MakeIterator();
109  fIsKeyIter = kFALSE;
110  continue;
111  } else
112  break;
113  }
114  donext = kFALSE;
115 
116  if (!fIsKeyIter) {
117  TKey* key = fDir->FindKey(fCurrent->GetName());
118  if (key!=0)
119  if (strcmp(fCurrent->ClassName(), key->GetClassName())==0) donext = kTRUE;
120  }
121  }
122  return fCurrent!=0;
123  }
124 
125  Bool_t IsContainerClass(TClass* cl) {
126  if (cl==0) return kFALSE;
127  return cl->InheritsFrom(TDirectory::Class()) ||
128  cl->InheritsFrom(TTree::Class()) ||
129  cl->InheritsFrom(TCanvas::Class()) ||
130  cl->InheritsFrom(THStack::Class());
131  }
132 
133  virtual Bool_t isfolder()
134  {
135  TClass* cl = 0;
136  if (fIsKeyIter) {
137  TKey* key = (TKey*) fCurrent;
138  TObject* obj = fDir->FindObject(key->GetName());
139  if (obj) cl = obj->IsA();
140  // if (fReadRight)
141  // cl = TGo4Proxy::GetClass(key->GetClassName());
142  } else {
143  cl = fCurrent->IsA();
144  }
145 
146  return IsContainerClass(cl);
147  }
148 
150  {
151  TObject* obj = fIsKeyIter ? fDir->Get(fCurrent->GetName()) : fCurrent;
152 
153  if (obj==0) return 0;
154 
155  if (obj->InheritsFrom(TTree::Class()))
156  return TGo4TreeProxy::ProduceIter((TTree*)obj);
157 
158  if (obj->InheritsFrom(TCanvas::Class()))
159  return TGo4CanvasProxy::ProduceIter((TCanvas*)obj);
160 
161  if (obj->InheritsFrom(THStack::Class()))
162  return TGo4HStackProxy::ProduceIter((THStack*)obj);
163 
164  TDirectory* subdir = dynamic_cast<TDirectory*> (obj);
165  return subdir==0 ? 0 : new TGo4DirLevelIter(subdir, fReadRight);
166  }
167 
168  virtual const char* name()
169  {
170  if (!fIsKeyIter) return fCurrent->GetName();
171  TKey* key = (TKey*) fCurrent;
172  // if (isfolder() || (key->GetCycle()==1)) return key->GetName();
173  fNameBuf.Form("%s;%d",key->GetName(),(int) key->GetCycle());
174  return fNameBuf.Data();
175  }
176 
177  virtual const char* info()
178  {
179  return fCurrent->GetTitle();
180  }
181 
182  virtual Int_t sizeinfo()
183  {
184  TClass* cl = 0;
185  Int_t sz = 0;
186  if (fIsKeyIter) {
187  TKey* key = (TKey*) fCurrent;
188  cl = TGo4Proxy::GetClass(key->GetClassName());
189  sz = key->GetNbytes();
190  TObject* obj = fDir->FindObject(key->GetName());
191  if (obj) sz = TGo4ObjectProxy::DefineObjectSize(obj);
192  } else {
193  cl = fCurrent->IsA();
194  sz = TGo4ObjectProxy::DefineObjectSize(fCurrent);
195  }
196  bool isdir = (cl!=0) && cl->InheritsFrom(TDirectory::Class());
197  return isdir ? 0 : sz;
198  }
199 
200  virtual Int_t GetKind()
201  {
202  if (isfolder()) return TGo4Access::kndFolder;
203 
204  if (fIsKeyIter && fReadRight) {
205  TKey* key = (TKey*) fCurrent;
206  TClass* cl = TGo4Proxy::GetClass(key->GetClassName());
207  if (cl==0) cl = gROOT->GetClass(key->GetClassName(), kTRUE, kTRUE);
209  }
210 
211  return TGo4Access::kndObject;
212  }
213 
214  virtual const char* GetClassName()
215  {
216  return fIsKeyIter ? ((TKey*) fCurrent)->GetClassName() : fCurrent->ClassName();
217  }
218 };
219 
220 //*************************************************************************
221 
223  TGo4Proxy(),
224  fDir(0),
225  fOwner(kFALSE),
226  fReadRight(kFALSE),
227  fxParentSlot(0)
228 {
229 }
230 
231 TGo4DirProxy::TGo4DirProxy(TDirectory* dir, Bool_t readright, Bool_t owner) :
232  TGo4Proxy(),
233  fDir(0),
234  fOwner(kFALSE),
235  fReadRight(kFALSE),
236  fxParentSlot(0)
237 {
238  SetDir(dir, readright, owner);
239 }
240 
242 {
243  ClearDir();
244 }
245 
246 void TGo4DirProxy::SetDir(TDirectory* dir, Bool_t readright, Bool_t owner)
247 {
248  ClearDir();
249  fDir = dir;
250  fReadRight = readright;
251  fOwner = owner;
252 }
253 
255 {
256  if (fOwner && (fDir!=0)) delete fDir;
257  fDir = 0;
258  fOwner = kFALSE;
259 }
260 
262 {
264 }
265 
267 {
268  return fDir!=0 ? fDir->ClassName() : 0;
269 }
270 
272 {
273  return fDir!=0 ? fDir->GetTitle() : 0;
274 }
275 
277 {
278  TFile* f = dynamic_cast<TFile*> (fDir);
279  return f==0 ? TGo4Proxy::GetObjectSizeInfo() : f->GetSize();
280 }
281 
282 TGo4Access* TGo4DirProxy::CreateAccess(TDirectory* dir, Bool_t readright, const char* name, TGo4Slot* browser_slot)
283 {
284  if (dir==0) return 0;
285 
286  if ((name==0) || (*name==0)) return new TGo4ObjectAccess(dir);
287 
288  TDirectory* curdir = dir;
289  const char* curname = name;
290 
291  while ((curdir!=0) && (curname!=0)) {
292  const char* slash = strchr(curname,'/');
293  TString partname;
294 
295  if (slash==0) {
296  partname = curname;
297  curname = 0;
298  } else {
299  partname.Append(curname, slash - curname);
300  curname = slash+1;
301  if (*curname==0) curname = 0; // if last symbol is slash than finish searching
302  }
303 
304  char namebuf[10000];
305  Short_t cyclebuf(9999);
306  TDirectory::DecodeNameCycle(partname.Data(), namebuf, cyclebuf, sizeof(namebuf));
307 
308  TObject* obj = curdir->GetList()->FindObject(namebuf);
309  if (obj==0) {
310  TKey* key = curdir->GetKey(namebuf, cyclebuf);
311  if (key==0) return 0;
312  if (!readright) return new TGo4KeyAccess(curdir, key);
313  curdir->cd();
314  obj = curdir->Get(partname.Data());
315  if (obj && obj->InheritsFrom(TCanvas::Class())) curdir->Add(obj);
316  if (obj && browser_slot)
317  browser_slot->ForwardEvent(browser_slot, TGo4Slot::evObjAssigned);
318  }
319 
320  if (obj==0) return 0;
321 
322  TTree* tr = dynamic_cast<TTree*> (obj);
323  if (tr!=0)
324  return TGo4TreeProxy::CreateAccess(tr, curname);
325 
326  TCanvas* canv = dynamic_cast<TCanvas*> (obj);
327  if (canv!=0)
328  return TGo4CanvasProxy::CreateAccess(canv, curname);
329 
330  THStack* hs = dynamic_cast<THStack*> (obj);
331  if (hs!=0)
332  return TGo4HStackProxy::CreateAccess(hs, curname);
333 
334  curdir = dynamic_cast<TDirectory*>(obj);
335 
336  if (curdir==0)
337  return new TGo4ObjectAccess(obj);
338  }
339  return 0;
340 }
341 
342 TGo4LevelIter* TGo4DirProxy::ProduceIter(TDirectory* dir, Bool_t readright)
343 {
344  return new TGo4DirLevelIter(dir, readright);
345 }
346 
347 void TGo4DirProxy::WriteData(TGo4Slot* slot, TDirectory* dir, Bool_t onlyobjs)
348 {
349  if (!onlyobjs) {
350  const char* filename = 0;
351  if ((fDir!=0) && fDir->InheritsFrom(TFile::Class()))
352  filename = fDir->GetName();
353 
354  slot->SetPar("DirProxy::FileName", filename);
355  slot->SetPar("DirProxy::gROOT", (fDir==gROOT) ? "true" : 0);
356  }
357 }
358 
359 void TGo4DirProxy::ReadData(TGo4Slot* slot, TDirectory* dir)
360 {
361  ClearDir();
362 
363  const char* filename = slot->GetPar("DirProxy::FileName");
364  const char* groot = slot->GetPar("DirProxy::gROOT");
365 
366  if (filename!=0) {
367  TFile* f = TFile::Open(filename);
368  if (f!=0) SetDir(f, kTRUE, kTRUE);
369  } else
370  if (groot!=0)
371  SetDir(gROOT, kFALSE, kFALSE);
372 }
373 
374 Bool_t TGo4DirProxy::UpdateObjectInFile(const char* filepath, TObject* obj)
375 {
376  if ((filepath==0) || (fDir==0)) return kFALSE;
377 
378  TFile* f = dynamic_cast<TFile*> (fDir);
379  if (f==0) return kFALSE;
380 
381  if (f->ReOpen("UPDATE")<0) return kFALSE;
382 
383  TString foldername, objname;
384  TGo4Slot::ProduceFolderAndName(filepath, foldername, objname);
385 
386  TDirectory* objdir = f;
387 
388  if (foldername.Length()>0) {
389  objdir = dynamic_cast<TDirectory*> (f->Get(foldername));
390  }
391 
392  char namebuf[10000];
393  Short_t cyclebuf;
394  TDirectory::DecodeNameCycle(objname.Data(), namebuf, cyclebuf, sizeof(namebuf));
395 
396  if (objdir!=0) {
397  objdir->cd();
398  objdir->Delete(objname.Data());
399  objdir->WriteTObject(obj, namebuf, "Overwrite");
400  }
401 
402  f->ReOpen("READ");
403 
404  return kTRUE;
405 }
406 
407 Bool_t TGo4DirProxy::IsFile() const
408 {
409  return dynamic_cast<TFile*>(fDir)!=0;
410 }
411 
412 const char* TGo4DirProxy::GetFileName() const
413 {
414  return IsFile() ? fDir->GetName() : 0;
415 }
virtual TGo4LevelIter * subiterator()
static TClass * GetClass(const char *classname, Bool_t load=kFALSE)
Definition: TGo4Proxy.cxx:75
TGo4KeyAccess(TDirectory *dir, TKey *key)
virtual Int_t GetObjectSizeInfo()
virtual Bool_t next()
virtual Bool_t CanGetObject() const
Bool_t fReadRight
Definition: TGo4DirProxy.h:26
static TGo4LevelIter * ProduceIter(TTree *tree)
virtual void ReadData(TGo4Slot *slot, TDirectory *dir)
virtual Bool_t GetObject(TObject *&obj, Bool_t &owner) const
static TGo4LevelIter * ProduceIter(THStack *canv)
Bool_t IsFile() const
static TGo4Access * CreateAccess(TCanvas *canv, const char *name)
TIterator * fIter
const char * GetPar(const char *name) const
Definition: TGo4Slot.cxx:621
virtual const char * GetContainedClassName()
static Long_t DefineObjectSize(TObject *obj)
static TGo4LevelIter * ProduceIter(TCanvas *canv)
static TGo4LevelIter * ProduceIter(TDirectory *dir, Bool_t readright)
TDirectory * fDir
virtual Int_t GetObjectKind()
virtual ~TGo4DirProxy()
virtual const char * info()
virtual Bool_t isfolder()
TDirectory * fDir
Definition: TGo4DirProxy.h:24
Bool_t UpdateObjectInFile(const char *filepath, TObject *obj)
TDirectory * fDir
const char * GetFileName() const
virtual const char * GetObjectClassName() const
virtual Int_t sizeinfo()
virtual Int_t GetKind()
Bool_t fOwner
Definition: TGo4DirProxy.h:25
virtual const char * name()
static TGo4Access * CreateAccess(THStack *canv, const char *name)
virtual const char * GetObjectName() const
static void ProduceFolderAndName(const char *fullname, TString &foldername, TString &objectname)
Definition: TGo4Slot.cxx:668
void ForwardEvent(TGo4Slot *source, Int_t id, void *param=0)
Definition: TGo4Slot.cxx:585
virtual ~TGo4DirLevelIter()
virtual Int_t GetObjectSizeInfo()
Definition: TGo4Proxy.h:111
static TGo4Access * CreateAccess(TTree *tree, const char *name)
TGo4DirLevelIter(TDirectory *dir, Bool_t readright)
void SetDir(TDirectory *dir, Bool_t readright, Bool_t owner)
Bool_t IsContainerClass(TClass *cl)
void SetPar(const char *name, const char *value)
Definition: TGo4Slot.cxx:609
virtual const char * GetContainedObjectInfo()
TObject * fCurrent
virtual const char * GetClassName()
virtual void WriteData(TGo4Slot *slot, TDirectory *dir, Bool_t onlyobjs)
static TGo4Access * CreateAccess(TDirectory *dir, Bool_t readright, const char *name, TGo4Slot *browser_slot=0)