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
/*
 *  See header file for a description of this class.
 *
 *  \author G. Cerminara - INFN Torino
 */

#include "DTTimeBoxPlotter.h"
#include "DTTimeBoxFitter.h"

#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>

#include "TFile.h"
#include "TCanvas.h"
#include "TROOT.h"
#include "TCollection.h"
#include "TSystem.h"
#include "TH2F.h"
// #include "TProfile.h"

using namespace std;

// Constructor
DTTimeBoxPlotter::DTTimeBoxPlotter(TFile* file) : theFile(file), theVerbosityLevel(0) {
  theFitter = new DTTimeBoxFitter();
  theFitter->setVerbosity(1);
}

// Destructor
DTTimeBoxPlotter::~DTTimeBoxPlotter() {}

TH1F* DTTimeBoxPlotter::plotTimeBox(int wheel, int station, int sector, const TString& drawOptions) {
  TString histoName = getHistoNameSuffix(wheel, station, sector) + "_hTimeBox";
  return plotHisto(histoName, drawOptions);
}

TH1F* DTTimeBoxPlotter::plotTimeBox(int wheel, int station, int sector, int sl, const TString& drawOptions) {
  TString histoName = getHistoNameSuffix(wheel, station, sector, sl) + "_hTimeBox";
  return plotHisto(histoName, drawOptions);
}

TH1F* DTTimeBoxPlotter::plotTimeBox(int wheel, int station, int sector, int sl, int layer, const TString& drawOptions) {
  TString histoName = getHistoNameSuffix(wheel, station, sector, sl, layer) + "_hTimeBox";
  return plotHisto(histoName, drawOptions);
}

TH1F* DTTimeBoxPlotter::plotTimeBox(
    int wheel, int station, int sector, int sl, int layer, int wire, const TString& drawOptions) {
  TString histoName = getHistoNameSuffix(wheel, station, sector, sl, layer, wire) + "_hTimeBox";
  return plotHisto(histoName, drawOptions);
}

void DTTimeBoxPlotter::printPDF() {
  TIter iter(gROOT->GetListOfCanvases());
  TCanvas* c;
  while ((c = (TCanvas*)iter())) {
    c->Print(0, "ps");
  }
  gSystem->Exec("ps2pdf *.ps");
}

TString DTTimeBoxPlotter::getHistoNameSuffix(int wheel, int station, int sector) {
  string histoName;
  stringstream theStream;
  theStream << "Ch_" << wheel << "_" << station << "_" << sector << "_SLall_Lall_Wall";
  theStream >> histoName;
  return TString(histoName.c_str());
}

TString DTTimeBoxPlotter::getHistoNameSuffix(int wheel, int station, int sector, int sl) {
  string histoName;
  stringstream theStream;
  theStream << "Ch_" << wheel << "_" << station << "_" << sector << "_SL" << sl;
  theStream >> histoName;
  return TString(histoName.c_str());
}

TString DTTimeBoxPlotter::getHistoNameSuffix(int wheel, int station, int sector, int sl, int layer) {
  string histoName;
  stringstream theStream;
  theStream << "Ch_" << wheel << "_" << station << "_" << sector << "_SL" << sl << "_L" << layer << "_Wall";
  theStream >> histoName;
  return TString(histoName.c_str());
}

TString DTTimeBoxPlotter::getHistoNameSuffix(int wheel, int station, int sector, int sl, int layer, int wire) {
  string histoName;
  stringstream theStream;
  theStream << "Ch_" << wheel << "_" << station << "_" << sector << "_SL" << sl << "_L" << layer << "_W" << wire;
  theStream >> histoName;
  return TString(histoName.c_str());
}

TH1F* DTTimeBoxPlotter::plotHisto(const TString& histoName, const TString& drawOptions) {
  TH1F* histo = (TH1F*)theFile->Get(histoName.Data());
  if (histo == 0) {
    cout << "***Error: Histogram: " << histoName << " doesn't exist!" << endl;
    return 0;
  }
  static int color;

  TCanvas* c;
  if (!drawOptions.Contains("same")) {
    color = 1;
    c = newCanvas("c_" + histoName);
    c->cd();
    histo->SetLineColor(color);
  } else {
    color++;
    histo->SetLineColor(color);
  }
  histo->Draw(TString("h" + drawOptions).Data());

  if (drawOptions.Contains("fit")) {
    theFitter->fitTimeBox(histo);
  }

  return histo;
}

TH2F* DTTimeBoxPlotter::plotHisto2D(const TString& histoName, const TString& drawOptions) {
  TH2F* histo = (TH2F*)theFile->Get(histoName.Data());
  if (histo == 0) {
    cout << "***Error: Histogram: " << histoName << " doesn't exist!" << endl;
    return 0;
  }
  static int color;

  TCanvas* c;
  if (!drawOptions.Contains("same")) {
    color = 1;
    c = newCanvas("c_" + histoName);
    c->cd();
    histo->SetLineColor(color);
  } else {
    color++;
    histo->SetLineColor(color);
  }
  histo->Draw(TString("h" + drawOptions).Data());
  return histo;
}

TCanvas* DTTimeBoxPlotter::newCanvas(TString name, TString title, int xdiv, int ydiv, int form, int w) {
  static int i = 1;
  if (name == "") {
    name = TString("Canvas ") + TString(i);
    i++;
  }
  TCanvas* c = 0;
  if (title == "")
    title = name;
  if (w < 0) {
    c = new TCanvas(name, title, form);
  } else {
    c = new TCanvas(name, title, form, w);
  }
  if (xdiv * ydiv != 0)
    c->Divide(xdiv, ydiv);
  c->cd(1);
  return c;
}

TCanvas* DTTimeBoxPlotter::newCanvas(TString name, int xdiv, int ydiv, int form, int w) {
  return newCanvas(name, name, xdiv, ydiv, form, w);
}
TCanvas* DTTimeBoxPlotter::newCanvas(int xdiv, int ydiv, int form) { return newCanvas("", "", xdiv, ydiv, form); }
TCanvas* DTTimeBoxPlotter::newCanvas(int form) { return newCanvas(0, 0, form); }

TCanvas* DTTimeBoxPlotter::newCanvas(TString name, int form, int w) { return newCanvas(name, name, 0, 0, form, w); }

// Set the verbosity of the output: 0 = silent, 1 = info, 2 = debug
void DTTimeBoxPlotter::setVerbosity(unsigned int lvl) {
  theVerbosityLevel = lvl;
  theFitter->setVerbosity(lvl);
}

void DTTimeBoxPlotter::setInteractiveFit(bool isInteractive) { theFitter->setInteractiveFit(isInteractive); }

void DTTimeBoxPlotter::setRebinning(int rebin) { theFitter->setRebinning(rebin); }