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