TGo4FitSlot.cxx

Go to the documentation of this file.
00001 // $Id: TGo4FitSlot.cxx 578 2010-02-17 09:35:37Z linev $
00002 //-----------------------------------------------------------------------
00003 //       The GSI Online Offline Object Oriented (Go4) Project
00004 //         Experiment Data Processing at EE department, GSI
00005 //-----------------------------------------------------------------------
00006 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
00007 //                     Planckstr. 1, 64291 Darmstadt, Germany
00008 // Contact:            http://go4.gsi.de
00009 //-----------------------------------------------------------------------
00010 // This software can be used under the license agreements as stated
00011 // in Go4License.txt file which is part of the distribution.
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 Bool_t TGo4FitSlot::SetObject(TObject* iObject, Bool_t iOwned, Bool_t CheckClass)
00115 {
00116    if (IsConnectedToSlot())
00117      return GetConnectedSlot()->SetObject(iObject, iOwned, CheckClass);
00118 
00119    if (CheckClass && iObject)
00120      if (!iObject->InheritsFrom(GetClass())) return kFALSE;
00121 
00122    if (fbOwned && fxObject) delete fxObject;
00123 
00124    fxObject = iObject;
00125    fbOwned = iOwned;
00126    CheckOwnership();
00127    return kTRUE;
00128 }
00129 
00130 TObject* TGo4FitSlot::GetObject() const
00131 {
00132    return IsConnectedToSlot() ? GetConnectedSlot()->GetObject() : fxObject;
00133 }
00134 
00135 const char* TGo4FitSlot::GetObjectName() const
00136 {
00137    TObject* obj = GetObject();
00138    if (obj && obj->InheritsFrom(TNamed::Class())) return obj->GetName();
00139                                                   else return 0;
00140 }
00141 
00142 TObject* TGo4FitSlot::CloneObject(const char* newname)
00143 {
00144    TObject* obj = GetObject();
00145    if (obj) return obj->Clone(newname);
00146        else return 0;
00147 }
00148 
00149 void TGo4FitSlot::Print(Option_t* option) const
00150 {
00151    cout << "Slot: " << GetName() << " for class: " << fxClass->GetName()
00152         << "  needed:" << fbNeeded << "  owned:" << fbOwned;
00153    if (IsConnectedToSlot()) {
00154      cout << "  Connected to: " << endl << "  ";
00155      GetConnectedSlot()->Print(option);
00156    } else
00157    if (fxObject) {
00158      cout << " object: " << GetObjectName() << endl;
00159      if (((strcmp(option,"**")==0) && fbOwned) || (strcmp(option,"***")==0))
00160          fxObject->Print(option);
00161    } else cout << " no object. " << endl;
00162 }
00163 
00164 void TGo4FitSlot::CheckOwnership()
00165 {
00166   if(fbOwned && !IsConnectedToSlot()) {
00167      TNamed* master = dynamic_cast<TNamed*> (GetOwner());
00168      TGo4FitNamed* slave = dynamic_cast<TGo4FitNamed*> (fxObject);
00169      if (master && slave) slave->SetOwner(master);
00170   }
00171 }
00172 
00173 Bool_t TGo4FitSlot::WillBeSaved()
00174 {
00175    return (fiSaveFlag==1) || ((fiSaveFlag==0) && fbOwned);
00176 }
00177 
00178 void TGo4FitSlot::SetSaveSettings(Int_t save, Int_t ownership)
00179 {
00180    fiSaveSlot = save;
00181    fiSaveOwnership = ownership;
00182 }
00183 
00184 Bool_t TGo4FitSlot::HasSaveSettings()
00185 {
00186   return (fiSaveSlot>=0) && (fiSaveOwnership>=0);
00187 }
00188 
00189 void TGo4FitSlot::Streamer(TBuffer& b)
00190 {
00191    if (b.IsReading()) {
00192 
00193      TGo4FitSlot::Class()->ReadBuffer(b, this);
00194 
00195      b >> fbNeeded;
00196      b >> fbOwned;
00197      Bool_t saveflag = kFALSE;
00198      b >> saveflag;
00199 
00200      if (saveflag) {
00201        b >> fxObject;
00202        if (fxObject->InheritsFrom(TH1::Class()))
00203           ((TH1*) fxObject)->SetDirectory(0);
00204        CheckOwnership();
00205      }
00206    } else {
00207 
00208      TGo4FitSlot::Class()->WriteBuffer(b, this);
00209 
00210      Bool_t saveflag = kFALSE;
00211      Bool_t saveown = kFALSE;
00212      if ((fiSaveSlot>=0) && (fiSaveOwnership>=0)) {
00213         saveflag = fiSaveSlot>0;
00214         saveown = fiSaveOwnership>0;
00215      } else {
00216         saveflag = (fxObject!=0) && (WillBeSaved() || IsConnectedToSlot());
00217         saveown = saveflag && GetOwned();
00218      }
00219 
00220      b << fbNeeded;
00221      b << saveown;
00222      b << saveflag;
00223      if (saveflag) b << fxObject;
00224    }
00225 
00226    fiSaveSlot = -1;
00227    fiSaveOwnership = -1;
00228 }
00229 
00230 //********************************************************************************
00231 
00232 TGo4FitSlotList::TGo4FitSlotList()
00233 {
00234    fxSlotList = 0;
00235    fbUpdateSlotList = kFALSE;
00236 }
00237 
00238 TGo4FitSlotList::~TGo4FitSlotList()
00239 {
00240    if (fxSlotList) delete fxSlotList;
00241 }
00242 
00243 void TGo4FitSlotList::FillSlotList(TSeqCollection* lst)
00244 {
00245    if(lst==0) return;
00246 }
00247 
00248 void TGo4FitSlotList::SetUpdateSlotList()
00249 {
00250    fbUpdateSlotList = kTRUE;
00251 }
00252 
00253 const TObjArray* TGo4FitSlotList::GetSlotList(Bool_t ForceUpdate)
00254 {
00255    if (fxSlotList==0) {
00256      fxSlotList= new TObjArray(10);
00257      FillSlotList(fxSlotList);
00258    } else
00259    if (fbUpdateSlotList || ForceUpdate) {
00260      fxSlotList->Clear();
00261      FillSlotList(fxSlotList);
00262    }
00263    fbUpdateSlotList = kFALSE;
00264    return fxSlotList;
00265 }
00266 
00267 Int_t TGo4FitSlotList::NumSlots()
00268 {
00269   return GetSlotList()->GetLast()+1;
00270 }
00271 
00272 TGo4FitSlot* TGo4FitSlotList::GetSlot(Int_t nslot)
00273 {
00274    const TObjArray* lst = GetSlotList();
00275    return (nslot>=0) && (nslot<=lst->GetLast()) ? dynamic_cast<TGo4FitSlot*> (lst->At(nslot)) : 0;
00276 }
00277 
00278 TGo4FitSlot* TGo4FitSlotList::FindSlot(const char* FullSlotName)
00279 {
00280    const TObjArray* lst = GetSlotList();
00281    for(Int_t i=0;i<=lst->GetLast();i++) {
00282       TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00283       if (slot && (strcmp(slot->GetFullName(), FullSlotName)==0)) return slot;
00284    }
00285    return 0;
00286 }
00287 
00288 Bool_t TGo4FitSlotList::ConnectSlots(TGo4FitSlot* slot1, TGo4FitSlot* slot2)
00289 {
00290   if ((slot1==0) || (slot2==0)) return kFALSE;
00291   return slot1->ConnectToSlot(slot2);
00292 }
00293 
00294 Bool_t TGo4FitSlotList::ConnectSlots(const char* Slot1FullName, const char* Slot2FullName)
00295 {
00296    return ConnectSlots(FindSlot(Slot1FullName), FindSlot(Slot2FullName));
00297 }
00298 
00299 TGo4FitSlot* TGo4FitSlotList::SetObject(TObject* obj, Bool_t iOwned)
00300 {
00301    if (obj==0) return 0;
00302 
00303    const TObjArray* lst = GetSlotList();
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 
00309       if (slot->IsEmpty() && slot->IsSuitable(obj)) {
00310         slot->SetObject(obj, iOwned);
00311         return slot;
00312       }
00313    }
00314    return 0;
00315 }
00316 
00317 TGo4FitSlot* TGo4FitSlotList::SetObject(const char* PlaceName, TObject* obj, Bool_t iOwned)
00318 {
00319    if (obj==0) return 0;
00320 
00321    if (PlaceName==0) return SetObject(obj, iOwned);
00322 
00323    const TObjArray* lst = GetSlotList();
00324 
00325    TGo4FitSlot* firstempty = 0, *last = 0;
00326    Int_t count = 0;
00327 
00328    for(Int_t i=0;i<=lst->GetLast();i++) {
00329       TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00330       if ((slot==0) || !slot->IsSuitable(obj)) continue;
00331 
00332       if (strcmp(slot->GetFullName(),PlaceName)==0) { firstempty = slot; count = 1; break; }
00333 
00334       if (strcmp(slot->GetOwnerFullName(), PlaceName)==0) {
00335           count++;
00336           last = slot;
00337           if (slot->IsEmpty() && (firstempty==0)) firstempty = slot;
00338         }
00339    }
00340 
00341    if (firstempty!=0) {
00342       firstempty->SetObject(obj, iOwned);
00343       return firstempty;
00344    } else
00345    if ((count==1) && (last!=0)) {
00346       last->SetObject(obj, iOwned);
00347       return last;
00348    }
00349 
00350    return 0;
00351 }
00352 
00353 TGo4FitSlot* TGo4FitSlotList::IsObjectInSlots(TObject* obj)
00354 {
00355    if (obj==0) return 0;
00356 
00357    const TObjArray* lst = GetSlotList();
00358 
00359    TGo4FitSlot* last = 0;
00360 
00361    for(Int_t i=0;i<=lst->GetLast();i++) {
00362       TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00363       if (slot==0) continue;
00364       if (slot->GetObject()==obj) {
00365         last=slot;
00366         if (slot->GetOwned()) return slot;
00367       }
00368    }
00369    return last;
00370 }
00371 
00372 Bool_t TGo4FitSlotList::CheckObjects(Bool_t MakeOut)
00373 {
00374    Bool_t res = kTRUE;
00375    const TObjArray* lst = GetSlotList();
00376    for(Int_t i=0;i<=lst->GetLast();i++) {
00377       TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00378       if (slot==0) continue;
00379       if (slot->IsRequired()) {
00380          if (MakeOut)
00381            cout << "Required data not provided" << endl <<
00382                    "   Name: " << slot->GetName() << endl <<
00383                    "   Class: " << slot->GetClass()->GetName() << endl <<
00384                    "   Description: " << slot->GetTitle() << endl <<
00385                    "   For object: " << slot->GetOwnerFullName() << endl;
00386          res = kFALSE;
00387        }
00388    }
00389    return res;
00390 }
00391 
00392 Bool_t TGo4FitSlotList::IsEmptySlots()
00393 {
00394    TObjArray list;
00395    const TObjArray* lst = GetSlotList();
00396    for(Int_t i=0;i<=lst->GetLast();i++) {
00397       TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00398       if (slot==0) continue;
00399       if (slot->IsEmpty()) return kTRUE;
00400    }
00401    return kFALSE;
00402 }
00403 
00404 
00405 void TGo4FitSlotList::ClearObjects(const char* PlaceName, Bool_t NonOwned)
00406 {
00407    const TObjArray* lst = GetSlotList();
00408 
00409    for(Int_t i=0;i<=lst->GetLast();i++) {
00410       TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00411       if (slot==0) continue;
00412 
00413       if ((PlaceName!=0) &&
00414          (strcmp(slot->GetFullName(),PlaceName)!=0) &&
00415          (strcmp(slot->GetOwnerFullName(), PlaceName)==0)) continue;
00416 
00417       ClearSlot(slot, NonOwned);
00418    }
00419 }
00420 
00421 void TGo4FitSlotList::ClearSlot(TGo4FitSlot* slot, Bool_t NonOwned)
00422 {
00423    if (slot==0) return;
00424 
00425    while(slot->GetConnectedSlot())
00426      slot = slot->GetConnectedSlot();
00427 
00428    TObject* obj = slot->GetObject();
00429    Bool_t owned = slot->GetOwned();
00430    if (obj==0) return;
00431    if (NonOwned || !owned) {
00432       if (!owned) slot->Clear();
00433       return;
00434    }
00435 
00436    for(Int_t i=0;i<NumSlots();i++) {
00437       TGo4FitSlot* sl = GetSlot(i);
00438       if ((sl==0) || sl->IsConnectedToSlot() || (slot==sl)) continue;
00439       if (sl->GetObject()==obj) {
00440          sl->SetOwned(kTRUE);
00441          slot->SetOwned(kFALSE);
00442          slot->Clear();
00443          return;
00444       }
00445    }
00446    slot->Clear();
00447 }
00448 
00449 void TGo4FitSlotList::SetSaveFlagForObjects(Int_t iSaveFlag, const char* PlaceName)
00450 {
00451    const TObjArray* lst = GetSlotList();
00452 
00453    for(Int_t i=0;i<=lst->GetLast();i++) {
00454       TGo4FitSlot* slot = dynamic_cast<TGo4FitSlot*> (lst->At(i));
00455       if (slot==0) continue;
00456 
00457       if ((PlaceName!=0) &&
00458          (strcmp(slot->GetFullName(),PlaceName)!=0) &&
00459          (strcmp(slot->GetOwnerFullName(), PlaceName)==0)) continue;
00460 
00461       slot->SetSaveFlag(iSaveFlag);
00462    }
00463 }
00464 
00465 void TGo4FitSlotList::CheckDuplicatesOnSlot()
00466 {
00467    const TObjArray* lst = GetSlotList();
00468 
00469    for(Int_t n1=lst->GetLast();n1>0;n1--) {
00470       TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
00471       if ((slot1==0) || !slot1->GetOwned() || slot1->IsConnectedToSlot()) continue;
00472       for(Int_t n2=n1-1;n2>=0;n2--) {
00473          TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
00474          if ((slot2==0) || !slot2->GetOwned() || slot2->IsConnectedToSlot()) continue;
00475          if ((void*)(slot1->GetObject())==(void*)(slot2->GetObject()))
00476            slot2->SetOwned(kFALSE);
00477       }
00478    }
00479 }
00480 
00481 void TGo4FitSlotList::PrepareSlotsForWriting()
00482 {
00483    const TObjArray* lst = GetSlotList();
00484 
00485    for(Int_t n1=0; n1<=lst->GetLast(); n1++) {
00486      TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
00487      if (slot1) slot1->SetSaveSettings(-1, -1);
00488    }
00489 
00490    for(Int_t n1=0; n1<=lst->GetLast(); n1++) {
00491        TGo4FitSlot* slot1 = dynamic_cast<TGo4FitSlot*> (lst->At(n1));
00492        if ((slot1==0) || slot1->HasSaveSettings()) continue;
00493 
00494        if (slot1->IsConnectedToSlot()) {
00495          slot1->SetSaveSettings(kTRUE, kFALSE);
00496          continue;
00497        }
00498 
00499        TObject* saveobj = slot1->GetObject();
00500 
00501        if (saveobj==0) {
00502          slot1->SetSaveSettings(kFALSE, kFALSE);
00503          continue;
00504        }
00505 
00506        Bool_t isobjsaved = kFALSE;
00507        Bool_t isobjowned = kFALSE;
00508 
00509        for(Int_t n2=0; n2<=lst->GetLast(); n2++) {
00510          TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
00511          if ((slot2==0) || slot2->IsConnectedToSlot() ||
00512              (slot2->GetObject()!=saveobj)) continue;
00513 
00514          if (slot2->WillBeSaved()) {
00515            isobjsaved = kTRUE;
00516            if (slot2->GetOwned()) {
00517              if (isobjowned) slot2->SetOwned(kFALSE);
00518                         else isobjowned = kTRUE;
00519            }
00520          }
00521        }
00522 
00523        for(Int_t n2=0; n2<=lst->GetLast(); n2++) {
00524          TGo4FitSlot* slot2 = dynamic_cast<TGo4FitSlot*> (lst->At(n2));
00525          if ((slot2==0) || slot2->IsConnectedToSlot() ||
00526              (slot2->GetObject()!=saveobj)) continue;
00527 
00528          if (isobjsaved)
00529            if (slot2->GetOwned()) slot2->SetSaveSettings(kTRUE, kTRUE);
00530                              else slot2->SetSaveSettings(kTRUE, kFALSE);
00531            else slot2->SetSaveSettings(kFALSE, kFALSE);
00532        }
00533 
00534        if (isobjsaved && !isobjowned)
00535          slot1->SetSaveSettings(kTRUE, kTRUE);
00536    }
00537 }

Generated on Thu Oct 28 15:54:12 2010 for Go4-Fitpackagev4.04-2 by  doxygen 1.5.1