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