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