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