Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4PolyCond.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4PolyCond.h"
00017 
00018 #include "Riostream.h"
00019 #include "RVersion.h"
00020 
00021 #include "TMath.h"
00022 #include "TROOT.h"
00023 #include "TH2.h"
00024 #include "TList.h"
00025 #include "TCutG.h"
00026 
00027 #include "TGo4PolyCondPainter.h"
00028 #include "TGo4Log.h"
00029 
00030 TString TGo4PolyCond::NextAvailableName()
00031 {
00032    TString res;
00033    Int_t cnt = 0;
00034    do {
00035       res.Form("CutG_%d",cnt++);
00036    } while (gROOT->GetListOfSpecials()->FindObject(res)!=0);
00037    return res;
00038 }
00039 
00040 // ----------------------------------------------------------
00041 TGo4PolyCond::TGo4PolyCond() :
00042    TGo4Condition(),
00043    fxCut(0)
00044 {
00045    SetDimension(2);
00046 }
00047 // ----------------------------------------------------------
00048 TGo4PolyCond::TGo4PolyCond(const char* name, const char* title) :
00049    TGo4Condition(name,title),
00050    fxCut(0)
00051 {
00052    SetDimension(2);
00053 }
00054 // ----------------------------------------------------------
00055 TGo4PolyCond::~TGo4PolyCond()
00056 {
00057    if(fxCut != 0)
00058        delete fxCut;
00059 }
00060 
00061 Double_t TGo4PolyCond::GetXLow()
00062 {
00063    if(fxCut==0) return 0;
00064    Int_t n=fxCut->GetN();
00065    Double_t* xarr=fxCut->GetX();
00066    fxCut->SetBit(kMustCleanup,0);
00067    Int_t nxmin=TMath::LocMin(n,xarr);
00068    return (xarr[nxmin]);
00069 }
00070 Double_t TGo4PolyCond::GetXUp()
00071 {
00072    if(fxCut==0) return 0;
00073    Int_t n=fxCut->GetN();
00074    Double_t* xarr=fxCut->GetX();
00075    Int_t nxmax=TMath::LocMax(n,xarr);
00076    return (xarr[nxmax]);
00077 }
00078 Double_t TGo4PolyCond::GetYLow()
00079 {
00080    if(fxCut==0) return 0;
00081    Int_t n=fxCut->GetN();
00082    Double_t* yarr=fxCut->GetY();
00083    Int_t nymin=TMath::LocMin(n,yarr);
00084    return (yarr[nymin]);
00085 }
00086 Double_t TGo4PolyCond::GetYUp()
00087 {
00088    if(fxCut==0) return 0;
00089    Int_t n=fxCut->GetN();
00090    Double_t* yarr=fxCut->GetY();
00091    Int_t nymax=TMath::LocMax(n,yarr);
00092    return (yarr[nymax]);
00093 }
00094 
00095 Bool_t TGo4PolyCond::IsPolygonType()
00096 {
00097    return kTRUE;
00098 }
00099 
00100 // ----------------------------------------------------------
00101 TCutG* TGo4PolyCond::GetCut(Bool_t changeowner)
00102 {
00103    TCutG* tempcut = fxCut;
00104 
00105    if(changeowner) {
00106       fxCut = 0;
00107       delete fxCutHis;
00108       fxCutHis=0;
00109    }
00110    return tempcut;
00111 }
00112 
00113 // ----------------------------------------------------------
00114 TCutG * TGo4PolyCond::CloneCut(TGo4PolyCond * source)
00115 {
00116   TCutG * tempcut = source->GetCut(false); // get fxCut pointer
00117   if(tempcut) return (TCutG *)tempcut->Clone(GetName());
00118          else return 0;
00119 }
00120 // ----------------------------------------------------------
00121 void TGo4PolyCond::SetValues(TCutG * newcut)
00122 {
00123    if(newcut==0) return;
00124    if(fxCut!=0) delete fxCut;
00125 
00126    fxCut = (TCutG*) newcut->Clone(NextAvailableName());
00127 
00128    // when updated from view, we store graphical attributes:
00129    SetLineColor(newcut->GetLineColor());
00130    SetLineWidth(newcut->GetLineWidth());
00131    SetLineStyle(newcut->GetLineStyle());
00132    SetFillColor(newcut->GetFillColor());
00133    SetFillStyle(newcut->GetFillStyle());
00134 
00135    delete fxCutHis; // fxCut changed, so discard previous fxCut histogram
00136    fxCutHis=0;
00137 //cout << "Set fxCut " << fxCut << " from " << newcut << endl;
00138 }
00139 // ----------------------------------------------------------
00140 void TGo4PolyCond::SetValuesDirect(TCutG * newcut)
00141 {
00142    if(newcut==0) return;
00143    if(fxCut!=0) delete fxCut;
00144 
00145    fxCut = newcut;
00146 
00147    // when updated from view, we store graphical attributes:
00148    SetLineColor(newcut->GetLineColor());
00149    SetLineWidth(newcut->GetLineWidth());
00150    SetLineStyle(newcut->GetLineStyle());
00151    SetFillColor(newcut->GetFillColor());
00152    SetFillStyle(newcut->GetFillStyle());
00153 
00154    delete fxCutHis; // fxCut changed, so discard previous fxCut histogram
00155    fxCutHis=0;
00156 //cout << "Set fxCut " << fxCut << " from " << newcut << endl;
00157 }
00158 
00159 
00160 // ----------------------------------------------------------
00161 void TGo4PolyCond::SetValues(Double_t * x, Double_t * y, Int_t len)
00162 {
00163    if(fxCut != 0) delete fxCut;
00164    fxCut = new TCutG(NextAvailableName(), len, x, y);
00165    fxCut->SetBit(kMustCleanup);
00166    //cout << "Set new fxCut " << fxCut << endl;
00167    delete fxCutHis; // discard previous fxCut histogram
00168    fxCutHis=0;
00169 }
00170 // ----------------------------------------------------------
00171 Bool_t TGo4PolyCond::Test(Double_t x, Double_t y)
00172 {
00173    IncCounts();
00174    if((!IsEnabled()) || (fxCut == 0)){
00175       if(FixedResult()) IncTrueCounts();
00176       return FixedResult();
00177    }
00178    Bool_t outside = (fxCut->IsInside(x,y) == 0);
00179    if(outside) return IsFalse();
00180    IncTrueCounts();
00181    return IsTrue();
00182 }
00183 // ----------------------------------------------------------
00184 void TGo4PolyCond::PrintCondition(Bool_t points){
00185 TGo4Condition::PrintCondition();
00186 //cout << "fxCut " << fxCut << endl;
00187 if(points){
00188    if(fxCut == 0){
00189 //cout << "Cut " << fxCut->GetName() << endl;
00190 cout << "No polygon specified!" << endl;
00191    }
00192 else fxCut->Print(0);
00193 }}
00194 // -----------------------------------------------
00195 // PrintBar switch is handled in condition baseclass now.
00196 //void TGo4PolyCond::Print(Option_t* opt) const{
00198 //   TGo4PolyCond* const localthis= const_cast<TGo4PolyCond* const>(this);
00199 //   localthis->PrintBar();
00200 //}
00201 // ----------------------------------------------------------
00202 Bool_t TGo4PolyCond::UpdateFrom(TGo4Condition * cond, Bool_t counts)
00203 {
00204   if(!TGo4Condition::UpdateFrom(cond,counts)) return kFALSE;
00205   if(cond->InheritsFrom(TGo4PolyCond::Class()))
00206      {
00207        TCutG * temp = CloneCut((TGo4PolyCond*)cond);  // get clone from source, still valid there!
00208        CleanupSpecials(); // remove all references to cloned TCutG from list of specials
00209        //cout << "Update " << GetName() << " from " << temp << endl;
00210        if(temp != 0)
00211           {
00212              //cout << "Delete fxCut " << fxCut << endl;
00213              if(fxCut != 0) delete fxCut;
00214              fxCut = temp;
00215              delete fxCutHis;
00216              fxCutHis=0;
00217              //cout << "Update new fxCut " << fxCut << endl;
00218              return kTRUE;
00219           }
00220        else
00221           {
00222              return kFALSE;
00223           }
00224     }
00225  else
00226    {
00227       cout << "Cannot update " << GetName() << " from " << cond->ClassName() << endl;
00228       return kFALSE;
00229    }
00230 }
00231 
00232 Double_t TGo4PolyCond::GetIntegral(TH1* histo, Option_t* opt)
00233 {
00235 #if ROOT_VERSION_CODE >= ROOT_VERSION(4,0,8)
00236 //#if __GO4ROOTVERSION__ >= 40008
00237    //cout <<"Integral with new root" << endl;
00238    if(fxCut)
00239       return (fxCut->Integral(dynamic_cast<TH2*>(histo),opt));
00240    else
00241       return 0;
00242 #else
00243    //cout <<"Integral with old root" << endl;
00244    if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00245    if(fxCutHis!=0)
00246       return (fxCutHis->Integral(opt));
00247    else
00248       return-1;
00249 #endif // 40008
00250 }
00251 
00252 Double_t TGo4PolyCond::GetMean(TH1* histo, Int_t axis)
00253 {
00254    if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00255    if(fxCutHis!=0)
00256       return (fxCutHis->GetMean(axis));
00257    else
00258       return-1;
00259 }
00260 Double_t TGo4PolyCond::GetRMS(TH1* histo, Int_t axis)
00261 {
00262    if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00263    if(fxCutHis!=0)
00264       return (fxCutHis->GetRMS(axis));
00265    else
00266       return -1;
00267 }
00268 Double_t TGo4PolyCond::GetXMax(TH1* histo)
00269 {
00270    Double_t result=0;
00271    if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00272    if(fxCutHis!=0)
00273       {
00274           TAxis* xax=fxCutHis->GetXaxis();
00275           Int_t maxbin=fxCutHis->GetMaximumBin();
00276           Int_t xmaxbin=maxbin%(fxCutHis->GetNbinsX()+2);
00277           result=xax->GetBinCenter(xmaxbin);
00278       }
00279    else
00280       {
00281          result=-1;
00282       }
00283    return result;
00284 }
00285 Double_t TGo4PolyCond::GetYMax(TH1* histo)
00286 {
00287    Double_t result=0;
00288    if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00289    if(fxCutHis!=0)
00290       {
00291          TAxis* yax=fxCutHis->GetYaxis();
00292          Int_t maxbin=fxCutHis->GetMaximumBin();
00293          Int_t maxybin=maxbin/(fxCutHis->GetNbinsX()+2);
00294          result=yax->GetBinCenter(maxybin);
00295       }
00296    else
00297       {
00298          result=-1;
00299       }
00300    return result;
00301 }
00302 Double_t TGo4PolyCond::GetCMax(TH1* histo)
00303 {
00304    if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00305    if(fxCutHis!=0)
00306       return(fxCutHis->GetMaximum());
00307    else
00308       return -1;
00309 }
00310 
00311 void TGo4PolyCond::SetPainter(TGo4ConditionPainter* painter)
00312 {
00313 if(painter==0) return;
00314 if(painter->InheritsFrom(TGo4PolyCondPainter::Class()))
00315    {
00316       if(fxPainter) delete fxPainter;
00317       fxPainter=painter;
00318       fxPainter->SetCondition(this);
00319    }
00320 else
00321    {
00322       TGo4Log::Warn("Could not set painter of class %s for TGo4PolyCond %s",
00323          painter->ClassName(),GetName());
00324    }
00325 
00326 
00327 }
00328 
00329 
00330 TGo4ConditionPainter* TGo4PolyCond::CreatePainter()
00331 {
00332    TGo4ConditionPainter* painter=new TGo4PolyCondPainter(GetName());
00333    painter->SetCondition(this);
00334    return painter;
00335 }
00336 
00337 TH2* TGo4PolyCond::CreateCutHistogram(TH1* source)
00338 {
00339    TH2* his=dynamic_cast<TH2*>(source);
00340    if(his==0) return 0;
00341    TH2* work= (TH2*) his->Clone();
00342    Int_t nx=work->GetNbinsX();
00343    Int_t ny=work->GetNbinsY();
00344    TAxis* xaxis = work->GetXaxis();
00345    TAxis* yaxis = work->GetYaxis();
00346    xaxis->SetRange(0,0); // expand work histogram to full range
00347    yaxis->SetRange(0,0);
00348    // set all bins outside fxCut to zero:
00349    for(Int_t i=0; i<nx;++i)
00350       {
00351        Double_t x = xaxis->GetBinCenter(i);
00352        for(Int_t j=0; j<ny;++j)
00353          {
00354             Double_t y = yaxis->GetBinCenter(j);
00355             if(fxCut && !(fxCut->IsInside(x,y)))
00356                work->SetBinContent(i,j,0);
00357          }
00358       }
00359    // prepare statistics:
00360    Stat_t s[11]={0}; // dimension is kNstat of TH1.cxx
00361    work->PutStats(s); // reset previous stats
00362    work->GetStats(s); // recalculate
00363    work->PutStats(s); // put back
00364    work->SetDirectory(0); // important for first draw from marker setup file!
00365    return work;
00366 }
00367 
00368 // ----------------------------------------------------------
00369 void TGo4PolyCond::CleanupSpecials()
00370 {
00371    TSeqCollection* specials=gROOT->GetListOfSpecials();
00372    TIter iter(specials);
00373    TObject* ob=0;
00374    while((ob = iter())!=0) {
00375      if(ob->InheritsFrom(TCutG::Class())) {
00376         specials->Remove(ob);
00377         //cout <<">>>>>>>>>> removed fxCut" <<ob<<" :" << ob->GetName() <<" from list of specials "<< endl;
00378      }
00379    }//while
00380 }
00381 
00382 Int_t TGo4PolyCond::GetMemorySize()
00383 {
00384    Int_t size = sizeof(*this);
00385    if (GetName()!=0) size+=strlen(GetName());
00386    if (GetTitle()!=0) size+=strlen(GetTitle());
00387    if (fxCut!=0) {
00388       size += sizeof(*fxCut);
00389 #if ROOT_VERSION_CODE > ROOT_VERSION(4,0,8)
00390       size += fxCut->GetMaxSize()*2*sizeof(Double_t);
00391 #else
00392       size += fxCut->GetN()*2*sizeof(Double_t);
00393 #endif
00394    }
00395    return size;
00396 }
00397 
00398 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:06 2008 for Go4-v3.04-1 by  doxygen 1.4.2