00001
00007 #ifndef __HVISITORS_H
00008 #define __HVISITORS_H
00009
00010 #include "jbltools/sfh/HVisitor.h"
00011
00012
00013 #include <TObject.h>
00014 #include <TH1.h>
00015 #include <TF1.h>
00016 #include <TLine.h>
00017
00018 #include <iostream>
00019 #include <cassert>
00020
00021 class TPad;
00022 class TF1;
00023 class TVirtualPS;
00024
00026
00029 class Writer: public ConstHVisitor {
00030 public:
00032 virtual void visit (RegO& h) const {
00033 if (TObject *to = dynamic_cast<TObject *>(&h)) to->Write();
00034 };
00035 };
00036
00038
00042 class Normalizer: public ConstHVisitor {
00043 public:
00045 Normalizer (Double_t c_ = 1,
00046 bool includeOverflow_ = true
00047 )
00048 : c (c_), includeOverflow(includeOverflow_) {}
00050 virtual void visit (RegO& h
00051 ) const {
00052 if (TH1 *th1 = dynamic_cast<TH1 *>(&h)) {
00053 Double_t scale = th1->GetSumOfWeights();
00054 if (includeOverflow) {
00055 int maxbin = 1;
00056 assert (th1->GetDimension() == 1);
00057 maxbin = th1->GetNbinsX()+1;
00058 scale += th1->GetBinContent(0)+
00059 th1->GetBinContent(maxbin);
00060 }
00061 if (scale > 0.) th1->Scale (c/scale);
00062 }
00063 };
00064 private:
00065 Double_t c;
00066 bool includeOverflow;
00067 };
00068
00070
00075 class BinWidthNormalizer: public ConstHVisitor {
00076 public:
00078 BinWidthNormalizer (Double_t width_ = 1
00079 )
00080 : width (width_) {}
00082 virtual void visit (RegO& h
00083 ) const {
00084 if (TH1 *th1 = dynamic_cast<TH1 *>(&h)) {
00085 for (int i = 1; i <= th1->GetNbinsX(); ++i) {
00086 Double_t c = width/th1->GetBinWidth(i);
00087 th1->SetBinContent (i, c*th1->GetBinContent (i));
00088 th1->SetBinError (i, c*th1->GetBinError (i));
00089 }
00090 }
00091 };
00092 private:
00093 Double_t width;
00094 };
00095
00097
00101 class AttMarkerSetter: public ConstHVisitor {
00102 public:
00104 AttMarkerSetter (Color_t tcolor_ = -1,
00105 Size_t msize_ = -1,
00106 Style_t mstyle_ = -1
00107 )
00108 : tcolor (tcolor_), msize (msize_), mstyle (mstyle_)
00109 {}
00110 virtual void visit (RegO& h
00111 ) const {
00112 if (TAttMarker *tam = dynamic_cast<TAttMarker *>(&h)) {
00113 if (tcolor >= 0) tam->SetMarkerColor (tcolor);
00114 if (msize >= 0) tam->SetMarkerSize (msize);
00115 if (mstyle >= 0) tam->SetMarkerStyle (mstyle);
00116 }
00117 }
00118 private:
00119 Color_t tcolor;
00120 Size_t msize;
00121 Style_t mstyle;
00122 };
00123
00125
00130 class AttLineSetter: public ConstHVisitor {
00131 public:
00133 AttLineSetter (Color_t lcolor_ = -1,
00134 Style_t lstyle_ = -1,
00135 Width_t lwidth_ = -1
00136 )
00137 : lcolor (lcolor_), lstyle (lstyle_), lwidth (lwidth_)
00138 {}
00140 virtual void visit (RegO& h
00141 ) const {
00142 if (TAttLine *tal = dynamic_cast<TAttLine *>(&h)) {
00143 if (lcolor >= 0) tal->SetLineColor (lcolor);
00144 if (lstyle >= 0) tal->SetLineStyle (lstyle);
00145 if (lwidth >= 0) tal->SetLineWidth (lwidth);
00146 }
00147 }
00148 virtual void visit (TLine& h
00149 ) const {
00150 if (TAttLine *tal = dynamic_cast<TAttLine *>(&h)) {
00151 if (lcolor >= 0) tal->SetLineColor (lcolor);
00152 if (lstyle >= 0) tal->SetLineStyle (lstyle);
00153 if (lwidth >= 0) tal->SetLineWidth (lwidth);
00154 }
00155 }
00156 private:
00157 Color_t lcolor;
00158 Style_t lstyle;
00159 Width_t lwidth;
00160 };
00161
00163
00168 class AttFillSetter: public ConstHVisitor {
00169 public:
00171 AttFillSetter (Color_t fcolor_ = -1,
00172 Style_t fstyle_ = -1
00173 )
00174 : fcolor (fcolor_), fstyle (fstyle_)
00175 {}
00177 virtual void visit (RegO& h
00178 ) const {
00179 if (TAttFill *taf = dynamic_cast<TAttFill *>(&h)) {
00180 if (fcolor >= 0) taf->SetFillColor (fcolor);
00181 if (fstyle >= 0) taf->SetFillStyle (fstyle);
00182 }
00183 }
00184 private:
00185 Color_t fcolor;
00186 Style_t fstyle;
00187 };
00188
00190 class ErrorEraser: public ConstHVisitor {
00191 public:
00193 ErrorEraser (){}
00195 virtual void visit (RegO& h
00196 ) const {
00197 if (TH1 *th1 = dynamic_cast<TH1 *>(&h)) {
00198 for (int i = 0; i <= th1->GetNbinsX()+1; ++i) {
00199 th1->SetBinError (i, 0.0);
00200 }
00201 }
00202 }
00203 };
00204
00206 class MinMaxSetter: public ConstHVisitor {
00207 public:
00208 MinMaxSetter (Double_t min_,
00209 Double_t max_
00210 )
00211 : min (min_), max (max_)
00212 {}
00214 virtual void visit (RegO& h
00215 ) const {
00216 if (TH1 *th1 = dynamic_cast<TH1 *>(&h)) {
00217 th1->SetMinimum (min);
00218 th1->SetMaximum (max);
00219 }
00220 }
00221 private:
00222 Double_t min;
00223 Double_t max;
00224 };
00225
00227
00230 class AxisTitleAttribSetter: public ConstHVisitor {
00231 public:
00233 AxisTitleAttribSetter(int axis_,
00234 const char *name_ = 0,
00235 Double_t size_ = -1,
00236 Double_t offset_ = -999.,
00237 Color_t lcolor_ = -1,
00238 Style_t font_ = -1)
00239 : axis (axis_),
00240 name (0),
00241 size (size_),
00242 offset (offset_),
00243 lcolor (lcolor_),
00244 font (font_)
00245 {
00246 if (name_) {
00247 name = new char [strlen (name_)+1];
00248 strcpy (name, name_);
00249 }
00250 }
00251
00253 virtual void visit (RegO& h
00254 ) const {
00255 if (TH1 *th1 = dynamic_cast<TH1 *>(&h)) {
00256 TAxis *ax = 0;
00257 switch (axis) {
00258 case 1: ax = th1->GetXaxis(); break;
00259 case 2: ax = th1->GetYaxis(); break;
00260 case 3: ax = th1->GetZaxis(); break;
00261 }
00262 if (ax == 0) return;
00263
00264 if (name) ax->SetTitle (name);
00265 if (size > 0) ax->SetTitleSize (size);
00266 if (offset !=-999.) ax->SetTitleOffset (offset);
00267 if (lcolor > -1) ax->SetTitleColor (lcolor);
00268 if (font > -1) ax->SetTitleFont (font);
00269 }
00270 }
00271
00272 ~AxisTitleAttribSetter() {
00273 delete[] name;
00274 name = 0;
00275 }
00276 private:
00277 int axis;
00278 char *name;
00279 Double_t size;
00280 Double_t offset;
00281 Color_t lcolor;
00282 Style_t font;
00283
00285 AxisTitleAttribSetter (const AxisTitleAttribSetter& source);
00287 AxisTitleAttribSetter& operator= (const AxisTitleAttribSetter& source);
00288 };
00289
00291
00294 class AxisLabelAttribSetter: public ConstHVisitor {
00295 public:
00297 AxisLabelAttribSetter(int axis_,
00298 Double_t size_,
00299 Color_t lcolor_ = -1,
00300 Style_t font_ = -1,
00301 Int_t ndiv_ = -1)
00302 : axis (axis_),
00303 size (size_),
00304 lcolor (lcolor_),
00305 font (font_),
00306 ndiv (ndiv_)
00307 {}
00308
00310 virtual void visit (RegO& h
00311 ) const {
00312 if (TH1 *th1 = dynamic_cast<TH1 *>(&h)) {
00313 TAxis *ax = 0;
00314 switch (axis) {
00315 case 1: ax = th1->GetXaxis(); break;
00316 case 2: ax = th1->GetYaxis(); break;
00317 case 3: ax = th1->GetZaxis(); break;
00318 }
00319 if (ax == 0) return;
00320
00321 if (size > 0) ax->SetLabelSize (size);
00322 if (lcolor > -1) ax->SetLabelColor (lcolor);
00323 if (font > -1) ax->SetLabelFont (font);
00324 if (ndiv > -1) ax->SetNdivisions (ndiv);
00325 }
00326 }
00327
00328 ~AxisLabelAttribSetter() {}
00329 private:
00330 int axis;
00331 Double_t size;
00332 Color_t lcolor;
00333 Style_t font;
00334 Int_t ndiv;
00335
00337 AxisLabelAttribSetter (const AxisLabelAttribSetter& source);
00339 AxisLabelAttribSetter& operator= (const AxisLabelAttribSetter& source);
00340 };
00345 static AttLineSetter blackline (1);
00350 static AttLineSetter redline (2);
00355 static AttLineSetter greenline (3);
00360 static AttLineSetter blueline (4);
00365 static AttLineSetter yellowline (5);
00370 static AttLineSetter magentaline (6);
00375 static AttLineSetter cyanline (7);
00376
00377 static AttFillSetter blackfill (1);
00378 static AttFillSetter redfill (2);
00379 static AttFillSetter greenfill (3);
00380 static AttFillSetter bluefill (4);
00381 static AttFillSetter yellowfill (5);
00382 static AttFillSetter magentafill (6);
00383 static AttFillSetter cyanfill (7);
00384
00385 static AttFillSetter hollowfill (-1, 0);
00386 static AttFillSetter solidfill (-1, 1001);
00387
00388 static AttMarkerSetter blackmarker (1);
00389 static AttMarkerSetter redmarker (2);
00390 static AttMarkerSetter greenmarker (3);
00391 static AttMarkerSetter bluemarker (4);
00392 static AttMarkerSetter yellowmarker (5);
00393 static AttMarkerSetter magentamarker (6);
00394 static AttMarkerSetter cyanmarker (7);
00395
00397 class AxisSetter: public ConstHVisitor {
00398 public:
00400 AxisSetter (Int_t n_, const Float_t *x_)
00401 : n (n_), x (x_)
00402 {}
00404 virtual void visit (RegO& h
00405 ) const {
00406 if (TH1 *th1 = dynamic_cast<TH1 *>(&h)) {
00407 TAxis *axis = th1->GetXaxis();
00408 if (axis) axis->Set (n, x);
00409 }
00410 }
00411 private:
00412 Int_t n;
00413 const Float_t *x;
00414 };
00415
00416
00417 #endif