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
#include <iostream>
#include "DPGAnalysis/SiStripTools/interface/CommonAnalyzer.h"
#include "TFile.h"
#include "TDirectory.h"
#include "TObject.h"
#include "TH1F.h"
#include "TNamed.h"
#include "TList.h"
#include "TKey.h"
#include "TClass.h"
#include <string>
#include <vector>

CommonAnalyzer::CommonAnalyzer(TFile* file, const char* run, const char* mod, const char* path, const char* prefix)
    : _file(file), _runnumber(run), _module(mod), _path(path), _prefix(prefix) {}

CommonAnalyzer::CommonAnalyzer(const CommonAnalyzer& dtca)
    : _file(dtca._file), _runnumber(dtca._runnumber), _module(dtca._module), _path(dtca._path), _prefix(dtca._prefix) {}

CommonAnalyzer& CommonAnalyzer::operator=(const CommonAnalyzer& dtca) {
  if (this != &dtca) {
    _file = dtca._file;
    _runnumber = dtca._runnumber;
    _module = dtca._module;
    _path = dtca._path;
    _prefix = dtca._prefix;
  }
  return *this;
}

void CommonAnalyzer::setRunNumber(const char* run) { _runnumber = run; }
void CommonAnalyzer::setFile(TFile* file) { _file = file; }
void CommonAnalyzer::setModule(const char* mod) { _module = mod; }
void CommonAnalyzer::setPath(const char* path) { _path = path; }
void CommonAnalyzer::setPrefix(const char* prefix) { _prefix = prefix; }

const std::string& CommonAnalyzer::getRunNumber() const { return _runnumber; }
const std::string& CommonAnalyzer::getModule() const { return _module; }
const std::string& CommonAnalyzer::getPath() const { return _path; }
const std::string& CommonAnalyzer::getPrefix() const { return _prefix; }

TObject* CommonAnalyzer::getObject(const char* name) const {
  TObject* obj = nullptr;

  std::string fullpath = _module + "/" + _path;
  if (_file) {
    bool ok = _file->cd(fullpath.c_str());
    if (ok && gDirectory) {
      obj = gDirectory->Get(name);
    }
  }
  return obj;
}

TNamed* CommonAnalyzer::getObjectWithSuffix(const char* name, const char* suffix) const {
  TNamed* obj = (TNamed*)getObject(name);

  if (obj) {
    if (!strstr(obj->GetTitle(), "run")) {
      char htitle[300];
      sprintf(htitle, "%s %s run %s", obj->GetTitle(), suffix, _runnumber.c_str());
      obj->SetTitle(htitle);
    }
  }
  return obj;
}

const std::vector<unsigned int> CommonAnalyzer::getRunList() const { return getList("run"); }

const std::vector<unsigned int> CommonAnalyzer::getFillList() const { return getList("fill"); }

const std::vector<unsigned int> CommonAnalyzer::getList(const char* what) const {
  std::vector<unsigned int> runlist;
  char searchstring[100];
  char decodestring[100];
  sprintf(searchstring, "%s_", what);
  sprintf(decodestring, "%s_%%u", what);

  std::string fullpath = _module + "/" + _path;
  if (_file) {
    bool ok = _file->cd(fullpath.c_str());
    if (ok && gDirectory) {
      TList* keys = gDirectory->GetListOfKeys();
      TListIter it(keys);
      TKey* key = nullptr;
      while ((key = (TKey*)it.Next())) {
        std::cout << key->GetName() << std::endl;
        TClass cl(key->GetClassName());
        if (cl.InheritsFrom("TDirectory") && strstr(key->GetName(), searchstring) != nullptr) {
          unsigned int run;
          sscanf(key->GetName(), decodestring, &run);
          runlist.push_back(run);
        }
      }
    }
  }
  //  sort(runlist);
  return runlist;
}

TH1F* CommonAnalyzer::getBinomialRatio(const CommonAnalyzer& denom, const char* name, const int rebin) const {
  TH1F* den = (TH1F*)denom.getObject(name);
  TH1F* num = (TH1F*)getObject(name);
  TH1F* ratio = nullptr;

  if (den != nullptr && num != nullptr) {
    TH1F* denreb = den;
    TH1F* numreb = num;
    if (rebin > 0) {
      denreb = (TH1F*)den->Rebin(rebin, "denrebinned");
      numreb = (TH1F*)num->Rebin(rebin, "numrebinned");
    }

    ratio = new TH1F(*numreb);
    ratio->SetDirectory(nullptr);
    ratio->Reset();
    ratio->Sumw2();
    ratio->Divide(numreb, denreb, 1, 1, "B");
    delete denreb;
    delete numreb;
  }

  return ratio;
}