Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:41:57

0001 /** 
0002  * A collection of simple ROOT macros
0003  *
0004  * N. Amapane 2002-2004
0005  */
0006 /*
0007  * Draw 2-D plots superimposed to their profiles
0008  *
0009  * 2003 NCA
0010  */
0011 // Draw a 2-D plot within the specified Y range and superimpose its X profile
0012 
0013 #include <sstream>
0014 #include <iomanip>
0015 
0016 #if !defined(__CINT__) || defined(__MAKECINT__)
0017 #include "TProfile.h"
0018 #include "TLegend.h"
0019 #include "TROOT.h"
0020 #include "TVirtualPad.h"
0021 #include "TLine.h"
0022 #include "TCanvas.h"
0023 #include "TPostScript.h"
0024 #include "TH1.h"
0025 #include "TH2.h"
0026 #include "TF1.h"
0027 #include "TAxis.h"
0028 #include "TMath.h"
0029 #include "TROOT.h"
0030 #include "TStyle.h"
0031 
0032 #include <iostream>
0033 
0034 using namespace std;
0035 
0036 #endif
0037 
0038 
0039 
0040 TString getPitchString(TH1 *histo, int prec = 5);
0041 
0042 
0043 TLegend * getLegend(float x1=0.48, float y1=0.81, float x2=0.98, float y2=0.995);
0044 void setStyle(TH1 *histo);
0045 void setStyle(TH2 *histo);
0046 
0047 
0048 void setStyle(TH1 *histo) {
0049   histo->GetXaxis()->SetTitleFont(gStyle->GetTitleFont());
0050   histo->GetXaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0051   histo->GetXaxis()->SetLabelFont(gStyle->GetLabelFont());
0052   histo->GetXaxis()->SetLabelSize(gStyle->GetLabelSize());
0053 
0054   histo->GetYaxis()->SetTitleFont(gStyle->GetTitleFont());
0055   histo->GetYaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0056   histo->GetYaxis()->SetLabelFont(gStyle->GetLabelFont());
0057   histo->GetYaxis()->SetLabelSize(gStyle->GetLabelSize());
0058 }
0059 
0060 void setStyle(TH2 *histo) {
0061   histo->GetXaxis()->SetTitleFont(gStyle->GetTitleFont());
0062   histo->GetXaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0063   histo->GetXaxis()->SetLabelFont(gStyle->GetLabelFont());
0064   histo->GetXaxis()->SetLabelSize(gStyle->GetLabelSize());
0065 
0066   histo->GetYaxis()->SetTitleFont(gStyle->GetTitleFont());
0067   histo->GetYaxis()->SetTitleSize(gStyle->GetTitleFontSize());
0068   histo->GetYaxis()->SetLabelFont(gStyle->GetLabelFont());
0069   histo->GetYaxis()->SetLabelSize(gStyle->GetLabelSize());
0070 }
0071 
0072 void plotAndProfileX (TH2* h2, float min, float max, bool profile=false) {
0073   setStyle(h2);
0074   gPad->SetGrid(1,1);
0075   gStyle->SetGridColor(15);
0076   h2->GetYaxis()->SetRangeUser(min,max);
0077   h2->Draw();
0078   if (profile) {
0079     TProfile* prof = h2->ProfileX();
0080     prof->SetMarkerColor(2);
0081     prof->SetLineColor(2);
0082     prof->Draw("same");
0083   }
0084   TLine * l = new TLine(h2->GetXaxis()->GetXmin(),0,h2->GetXaxis()->GetXmax(),0);
0085   l->SetLineColor(3);
0086   l->Draw();
0087 }
0088 
0089 // void plotAndProfileY (TH2* h2, float min, float max) {
0090 //   h2->GetYaxis()->SetRangeUser(min,max);
0091 //   h2->Draw();
0092 //   TProfile* prof = h2->ProfileY();
0093 //   prof->SetMarkerStyle(8);
0094 //   prof->SetMarkerSize(0.7);
0095 //   prof->SetMarkerColor(2);
0096 //   prof->SetLineColor(2);
0097 //   prof->Draw("same");
0098 // }
0099 
0100 // Draw a 2-D plot within the specified Y range and superimpose its X profile,
0101 // setting as sigmas that of the fit (and not the error of the mean)
0102 void plotAndProfileXSpread (TH2* h2, float min, float max, bool profile=false, float ymin=-5., float ymax=5.) {
0103   setStyle(h2);
0104   gPad->SetGrid(1,1);
0105   gStyle->SetGridColor(15);
0106   gStyle->SetOptStat(0);
0107   // h2->RebinX(3);
0108   // h2->RebinY(2);
0109   // h2->SetXTitle("distance from anode (cm)");
0110   // h2->SetYTitle("(d_{reco}-d_{sim})/#sigma_{reco}");
0111   h2->SetMarkerColor(2);
0112   h2->SetLineColor(2);
0113   h2->GetYaxis()->SetTitleOffset(1.4);
0114   h2->GetXaxis()->SetRangeUser(min,max);
0115   h2->GetYaxis()->SetRangeUser(ymin,ymax);
0116   h2->DrawCopy("box");
0117   if (profile) {
0118     TProfile* prof = h2->ProfileX("profile",-1,-1,"s");
0119     prof->SetMarkerStyle(20);
0120     prof->SetMarkerSize(1.2);
0121     prof->SetMarkerColor(1);
0122     prof->SetLineColor(1);
0123     prof->SetLineWidth(2);
0124     prof->DrawCopy("same e1");
0125   }
0126   TLine * l = new TLine(h2->GetXaxis()->GetXmin(),0,h2->GetXaxis()->GetXmax(),0);
0127   l->SetLineColor(3);
0128   l->Draw();
0129 }
0130 /*
0131  * Draw and format a fitted histogram 
0132  *
0133  * 2003 NCA
0134  */
0135 // Fit a histogram with a gaussian and draw it in the range 
0136 // mean+-nsigmas*RMS.
0137 void drawGFit(TH1 * h1, float nsigmas, float min, float max){
0138   float minfit = h1->GetMean() - h1->GetRMS();
0139   float maxfit = h1->GetMean() + h1->GetRMS();
0140   drawGFit(h1, min, max, minfit, maxfit);
0141   gPad->Draw();
0142 }
0143 // Fit a histogram with a gaussian and draw it in the specified range.
0144 void drawGFit(TH1 * h1, float min, float max){
0145   drawGFit(h1, min, max, min, max);
0146   gPad->Draw();
0147 }
0148 // Fit a histogram in the range (minfit, maxfit) with a gaussian and
0149 // draw it in the range (min, max)
0150 void drawGFit(TH1 * h1, float min, float max, float minfit, float maxfit) {
0151   setStyle(h1);
0152   static int i = 0;
0153   i++;
0154   gPad->SetGrid(1,1);
0155   gStyle->SetGridColor(15);
0156   h1->GetXaxis()->SetRangeUser(min,max);
0157   TString  fitName = "g";
0158   fitName += i;
0159     TF1* g1 = new TF1(fitName.Data(),"gaus",minfit,maxfit);
0160   g1->SetLineColor(2);
0161   g1->SetLineWidth(2);
0162   h1->Fit(g1,"R");
0163   h1->Draw();
0164 //   TPaveStats *st = (TPaveStats*)h1->GetListOfFunctions()->FindObject("stats");
0165 //   st->SetX2NDC(0.905);
0166 //   st->SetY2NDC(0.905);
0167 }
0168 /*
0169  * Create a new TCanvas setting its properties
0170  *
0171  * 2003 NCA 
0172  */
0173 // Specify name, title, x/y divisions, form or x,y sizes.
0174 // If no name is specified, a new name is generated automatically
0175 TCanvas * newCanvas(TString name="", TString title="",
0176                      Int_t xdiv=0, Int_t ydiv=0, Int_t form = 1, Int_t w=-1){
0177   static int i = 1;
0178   if (name == "") {
0179     name = TString("Canvas ") + i;
0180     i++;
0181   }
0182   if (title == "") title = name;
0183   if (w<0) {
0184     TCanvas * c = new TCanvas(name,title, form);
0185   } else {
0186     TCanvas * c = new TCanvas(name,title,form,w);
0187   }
0188   if (xdiv*ydiv!=0) c->Divide(xdiv,ydiv);
0189   c->cd(1);
0190   return c;
0191 }
0192 // Create a new canvas with an automatic generated name and the specified 
0193 // divisions and form
0194 TCanvas * newCanvas(Int_t xdiv, Int_t ydiv, Int_t form = 1) {
0195   return newCanvas("","",xdiv,ydiv,form);
0196 }
0197 // Create a new canvas with an automatic generated name and the specified 
0198 // form
0199 TCanvas * newCanvas(Int_t form = 1)
0200 {
0201   return newCanvas(0,0,form);
0202 }
0203 // ...without specifying the title...
0204 TCanvas * newCanvas(TString name, Int_t xdiv, Int_t ydiv, Int_t form,
0205                     Int_t w) {
0206   return newCanvas(name, name,xdiv,ydiv,form,w);
0207 }
0208 // ...without specifying title and divisions.
0209 TCanvas * newCanvas(TString name, Int_t form, Int_t w=-1)
0210 {
0211   return newCanvas(name, name, 0,0,form,w);
0212 }
0213 /*
0214  * Print all open canvases to PS or EPS files.
0215  *
0216  * 2003 NCA 
0217  */
0218 // Print all canvases in a single PS file
0219 void printCanvasesPS(TString name){
0220   TPostScript * ps = new TPostScript(name,112);
0221   TIter iter(gROOT->GetListOfCanvases());
0222   TCanvas *c;
0223   while( (c = (TCanvas *)iter()) )
0224     {
0225       c->cd();
0226       cout << "Printing " << c->GetName() << endl;
0227       ps->NewPage();
0228       c->Draw();
0229     }
0230   cout << " File " << name << " was created" << endl;
0231   ps->Close();
0232 }
0233 // Print all canvases in separate EPS files
0234 void printCanvasesEps(){
0235   TIter iter(gROOT->GetListOfCanvases());
0236   TCanvas *c;
0237   while( (c = (TCanvas *)iter()) ) {
0238     c->Print(0,".eps");
0239   }
0240 }
0241 // Print all canvases in separate EPS files (another way)
0242 void printCanvasesEps2() {
0243   gROOT->GetListOfCanvases()->Print(".eps");
0244 }
0245 // Print all canvases in separate EPS files
0246 void printCanvases(TString type=".eps"){
0247   TIter iter(gROOT->GetListOfCanvases());
0248   TCanvas *c;
0249   while( (c = (TCanvas *)iter()) ) {
0250     c->cd();
0251     c->Print(0,type);
0252   }
0253 }
0254 /*
0255  * Define different TStyles; use them with:
0256  * getStyle->cd();
0257  *
0258  * 2003 NCA
0259  */
0260 TStyle * getStyle(TString name="myStyle")
0261 {
0262   TStyle *theStyle;
0263   if ( name == "myStyle" ) {
0264     theStyle = new TStyle("myStyle", "myStyle");
0265     //    theStyle->SetOptStat(0);
0266     theStyle->SetPadBorderMode(0);
0267     theStyle->SetCanvasBorderMode(0);
0268     theStyle->SetPadColor(0);
0269     theStyle->SetCanvasColor(0);
0270     theStyle->SetMarkerStyle(8);
0271     theStyle->SetMarkerSize(0.7);
0272     theStyle->SetStatH(0.3);
0273     theStyle->SetStatW(0.15);
0274     //   theStyle->SetTextFont(132);
0275     //   theStyle->SetTitleFont(132);
0276     theStyle->SetTitleBorderSize(1);
0277     theStyle->SetPalette(1);
0278 
0279   } else if( name == "tdr" ) {
0280     theStyle = new TStyle("tdrStyle","Style for P-TDR");
0281 
0282     // For the canvas:
0283     theStyle->SetCanvasBorderMode(0);
0284     theStyle->SetCanvasColor(kWhite);
0285     theStyle->SetCanvasDefH(600); //Height of canvas
0286     theStyle->SetCanvasDefW(600); //Width of canvas
0287     theStyle->SetCanvasDefX(0);   //POsition on screen
0288     theStyle->SetCanvasDefY(0);
0289 
0290     // For the Pad:
0291     theStyle->SetPadBorderMode(0);
0292     // theStyle->SetPadBorderSize(Width_t size = 1);
0293     theStyle->SetPadColor(kWhite);
0294     theStyle->SetPadGridX(true);
0295     theStyle->SetPadGridY(true);
0296     theStyle->SetGridColor(0);
0297     theStyle->SetGridStyle(3);
0298     theStyle->SetGridWidth(1);
0299 
0300     // For the frame:
0301     theStyle->SetFrameBorderMode(0);
0302     theStyle->SetFrameBorderSize(1);
0303     theStyle->SetFrameFillColor(0);
0304     theStyle->SetFrameFillStyle(0);
0305     theStyle->SetFrameLineColor(1);
0306     theStyle->SetFrameLineStyle(1);
0307     theStyle->SetFrameLineWidth(1);
0308 
0309     // For the histo:
0310     // theStyle->SetHistFillColor(1);
0311     // theStyle->SetHistFillStyle(0);
0312     theStyle->SetHistLineColor(1);
0313     theStyle->SetHistLineStyle(0);
0314     theStyle->SetHistLineWidth(1);
0315     // theStyle->SetLegoInnerR(Float_t rad = 0.5);
0316     // theStyle->SetNumberContours(Int_t number = 20);
0317 
0318     theStyle->SetEndErrorSize(2);
0319 //     theStyle->SetErrorMarker(20);
0320     theStyle->SetErrorX(0.);
0321   
0322     theStyle->SetMarkerStyle(20);
0323 
0324     //For the fit/function:
0325     theStyle->SetOptFit(1);
0326     theStyle->SetFitFormat("5.4g");
0327     theStyle->SetFuncColor(2);
0328     theStyle->SetFuncStyle(1);
0329     theStyle->SetFuncWidth(1);
0330 
0331     //For the date:
0332     theStyle->SetOptDate(0);
0333     // theStyle->SetDateX(Float_t x = 0.01);
0334     // theStyle->SetDateY(Float_t y = 0.01);
0335 
0336     // For the statistics box:
0337     theStyle->SetOptFile(0);
0338 //     theStyle->SetOptStat(0); // To display the mean and RMS:   SetOptStat("mr");
0339     theStyle->SetOptStat(10);
0340     theStyle->SetStatColor(kWhite);
0341     theStyle->SetStatFont(42);
0342     theStyle->SetStatFontSize(0.07);
0343     theStyle->SetStatTextColor(1);
0344     theStyle->SetStatFormat("6.4g");
0345     theStyle->SetStatBorderSize(1);
0346     theStyle->SetStatH(0.3);
0347     theStyle->SetStatW(0.2);
0348     // theStyle->SetStatStyle(Style_t style = 1001);
0349     // theStyle->SetStatX(Float_t x = 0);
0350     // theStyle->SetStatY(Float_t y = 0);
0351 
0352     // Margins:
0353     theStyle->SetPadTopMargin(0.05);
0354     theStyle->SetPadBottomMargin(0.13);
0355     theStyle->SetPadLeftMargin(0.16);
0356     theStyle->SetPadRightMargin(0.02);
0357 
0358     // For the Global title:
0359 
0360     theStyle->SetOptTitle(0);
0361     theStyle->SetTitleFont(42);
0362     theStyle->SetTitleColor(1);
0363     theStyle->SetTitleTextColor(1);
0364     theStyle->SetTitleFillColor(10);
0365     theStyle->SetTitleFontSize(0.05);
0366     // theStyle->SetTitleH(0); // Set the height of the title box
0367     // theStyle->SetTitleW(0); // Set the width of the title box
0368     // theStyle->SetTitleX(0); // Set the position of the title box
0369     // theStyle->SetTitleY(0.985); // Set the position of the title box
0370     // theStyle->SetTitleStyle(Style_t style = 1001);
0371     // theStyle->SetTitleBorderSize(2);
0372 
0373     // For the axis titles:
0374 
0375     theStyle->SetTitleColor(1, "XYZ");
0376     theStyle->SetTitleFont(42, "XYZ");
0377     theStyle->SetTitleSize(0.06, "XYZ");
0378     // theStyle->SetTitleXSize(Float_t size = 0.02); // Another way to set the size?
0379     // theStyle->SetTitleYSize(Float_t size = 0.02);
0380     theStyle->SetTitleXOffset(0.9);
0381     theStyle->SetTitleYOffset(1.25);
0382     // theStyle->SetTitleOffset(1.1, "Y"); // Another way to set the Offset
0383 
0384     // For the axis labels:
0385 
0386     theStyle->SetLabelColor(1, "XYZ");
0387     theStyle->SetLabelFont(42, "XYZ");
0388     theStyle->SetLabelOffset(0.007, "XYZ");
0389     theStyle->SetLabelSize(0.045, "XYZ");
0390 
0391     // For the axis:
0392 
0393     theStyle->SetAxisColor(1, "XYZ");
0394     theStyle->SetStripDecimals(kTRUE);
0395     theStyle->SetTickLength(0.03, "XYZ");
0396     theStyle->SetNdivisions(510, "XYZ");
0397     theStyle->SetPadTickX(1);  // To get tick marks on the opposite side of the frame
0398     theStyle->SetPadTickY(1);
0399 
0400     // Change for log plots:
0401     theStyle->SetOptLogx(0);
0402     theStyle->SetOptLogy(0);
0403     theStyle->SetOptLogz(0);
0404 
0405     // Postscript options:
0406     theStyle->SetPaperSize(20.,20.);
0407     // theStyle->SetLineScalePS(Float_t scale = 3);
0408     // theStyle->SetLineStyleString(Int_t i, const char* text);
0409     // theStyle->SetHeaderPS(const char* header);
0410     // theStyle->SetTitlePS(const char* pstitle);
0411 
0412     // theStyle->SetBarOffset(Float_t baroff = 0.5);
0413     // theStyle->SetBarWidth(Float_t barwidth = 0.5);
0414     // theStyle->SetPaintTextFormat(const char* format = "g");
0415     // theStyle->SetPalette(Int_t ncolors = 0, Int_t* colors = 0);
0416     // theStyle->SetTimeOffset(Double_t toffset);
0417     // theStyle->SetHistMinimumZero(kTRUE);
0418 
0419 
0420     //   style->SetOptFit(101);
0421     //   style->SetOptStat(1111111); 
0422 
0423   } else {
0424     // Avoid modifying the default style!
0425     theStyle = gStyle;
0426   }
0427   return theStyle;
0428 }