GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitSlot.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitSlot.cxx 1844 2016-02-12 12:42:48Z adamczew $
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 "TGo4FitSlot.h"
15 
16 #include "Riostream.h"
17 #include "TBuffer.h"
18 
19 #include "TClass.h"
20 #include "TH1.h"
21 #include "TSeqCollection.h"
22 #include "TObjArray.h"
23 
25  TGo4FitNamed(),
26  fxClass(0),
27  fiSaveFlag(0),
28  fbOwned(kFALSE),
29  fbNeeded(kFALSE),
30  fxObject(0),
31  fiSaveSlot(-1),
32  fiSaveOwnership(-1)
33 {
34 }
35 
36 TGo4FitSlot::TGo4FitSlot(TNamed* iOwner, TClass* iClass) :
37  TGo4FitNamed(),
38  fxClass(iClass),
39  fiSaveFlag(0),
40  fbOwned(kFALSE),
41  fbNeeded(kFALSE),
42  fxObject(0),
43  fiSaveSlot(-1),
44  fiSaveOwnership(-1)
45 {
46  SetOwner(iOwner);
47 }
48 
49 TGo4FitSlot::TGo4FitSlot(const char* iName, const char* iTitle,
50  TNamed* iOwner, TClass* iClass,
51  Bool_t iNeeded, TObject* iObject, Bool_t iOwned) :
52  TGo4FitNamed(iName,iTitle, iOwner),
53  fxClass(iClass),
54  fiSaveFlag(0),
55  fbOwned(iOwned),
56  fbNeeded(iNeeded),
57  fxObject(iObject),
58  fiSaveSlot(-1),
59  fiSaveOwnership(-1)
60 {
61 }
62 
64 {
65  if (fbOwned && fxObject)
66  delete fxObject;
67 }
68 
69 void TGo4FitSlot::SetDefaults(TNamed* iOwner, TClass* iClass)
70 {
71  SetOwner(iOwner);
72  fxClass = iClass;
73 }
74 
76 {
77  if (!CanConnectToSlot(slot)) return kFALSE;
78  if (fbOwned && fxObject) delete fxObject;
79  fxObject = slot;
80  fbOwned = kFALSE;
81  return kTRUE;
82 }
83 
85 {
86  if ((slot==0) || (slot==this)) return kFALSE;
87  if (!GetClass()->InheritsFrom(slot->GetClass())) return kFALSE;
88  TGo4FitSlot* conn = slot;
89  while ( (conn = conn->GetConnectedSlot())!=0)
90  if (conn == this) return kFALSE;
91  return kTRUE;
92 }
93 
95 {
96  if (IsConnectedToSlot()) {
97  fxObject = 0;
98  fbOwned = kFALSE;
99  }
100 }
101 
102 Bool_t TGo4FitSlot::IsSuitable(TObject* obj)
103 {
104  if (obj==0) return kFALSE;
105  if (IsConnectedToSlot()) return GetConnectedSlot()->IsSuitable(obj);
106  else return obj->InheritsFrom(GetClass());
107 }
108 
110 {
111  if (cl==0) return kFALSE;
113  else return cl->InheritsFrom(GetClass());
114 }
115 
117 {
118  fxObject = 0;
119  fbOwned = kFALSE;
120 }
121 
122 Bool_t TGo4FitSlot::SetObject(TObject* iObject, Bool_t iOwned, Bool_t CheckClass)
123 {
124  if (IsConnectedToSlot())
125  return GetConnectedSlot()->SetObject(iObject, iOwned, CheckClass);
126 
127  if (CheckClass && iObject)
128  if (!iObject->InheritsFrom(GetClass())) return kFALSE;
129 
130  if (fbOwned && fxObject) delete fxObject;
131 
132  fxObject = iObject;
133  fbOwned = iOwned;
134  CheckOwnership();
135  return kTRUE;
136 }
137 
138 TObject* TGo4FitSlot::GetObject() const
139 {
141 }
142 
143 const char* TGo4FitSlot::GetObjectName() const
144 {
145  TObject* obj = GetObject();
146  if (obj && obj->InheritsFrom(TNamed::Class())) return obj->GetName();
147  else return 0;
148 }
149 
150 TObject* TGo4FitSlot::CloneObject(const char* newname)
151 {
152  TObject* obj = GetObject();
153  if (obj) return obj->Clone(newname);
154  else return 0;
155 }
156 
157 void TGo4FitSlot::Print(Option_t* option) const
158 {
159  std::cout << "Slot: " << GetName() << " for class: " << fxClass->GetName()
160  << " needed:" << fbNeeded << " owned:" << fbOwned;
161  if (IsConnectedToSlot()) {
162  std::cout << " Connected to: " << std::endl << " ";
163  GetConnectedSlot()->Print(option);
164  } else
165  if (fxObject) {
166  std::cout << " object: " << GetObjectName() << std::endl;
167  if (((strcmp(option,"**")==0) && fbOwned) || (strcmp(option,"***")==0))
168  fxObject->Print(option);
169  } else
170  std::cout << " no object. " << std::endl;
171 }
172 
174 {
175  if(fbOwned && !IsConnectedToSlot()) {
176  TNamed* master = dynamic_cast<TNamed*> (GetOwner());
177  TGo4FitNamed* slave = dynamic_cast<TGo4FitNamed*> (fxObject);
178  if (master && slave) slave->SetOwner(master);
179  }
180 }
181 
183 {
184  return (fiSaveFlag==1) || ((fiSaveFlag==0) && fbOwned);
185 }
186 
187 void TGo4FitSlot::SetSaveSettings(Int_t save, Int_t ownership)
188 {
189  fiSaveSlot = save;
190  fiSaveOwnership = ownership;
191 }
192 
194 {
195  return (fiSaveSlot>=0) && (fiSaveOwnership>=0);
196 }
197 
198 void TGo4FitSlot::Streamer(TBuffer& b)
199 {
200  if (b.IsReading()) {
201 
202  TGo4FitSlot::Class()->ReadBuffer(b, this);
203 
204  b >> fbNeeded;
205  b >> fbOwned;
206  Bool_t saveflag = kFALSE;
207  b >> saveflag;
208 
209  if (saveflag) {
210  b >> fxObject;
211  if (fxObject->InheritsFrom(TH1::Class()))
212  ((TH1*) fxObject)->SetDirectory(0);
213  CheckOwnership();
214  }
215  } else {
216 
217  TGo4FitSlot::Class()->WriteBuffer(b, this);
218 
219  Bool_t saveflag = kFALSE;
220  Bool_t saveown = kFALSE;
221  if ((fiSaveSlot>=0) && (fiSaveOwnership>=0)) {
222  saveflag = fiSaveSlot>0;
223  saveown = fiSaveOwnership>0;
224  } else {
225  saveflag = (fxObject!=0) && (WillBeSaved() || IsConnectedToSlot());
226  saveown = saveflag && GetOwned();
227  }
228 
229  b << fbNeeded;
230  b << saveown;
231  b << saveflag;
232  if (saveflag) b << fxObject;
233  }
234 
235  fiSaveSlot = -1;
236  fiSaveOwnership = -1;
237 }
238 
239 //********************************************************************************
240 
242 {
243  fxSlotList = 0;
244  fbUpdateSlotList = kFALSE;
245 }
246 
248 {
249  if (fxSlotList) { delete fxSlotList; fxSlotList = 0; }
250 }
251 
252 void TGo4FitSlotList::FillSlotList(TSeqCollection* lst)
253 {
254  if (lst==0) return;
255 }
256 
258 {
259  fbUpdateSlotList = kTRUE;
260 }
261 
262 const TObjArray* TGo4FitSlotList::GetSlotList(Bool_t ForceUpdate)
263 {
264  if (fxSlotList==0) {
265  fxSlotList = new TObjArray(10);
267  } else
268  if (fbUpdateSlotList || ForceUpdate) {
269  fxSlotList->Clear();
271  }
272  fbUpdateSlotList = kFALSE;
273  return fxSlotList;
274 }
275 
277 {
278  return GetSlotList()->GetLast()+1;
279 }
280 
282 {
283  const TObjArray* lst = GetSlotList();
284  return (nslot>=0) && (nslot<=lst->GetLast()) ? dynamic_cast<TGo4FitSlot*> (lst->At(nslot)) : 0;
285 }
286 
287 TGo4FitSlot* TGo4FitSlotList::FindSlot(const char* FullSlotName)
288 {
289  const TObjArray* lst = GetSlotList();
290  for(Int_t i=0;i<=lst->GetLast();i++) {
291  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
292  if (slot && (strcmp(slot->GetFullName(), FullSlotName)==0)) return slot;
293  }
294  return 0;
295 }
296 
298 {
299  if ((slot1==0) || (slot2==0)) return kFALSE;
300  return slot1->ConnectToSlot(slot2);
301 }
302 
303 Bool_t TGo4FitSlotList::ConnectSlots(const char* Slot1FullName, const char* Slot2FullName)
304 {
305  return ConnectSlots(FindSlot(Slot1FullName), FindSlot(Slot2FullName));
306 }
307 
308 TGo4FitSlot* TGo4FitSlotList::SetObject(TObject* obj, Bool_t iOwned)
309 {
310  if (obj==0) return 0;
311 
312  const TObjArray* lst = GetSlotList();
313 
314  for(Int_t i=0;i<=lst->GetLast();i++) {
315  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
316  if (slot==0) continue;
317 
318  if (slot->IsEmpty() && slot->IsSuitable(obj)) {
319  slot->SetObject(obj, iOwned);
320  return slot;
321  }
322  }
323  return 0;
324 }
325 
326 TGo4FitSlot* TGo4FitSlotList::SetObject(const char* PlaceName, TObject* obj, Bool_t iOwned)
327 {
328  if (obj==0) return 0;
329 
330  if (PlaceName==0) return SetObject(obj, iOwned);
331 
332  const TObjArray* lst = GetSlotList();
333 
334  TGo4FitSlot* firstempty = 0, *last = 0;
335  Int_t count = 0;
336 
337  for(Int_t i=0;i<=lst->GetLast();i++) {
338  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
339  if ((slot==0) || !slot->IsSuitable(obj)) continue;
340 
341  if (strcmp(slot->GetFullName(),PlaceName)==0) { firstempty = slot; count = 1; break; }
342 
343  if (strcmp(slot->GetOwnerFullName(), PlaceName)==0) {
344  count++;
345  last = slot;
346  if (slot->IsEmpty() && (firstempty==0)) firstempty = slot;
347  }
348  }
349 
350  if (firstempty!=0) {
351  firstempty->SetObject(obj, iOwned);
352  return firstempty;
353  } else
354  if ((count==1) && (last!=0)) {
355  last->SetObject(obj, iOwned);
356  return last;
357  }
358 
359  return 0;
360 }
361 
363 {
364  if (obj==0) return 0;
365 
366  const TObjArray* lst = GetSlotList();
367 
368  TGo4FitSlot* last = 0;
369 
370  for(Int_t i=0;i<=lst->GetLast();i++) {
371  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
372  if (slot==0) continue;
373  if (slot->GetObject()==obj) {
374  last=slot;
375  if (slot->GetOwned()) return slot;
376  }
377  }
378  return last;
379 }
380 
381 Bool_t TGo4FitSlotList::CheckObjects(Bool_t MakeOut)
382 {
383  Bool_t res = kTRUE;
384  const TObjArray* lst = GetSlotList();
385  for(Int_t i=0;i<=lst->GetLast();i++) {
386  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
387  if (slot==0) continue;
388  if (slot->IsRequired()) {
389  if (MakeOut)
390  std::cout << "Required data not provided" << std::endl <<
391  " Name: " << slot->GetName() << std::endl <<
392  " Class: " << slot->GetClass()->GetName() << std::endl <<
393  " Description: " << slot->GetTitle() << std::endl <<
394  " For object: " << slot->GetOwnerFullName() << std::endl;
395  res = kFALSE;
396  }
397  }
398  return res;
399 }
400 
402 {
403  TObjArray list;
404  const TObjArray* lst = GetSlotList();
405  for(Int_t i=0;i<=lst->GetLast();i++) {
406  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
407  if (slot==0) continue;
408  if (slot->IsEmpty()) return kTRUE;
409  }
410  return kFALSE;
411 }
412 
413 
414 void TGo4FitSlotList::ClearObjects(const char* PlaceName, Bool_t NonOwned)
415 {
416  const TObjArray* lst = GetSlotList();
417 
418  for(Int_t i=0;i<=lst->GetLast();i++) {
419  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
420  if (slot==0) continue;
421 
422  if ((PlaceName!=0) &&
423  (strcmp(slot->GetFullName(),PlaceName)!=0) &&
424  (strcmp(slot->GetOwnerFullName(), PlaceName)==0)) continue;
425 
426  ClearSlot(slot, NonOwned);
427  }
428 }
429 
430 void TGo4FitSlotList::ClearSlot(TGo4FitSlot* slot, Bool_t NonOwned)
431 {
432  if (slot==0) return;
433 
434  while(slot->GetConnectedSlot())
435  slot = slot->GetConnectedSlot();
436 
437  TObject* obj = slot->GetObject();
438  Bool_t owned = slot->GetOwned();
439  if (obj==0) return;
440  if (NonOwned || !owned) {
441  if (!owned) slot->Clear();
442  return;
443  }
444 
445  for(Int_t i=0;i<NumSlots();i++) {
446  TGo4FitSlot* sl = GetSlot(i);
447  if ((sl==0) || sl->IsConnectedToSlot() || (slot==sl)) continue;
448  if (sl->GetObject()==obj) {
449  sl->SetOwned(kTRUE);
450  slot->SetOwned(kFALSE);
451  slot->Clear();
452  return;
453  }
454  }
455  slot->Clear();
456 }
457 
458 void TGo4FitSlotList::SetSaveFlagForObjects(Int_t iSaveFlag, const char* PlaceName)
459 {
460  const TObjArray* lst = GetSlotList();
461 
462  for(Int_t i=0;i<=lst->GetLast();i++) {
463  TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
464  if (slot==0) continue;
465 
466  if ((PlaceName!=0) &&
467  (strcmp(slot->GetFullName(),PlaceName)!=0) &&
468  (strcmp(slot->GetOwnerFullName(), PlaceName)==0)) continue;
469 
470  slot->SetSaveFlag(iSaveFlag);
471  }
472 }
473 
475 {
476  const TObjArray* lst = GetSlotList();
477 
478  for(Int_t n1=lst->GetLast();n1>0;n1--) {
479  TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
480  if ((slot1==0) || !slot1->GetOwned() || slot1->IsConnectedToSlot()) continue;
481  for(Int_t n2=n1-1;n2>=0;n2--) {
482  TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
483  if ((slot2==0) || !slot2->GetOwned() || slot2->IsConnectedToSlot()) continue;
484  if ((void*)(slot1->GetObject())==(void*)(slot2->GetObject()))
485  slot2->SetOwned(kFALSE);
486  }
487  }
488 }
489 
491 {
492  const TObjArray* lst = GetSlotList();
493 
494  for(Int_t n1=0; n1<=lst->GetLast(); n1++) {
495  TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
496  if (slot1) slot1->SetSaveSettings(-1, -1);
497  }
498 
499  for(Int_t n1=0; n1<=lst->GetLast(); n1++) {
500  TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
501  if ((slot1==0) || slot1->HasSaveSettings()) continue;
502 
503  if (slot1->IsConnectedToSlot()) {
504  slot1->SetSaveSettings(kTRUE, kFALSE);
505  continue;
506  }
507 
508  TObject* saveobj = slot1->GetObject();
509 
510  if (saveobj==0) {
511  slot1->SetSaveSettings(kFALSE, kFALSE);
512  continue;
513  }
514 
515  Bool_t isobjsaved = kFALSE;
516  Bool_t isobjowned = kFALSE;
517 
518  for(Int_t n2=0; n2<=lst->GetLast(); n2++) {
519  TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
520  if ((slot2==0) || slot2->IsConnectedToSlot() ||
521  (slot2->GetObject()!=saveobj)) continue;
522 
523  if (slot2->WillBeSaved()) {
524  isobjsaved = kTRUE;
525  if (slot2->GetOwned()) {
526  if (isobjowned) slot2->SetOwned(kFALSE);
527  else isobjowned = kTRUE;
528  }
529  }
530  }
531 
532  for(Int_t n2=0; n2<=lst->GetLast(); n2++) {
533  TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
534  if ((slot2==0) || slot2->IsConnectedToSlot() ||
535  (slot2->GetObject()!=saveobj)) continue;
536 
537  if (isobjsaved)
538  if (slot2->GetOwned()) slot2->SetSaveSettings(kTRUE, kTRUE);
539  else slot2->SetSaveSettings(kTRUE, kFALSE);
540  else slot2->SetSaveSettings(kFALSE, kFALSE);
541  }
542 
543  if (isobjsaved && !isobjowned)
544  slot1->SetSaveSettings(kTRUE, kTRUE);
545  }
546 }
void SetUpdateSlotList()
void SetSaveSettings(Int_t save=-1, Int_t own=-1)
Bool_t fbOwned
0 - slot save if owned(default), 1 - saved always, 2 - saved never
Definition: TGo4FitSlot.h:232
const char * GetObjectName() const
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
TGo4FitSlot * GetSlot(Int_t nslot)
TGo4FitSlot * IsObjectInSlots(TObject *obj)
virtual ~TGo4FitSlot()
Definition: TGo4FitSlot.cxx:63
void ClearConnectionToSlot()
Definition: TGo4FitSlot.cxx:94
Bool_t IsEmptySlots()
virtual void Clear(Option_t *="")
Definition: TGo4FitSlot.h:156
virtual ~TGo4FitSlotList()
const char * GetFullName()
void ClearSlot(TGo4FitSlot *slot, Bool_t NonOwned)
void SetSaveFlag(Int_t iSaveFlag)
Definition: TGo4FitSlot.h:75
void SetOwner(TNamed *iOwner)
Definition: TGo4FitNamed.h:58
Int_t fiSaveSlot
Definition: TGo4FitSlot.h:244
Bool_t HasSaveSettings()
TClass * fxClass
Definition: TGo4FitSlot.h:221
Bool_t IsEmpty() const
Definition: TGo4FitSlot.h:183
Bool_t IsConnectedToSlot() const
Definition: TGo4FitSlot.h:129
void SetSaveFlagForObjects(Int_t iSaveFlag, const char *PlaceName=0)
Int_t fiSaveFlag
Definition: TGo4FitSlot.h:226
void PrepareSlotsForWriting()
Bool_t fbNeeded
Definition: TGo4FitSlot.h:237
Bool_t GetOwned()
Definition: TGo4FitSlot.h:161
const TObjArray * GetSlotList(Bool_t ForceUpdate=kFALSE)
Bool_t ConnectSlots(TGo4FitSlot *slot1, TGo4FitSlot *slot2)
Bool_t IsRequired() const
Definition: TGo4FitSlot.h:193
Bool_t IsSuitableClass(TClass *cl)
TGo4FitSlot * GetConnectedSlot() const
Definition: TGo4FitSlot.h:124
void CheckDuplicatesOnSlot()
void CheckOwnership()
Bool_t SetObject(TObject *iObject, Bool_t iOwned=kFALSE, Bool_t CheckClass=kTRUE)
TObject * fxObject
Definition: TGo4FitSlot.h:242
TClass * GetClass()
Definition: TGo4FitSlot.h:61
virtual void FillSlotList(TSeqCollection *lst)
Bool_t CheckObjects(Bool_t MakeOut=kTRUE)
TNamed * GetOwner()
Definition: TGo4FitNamed.h:63
Bool_t ConnectToSlot(TGo4FitSlot *slot)
Definition: TGo4FitSlot.cxx:75
TObject * CloneObject(const char *newname=0)
const char * GetOwnerFullName()
TObjArray * fxSlotList
Definition: TGo4FitSlot.h:369
void SetDefaults(TNamed *iOwner, TClass *iClass)
Definition: TGo4FitSlot.cxx:69
Bool_t WillBeSaved()
Bool_t fbUpdateSlotList
Definition: TGo4FitSlot.h:370
Bool_t IsSuitable(TObject *obj)
void ClearObject()
Int_t fiSaveOwnership
Definition: TGo4FitSlot.h:245
TObject * GetObject() const
TGo4FitSlot * FindSlot(const char *FullSlotName)
void SetOwned(Bool_t iOwned=kTRUE)
Definition: TGo4FitSlot.h:167
virtual void Print(Option_t *option) const
void ClearObjects(const char *PlaceName=0, Bool_t NonOwned=kTRUE)
Bool_t CanConnectToSlot(TGo4FitSlot *slot)
Definition: TGo4FitSlot.cxx:84