Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/** 
 * A collection of simple ROOT macros
 *
 * N. Amapane 2002-2004
 */
/*
 * Draw 2-D plots superimposed to their profiles
 *
 * 2003 NCA
 */
// Draw a 2-D plot within the specified Y range and superimpose its X profile
void plotAndProfileX (TH2* h2, float min, float max, bool profile=false) {
  gPad->SetGrid(1,1);
  gStyle->SetGridColor(15);
  h2->GetYaxis()->SetRangeUser(min,max);
  h2->Draw();
  if (profile) {
    TProfile* prof = h2->ProfileX();
    prof->SetMarkerColor(2);
    prof->SetLineColor(2);
    prof->Draw("same");
  }
  TLine * l = new TLine(h2->GetXaxis()->GetXmin(),0,h2->GetXaxis()->GetXmax(),0);
  l->SetLineColor(3);
  l->Draw();
}
// void plotAndProfileY (TH2* h2, float min, float max) {
//   h2->GetYaxis()->SetRangeUser(min,max);
//   h2->Draw();
//   TProfile* prof = h2->ProfileY();
//   prof->SetMarkerStyle(8);
//   prof->SetMarkerSize(0.7);
//   prof->SetMarkerColor(2);
//   prof->SetLineColor(2);
//   prof->Draw("same");
// }
/*
 * Draw and format a fitted histogram 
 *
 * 2003 NCA
 */
// Fit a histogram with a gaussian and draw it in the range 
// mean+-nsigmas*RMS.
void drawGFit(TH1 * h1, float nsigmas, float min, float max){
  float minfit = h1->GetMean() - h1->GetRMS();
  float maxfit = h1->GetMean() + h1->GetRMS();
  drawGFit(h1, min, max, minfit, maxfit);
  gPad->Draw();
}
// Fit a histogram with a gaussian and draw it in the specified range.
void drawGFit(TH1 * h1, float min, float max){
  drawGFit(h1, min, max, min, max);
  gPad->Draw();
}
// Fit a histogram in the range (minfit, maxfit) with a gaussian and
// draw it in the range (min, max)
void drawGFit(TH1 * h1, float min, float max, float minfit, float maxfit) {
  static int i = 0;
  i++;
  gPad->SetGrid(1,1);
  gStyle->SetGridColor(15);
  h1->GetXaxis()->SetRangeUser(min,max);
  TF1* g1 = new TF1(TString("g")+i,"gaus",minfit,maxfit);
  g1->SetLineColor(2);
  g1->SetLineWidth(2);
  h1->Fit(g1,"R");
//   TPaveStats *st = (TPaveStats*)h1->GetListOfFunctions()->FindObject("stats");
//   st->SetX2NDC(0.905);
//   st->SetY2NDC(0.905);
}
/*
 * Create a new TCanvas setting its properties
 *
 * 2003 NCA 
 */
// Specify name, title, x/y divisions, form or x,y sizes.
// If no name is specified, a new name is generated automatically
TCanvas * newCanvas(TString name="", TString title="",
                     Int_t xdiv=0, Int_t ydiv=0, Int_t form = 1, Int_t w=-1){
  static int i = 1;
  if (name == "") {
    name = TString("Canvas ") + i;
    i++;
  }
  if (title == "") title = name;
  if (w<0) {
    TCanvas * c = new TCanvas(name,title, form);
  } else {
    TCanvas * c = new TCanvas(name,title,form,w);
  }
  if (xdiv*ydiv!=0) c->Divide(xdiv,ydiv);
  c->cd(1);
  return c;
}
// Create a new canvas with an automatic generated name and the specified 
// divisions and form
TCanvas * newCanvas(Int_t xdiv, Int_t ydiv, Int_t form = 1) {
  return newCanvas("","",xdiv,ydiv,form);
}
// Create a new canvas with an automatic generated name and the specified 
// form
TCanvas * newCanvas(Int_t form = 1)
{
  return newCanvas(0,0,form);
}
// ...without specifying the title...
TCanvas * newCanvas(TString name, Int_t xdiv, Int_t ydiv, Int_t form,
                    Int_t w) {
  return newCanvas(name, name,xdiv,ydiv,form,w);
}
// ...without specifying title and divisions.
TCanvas * newCanvas(TString name, Int_t form, Int_t w=-1)
{
  return newCanvas(name, name, 0,0,form,w);
}
/*
 * Print all open canvases to PS or EPS files.
 *
 * 2003 NCA 
 */
// Print all canvases in a single PS file
void printCanvasesPS(TString name){
  TPostScript * ps = new TPostScript(name,112);
  TIter iter(gROOT->GetListOfCanvases());
  TCanvas *c;
  while( (c = (TCanvas *)iter()) )
    {
      cout << "Printing " << c->GetName() << endl;
      ps->NewPage();
      c->Draw();
    }
  cout << " File " << name << " was created" << endl;
  ps->Close();
}
// Print all canvases in separate EPS files
void printCanvasesEps(){
  TIter iter(gROOT->GetListOfCanvases());
  TCanvas *c;
  while( (c = (TCanvas *)iter()) ) {
    c->Print(0,"eps");
  }
}
// Print all canvases in separate EPS files (another way)
void printCanvasesEps2() {
  gROOT->GetListOfCanvases()->Print("eps");
}
// Print all canvases in separate EPS files
void printCanvases(TString type="eps"){
  TIter iter(gROOT->GetListOfCanvases());
  TCanvas *c;
  while( (c = (TCanvas *)iter()) ) {
    c->Print(0,type);
  }
}
/*
 * Define different TStyles; use them with:
 * getStyle->cd();
 *
 * 2003 NCA
 */
TStyle * getStyle(TString name="myStyle")
{
  TStyle *theStyle;
  if ( name == "myStyle" ) {
    theStyle = new TStyle("myStyle", "myStyle");
    //    theStyle->SetOptStat(0);
    theStyle->SetPadBorderMode(0);
    theStyle->SetCanvasBorderMode(0);
    theStyle->SetPadColor(0);
    theStyle->SetStatColor(0);
    theStyle->SetCanvasColor(0);
    theStyle->SetMarkerStyle(8);
    theStyle->SetMarkerSize(0.7);
    //   theStyle->SetTextFont(132);
    //   theStyle->SetTitleFont(132);
    theStyle->SetTitleBorderSize(1);
    theStyle->SetTitleFillColor(0);
    theStyle->SetPalette(1);
// } else if { ... 
  } else {
    // Avoid modifying the default style!
    theStyle = gStyle;
  }
  return theStyle;
}