Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:44

0001 /** 
0002  * A collection of simple ROOT macros
0003  *
0004  * N. Amapane 2002-2004
0005  */
0006 
0007 
0008 
0009 
0010 
0011 
0012 /*
0013  * Draw 2-D plots superimposed to their profiles
0014  *
0015  * 2003 NCA
0016  */
0017 
0018 // Draw a 2-D plot within the specified Y range and superimpose its X profile
0019 void plotAndProfileX (TH2* h2, float min, float max, bool profile=false) {
0020   gPad->SetGrid(1,1);
0021   gStyle->SetGridColor(15);
0022   h2->GetYaxis()->SetRangeUser(min,max);
0023   h2->Draw();
0024   if (profile) {
0025     TProfile* prof = h2->ProfileX();
0026     prof->SetMarkerColor(2);
0027     prof->SetLineColor(2);
0028     prof->Draw("same");
0029   }
0030   TLine * l = new TLine(h2->GetXaxis()->GetXmin(),0,h2->GetXaxis()->GetXmax(),0);
0031   l->SetLineColor(3);
0032   l->Draw();
0033 }
0034 
0035 // void plotAndProfileY (TH2* h2, float min, float max) {
0036 //   h2->GetYaxis()->SetRangeUser(min,max);
0037 //   h2->Draw();
0038 //   TProfile* prof = h2->ProfileY();
0039 //   prof->SetMarkerStyle(8);
0040 //   prof->SetMarkerSize(0.7);
0041 //   prof->SetMarkerColor(2);
0042 //   prof->SetLineColor(2);
0043 //   prof->Draw("same");
0044 // }
0045 
0046 
0047 
0048 
0049 
0050 
0051 
0052 /*
0053  * Draw and format a fitted histogram 
0054  *
0055  * 2003 NCA
0056  */
0057 
0058 // Fit a histogram with a gaussian and draw it in the specified range.
0059 void drawGFit(TH1 * h1, float min, float max){
0060   drawGFit(h1, min, max, min, max);
0061   gPad->Draw();
0062 }
0063 
0064 // Fit a histogram in the range (minfit, maxfit) with a gaussian and
0065 // draw it in the range (min, max)
0066 void drawGFit(TH1 * h1, float min, float max, float minfit, float maxfit) {
0067   static int i = 0;
0068   i++;
0069   gPad->SetGrid(1,1);
0070   gStyle->SetGridColor(15);
0071 
0072   h1->GetXaxis()->SetRangeUser(min,max);
0073   TF1* g1 = new TF1(TString("g")+i,"gaus",minfit,maxfit);
0074   g1->SetLineColor(2);
0075   g1->SetLineWidth(2);
0076   h1->Fit(g1,"R");
0077 //   TPaveStats *st = (TPaveStats*)h1->GetListOfFunctions()->FindObject("stats");
0078 //   st->SetX2NDC(0.905);
0079 //   st->SetY2NDC(0.905);
0080 
0081 }
0082 
0083 
0084 
0085 
0086 
0087 
0088 
0089 /*
0090  * Create a new TCanvas setting its properties
0091  *
0092  * 2003 NCA 
0093  */
0094 
0095 // Specify name, title, x/y divisions, form or x,y sizes.
0096 // If no name is specified, a new name is generated automatically
0097 TCanvas * newCanvas(TString name="", TString title="",
0098                      Int_t xdiv=0, Int_t ydiv=0, Int_t form = 1, Int_t w=-1){
0099 
0100   static int i = 1;
0101 
0102   if (name == "") {
0103     name = TString("Canvas ") + i;
0104     i++;
0105   }
0106 
0107   if (title == "") title = name;
0108 
0109   if (w<0) {
0110     TCanvas * c = new TCanvas(name,title, form);
0111   } else {
0112     TCanvas * c = new TCanvas(name,title,form,w);
0113   }
0114 
0115   if (xdiv*ydiv!=0) c->Divide(xdiv,ydiv);
0116   c->cd(1);
0117   return c;
0118 }
0119 
0120 // Create a new canvas with an automatic generated name and the specified 
0121 // divisions and form
0122 TCanvas * newCanvas(Int_t xdiv, Int_t ydiv, Int_t form = 1) {
0123   return newCanvas("","",xdiv,ydiv,form);
0124 }
0125 
0126 // Create a new canvas with an automatic generated name and the specified 
0127 // form
0128 TCanvas * newCanvas(Int_t form = 1)
0129 {
0130   return newCanvas(0,0,form);
0131 }
0132 
0133 // ...without specifying the title...
0134 TCanvas * newCanvas(TString name, Int_t xdiv=0, Int_t ydiv=0, Int_t form = 1,
0135                     Int_t w=-1) {
0136   return newCanvas(name, name,xdiv,ydiv,form,w);
0137 }
0138 
0139 // ...without specifying title and divisions.
0140 TCanvas * newCanvas(TString name, Int_t form = 1, Int_t w=-1)
0141 {
0142   return newCanvas(name, name, 0,0,form,w);
0143 }
0144 
0145 
0146 
0147 
0148 
0149 
0150 /*
0151  * Print all open canvases to PS or EPS files.
0152  *
0153  * 2003 NCA 
0154  */
0155 
0156 // Print all canvases in a single PS file
0157 void printCanvasesPS(TString name){
0158   TPostScript * ps = new TPostScript(name,112);
0159   TIter iter(gROOT->GetListOfCanvases());
0160 
0161   TCanvas *c;
0162 
0163   while( (c = (TCanvas *)iter()) )
0164     {
0165       cout << "Printing " << c->GetName() << endl;
0166       ps->NewPage();
0167       c->Draw();
0168     }
0169   cout << " File " << name << " was created" << endl;
0170   ps->Close();
0171 }
0172 
0173 // Print all canvases in separate EPS files
0174 void printCanvasesEps(){
0175   TIter iter(gROOT->GetListOfCanvases());
0176   TCanvas *c;
0177   while( (c = (TCanvas *)iter()) ) {
0178     c->Print(0,"eps");
0179   }
0180 }
0181 
0182 // Print all canvases in separate EPS files (another way)
0183 void printCanvasesEps2() {
0184   gROOT->GetListOfCanvases()->Print("eps");
0185 }
0186 
0187 
0188 // Print all canvases in separate EPS files
0189 void printCanvases(TString type="eps"){
0190   TIter iter(gROOT->GetListOfCanvases());
0191   TCanvas *c;
0192   while( (c = (TCanvas *)iter()) ) {
0193     c->Print(0,type);
0194   }
0195 }
0196 
0197 
0198 
0199 
0200 
0201 
0202 /*
0203  * Define different TStyles; use them with:
0204  * getStyle->cd();
0205  *
0206  * 2003 NCA
0207  */
0208 
0209 TStyle * getStyle(TString name="myStyle")
0210 {
0211   TStyle *theStyle;
0212 
0213   if ( name == "myStyle" ) {
0214     theStyle = new TStyle("myStyle", "myStyle");
0215     //    theStyle->SetOptStat(0);
0216     theStyle->SetPadBorderMode(0);
0217     theStyle->SetCanvasBorderMode(0);
0218     theStyle->SetPadColor(0);
0219     theStyle->SetCanvasColor(0);
0220     theStyle->SetMarkerStyle(8);
0221     theStyle->SetMarkerSize(0.7);
0222     //   theStyle->SetTextFont(132);
0223     //   theStyle->SetTitleFont(132);
0224     theStyle->SetTitleBorderSize(1);
0225     theStyle->SetPalette(1);
0226 
0227 // } else if { ... 
0228 
0229   } else {
0230     // Avoid modifying the default style!
0231     theStyle = gStyle;
0232   }
0233   return theStyle;
0234 }