00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4PolyCond.h"
00017
00018 #include <iostream.h>
00019
00020 #include "TH2.h"
00021
00022 #include "TGo4PolyCondPainter.h"
00023 #include "Go4Log/TGo4Log.h"
00024
00025
00026
00027 TGo4PolyCond::TGo4PolyCond(const Text_t * name, const Text_t * title)
00028 : TGo4Condition(name,title)
00029 {
00030 cut = 0;
00031 fiPoints = 0;
00032 SetDimension(2);
00033 }
00034
00035 TGo4PolyCond::TGo4PolyCond(const Text_t * name)
00036 : TGo4Condition(name,"Go4 polygon condition")
00037 {
00038 cut = 0;
00039 fiPoints = 0;
00040 SetDimension(2);
00041 }
00042
00043 TGo4PolyCond::TGo4PolyCond()
00044 : TGo4Condition()
00045 {
00046 cut = 0;
00047 fiPoints = 0;
00048 SetDimension(2);
00049 }
00050
00051 TGo4PolyCond::~TGo4PolyCond()
00052 {
00053 if(cut != 0) delete cut;
00054 }
00055
00056 Double_t TGo4PolyCond::GetXLow()
00057 {
00058 if(cut==0) return 0;
00059 Int_t n=cut->GetN();
00060 Double_t* xarr=cut->GetX();
00061 Int_t nxmin=TMath::LocMin(n,xarr);
00062 return (xarr[nxmin]);
00063 }
00064 Double_t TGo4PolyCond::GetXUp()
00065 {
00066 if(cut==0) return 0;
00067 Int_t n=cut->GetN();
00068 Double_t* xarr=cut->GetX();
00069 Int_t nxmax=TMath::LocMax(n,xarr);
00070 return (xarr[nxmax]);
00071 }
00072 Double_t TGo4PolyCond::GetYLow()
00073 {
00074 if(cut==0) return 0;
00075 Int_t n=cut->GetN();
00076 Double_t* yarr=cut->GetY();
00077 Int_t nymin=TMath::LocMin(n,yarr);
00078 return (yarr[nymin]);
00079 }
00080 Double_t TGo4PolyCond::GetYUp()
00081 {
00082 if(cut==0) return 0;
00083 Int_t n=cut->GetN();
00084 Double_t* yarr=cut->GetY();
00085 Int_t nymax=TMath::LocMax(n,yarr);
00086 return (yarr[nymax]);
00087 }
00088
00089 Bool_t TGo4PolyCond::IsPolygonType()
00090 {
00091 return kTRUE;
00092 }
00093
00094
00095 TCutG* TGo4PolyCond::GetCut(Bool_t changeowner){
00096 if(InheritsFrom("TGo4PolyCond")){
00097 TCutG* tempcut = cut;
00098
00099
00100 if(changeowner){
00101 cut = 0;
00102 delete fxCutHis;
00103 fxCutHis=0;
00104 }
00105 return tempcut;
00106 }
00107 else{
00108 cout << "Cannot return cut " << GetName() << " class " << ClassName() << endl;
00109 return 0;
00110 }
00111 }
00112
00113 TCutG * TGo4PolyCond::CloneCut(TGo4PolyCond * source){
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 if(newcut==0) return;
00121 if(cut != 0) delete cut;
00122 cut = (TCutG *) newcut->Clone(GetName());
00123
00124 SetLineColor(newcut->GetLineColor());
00125 SetLineWidth(newcut->GetLineWidth());
00126 SetLineStyle(newcut->GetLineStyle());
00127 SetFillColor(newcut->GetFillColor());
00128 SetFillStyle(newcut->GetFillStyle());
00129
00130 delete fxCutHis;
00131 fxCutHis=0;
00132
00133 }
00134
00135 void TGo4PolyCond::SetValues(Double_t * x, Double_t * y, Int_t len){
00136
00137 if(cut != 0) delete cut;
00138 cut = new TCutG(GetName(),len,x,y);
00139
00140 delete fxCutHis;
00141 fxCutHis=0;
00142 }
00143
00144 Bool_t TGo4PolyCond::Test(Double_t x, Double_t y){
00145 IncCounts();
00146 if((!IsEnabled()) || (cut == 0)){
00147 if(FixedResult()) IncTrueCounts();
00148 return FixedResult();
00149 }
00150 Bool_t outside = (cut->IsInside(x,y) == 0);
00151 if(outside) return IsFalse();
00152 IncTrueCounts();
00153 return IsTrue();
00154 }
00155
00156 void TGo4PolyCond::PrintCondition(Bool_t points){
00157 TGo4Condition::PrintCondition();
00158
00159 if(points){
00160 if(cut == 0){
00161
00162 cout << "No polygon specified!" << endl;
00163 }
00164 else cut->Print(0);
00165 }}
00166
00167
00168
00170
00171
00172
00173
00174 Bool_t TGo4PolyCond::UpdateFrom(TGo4Condition * cond, Bool_t counts){
00175 if(!TGo4Condition::UpdateFrom(cond,counts)) return kFALSE;
00176 if(cond->InheritsFrom("TGo4PolyCond"))
00177 {
00178 TCutG * temp = CloneCut((TGo4PolyCond*)cond);
00179 CleanupSpecials();
00180
00181 if(temp != 0)
00182 {
00183
00184 if(cut != 0)delete cut;
00185 cut = temp;
00186 delete fxCutHis;
00187 fxCutHis=0;
00188
00189 return kTRUE;
00190 }
00191 else
00192 {
00193 return kFALSE;
00194 }
00195 }
00196 else
00197 {
00198 cout << "Cannot update " << GetName() << " from " << cond->ClassName() << endl;
00199 return kFALSE;
00200 }
00201 }
00202
00203 Double_t TGo4PolyCond::GetIntegral(TH1* histo, Option_t* opt)
00204 {
00206 #if __GO4ROOTVERSION__ >= 40008
00207
00208 if(cut)
00209 return (cut->Integral(dynamic_cast<TH2*>(histo),opt));
00210 else
00211 return 0;
00212 #else
00213
00214 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00215 if(fxCutHis!=0)
00216 return (fxCutHis->Integral(opt));
00217 else
00218 return-1;
00219 #endif // 40008
00220 }
00221
00222 Double_t TGo4PolyCond::GetMean(TH1* histo, Int_t axis)
00223 {
00224 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00225 if(fxCutHis!=0)
00226 return (fxCutHis->GetMean(axis));
00227 else
00228 return-1;
00229 }
00230 Double_t TGo4PolyCond::GetRMS(TH1* histo, Int_t axis)
00231 {
00232 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00233 if(fxCutHis!=0)
00234 return (fxCutHis->GetRMS(axis));
00235 else
00236 return -1;
00237 }
00238 Double_t TGo4PolyCond::GetXMax(TH1* histo)
00239 {
00240 Double_t result=0;
00241 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00242 if(fxCutHis!=0)
00243 {
00244 TAxis* xax=fxCutHis->GetXaxis();
00245 Int_t maxbin=fxCutHis->GetMaximumBin();
00246 Int_t xmaxbin=maxbin%(fxCutHis->GetNbinsX()+2);
00247 result=xax->GetBinCenter(xmaxbin);
00248 }
00249 else
00250 {
00251 result=-1;
00252 }
00253 return result;
00254 }
00255 Double_t TGo4PolyCond::GetYMax(TH1* histo)
00256 {
00257 Double_t result=0;
00258 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00259 if(fxCutHis!=0)
00260 {
00261 TAxis* yax=fxCutHis->GetYaxis();
00262 Int_t maxbin=fxCutHis->GetMaximumBin();
00263 Int_t maxybin=maxbin/(fxCutHis->GetNbinsX()+2);
00264 result=yax->GetBinCenter(maxybin);
00265 }
00266 else
00267 {
00268 result=-1;
00269 }
00270 return result;
00271 }
00272 Double_t TGo4PolyCond::GetCMax(TH1* histo)
00273 {
00274 if(fxCutHis==0) fxCutHis=CreateCutHistogram(histo);
00275 if(fxCutHis!=0)
00276 return(fxCutHis->GetMaximum());
00277 else
00278 return -1;
00279 }
00280
00281 void TGo4PolyCond::SetPainter(TGo4ConditionPainter* painter)
00282 {
00283 if(painter==0) return;
00284 if(painter->InheritsFrom("TGo4PolyCondPainter"))
00285 {
00286 if(fxPainter) delete fxPainter;
00287 fxPainter=painter;
00288 fxPainter->SetCondition(this);
00289 }
00290 else
00291 {
00292 TGo4Log::Warn("Could not set painter of class %s for TGo4PolyCond %s",
00293 painter->IsA()->GetName(),GetName());
00294 }
00295
00296
00297 }
00298
00299
00300 TGo4ConditionPainter* TGo4PolyCond::CreatePainter()
00301 {
00302 TGo4ConditionPainter* painter=new TGo4PolyCondPainter(GetName());
00303 painter->SetCondition(this);
00304 return painter;
00305 }
00306
00307 TH2* TGo4PolyCond::CreateCutHistogram(TH1* source)
00308 {
00309 TH2* his=dynamic_cast<TH2*>(source);
00310 if(his==0) return 0;
00311 TH2* work= (TH2*) his->Clone();
00312 Int_t nx=work->GetNbinsX();
00313 Int_t ny=work->GetNbinsY();
00314 TAxis* xaxis = work->GetXaxis();
00315 TAxis* yaxis = work->GetYaxis();
00316 xaxis->SetRange(0,0);
00317 yaxis->SetRange(0,0);
00318
00319 for(Int_t i=0; i<nx;++i)
00320 {
00321 Double_t x = xaxis->GetBinCenter(i);
00322 for(Int_t j=0; j<ny;++j)
00323 {
00324 Double_t y = yaxis->GetBinCenter(j);
00325 if(cut && !(cut->IsInside(x,y)))
00326 work->SetBinContent(i,j,0);
00327 }
00328 }
00329
00330 Stat_t s[11]={0};
00331 work->PutStats(s);
00332 work->GetStats(s);
00333 work->PutStats(s);
00334 work->SetDirectory(0);
00335 return work;
00336 }
00337
00338
00339
00340
00341 void TGo4PolyCond::CleanupSpecials()
00342 {
00343 TSeqCollection* specials=gROOT->GetListOfSpecials();
00344 TIter iter(specials);
00345 TObject* ob=0;
00346 while((ob = iter())!=0) {
00347 if(ob->InheritsFrom(TCutG::Class())) {
00348 specials->Remove(ob);
00349
00350 }
00351 }
00352 }
00353 ClassImp(TGo4PolyCond)
00354
00355
00356
00357