Просмотр исходного кода

Added new feature to the DrawTGraph, added DrawPads.C macro

PeterParfenov 1 год назад
Родитель
Сommit
195ec7ce59
2 измененных файлов с 97 добавлено и 6 удалено
  1. 62 0
      DrawPads.C
  2. 35 6
      DrawTGraph.C

+ 62 - 0
DrawPads.C

@@ -0,0 +1,62 @@
+std::vector<TPad*> MakePadGrid(int Ncolumns=1, int Nrows=1, double left_margin=0.2, double right_margin=0.02, double bottom_margin=0.2, double top_margin=0.02)
+{
+    std::vector<TPad*> vpads;
+    double xl, xh, yl, yh; //(x,y) low and high coordinates for pads
+    double ml, mr, mt, mb; // margins for pads (left, right, top, bottom)
+    const double min_margin = 0.002; // minimal margin to avoid cropping drawn axes
+    int Nsize = Ncolumns*Nrows;
+
+    for (int iRow=0; iRow<Nrows; iRow++)
+    {
+        for (int iColumn=0; iColumn<Ncolumns; iColumn++)
+        {
+            xl = (iColumn == 0) ? 0. : (double)( iColumn+left_margin ) / (double) ( Ncolumns+left_margin+right_margin );
+            xh = (iColumn == Ncolumns-1) ? 1. : (double)( iColumn+1+left_margin ) / (double) ( Ncolumns+left_margin+right_margin );
+            yl = (iRow == Nrows-1) ? 0. : 1. - (double)( iRow+1+top_margin ) / (double) ( Nrows+top_margin+bottom_margin );
+            yh = (iRow == 0) ? 1. : 1. - (double)( iRow+top_margin ) / (double) ( Nrows+top_margin+bottom_margin );
+
+            ml = (iColumn == 0) ? left_margin/(1.+left_margin) : min_margin;
+            mr = (iColumn == Ncolumns-1) ? right_margin/(1.+right_margin) : min_margin;
+            mb = (iRow == Nrows-1) ? bottom_margin/(1.+bottom_margin) : min_margin;
+            mt = (iRow == 0) ? top_margin/(1.+top_margin) : min_margin;
+
+            std::cout << Form("\t%3i (Column %2i, Row %2i ): Pad coordinates (xlow, ylow, xhigh,yhigh) in NDC: (%1.2f, %1.2f, %1.2f, %1.2f)", iColumn+Ncolumns*iRow, iColumn, iRow, xl, yl, xh, yh) << std::endl;
+
+            vpads.push_back(new TPad(Form("pad_%i", iColumn+Ncolumns*iRow), Form("pad %i: %i column, %i row", iColumn+Ncolumns*iRow, iColumn, iRow), xl, yl, xh, yh));
+            if (!vpads.at(vpads.size()-1))
+            {
+                std::cerr << "Error: something has gone wrong, vector has nullptr at " << vpads.at(vpads.size()-1) << std::endl;
+            }
+            else
+            {
+                vpads.at(vpads.size()-1)->SetMargin(ml, mr, mb, mt);
+            }
+        }
+    }
+
+    return vpads;
+}
+
+void DrawPads(int Ncolumns=1, int Nrows=1, double left_margin=0.2, double right_margin=0.02, double bottom_margin=0.2, double top_margin=0.02)
+{
+    TCanvas *canvas = new TCanvas("canvas", "", 900, 900);
+    TLatex tex;
+    canvas->cd();
+    std::vector<TPad*> vpads = MakePadGrid(Ncolumns, Nrows, left_margin, right_margin, bottom_margin, top_margin);
+    TH1D* hist = new TH1D("hist","hist;x;y",100,-5.,5.);
+    hist->GetXaxis()->SetRangeUser(-4.9,4.9);
+    hist->GetYaxis()->SetRangeUser(-0.1,9.9);
+
+    for (int i=0; i<vpads.size(); i++)
+    {
+        vpads.at(i)->Draw();
+    }
+
+    for (int i=0; i<vpads.size(); i++)
+    {
+        vpads.at(i)->cd();
+        hist->Draw();
+        tex.SetTextSize(0.2);
+        tex.DrawLatexNDC(0.48, 0.45, Form("%i", i));
+    }
+}

