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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
// Original Author: Gero Flucke
// last change    : $Date: 2009/01/20 20:21:38 $
// by             : $Author: flucke $

#include "PlotMilleMonitor.h"
#include <TH1.h>
// #include <TH2.h>
// #include <TProfile.h>
#include <TF1.h>
#include <TObjArray.h>

#include <TError.h>
#include <TFile.h>
#include <TROOT.h>
//#include <TCanvas.h>

//#include <iostream>

#include "GFUtils/GFHistManager.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
PlotMilleMonitor::PlotMilleMonitor(const char *fileLegendList)
  : fHistManager(new GFHistManager) //, fFile(TFile::Open(fileName))
{
  //  if (!fFile) ::Error("PlotMilleMonitor", "Could not open file '%s'", fileName);

  if (!this->OpenFilesLegends(fileLegendList)) {
    ::Error("PlotMilleMonitor", "Problem opening files from '%s'", fileLegendList);
  }
  fHistManager->SameWithStats(true);
  fHistManager->SetLegendX1Y1X2Y2(0.14, 0.7, 0.45, 0.9);

}

////////////////////////////////////////////////////////////////////////////////////////////////////
PlotMilleMonitor::~PlotMilleMonitor()
{
  delete fHistManager;

  for (unsigned int i = 0; i < fFileLegends.size(); ++i) delete fFileLegends[i].first;
  //  delete fFile;
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////
// void PlotMilleMonitor::DrawAll(Option_t *opt)
// {
  
//   const TString o(opt);
//   bool wasBatch = fHistManager->SetBatch();
//   fHistManager->Clear();
  
// //   if (o.Contains("d", TString::kIgnoreCase)) this->DrawParamDiff(true);
//   if (o.Contains("r", TString::kIgnoreCase)) this->DrawParamResult(true);
//   if (o.Contains("o", TString::kIgnoreCase)) this->DrawOrigParam(true);
//   if (o.Contains("g", TString::kIgnoreCase)) this->DrawGlobCorr(true);
//   if (o.Contains("p", TString::kIgnoreCase)) this->DrawPull("add");
//   if (o.Contains("m", TString::kIgnoreCase)) this->DrawMisVsLocation(true);
//   if (o.Contains("e", TString::kIgnoreCase)) this->DrawErrorVsHit(true);
//   if (o.Contains("h", TString::kIgnoreCase)) this->DrawHitMaps(true);
  
//   fHistManager->SetBatch(wasBatch);
//   if (!wasBatch) fHistManager->Draw();
//}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PlotMilleMonitor::DrawAllByHit(const char *xy, Option_t * option)
{
  bool wasBatch = fHistManager->SetBatch();
  
  TString opt(option);
  this->DrawResidualsByHit("resid", xy, opt);
  opt += " add";
  this->DrawResidualsByHit("reduResid", xy, opt);
  this->DrawResidualsByHit("sigma", xy, opt);
  this->DrawResidualsByHit("angle", xy, opt);

  fHistManager->SetBatch(wasBatch);
  if (!wasBatch) fHistManager->Draw();
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////
// void PlotMilleMonitor::DrawResidualsByHit(const char *histName, const char *xy, Option_t * option)
// {
//   const TString opt(option);
//   const Int_t layer = this->PrepareAdd(opt.Contains("add", TString::kIgnoreCase));
//   const bool addSumary = opt.Contains("sum", TString::kIgnoreCase);
//   const bool onlySummary = opt.Contains("sumonly", TString::kIgnoreCase);

//   const unsigned int nHitsMax = 29;
//   TH1 *hMean = 0, *hRms = 0;
//   if (addSumary) {
//     hMean = new TH1F(this->Unique(Form("%sMean", histName)),
//                      Form("mean of %s;N(hit)", histName), nHitsMax, 0, nHitsMax);
//     hRms  = new TH1F(this->Unique(Form("%sRms", histName)),
//                      Form("RMS of %s;N(hit)", histName, histName), nHitsMax, 0, nHitsMax);
//   }
//   for (unsigned int i = 0; i < nHitsMax; ++i) {
//     TString completeName(Form("residuals/%s/%s_%u", xy, histName, i));
//     TH1 *h = 0;
//     if (fFile) fFile->GetObject(completeName, h);
//     if (!h) {
//       ::Warning("PlotMilleMonitor::DrawResidualsByHit", "No hist '%s'!", completeName.Data());
//     } else {
//       if (addSumary) {
//         if (i == 0) {
//           hMean->SetYTitle(Form("<%s>", h->GetXaxis()->GetTitle()));
//           hRms->SetYTitle(Form("RMS(%s)", h->GetXaxis()->GetTitle()));
//         }
//         hMean->SetBinContent(i, h->GetMean());
//         hMean->SetBinError(i, h->GetMeanError());
//         hRms->SetBinContent(i, h->GetRMS());
//         hRms->SetBinError(i, h->GetRMSError());
//       }
//       if (onlySummary) {
//         delete h;
//       } else {
//         fHistManager->AddHist(h, layer);
//       }
//     }
//   }
//   fHistManager->AddHist(hMean, layer + (1 * !onlySummary));
//   fHistManager->AddHist(hRms,  layer + (1 * !onlySummary));
    
//   fHistManager->Draw();
//}

////////////////////////////////////////////////////////////////////////////////////////////////////
void PlotMilleMonitor::DrawResidualsByHit(const char *histName, const char *xy, Option_t *option)
{
  Int_t layer = this->PrepareAdd(TString(option).Contains("add", TString::kIgnoreCase));

  for (unsigned int iFile = 0; iFile < fFileLegends.size(); ++iFile) {
    this->AddResidualsByHit(histName, fFileLegends[iFile], layer, xy, option);
  }

  fHistManager->Draw();
}


////////////////////////////////////////////////////////////////////////////////////////////////////
Int_t PlotMilleMonitor::AddResidualsByHit(const char *histName,
                                          std::pair<TFile*,TString> &fileLeg, Int_t layer,
                                          const char *xy, Option_t *option)
{
  // options: - 'sum' to draw mean and RMS vs hit
  //          - 'sumonly' to skip single hit hists  
  // returns how many layers have been added

  const TString opt(option);
  const bool addSumary = opt.Contains("sum", TString::kIgnoreCase);
  const bool onlySummary = opt.Contains("sumonly", TString::kIgnoreCase);
  const bool norm = opt.Contains("norm", TString::kIgnoreCase);
  const bool gaus = opt.Contains("gaus", TString::kIgnoreCase);

  const unsigned int nHitsMax = 30;
  TH1 *hMean = 0, *hRms = 0; //, *hMeanGaus = 0, *hSigmaGaus = 0;
  if (addSumary) {
    hMean = new TH1F(this->Unique(Form("%sMean", histName)),
                     Form("mean of %s;N(hit)", histName), nHitsMax, 0, nHitsMax);
    hRms  = new TH1F(this->Unique(Form("%sRms", histName)),
                     Form("%s of %s;N(hit)", (gaus ? "#sigma" : "RMS"), histName),
		     nHitsMax, 0, nHitsMax);
  }
  for (unsigned int i = 0; i < nHitsMax; ++i) {
    const TString completeName(Form("residuals/%s/%s_%u", xy, histName, i));
    TH1 *h = 0;
    fileLeg.first->GetObject(completeName, h);
    if (!h) {
      ::Warning("PlotMilleMonitor::DrawResidualsByHit", "No hist '%s'!", completeName.Data());
    } else {
      if (gaus) {
	h->Fit("gaus", "Q0L"); // "0": do not directly draw, "Q": quiet, "L" likelihood for bin=0 treatment
	h->GetFunction("gaus")->ResetBit(TF1::kNotDraw);
      }
      if (addSumary) {
        if (i == 0) {
          hMean->SetYTitle(Form("<%s>", h->GetXaxis()->GetTitle()));
          hRms->SetYTitle(Form("%s(%s)", (gaus ? "#sigma" : "RMS"), h->GetXaxis()->GetTitle()));
        }
	if (gaus) {
	  TF1 *func = static_cast<TF1*>(h->GetFunction("gaus"));
	  hMean->SetBinContent(i+1, func->GetParameter(1));
	  hMean->SetBinError  (i+1, func->GetParError(1));
	  hRms-> SetBinContent(i+1, func->GetParameter(2));
	  hRms-> SetBinError  (i+1, func->GetParError(2));
	} else {
	  hMean->SetBinContent(i+1, h->GetMean());
	  hMean->SetBinError  (i+1, h->GetMeanError());
	  hRms-> SetBinContent(i+1, h->GetRMS());
	  hRms-> SetBinError  (i+1, h->GetRMSError());
	}
      }
      if (onlySummary) {
        delete h;
      } else {
        if (norm && h->GetEntries()) h->Scale(1./h->GetEntries());
        fHistManager->AddHistSame(h, layer, i, fileLeg.second);
      }
    }
  }
  if (hMean) fHistManager->AddHistSame(hMean, layer + (1 * !onlySummary), 0, fileLeg.second);
  if (hRms)  fHistManager->AddHistSame(hRms,  layer + (1 * !onlySummary), 1, fileLeg.second);

  return ((addSumary && onlySummary) || (!addSumary) ? 1 : 2);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
Int_t PlotMilleMonitor::PrepareAdd(bool addPlots)
{
  if (addPlots) {
    return fHistManager->GetNumLayers();
  } else {
    fHistManager->Clear();
    return 0;
  }
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////
TString PlotMilleMonitor::Unique(const char *name) const
{
  if (!gROOT->FindObject(name)) return name;
  
  UInt_t i = 1;
  while (gROOT->FindObject(Form("%s_%u", name, i))) ++i;
  
  return Form("%s_%u", name, i);
}


//________________________________________________________
bool PlotMilleMonitor::OpenFilesLegends(const char *fileLegendList)
{
  bool allOk = true;
  
  TObjArray *fileLegPairs = TString(fileLegendList).Tokenize(",");
  for (Int_t iF = 0; iF < fileLegPairs->GetEntriesFast(); ++iF) {
    TObjArray *aFileLegPair = TString(fileLegPairs->At(iF)->GetName()).Tokenize("=");

    const char *legend = "";
    if (aFileLegPair->GetEntriesFast() >= 2) {
      if (aFileLegPair->GetEntriesFast() > 2) {
	::Error("TifResidualOverlay::OpenFilesLegends",
                "File-legend pair %s: %d (>2) '=' separated parts!",
		fileLegPairs->At(iF)->GetName(), aFileLegPair->GetEntriesFast());
      }
      legend = aFileLegPair->At(1)->GetName();
    } else if (aFileLegPair->GetEntriesFast() < 1) {
      continue; // empty: should report error?
    } // else if (aFileLegPair->GetEntriesFast() == 1): That's OK, use empty legend.

    TFile *file = TFile::Open(aFileLegPair->At(0)->GetName());
    if (!file) {
      allOk = false;
    } else {
      // std::pair<TFile*, const TString> fileLeg(file, legend);
      fFileLegends.push_back(std::make_pair(file, legend));
    }
    delete aFileLegPair;
  }
  delete fileLegPairs;

  return allOk;
}