#include "TCanvas.h" #include "TROOT.h" #include "TStyle.h" #include "TH1F.h" #include "TH1D.h" #include "TGraphErrors.h" #include "TLegend.h" #include "TFile.h" #include "TString.h" #include #include #include class Plotter; class FileHandler; /* varname -> */ class VarType { public: Double_t x1,x2; // default range bool logx; TString hName; // histo name TString axtxt; // axis title // std::vector values; // std::vector errors; VarType(const TString & hn,const TString & axt="",Double_t _x1=0.,Double_t _x2=1.,bool _logx=false) : x1(_x1), x2(_x2), logx(_logx), hName(hn), axtxt(axt) //, values(20,0.), errors(20,0.), updated(false) {} VarType() : x1(0.), x2(1.), logx(false) {} }; /* plotname -> */ class PlotType { public: VarType xvar, yvar; bool logx, logy; Double_t x1,x2; Double_t y1,y2; TString header; TString leg; PlotType(Double_t _x1, Double_t _y1, Double_t _x2, Double_t _y2); PlotType(const TString & _xvar,const TString & _yvar,Plotter * pl); PlotType(const TString & _legpos); // legend coordinates PlotType(); PlotType & SetLegend(const TString & _header, const TString & _leg="topleft"); }; /* plots */ class SinglePlot { TString name; PlotType plot; TH1 * frame; std::vector graphs; TLegend * leg; Plotter * myPlotter; public: SinglePlot(const TString & _name, Plotter * _myPlotter); void CreateLegend(const TString & head="", const TString & pos=""); TGraph * CreatePlot(const TString & legtitle); void DrawLegend() { if (leg) leg->Draw(); } void SetTitle(const TString & tit) { if (frame) frame->SetTitle(tit); } }; /* base class */ class Plotter { protected: std::string name; // main canvas TCanvas * c1; // current file TFile * ff; // global title TString header; // list of all plots std::vector plots; // setup in constructor of derived class: std::map lookVarName; std::map lookPlotName; std::map lookFileName; // file cache std::map files; // start path TString path; friend class PlotType; friend class SinglePlot; public : Plotter(const std::string & _name = "dd1"); virtual void Initialize(TString pre=".."); virtual void CreateCanvas(TString id="")=0; bool Draw(TString name, TString select, TString option); void DrawLegends(); TCanvas * GetCanvas() { return c1; } std::string GetName() { return name; } void Print(TString n); virtual ~Plotter() {}; }; // ====================================================================== // PlotType // ====================================================================== PlotType::PlotType(const TString & _xvar,const TString & _yvar,Plotter * pl) { xvar=pl->lookVarName[_xvar]; x1=xvar.x1; x2=xvar.x2; logx=xvar.logx; yvar=pl->lookVarName[_yvar]; y1=yvar.x1; y2=yvar.x2; logy=yvar.logx; } PlotType::PlotType(const TString & legpos) // legend coordinates : x1(0.15), x2(0.5), y1(0.65), y2(0.9) { if (legpos=="topleft") { x1=0.2; x2=0.4; y1=0.65; y2=0.9; } if (legpos=="bottomleft") { x1=0.2; x2=0.4; y1=0.2; y2=0.45; } if (legpos=="topright") { x1=0.6; x2=0.85; y1=0.65; y2=0.9; } if (legpos=="bottomright") { x1=0.6; x2=0.85; y1=0.2; y2=0.45; } } PlotType::PlotType(Double_t _x1, Double_t _y1, Double_t _x2, Double_t _y2) : x1(_x1), x2(_x2), y1(_y1), y2(_y2) {} PlotType::PlotType() // dummy : x1(0.), x2(1.), y1(0.), y2(1.) {} PlotType & PlotType::SetLegend(const TString & _header, const TString & _leg) { header=_header; leg=_leg; return *this; } // ====================================================================== // SinglePlot // ====================================================================== SinglePlot::SinglePlot(const TString & _name, Plotter * _myPlotter) : name(_name), frame(0), leg(0), myPlotter(_myPlotter) { plot = myPlotter->lookPlotName[name]; } void SinglePlot::CreateLegend(const TString & head, const TString & pos) { TString id="legend "+pos; TString header = head; // check if not set if (head=="" and pos=="") { id = "legend "+plot.leg; header = plot.header; } // create legend PlotType pv = myPlotter->lookPlotName[id]; leg = new TLegend(pv.x1,pv.y1,pv.x2,pv.y2); leg->SetTextSize(0.04); if (head!="") leg->SetHeader(header); } TGraph * SinglePlot::CreatePlot(const TString & legtitle) { if (!frame) { // create frame if (plot.logx) gPad->SetLogx(); gPad->SetGrid(); frame=gPad->DrawFrame(plot.x1,plot.y1,plot.x2,plot.y2); frame->GetXaxis()->SetTitle(plot.xvar.axtxt); frame->GetYaxis()->SetTitle(plot.yvar.axtxt); } // add plot from current file TFile * ff = myPlotter->ff; TH1D* h0 = (TH1D*)ff->Get(plot.xvar.hName); TH1D* h1 = (TH1D*)ff->Get(plot.yvar.hName); Int_t nbins = h0->GetNbinsX(); Double_t x[20]; Double_t y[20], yerr[20]; for (Int_t ii=0; iiGetBinContent(ii+1); y[ii] = h1->GetBinContent(ii+1); yerr[ii] = h1->GetBinError(ii+1); } static const Int_t col[6] = {2, 1, 3, 4, 6, 9}; static const Int_t mar[6] = {20, 21, 22, 23, 28, 3}; Int_t idx = graphs.size()%6; TGraphErrors * gr = new TGraphErrors(nbins,x,y,0,yerr); graphs.push_back(gr); gr->SetMarkerColor(col[idx]); gr->SetMarkerStyle(mar[idx]); leg->AddEntry(gr, legtitle, "p"); return gr; } // ====================================================================== // Plotter // ====================================================================== Plotter::Plotter(const std::string & _name) : name(_name), c1(0) { lookPlotName["legend topleft"]=PlotType("topleft"); lookPlotName["legend bottomleft"]=PlotType("bottomleft"); lookPlotName["legend topright"]=PlotType("topright"); lookPlotName["legend bottomright"]=PlotType("bottomright"); } void Plotter::Initialize(TString pre) { path=pre; gROOT->Reset(); gROOT->SetStyle("Plain"); gStyle->SetOptStat(0); gStyle->SetNdivisions(210, "y"); gStyle->SetLabelSize(0.06, "x"); gStyle->SetLabelSize(0.06, "y"); gStyle->SetTitleOffset(0.9, "x"); gStyle->SetTitleOffset(0.7, "y"); gStyle->SetTitleSize(0.07, "x"); gStyle->SetTitleSize(0.07, "y"); gStyle->SetMarkerSize(1.5); gStyle->SetPadBottomMargin(0.13); gStyle->SetPadTopMargin(0.09); gStyle->SetPadLeftMargin(0.1); gStyle->SetPadRightMargin(0.05); gStyle->SetPadBorderMode(0); // std::cerr<<" Style initialised "<::iterator iter = files.find(file); if( iter != files.end() ) ff=iter->second; else { ff = TFile::Open(file); files[file]=ff; } if (!ff) return false; // file not found - ignore for (size_t j=0;jcd(j+1); TGraph*gr =plots[j]->CreatePlot(txtx); if (j==0) plots[j]->SetTitle(header); gr->Draw("p"); gPad->Modified(); gPad->Update(); } c1->Modified(); c1->Update(); return true; } void Plotter::DrawLegends() { c1->cd(1); // draw head // legen->Draw(); for (size_t j=0;jcd(j+1); plots[j]->DrawLegend(); } c1->Update(); } void Plotter::Print(TString n) { c1->Print(n); }