+ 35 - 6
DrawTGraph.C

@@ -176,7 +176,10 @@ TCanvas *DrawTGraph(std::vector<TGraphErrors *> vgr, TString str,
                     Double_t x_low = 0.0, Double_t x_high = 1.0,
                     Double_t y_low = 0.0, Double_t y_high = 1.0,
                     Double_t leg_x_low = 0.22, Double_t leg_y_low = 0.55,
-                    Double_t leg_x_high = 0.55, Double_t leg_y_high = 0.89, TString str_ratio = "Ratio", Bool_t isLogy = false, Bool_t isOnly1Line = false, Bool_t isPMC = true, std::vector<std::pair<Int_t, Int_t>> vBorders={})
+                    Double_t leg_x_high = 0.55, Double_t leg_y_high = 0.89, 
+                    TString str_ratio = "Ratio", Bool_t isLogy = false, 
+                    Bool_t isOnly1Line = false, Bool_t isPMC = true, 
+                    std::vector<std::pair<Int_t, Int_t>> vBorders={})
 {
   // Setting up global variables for the plot
   gROOT->SetStyle("Pub");
@@ -830,15 +833,17 @@ void SaveTGraph(TString outFileName, TGraphErrors *const &gr1, TGraphErrors *con
 }
 
 // Convert TProfile into TGraphErrors
-TGraphErrors *ConvertProfile(TProfile *const &pr)
+TGraphErrors *ConvertProfile(TProfile *const &pr, 
+                            Double_t scaleX=1.0, Double_t scaleY=1.0,
+                            Double_t scaleEX=1.0, Double_t scaleEY=1.0)
 {
   std::vector<Double_t> vX, vY, vEx, vEy;
   for (int ibin = 0; ibin < pr->GetNbinsX(); ibin++)
   {
-    vX.push_back(pr->GetBinCenter(ibin + 1));
-    vEx.push_back(0.);
-    vY.push_back(pr->GetBinContent(ibin + 1));
-    vEy.push_back(pr->GetBinError(ibin + 1));
+    vX.push_back(scaleX*pr->GetBinCenter(ibin + 1));
+    vEx.push_back(scaleEX*0.);
+    vY.push_back(scaleY*pr->GetBinContent(ibin + 1));
+    vEy.push_back(scaleEY*pr->GetBinError(ibin + 1));
   }
   TGraphErrors *gr = new TGraphErrors(vX.size(), &vX[0], &vY[0], &vEx[0], &vEy[0]);
   gr->SetTitle(pr->GetTitle());
@@ -969,6 +974,30 @@ TGraphErrors *ShiftX(TGraphErrors *const &gr, double shift)
   return graph;
 }
 
+TH1D *ConvertTH1I2TH1D(TH1I *const& hist, bool is_resample = false)
+{
+  TH1D *result = new TH1D(Form("th1i2th1d_%s", hist->GetName()), Form("converted %s", hist->GetTitle()), hist->GetNbinsX(), hist->GetXaxis()->GetXmin(), hist->GetXaxis()->GetXmax());
+
+  if (!is_resample)
+  {
+    for (int ibin=0; ibin<hist->GetNbinsX(); ibin++)
+    {
+      result->SetBinContent(ibin+1, hist->GetBinContent(ibin+1));
+      result->SetBinError(ibin+1, hist->GetBinError(ibin+1));
+      result->SetEntries(hist->GetEntries());
+    }
+  }
+
+  if (is_resample)
+  {
+    for (int ientry=0; ientry<hist->GetEntries(); ientry++)
+    {
+      result->Fill(hist->GetRandom());
+    }
+  }
+
+  return result;
+}
 
 
 // Test function. Shows basic usage of DrawTGraph()