00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4FitSlot.h"
00015
00016 #include "Riostream.h"
00017 #include "TClass.h"
00018 #include "TH1.h"
00019 #include "TSeqCollection.h"
00020 #include "TObjArray.h"
00021
00022 TGo4FitSlot::TGo4FitSlot() :
00023 TGo4FitNamed(),
00024 fxClass(0),
00025 fiSaveFlag(0),
00026 fbOwned(kFALSE),
00027 fbNeeded(kFALSE),
00028 fxObject(0),
00029 fiSaveSlot(-1),
00030 fiSaveOwnership(-1)
00031 {
00032 }
00033
00034 TGo4FitSlot::TGo4FitSlot(TNamed* iOwner, TClass* iClass) :
00035 TGo4FitNamed(),
00036 fxClass(iClass),
00037 fiSaveFlag(0),
00038 fbOwned(kFALSE),
00039 fbNeeded(kFALSE),
00040 fxObject(0),
00041 fiSaveSlot(-1),
00042 fiSaveOwnership(-1)
00043 {
00044 SetOwner(iOwner);
00045 }
00046
00047 TGo4FitSlot::TGo4FitSlot(const char* iName, const char* iTitle,
00048 TNamed* iOwner, TClass* iClass,
00049 Bool_t iNeeded, TObject* iObject, Bool_t iOwned) :
00050 TGo4FitNamed(iName,iTitle, iOwner),
00051 fxClass(iClass),
00052 fiSaveFlag(0),
00053 fbOwned(iOwned),
00054 fbNeeded(iNeeded),
00055 fxObject(iObject),
00056 fiSaveSlot(-1),
00057 fiSaveOwnership(-1)
00058 {
00059 }
00060
00061 TGo4FitSlot::~TGo4FitSlot()
00062 {
00063 if (fbOwned && fxObject)
00064 delete fxObject;
00065 }
00066
00067 void TGo4FitSlot::SetDefaults(TNamed* iOwner, TClass* iClass)
00068 {
00069 SetOwner(iOwner);
00070 fxClass = iClass;
00071 }
00072
00073 Bool_t TGo4FitSlot::ConnectToSlot(TGo4FitSlot* slot)
00074 {
00075 if (!CanConnectToSlot(slot)) return kFALSE;
00076 if (fbOwned && fxObject) delete fxObject;
00077 fxObject = slot;
00078 fbOwned = kFALSE;
00079 return kTRUE;
00080 }
00081
00082 Bool_t TGo4FitSlot::CanConnectToSlot(TGo4FitSlot* slot)
00083 {
00084 if ((slot==0) || (slot==this)) return kFALSE;
00085 if (!GetClass()->InheritsFrom(slot->GetClass())) return kFALSE;
00086 TGo4FitSlot* conn = slot;
00087 while ( (conn = conn->GetConnectedSlot())!=0)
00088 if (conn == this) return kFALSE;
00089 return kTRUE;
00090 }
00091
00092 void TGo4FitSlot::ClearConnectionToSlot()
00093 {
00094 if (IsConnectedToSlot()) {
00095 fxObject = 0;
00096 fbOwned = kFALSE;
00097 }
00098 }
00099
00100 Bool_t TGo4FitSlot::IsSuitable(TObject* obj)
00101 {
00102 if (obj==0) return kFALSE;
00103 if (IsConnectedToSlot()) return GetConnectedSlot()->IsSuitable(obj);
00104 else return obj->InheritsFrom(GetClass());
00105 }
00106
00107 Bool_t TGo4FitSlot::IsSuitableClass(TClass* cl)
00108 {
00109 if (cl==0) return kFALSE;
00110 if (IsConnectedToSlot()) return GetConnectedSlot()->IsSuitableClass(cl);
00111 else return cl->InheritsFrom(GetClass());
00112 }
00113
00114 void TGo4FitSlot::ClearObject()
00115 {
00116 fxObject = 0;
00117 fbOwned = kFALSE;
00118 }
00119
00120 Bool_t TGo4FitSlot::SetObject(TObject* iObject, Bool_t iOwned, Bool_t CheckClass)
00121 {
00122 if (IsConnectedToSlot())
00123 return GetConnectedSlot()->SetObject(iObject, iOwned, CheckClass);
00124
00125 if (CheckClass && iObject)
00126 if (!iObject->InheritsFrom(GetClass())) return kFALSE;
00127
00128 if (fbOwned && fxObject) delete fxObject;
00129
00130 fxObject = iObject;
00131 fbOwned = iOwned;
00132 CheckOwnership();
00133 return kTRUE;
00134 }
00135
00136 TObject* TGo4FitSlot::GetObject() const
00137 {
00138 return IsConnectedToSlot() ? GetConnectedSlot()->GetObject() : fxObject;
00139 }
00140
00141 const char* TGo4FitSlot::GetObjectName() const
00142 {
00143 TObject* obj = GetObject();
00144 if (obj && obj->InheritsFrom(TNamed::Class())) return obj->GetName();
00145 else return 0;
00146 }
00147
00148 TObject* TGo4FitSlot::CloneObject(const char* newname)
00149 {
00150 TObject* obj = GetObject();
00151 if (obj) return obj->Clone(newname);
00152 else return 0;
00153 }
00154
00155 void TGo4FitSlot::Print(Option_t* option) const
00156 {
00157 std::cout << "Slot: " << GetName() << " for class: " << fxClass->GetName()
00158 << " needed:" << fbNeeded << " owned:" << fbOwned;
00159 if (IsConnectedToSlot()) {
00160 std::cout << " Connected to: " << std::endl << " ";
00161 GetConnectedSlot()->Print(option);
00162 } else
00163 if (fxObject) {
00164 std::cout << " object: " << GetObjectName() << std::endl;
00165 if (((strcmp(option,"**")==0) && fbOwned) || (strcmp(option,"***")==0))
00166 fxObject->Print(option);
00167 } else
00168 std::cout << " no object. " << std::endl;
00169 }
00170
00171 void TGo4FitSlot::CheckOwnership()
00172 {
00173 if(fbOwned && !IsConnectedToSlot()) {
00174 TNamed* master = dynamic_cast<TNamed*> (GetOwner());
00175 TGo4FitNamed* slave = dynamic_cast<TGo4FitNamed*> (fxObject);
00176 if (master && slave) slave->SetOwner(master);
00177 }
00178 }
00179
00180 Bool_t TGo4FitSlot::WillBeSaved()
00181 {
00182 return (fiSaveFlag==1) || ((fiSaveFlag==0) && fbOwned);
00183 }
00184
00185 void TGo4FitSlot::SetSaveSettings(Int_t save, Int_t ownership)
00186 {
00187 fiSaveSlot = save;
00188 fiSaveOwnership = ownership;
00189 }
00190
00191 Bool_t TGo4FitSlot::HasSaveSettings()
00192 {
00193 return (fiSaveSlot>=0) && (fiSaveOwnership>=0);
00194 }
00195
00196 void TGo4FitSlot::Streamer(TBuffer& b)
00197 {
00198 if (b.IsReading()) {
00199
00200 TGo4FitSlot::Class()->ReadBuffer(b, this);
00201
00202 b >> fbNeeded;
00203 b >> fbOwned;
00204 Bool_t saveflag = kFALSE;
00205 b >> saveflag;
00206
00207 if (saveflag) {
00208 b >> fxObject;
00209 if (fxObject->InheritsFrom(TH1::Class()))
00210 ((TH1*) fxObject)->SetDirectory(0);
00211 CheckOwnership();
00212 }
00213 } else {
00214
00215 TGo4FitSlot::Class()->WriteBuffer(b, this);
00216
00217 Bool_t saveflag = kFALSE;
00218 Bool_t saveown = kFALSE;
00219 if ((fiSaveSlot>=0) && (fiSaveOwnership>=0)) {
00220 saveflag = fiSaveSlot>0;
00221 saveown = fiSaveOwnership>0;
00222 } else {
00223 saveflag = (fxObject!=0) && (WillBeSaved() || IsConnectedToSlot());
00224 saveown = saveflag && GetOwned();
00225 }
00226
00227 b << fbNeeded;
00228 b << saveown;
00229 b << saveflag;
00230 if (saveflag) b << fxObject;
00231 }
00232
00233 fiSaveSlot = -1;
00234 fiSaveOwnership = -1;
00235 }
00236
00237
00238
00239 TGo4FitSlotList::TGo4FitSlotList()
00240 {
00241 fxSlotList = 0;
00242 fbUpdateSlotList = kFALSE;
00243 }
00244
00245 TGo4FitSlotList::~TGo4FitSlotList()
00246 {
00247 if (fxSlotList) delete fxSlotList;
00248 }
00249
00250 void TGo4FitSlotList::FillSlotList(TSeqCollection* lst)
00251 {
00252 if(lst==0) return;
00253 }
00254
00255 void TGo4FitSlotList::SetUpdateSlotList()
00256 {
00257 fbUpdateSlotList = kTRUE;
00258 }
00259
00260 const TObjArray* TGo4FitSlotList::GetSlotList(Bool_t ForceUpdate)
00261 {
00262 if (fxSlotList==0) {
00263 fxSlotList= new TObjArray(10);
00264 FillSlotList(fxSlotList);
00265 } else
00266 if (fbUpdateSlotList || ForceUpdate) {
00267 fxSlotList->Clear();
00268 FillSlotList(fxSlotList);
00269 }
00270 fbUpdateSlotList = kFALSE;
00271 return fxSlotList;
00272 }
00273
00274 Int_t TGo4FitSlotList::NumSlots()
00275 {
00276 return GetSlotList()->GetLast()+1;
00277 }
00278
00279 TGo4FitSlot* TGo4FitSlotList::GetSlot(Int_t nslot)
00280 {
00281 const TObjArray* lst = GetSlotList();
00282 return (nslot>=0) && (nslot<=lst->GetLast()) ? dynamic_cast<TGo4FitSlot*> (lst->At(nslot)) : 0;
00283 }
00284
00285 TGo4FitSlot* TGo4FitSlotList::FindSlot(const char* FullSlotName)
00286 {
00287 const TObjArray* lst = GetSlotList();
00288 for(Int_t i=0;i<=lst->GetLast();i++) {
00289 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00290 if (slot && (strcmp(slot->GetFullName(), FullSlotName)==0)) return slot;
00291 }
00292 return 0;
00293 }
00294
00295 Bool_t TGo4FitSlotList::ConnectSlots(TGo4FitSlot* slot1, TGo4FitSlot* slot2)
00296 {
00297 if ((slot1==0) || (slot2==0)) return kFALSE;
00298 return slot1->ConnectToSlot(slot2);
00299 }
00300
00301 Bool_t TGo4FitSlotList::ConnectSlots(const char* Slot1FullName, const char* Slot2FullName)
00302 {
00303 return ConnectSlots(FindSlot(Slot1FullName), FindSlot(Slot2FullName));
00304 }
00305
00306 TGo4FitSlot* TGo4FitSlotList::SetObject(TObject* obj, Bool_t iOwned)
00307 {
00308 if (obj==0) return 0;
00309
00310 const TObjArray* lst = GetSlotList();
00311
00312 for(Int_t i=0;i<=lst->GetLast();i++) {
00313 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00314 if (slot==0) continue;
00315
00316 if (slot->IsEmpty() && slot->IsSuitable(obj)) {
00317 slot->SetObject(obj, iOwned);
00318 return slot;
00319 }
00320 }
00321 return 0;
00322 }
00323
00324 TGo4FitSlot* TGo4FitSlotList::SetObject(const char* PlaceName, TObject* obj, Bool_t iOwned)
00325 {
00326 if (obj==0) return 0;
00327
00328 if (PlaceName==0) return SetObject(obj, iOwned);
00329
00330 const TObjArray* lst = GetSlotList();
00331
00332 TGo4FitSlot* firstempty = 0, *last = 0;
00333 Int_t count = 0;
00334
00335 for(Int_t i=0;i<=lst->GetLast();i++) {
00336 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00337 if ((slot==0) || !slot->IsSuitable(obj)) continue;
00338
00339 if (strcmp(slot->GetFullName(),PlaceName)==0) { firstempty = slot; count = 1; break; }
00340
00341 if (strcmp(slot->GetOwnerFullName(), PlaceName)==0) {
00342 count++;
00343 last = slot;
00344 if (slot->IsEmpty() && (firstempty==0)) firstempty = slot;
00345 }
00346 }
00347
00348 if (firstempty!=0) {
00349 firstempty->SetObject(obj, iOwned);
00350 return firstempty;
00351 } else
00352 if ((count==1) && (last!=0)) {
00353 last->SetObject(obj, iOwned);
00354 return last;
00355 }
00356
00357 return 0;
00358 }
00359
00360 TGo4FitSlot* TGo4FitSlotList::IsObjectInSlots(TObject* obj)
00361 {
00362 if (obj==0) return 0;
00363
00364 const TObjArray* lst = GetSlotList();
00365
00366 TGo4FitSlot* last = 0;
00367
00368 for(Int_t i=0;i<=lst->GetLast();i++) {
00369 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00370 if (slot==0) continue;
00371 if (slot->GetObject()==obj) {
00372 last=slot;
00373 if (slot->GetOwned()) return slot;
00374 }
00375 }
00376 return last;
00377 }
00378
00379 Bool_t TGo4FitSlotList::CheckObjects(Bool_t MakeOut)
00380 {
00381 Bool_t res = kTRUE;
00382 const TObjArray* lst = GetSlotList();
00383 for(Int_t i=0;i<=lst->GetLast();i++) {
00384 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00385 if (slot==0) continue;
00386 if (slot->IsRequired()) {
00387 if (MakeOut)
00388 std::cout << "Required data not provided" << std::endl <<
00389 " Name: " << slot->GetName() << std::endl <<
00390 " Class: " << slot->GetClass()->GetName() << std::endl <<
00391 " Description: " << slot->GetTitle() << std::endl <<
00392 " For object: " << slot->GetOwnerFullName() << std::endl;
00393 res = kFALSE;
00394 }
00395 }
00396 return res;
00397 }
00398
00399 Bool_t TGo4FitSlotList::IsEmptySlots()
00400 {
00401 TObjArray list;
00402 const TObjArray* lst = GetSlotList();
00403 for(Int_t i=0;i<=lst->GetLast();i++) {
00404 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00405 if (slot==0) continue;
00406 if (slot->IsEmpty()) return kTRUE;
00407 }
00408 return kFALSE;
00409 }
00410
00411
00412 void TGo4FitSlotList::ClearObjects(const char* PlaceName, Bool_t NonOwned)
00413 {
00414 const TObjArray* lst = GetSlotList();
00415
00416 for(Int_t i=0;i<=lst->GetLast();i++) {
00417 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00418 if (slot==0) continue;
00419
00420 if ((PlaceName!=0) &&
00421 (strcmp(slot->GetFullName(),PlaceName)!=0) &&
00422 (strcmp(slot->GetOwnerFullName(), PlaceName)==0)) continue;
00423
00424 ClearSlot(slot, NonOwned);
00425 }
00426 }
00427
00428 void TGo4FitSlotList::ClearSlot(TGo4FitSlot* slot, Bool_t NonOwned)
00429 {
00430 if (slot==0) return;
00431
00432 while(slot->GetConnectedSlot())
00433 slot = slot->GetConnectedSlot();
00434
00435 TObject* obj = slot->GetObject();
00436 Bool_t owned = slot->GetOwned();
00437 if (obj==0) return;
00438 if (NonOwned || !owned) {
00439 if (!owned) slot->Clear();
00440 return;
00441 }
00442
00443 for(Int_t i=0;i<NumSlots();i++) {
00444 TGo4FitSlot* sl = GetSlot(i);
00445 if ((sl==0) || sl->IsConnectedToSlot() || (slot==sl)) continue;
00446 if (sl->GetObject()==obj) {
00447 sl->SetOwned(kTRUE);
00448 slot->SetOwned(kFALSE);
00449 slot->Clear();
00450 return;
00451 }
00452 }
00453 slot->Clear();
00454 }
00455
00456 void TGo4FitSlotList::SetSaveFlagForObjects(Int_t iSaveFlag, const char* PlaceName)
00457 {
00458 const TObjArray* lst = GetSlotList();
00459
00460 for(Int_t i=0;i<=lst->GetLast();i++) {
00461 TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00462 if (slot==0) continue;
00463
00464 if ((PlaceName!=0) &&
00465 (strcmp(slot->GetFullName(),PlaceName)!=0) &&
00466 (strcmp(slot->GetOwnerFullName(), PlaceName)==0)) continue;
00467
00468 slot->SetSaveFlag(iSaveFlag);
00469 }
00470 }
00471
00472 void TGo4FitSlotList::CheckDuplicatesOnSlot()
00473 {
00474 const TObjArray* lst = GetSlotList();
00475
00476 for(Int_t n1=lst->GetLast();n1>0;n1--) {
00477 TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
00478 if ((slot1==0) || !slot1->GetOwned() || slot1->IsConnectedToSlot()) continue;
00479 for(Int_t n2=n1-1;n2>=0;n2--) {
00480 TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
00481 if ((slot2==0) || !slot2->GetOwned() || slot2->IsConnectedToSlot()) continue;
00482 if ((void*)(slot1->GetObject())==(void*)(slot2->GetObject()))
00483 slot2->SetOwned(kFALSE);
00484 }
00485 }
00486 }
00487
00488 void TGo4FitSlotList::PrepareSlotsForWriting()
00489 {
00490 const TObjArray* lst = GetSlotList();
00491
00492 for(Int_t n1=0; n1<=lst->GetLast(); n1++) {
00493 TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
00494 if (slot1) slot1->SetSaveSettings(-1, -1);
00495 }
00496
00497 for(Int_t n1=0; n1<=lst->GetLast(); n1++) {
00498 TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
00499 if ((slot1==0) || slot1->HasSaveSettings()) continue;
00500
00501 if (slot1->IsConnectedToSlot()) {
00502 slot1->SetSaveSettings(kTRUE, kFALSE);
00503 continue;
00504 }
00505
00506 TObject* saveobj = slot1->GetObject();
00507
00508 if (saveobj==0) {
00509 slot1->SetSaveSettings(kFALSE, kFALSE);
00510 continue;
00511 }
00512
00513 Bool_t isobjsaved = kFALSE;
00514 Bool_t isobjowned = kFALSE;
00515
00516 for(Int_t n2=0; n2<=lst->GetLast(); n2++) {
00517 TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
00518 if ((slot2==0) || slot2->IsConnectedToSlot() ||
00519 (slot2->GetObject()!=saveobj)) continue;
00520
00521 if (slot2->WillBeSaved()) {
00522 isobjsaved = kTRUE;
00523 if (slot2->GetOwned()) {
00524 if (isobjowned) slot2->SetOwned(kFALSE);
00525 else isobjowned = kTRUE;
00526 }
00527 }
00528 }
00529
00530 for(Int_t n2=0; n2<=lst->GetLast(); n2++) {
00531 TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
00532 if ((slot2==0) || slot2->IsConnectedToSlot() ||
00533 (slot2->GetObject()!=saveobj)) continue;
00534
00535 if (isobjsaved)
00536 if (slot2->GetOwned()) slot2->SetSaveSettings(kTRUE, kTRUE);
00537 else slot2->SetSaveSettings(kTRUE, kFALSE);
00538 else slot2->SetSaveSettings(kFALSE, kFALSE);
00539 }
00540
00541 if (isobjsaved && !isobjowned)
00542 slot1->SetSaveSettings(kTRUE, kTRUE);
00543 }
00544 }