Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:31

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 void plotAndProfileX (TH2* h2, float min, float max, bool profile=false) {
0013   gPad->SetGrid(1,1);
0014   gStyle->SetGridColor(15);
0015   h2->GetYaxis()->SetRangeUser(min,max);
0016   h2->Draw();
0017   if (profile) {
0018     TProfile* prof = h2->ProfileX();
0019     prof->SetMarkerColor(2);
0020     prof->SetLineColor(2);
0021     prof->Draw("same");
0022   }
0023   TLine * l = new TLine(h2->GetXaxis()->GetXmin(),0,h2->GetXaxis()->GetXmax(),0);
0024   l->SetLineColor(3);
0025   l->Draw();
0026 }
0027 // void plotAndProfileY (TH2* h2, float min, float max) {
0028 //   h2->GetYaxis()->SetRangeUser(min,max);
0029 //   h2->Draw();
0030 //   TProfile* prof = h2->ProfileY();
0031 //   prof->SetMarkerStyle(8);
0032 //   prof->SetMarkerSize(0.7);
0033 //   prof->SetMarkerColor(2);
0034 //   prof->SetLineColor(2);
0035 //   prof->Draw("same");
0036 // }
0037 /*
0038  * Draw and format a fitted histogram 
0039  *
0040  * 2003 NCA
0041  */
0042 // Fit a histogram with a gaussian and draw it in the range 
0043 // mean+-nsigmas*RMS.
0044 void drawGFit(TH1 * h1, float nsigmas, float min, float max){
0045   float minfit = h1->GetMean() - h1->GetRMS();
0046   float maxfit = h1->GetMean() + h1->GetRMS();
0047   drawGFit(h1, min, max, minfit, maxfit);
0048   gPad->Draw();
0049 }
0050 // Fit a histogram with a gaussian and draw it in the specified range.
0051 void drawGFit(TH1 * h1, float min, float max){
0052   drawGFit(h1, min, max, min, max);
0053   gPad->Draw();
0054 }
0055 // Fit a histogram in the range (minfit, maxfit) with a gaussian and
0056 // draw it in the range (min, max)
0057 void drawGFit(TH1 * h1, float min, float max, float minfit, float maxfit) {
0058   static int i = 0;
0059   i++;
0060   gPad->SetGrid(1,1);
0061   gStyle->SetGridColor(15);
0062   h1->GetXaxis()->SetRangeUser(min,max);
0063   TF1* g1 = new TF1(TString("g")+i,"gaus",minfit,maxfit);
0064   g1->SetLineColor(2);
0065   g1->SetLineWidth(2);
0066   h1->Fit(g1,"R");
0067 //   TPaveStats *st = (TPaveStats*)h1->GetListOfFunctions()->FindObject("stats");
0068 //   st->SetX2NDC(0.905);
0069 //   st->SetY2NDC(0.905);
0070 }
0071 /*
0072  * Create a new TCanvas setting its properties
0073  *
0074  * 2003 NCA 
0075  */
0076 // Specify name, title, x/y divisions, form or x,y sizes.
0077 // If no name is specified, a new name is generated automatically
0078 TCanvas * newCanvas(TString name="", TString title="",
0079                      Int_t xdiv=0, Int_t ydiv=0, Int_t form = 1, Int_t w=-1){
0080   static int i = 1;
0081   if (name == "") {
0082     name = TString("Canvas ") + i;
0083     i++;
0084   }
0085   if (title == "") title = name;
0086   if (w<0) {
0087     TCanvas * c = new TCanvas(name,title, form);
0088   } else {
0089     TCanvas * c = new TCanvas(name,title,form,w);
0090   }
0091   if (xdiv*ydiv!=0) c->Divide(xdiv,ydiv);
0092   c->cd(1);
0093   return c;
0094 }
0095 // Create a new canvas with an automatic generated name and the specified 
0096 // divisions and form
0097 TCanvas * newCanvas(Int_t xdiv, Int_t ydiv, Int_t form = 1) {
0098   return newCanvas("","",xdiv,ydiv,form);
0099 }
0100 // Create a new canvas with an automatic generated name and the specified 
0101 // form
0102 TCanvas * newCanvas(Int_t form = 1)
0103 {
0104   return newCanvas(0,0,form);
0105 }
0106 // ...without specifying the title...
0107 TCanvas * newCanvas(TString name, Int_t xdiv, Int_t ydiv, Int_t form,
0108                     Int_t w) {
0109   return newCanvas(name, name,xdiv,ydiv,form,w);
0110 }
0111 // ...without specifying title and divisions.
0112 TCanvas * newCanvas(TString name, Int_t form, Int_t w=-1)
0113 {
0114   return newCanvas(name, name, 0,0,form,w);
0115 }
0116 /*
0117  * Print all open canvases to PS or EPS files.
0118  *
0119  * 2003 NCA 
0120  */
0121 // Print all canvases in a single PS file
0122 void printCanvasesPS(TString name){
0123   TPostScript * ps = new TPostScript(name,112);
0124   TIter iter(gROOT->GetListOfCanvases());
0125   TCanvas *c;
0126   while( (c = (TCanvas *)iter()) )
0127     {
0128       cout << "Printing " << c->GetName() << endl;
0129       ps->NewPage();
0130       c->Draw();
0131     }
0132   cout << " File " << name << " was created" << endl;
0133   ps->Close();
0134 }
0135 // Print all canvases in separate EPS files
0136 void printCanvasesEps(){
0137   TIter iter(gROOT->GetListOfCanvases());
0138   TCanvas *c;
0139   while( (c = (TCanvas *)iter()) ) {
0140     c->Print(0,"eps");
0141   }
0142 }
0143 // Print all canvases in separate EPS files (another way)
0144 void printCanvasesEps2() {
0145   gROOT->GetListOfCanvases()->Print("eps");
0146 }
0147 // Print all canvases in separate EPS files
0148 void printCanvases(TString type="eps"){
0149   TIter iter(gROOT->GetListOfCanvases());
0150   TCanvas *c;
0151   while( (c = (TCanvas *)iter()) ) {
0152     c->Print(0,type);
0153   }
0154 }
0155 /*
0156  * Define different TStyles; use them with:
0157  * getStyle->cd();
0158  *
0159  * 2003 NCA
0160  */
0161 TStyle * getStyle(TString name="myStyle")
0162 {
0163   TStyle *theStyle;
0164   if ( name == "myStyle" ) {
0165     theStyle = new TStyle("myStyle", "myStyle");
0166     //    theStyle->SetOptStat(0);
0167     theStyle->SetPadBorderMode(0);
0168     theStyle->SetCanvasBorderMode(0);
0169     theStyle->SetPadColor(0);
0170     theStyle->SetStatColor(0);
0171     theStyle->SetCanvasColor(0);
0172     theStyle->SetMarkerStyle(8);
0173     theStyle->SetMarkerSize(0.7);
0174     //   theStyle->SetTextFont(132);
0175     //   theStyle->SetTitleFont(132);
0176     theStyle->SetTitleBorderSize(1);
0177     theStyle->SetTitleFillColor(0);
0178     theStyle->SetPalette(1);
0179 // } else if { ... 
0180   } else {
0181     // Avoid modifying the default style!
0182     theStyle = gStyle;
0183   }
0184   return theStyle;
0185 }