00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4PolyCond.h"
00015
00016 #include "Riostream.h"
00017 #include "RVersion.h"
00018
00019 #include "TMath.h"
00020 #include "TROOT.h"
00021 #include "TH2.h"
00022 #include "TList.h"
00023 #include "TCutG.h"
00024
00025 #include "TGo4PolyCondPainter.h"
00026 #include "TGo4Log.h"
00027
00028 TString TGo4PolyCond::NextAvailableName()
00029 {
00030 TString res;
00031 Int_t cnt = 0;
00032 do {
00033 res.Form("CutG_%d",cnt++);
00034 } while (gROOT->GetListOfSpecials()->FindObject(res)!=0);
00035 return res;
00036 }
00037
00038
00039 TGo4PolyCond::TGo4PolyCond() :
00040 TGo4Condition(),
00041 fxCut(0)
00042 {
00043 SetDimension(2);
00044 }
00045
00046 TGo4PolyCond::TGo4PolyCond(const char* name, const char* title) :
00047 TGo4Condition(name,title),
00048 fxCut(0)
00049 {
00050 SetDimension(2);
00051 }
00052
00053 TGo4PolyCond::~TGo4PolyCond()
00054 {
00055 if(fxCut != 0)
00056 delete fxCut;
00057 }
00058
00059 Double_t TGo4PolyCond::GetXLow()
00060 {
00061 if(fxCut==0) return 0;
00062 Int_t n=fxCut->GetN();
00063 Double_t* xarr=fxCut->GetX();
00064 fxCut->SetBit(kMustCleanup,0);
00065 Int_t nxmin=TMath::LocMin(n,xarr);
00066 return (xarr[nxmin]);
00067 }
00068 Double_t TGo4PolyCond::GetXUp()
00069 {
00070 if(fxCut==0) return 0;
00071 Int_t n=fxCut->GetN();
00072 Double_t* xarr=fxCut->GetX();
00073 Int_t nxmax=TMath::LocMax(n,xarr);
00074 return (xarr[nxmax]);
00075 }
00076 Double_t TGo4PolyCond::GetYLow()
00077 {
00078 if(fxCut==0) return 0;
00079 Int_t n=fxCut->GetN();
00080 Double_t* yarr=fxCut->GetY();
00081 Int_t nymin=TMath::LocMin(n,yarr);
00082 return (yarr[nymin]);
00083 }
00084 Double_t TGo4PolyCond::GetYUp()
00085 {
00086 if(fxCut==0) return 0;
00087 Int_t n=fxCut->GetN();
00088 Double_t* yarr=fxCut->GetY();
00089 Int_t nymax=TMath::LocMax(n,yarr);
00090 return (yarr[nymax]);
00091 }
00092
00093 Bool_t TGo4PolyCond::IsPolygonType()
00094 {
00095 return kTRUE;
00096 }
00097
00098
00099 TCutG* TGo4PolyCond::GetCut(Bool_t changeowner)
00100 {
00101 TCutG* tempcut = fxCut;
00102
00103 if(changeowner) {
00104 fxCut = 0;
00105 delete fxCutHis;
00106 fxCutHis=0;
00107 }
00108 return tempcut;
00109 }
00110
00111
00112 TCutG * TGo4PolyCond::CloneCut(TGo4PolyCond * source)
00113 {
00114 TCutG * tempcut = source->GetCut(false);
00115 if(tempcut) return (TCutG *)tempcut->Clone(GetName());
00116 else return 0;
00117 }
00118
00119 void TGo4PolyCond::SetValues(TCutG * newcut)
00120 {
00121 if(newcut==0) return;
00122 if(fxCut!=0) delete fxCut;
00123
00124 fxCut = (TCutG*) newcut->Clone(NextAvailableName());
00125
00126
00127 SetLineColor(newcut->GetLineColor());
00128 SetLineWidth(newcut->GetLineWidth());
00129 SetLineStyle(newcut->GetLineStyle());
00130 SetFillColor(newcut->GetFillColor());
00131 SetFillStyle(newcut->GetFillStyle());
00132
00133 delete fxCutHis;
00134 fxCutHis=0;
00135
00136 }
00137
00138 void TGo4PolyCond::SetValuesDirect(TCutG * newcut)
00139 {
00140 if(newcut==0) return;
00141 if(fxCut!=0) delete fxCut;
00142
00143 fxCut = newcut;
00144
00145
00146 SetLineColor(newcut->GetLineColor());
00147 SetLineWidth(newcut->GetLineWidth());
00148 SetLineStyle(newcut->GetLineStyle());
00149 SetFillColor(newcut->GetFillColor());
00150 SetFillStyle(newcut->GetFillStyle());
00151
00152 delete fxCutHis;
00153 fxCutHis=0;
00154
00155 }
00156
00157
00158
00159 void TGo4PolyCond::SetValues(Double_t * x, Double_t * y, Int_t len)
00160 {
00161 if(fxCut != 0) delete fxCut;
00162 fxCut = new TCutG(NextAvailableName(), len, x, y);
00163 fxCut->SetBit(kMustCleanup);
00164
00165 delete fxCutHis;
00166 fxCutHis=0;
00167 }
00168
00169 Bool_t TGo4PolyCond::Test(Double_t x, Double_t y)
00170 {
00171 IncCounts();
00172 if((!IsEnabled()) || (fxCut == 0)){
00173 if(FixedResult()) IncTrueCounts();
00174 return FixedResult();
00175 }
00176 Bool_t outside = (fxCut->IsInside(x,y) == 0);
00177 if(outside) return IsFalse();
00178 IncTrueCounts();
00179 return IsTrue();
00180 }
00181
00182 void TGo4PolyCond::PrintCondition(Bool_t points)
00183 {
00184 TGo4Condition::PrintCondition();
00185 if(points) {
00186 if(fxCut == 0)
00187 std::cout << "No polygon specified!" << std::endl;
00188 else
00189 fxCut->Print(0);
00190 }
00191 }
00192
00193
00194
00195
00197
00198
00199
00200
00201 Bool_t TGo4PolyCond::UpdateFrom(TGo4Condition * cond, Bool_t counts)
00202 {
00203 if(!TGo4Condition::UpdateFrom(cond,counts)) return kFALSE;
00204 if(cond->InheritsFrom(TGo4PolyCond::Class()))
00205 {
00206 TCutG * temp = CloneCut((TGo4PolyCond*)cond);
00207 CleanupSpecials();
00208
00209 if(temp != 0)
00210 {
00211
00212 if(fxCut != 0) delete fxCut;
00213 fxCut = temp;
00214 delete fxCutHis;
00215 fxCutHis=0;
00216
00217 return kTRUE;
00218 }
00219 else
00220 {
00221 return kFALSE;
00222 }
00223 }
00224 else
00225 {
00226 std::cout << "Cannot update " << GetName() << " from " << cond->ClassName() << std::endl;
00227 return kFALSE;
00228 }
00229 }
00230
00231 Double_t TGo4PolyCond::GetIntegral(TH1* histo, Option_t* opt)
00232 {
00234 #if ROOT_VERSION_CODE >= ROOT_VERSION(4,0,8)
00235
00236
00237 if(fxCut)
00238 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,25,1)
00239 return (fxCut->IntegralHist(dynamic_cast<TH2*>(histo),opt));
00240 #else
00241 return (fxCut->Integral(dynamic_cast<TH2*>(histo),opt));
00242 #endif
00243 else
00244 return 0;
00245 #else
00246
00247 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00248 if(fxCutHis!=0)
00249 return (fxCutHis->Integral(opt));
00250 else
00251 return-1;
00252 #endif // 40008
00253 }
00254
00255 Double_t TGo4PolyCond::GetMean(TH1* histo, Int_t axis)
00256 {
00257 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00258 if(fxCutHis!=0)
00259 return (fxCutHis->GetMean(axis));
00260 else
00261 return-1;
00262 }
00263 Double_t TGo4PolyCond::GetRMS(TH1* histo, Int_t axis)
00264 {
00265 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00266 if(fxCutHis!=0)
00267 return (fxCutHis->GetRMS(axis));
00268 else
00269 return -1;
00270 }
00271 Double_t TGo4PolyCond::GetXMax(TH1* histo)
00272 {
00273 Double_t result=0;
00274 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00275 if(fxCutHis!=0)
00276 {
00277 TAxis* xax=fxCutHis->GetXaxis();
00278 Int_t maxbin=fxCutHis->GetMaximumBin();
00279 Int_t xmaxbin=maxbin%(fxCutHis->GetNbinsX()+2);
00280 result=xax->GetBinCenter(xmaxbin);
00281 }
00282 else
00283 {
00284 result=-1;
00285 }
00286 return result;
00287 }
00288 Double_t TGo4PolyCond::GetYMax(TH1* histo)
00289 {
00290 Double_t result=0;
00291 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00292 if(fxCutHis!=0)
00293 {
00294 TAxis* yax=fxCutHis->GetYaxis();
00295 Int_t maxbin=fxCutHis->GetMaximumBin();
00296 Int_t maxybin=maxbin/(fxCutHis->GetNbinsX()+2);
00297 result=yax->GetBinCenter(maxybin);
00298 }
00299 else
00300 {
00301 result=-1;
00302 }
00303 return result;
00304 }
00305 Double_t TGo4PolyCond::GetCMax(TH1* histo)
00306 {
00307 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00308 if(fxCutHis!=0)
00309 return(fxCutHis->GetMaximum());
00310 else
00311 return -1;
00312 }
00313
00314 void TGo4PolyCond::SetPainter(TGo4ConditionPainter* painter)
00315 {
00316 if(painter==0) return;
00317 if(painter->InheritsFrom(TGo4PolyCondPainter::Class()))
00318 {
00319 if(fxPainter) delete fxPainter;
00320 fxPainter=painter;
00321 fxPainter->SetCondition(this);
00322 }
00323 else
00324 {
00325 TGo4Log::Warn("Could not set painter of class %s for TGo4PolyCond %s",
00326 painter->ClassName(),GetName());
00327 }
00328
00329
00330 }
00331
00332
00333 TGo4ConditionPainter* TGo4PolyCond::CreatePainter()
00334 {
00335 TGo4ConditionPainter* painter=new TGo4PolyCondPainter(GetName());
00336 painter->SetCondition(this);
00337 return painter;
00338 }
00339
00340 TH2* TGo4PolyCond::CreateCutHistogram(TH1* source)
00341 {
00342 TH2* his=dynamic_cast<TH2*>(source);
00343 if(his==0) return 0;
00344 TH2* work= (TH2*) his->Clone();
00345 Int_t nx=work->GetNbinsX();
00346 Int_t ny=work->GetNbinsY();
00347 TAxis* xaxis = work->GetXaxis();
00348 TAxis* yaxis = work->GetYaxis();
00349 xaxis->SetRange(0,0);
00350 yaxis->SetRange(0,0);
00351
00352 for(Int_t i=0; i<nx;++i)
00353 {
00354 Double_t x = xaxis->GetBinCenter(i);
00355 for(Int_t j=0; j<ny;++j)
00356 {
00357 Double_t y = yaxis->GetBinCenter(j);
00358 if(fxCut && !(fxCut->IsInside(x,y)))
00359 work->SetBinContent(i,j,0);
00360 }
00361 }
00362
00363 Stat_t s[11]={0};
00364 work->PutStats(s);
00365 work->GetStats(s);
00366 work->PutStats(s);
00367 work->SetDirectory(0);
00368 return work;
00369 }
00370
00371
00372 void TGo4PolyCond::CleanupSpecials()
00373 {
00374 TSeqCollection* specials=gROOT->GetListOfSpecials();
00375 TIter iter(specials);
00376 TObject* ob=0;
00377 while((ob = iter())!=0) {
00378 if(ob->InheritsFrom(TCutG::Class())) {
00379 specials->Remove(ob);
00380
00381 }
00382 }
00383 }
00384
00385 Int_t TGo4PolyCond::GetMemorySize()
00386 {
00387 Int_t size = sizeof(*this);
00388 if (GetName()!=0) size+=strlen(GetName());
00389 if (GetTitle()!=0) size+=strlen(GetTitle());
00390 if (fxCut!=0) {
00391 size += sizeof(*fxCut);
00392 #if ROOT_VERSION_CODE > ROOT_VERSION(4,0,8)
00393 size += fxCut->GetMaxSize()*2*sizeof(Double_t);
00394 #else
00395 size += fxCut->GetN()*2*sizeof(Double_t);
00396 #endif
00397 }
00398 return size;
00399 }
00400
00401 void TGo4PolyCond::SavePrimitive(std::ostream& out, Option_t* opt)
00402 {
00403 static int cnt = 0;
00404 TString line, varname = MakeScript(out, Form("polycond%d", cnt++), opt);
00405
00406 if ((fxCut==0) || (fxCut->GetN()==0))
00407 line.Form(" %s->SetValues(0, 0, 0);", varname.Data());
00408 else {
00409 TString xname = varname;
00410 xname.ReplaceAll("->At(","_sub");
00411 xname.ReplaceAll(")","");
00412 TString yname = xname + "_y";
00413 xname = xname + "_x";
00414 line.Form(" Double_t %s[%d], %s[%d];", xname.Data(), fxCut->GetN(), yname.Data(), fxCut->GetN());
00415 out << line << std::endl;
00416 for (Int_t n=0;n<fxCut->GetN();n++) {
00417 Double_t x,y;
00418 fxCut->GetPoint(n, x, y);
00419 line.Form(" %s[%d] = %f; %s[%d] = %f;", xname.Data(), n, x, yname.Data(), n, y);
00420 out << line << std::endl;
00421 }
00422 line.Form(" %s->SetValues(%s, %s, %d);", varname.Data(), xname.Data(), yname.Data(), fxCut->GetN());
00423 }
00424
00425 out << line << std::endl;
00426 }