00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4WinCond.h"
00015
00016 #include "Riostream.h"
00017 #include "TH1.h"
00018 #include "snprintf.h"
00019
00020 #include "TGo4WinCondPainter.h"
00021 #include "TGo4Log.h"
00022
00023
00024
00025
00026 TGo4WinCond::TGo4WinCond() :
00027 TGo4Condition(),
00028 fLow1(0),
00029 fUp1(0),
00030 fLow2(0),
00031 fUp2(0),
00032 fiSaveXMin(0),
00033 fiSaveXMax(0),
00034 fiSaveYMin(0),
00035 fiSaveYMax(0)
00036 {
00037 GO4TRACE((15,"TGo4WinCond::TGo4WinCond()",__LINE__, __FILE__));
00038 }
00039
00040 TGo4WinCond::TGo4WinCond(const char* name, const char* title) :
00041 TGo4Condition(name,title),
00042 fLow1(0),
00043 fUp1(0),
00044 fLow2(0),
00045 fUp2(0),
00046 fiSaveXMin(0),
00047 fiSaveXMax(0),
00048 fiSaveYMin(0),
00049 fiSaveYMax(0)
00050 {
00051 GO4TRACE((15,"TGo4WinCond::TGo4WinCond(name,title)",__LINE__, __FILE__));
00052 }
00053
00054
00055 TGo4WinCond::~TGo4WinCond()
00056 {
00057 GO4TRACE((15,"TGo4WinCond::~TGo4WinCond()",__LINE__, __FILE__));
00058 }
00059
00060
00061 Bool_t TGo4WinCond::Test(Double_t v1, Double_t v2){
00062 IncCounts();
00063 if(!IsEnabled()){
00064 if(FixedResult()) IncTrueCounts();
00065 return FixedResult();
00066 }
00067 if(v1 < fLow1) return IsFalse();
00068 if(v1 >= fUp1) return IsFalse();
00069 if(v2 < fLow2) return IsFalse();
00070 if(v2 >= fUp2) return IsFalse();
00071 IncTrueCounts();
00072 return IsTrue();
00073 }
00074
00075 Bool_t TGo4WinCond::Test(Double_t v1){
00076 IncCounts();
00077 if(!IsEnabled()){
00078 if(FixedResult()) IncTrueCounts();
00079 return FixedResult();
00080 }
00081 if(v1 < fLow1) return IsFalse();
00082 if(v1 >= fUp1) return IsFalse();
00083 IncTrueCounts();
00084 return IsTrue();
00085 }
00086
00087 void TGo4WinCond::SetValues(Double_t low1, Double_t up1, Double_t low2, Double_t up2)
00088 {
00089 fLow1 = low1;
00090 fUp1 = up1;
00091 fLow2 = low2;
00092 fUp2 = up2;
00093 SetDimension(2);
00094 }
00095
00096 void TGo4WinCond::SetValues(Double_t low1, Double_t up1)
00097 {
00098 fLow1 = low1;
00099 fUp1 = up1;
00100 SetDimension(1);
00101 }
00102
00103 void TGo4WinCond::GetValues(Int_t & dim, Double_t & x1, Double_t & y1, Double_t & x2, Double_t & y2)
00104 {
00105 x1 = fLow1;
00106 y1 = fUp1;
00107 x2 = fLow2;
00108 y2 = fUp2;
00109 dim = GetDimension();
00110 }
00111
00112 Bool_t TGo4WinCond::IsPolygonType()
00113 {
00114 return kFALSE;
00115 }
00116
00117 void TGo4WinCond::PrintCondition(Bool_t limits)
00118 {
00119 TGo4Condition::PrintCondition();
00120 if(limits) {
00121 char line[128];
00122 if(GetDimension()==1) snprintf(line,127,"[%8.2f,%8.2f]",fLow1,fUp1);
00123 else snprintf(line,127,"[%8.2f,%8.2f][%8.2f,%8.2f]",fLow1,fUp1,fLow2,fUp2);
00124 std::cout << line << std::endl;
00125 }
00126 }
00127
00128
00129 Double_t TGo4WinCond::GetIntegral(TH1* histo, Option_t* opt)
00130 {
00131 if(histo==0) return 0;
00132 Double_t result=0;
00133 SetHistogramRanges(histo);
00134 result=histo->Integral(opt);
00135 RestoreHistogramRanges(histo);
00136 return result;
00137 }
00138
00139 Double_t TGo4WinCond::GetMean(TH1* histo, Int_t axis)
00140 {
00141 if(histo==0) return 0;
00142 Double_t result=0;
00143 SetHistogramRanges(histo);
00144 result=histo->GetMean(axis);
00145 RestoreHistogramRanges(histo);
00146 return result;
00147 }
00148
00149 Double_t TGo4WinCond::GetRMS(TH1* histo, Int_t axis)
00150 {
00151 if(histo==0) return 0;
00152 Double_t result=0;
00153 SetHistogramRanges(histo);
00154 result=histo->GetRMS(axis);
00155 RestoreHistogramRanges(histo);
00156 return result;
00157 }
00158
00159 Double_t TGo4WinCond::GetXMax(TH1* histo)
00160 {
00161 if(histo==0) return 0;
00162 Double_t result=0;
00163 SetHistogramRanges(histo);
00164 TAxis* xax=histo->GetXaxis();
00165 Int_t maxbin=histo->GetMaximumBin();
00166 if(histo->GetDimension()==1)
00167 {
00168 result=xax->GetBinCenter(maxbin);
00169
00170 }
00171 else if (histo->GetDimension()==2)
00172 {
00173 Int_t xmaxbin=maxbin%(histo->GetNbinsX()+2);
00174 result=xax->GetBinCenter(xmaxbin);
00175 }
00176 else
00177 {
00178 result=0;
00179 }
00180 RestoreHistogramRanges(histo);
00181 return result;
00182 }
00183 Double_t TGo4WinCond::GetYMax(TH1* histo)
00184 {
00185 if(histo==0) return 0;
00186 Double_t result=0;
00187 SetHistogramRanges(histo);
00188 if(histo->GetDimension()==1)
00189 {
00190 result=histo->GetMaximum();
00191 }
00192 else if (histo->GetDimension()==2)
00193 {
00194 TAxis* yax=histo->GetYaxis();
00195 Int_t maxbin=histo->GetMaximumBin();
00196 Int_t maxybin=maxbin/(histo->GetNbinsX()+2);
00197 result=yax->GetBinCenter(maxybin);
00198 }
00199 else
00200 {
00201 result=0;
00202 }
00203 RestoreHistogramRanges(histo);
00204 return result;
00205 }
00206
00207 Double_t TGo4WinCond::GetCMax(TH1* histo)
00208 {
00209 if(histo==0) return 0;
00210 Double_t result=0;
00211 SetHistogramRanges(histo);
00212 result=histo->GetMaximum();
00213 RestoreHistogramRanges(histo);
00214 return result;
00215 }
00216
00217 void TGo4WinCond::SetHistogramRanges(TH1* histo)
00218 {
00219 if(histo==0) return;
00220 Double_t xmin=fLow1;
00221 Double_t xmax=fUp1;
00222 Double_t ymin=fLow2;
00223 Double_t ymax=fUp2;
00224 TAxis* xax=histo->GetXaxis();
00225 fiSaveXMin=xax->GetFirst();
00226 fiSaveXMax=xax->GetLast();
00227 Int_t xminbin=xax->FindBin(xmin);
00228 Int_t xmaxbin=xax->FindBin(xmax);
00229 Int_t yminbin=0;
00230 Int_t ymaxbin=0;
00231 TAxis* yax=histo->GetYaxis();
00232 if(yax && histo->GetDimension()>1)
00233 {
00234 fiSaveYMin=yax->GetFirst();
00235 fiSaveYMax=yax->GetLast();
00236 yminbin=yax->FindBin(ymin);
00237 ymaxbin=yax->FindBin(ymax);
00238 }
00239
00240 xax->SetRange(xminbin,xmaxbin);
00241 if(yax&& histo->GetDimension()>1)
00242 yax->SetRange(yminbin,ymaxbin);
00243 }
00244
00245 void TGo4WinCond::RestoreHistogramRanges(TH1* histo)
00246 {
00247 if(histo==0) return;
00248 TAxis* xax=histo->GetXaxis();
00249 TAxis* yax=histo->GetYaxis();
00250 xax->SetRange(fiSaveXMin,fiSaveXMax);
00251 if(yax&& histo->GetDimension()>1)
00252 yax->SetRange(fiSaveYMin,fiSaveYMax);
00253
00254
00255 }
00256
00257
00258
00259
00260 Bool_t TGo4WinCond::UpdateFrom(TGo4Condition * cond, Bool_t counts)
00261 {
00262 if(!TGo4Condition::UpdateFrom(cond,counts)) return kFALSE;
00263 if(!cond->InheritsFrom(TGo4WinCond::Class())) {
00264 std::cout << "Cannot update " << GetName() << " from " << cond->ClassName() << std::endl;
00265 return kFALSE;
00266 }
00267 Int_t dimension=0;
00268 ((TGo4WinCond*)cond)->GetValues(dimension,fLow1,fUp1,fLow2,fUp2);
00269 SetDimension(dimension);
00270 return kTRUE;
00271 }
00272
00273 void TGo4WinCond::SetPainter(TGo4ConditionPainter* painter)
00274 {
00275
00276
00277 if(painter==0) return;
00278 if(painter->InheritsFrom(TGo4WinCondPainter::Class()))
00279 {
00280 if(fxPainter) delete fxPainter;
00281 fxPainter=painter;
00282 fxPainter->SetCondition(this);
00283 }
00284 else
00285 {
00286 TGo4Log::Warn("Could not set painter of class %s for TGo4WinCond %s",
00287 painter->ClassName(),GetName());
00288 }
00289 }
00290
00291 TGo4ConditionPainter* TGo4WinCond::CreatePainter()
00292 {
00293 TGo4ConditionPainter* painter=new TGo4WinCondPainter(GetName());
00294 painter->SetCondition(this);
00295 return painter;
00296 }
00297
00298 Int_t TGo4WinCond::GetMemorySize()
00299 {
00300 Int_t size = sizeof(*this);
00301 if (GetName()!=0) size+=strlen(GetName());
00302 if (GetTitle()!=0) size+=strlen(GetTitle());
00303 return size;
00304 }
00305
00306 void TGo4WinCond::SavePrimitive(std::ostream& out, Option_t* opt)
00307 {
00308 static int cnt = 0;
00309
00310 TString varname = MakeScript(out, Form("wincond%d", cnt++), opt);
00311
00312 Int_t dim;
00313 Double_t xl,xu,yl,yu;
00314 GetValues(dim,xl,xu,yl,yu);
00315
00316 if(dim==1) out << Form(" %s->SetValues(%f, %f);", varname.Data(), xl, xu) << std::endl;
00317 else out << Form(" %s->SetValues(%f, %f, %f, %f);", varname.Data(), xl, xu, yl, yu) << std::endl;
00318 }
00319
00320