Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:26:10

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