Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:06:42

0001 #include <iostream>
0002 #include "DPGAnalysis/SiStripTools/interface/CommonAnalyzer.h"
0003 #include "TFile.h"
0004 #include "TDirectory.h"
0005 #include "TObject.h"
0006 #include "TH1F.h"
0007 #include "TNamed.h"
0008 #include "TList.h"
0009 #include "TKey.h"
0010 #include "TClass.h"
0011 #include <string>
0012 #include <vector>
0013 
0014 CommonAnalyzer::CommonAnalyzer(TFile* file, const char* run, const char* mod, const char* path, const char* prefix)
0015     : _file(file), _runnumber(run), _module(mod), _path(path), _prefix(prefix) {}
0016 
0017 CommonAnalyzer::CommonAnalyzer(const CommonAnalyzer& dtca)
0018     : _file(dtca._file), _runnumber(dtca._runnumber), _module(dtca._module), _path(dtca._path), _prefix(dtca._prefix) {}
0019 
0020 CommonAnalyzer& CommonAnalyzer::operator=(const CommonAnalyzer& dtca) {
0021   if (this != &dtca) {
0022     _file = dtca._file;
0023     _runnumber = dtca._runnumber;
0024     _module = dtca._module;
0025     _path = dtca._path;
0026     _prefix = dtca._prefix;
0027   }
0028   return *this;
0029 }
0030 
0031 void CommonAnalyzer::setRunNumber(const char* run) { _runnumber = run; }
0032 void CommonAnalyzer::setFile(TFile* file) { _file = file; }
0033 void CommonAnalyzer::setModule(const char* mod) { _module = mod; }
0034 void CommonAnalyzer::setPath(const char* path) { _path = path; }
0035 void CommonAnalyzer::setPrefix(const char* prefix) { _prefix = prefix; }
0036 
0037 const std::string& CommonAnalyzer::getRunNumber() const { return _runnumber; }
0038 const std::string& CommonAnalyzer::getModule() const { return _module; }
0039 const std::string& CommonAnalyzer::getPath() const { return _path; }
0040 const std::string& CommonAnalyzer::getPrefix() const { return _prefix; }
0041 
0042 TObject* CommonAnalyzer::getObject(const char* name) const {
0043   TObject* obj = nullptr;
0044 
0045   std::string fullpath = _module + "/" + _path;
0046   if (_file) {
0047     bool ok = _file->cd(fullpath.c_str());
0048     if (ok && gDirectory) {
0049       obj = gDirectory->Get(name);
0050     }
0051   }
0052   return obj;
0053 }
0054 
0055 TNamed* CommonAnalyzer::getObjectWithSuffix(const char* name, const char* suffix) const {
0056   TNamed* obj = (TNamed*)getObject(name);
0057 
0058   if (obj) {
0059     if (!strstr(obj->GetTitle(), "run")) {
0060       char htitle[300];
0061       sprintf(htitle, "%s %s run %s", obj->GetTitle(), suffix, _runnumber.c_str());
0062       obj->SetTitle(htitle);
0063     }
0064   }
0065   return obj;
0066 }
0067 
0068 const std::vector<unsigned int> CommonAnalyzer::getRunList() const { return getList("run"); }
0069 
0070 const std::vector<unsigned int> CommonAnalyzer::getFillList() const { return getList("fill"); }
0071 
0072 const std::vector<unsigned int> CommonAnalyzer::getList(const char* what) const {
0073   std::vector<unsigned int> runlist;
0074   char searchstring[100];
0075   char decodestring[100];
0076   sprintf(searchstring, "%s_", what);
0077   sprintf(decodestring, "%s_%%u", what);
0078 
0079   std::string fullpath = _module + "/" + _path;
0080   if (_file) {
0081     bool ok = _file->cd(fullpath.c_str());
0082     if (ok && gDirectory) {
0083       TList* keys = gDirectory->GetListOfKeys();
0084       TListIter it(keys);
0085       TKey* key = nullptr;
0086       while ((key = (TKey*)it.Next())) {
0087         std::cout << key->GetName() << std::endl;
0088         TClass cl(key->GetClassName());
0089         if (cl.InheritsFrom("TDirectory") && strstr(key->GetName(), searchstring) != nullptr) {
0090           unsigned int run;
0091           sscanf(key->GetName(), decodestring, &run);
0092           runlist.push_back(run);
0093         }
0094       }
0095     }
0096   }
0097   //  sort(runlist);
0098   return runlist;
0099 }
0100 
0101 TH1F* CommonAnalyzer::getBinomialRatio(const CommonAnalyzer& denom, const char* name, const int rebin) const {
0102   TH1F* den = (TH1F*)denom.getObject(name);
0103   TH1F* num = (TH1F*)getObject(name);
0104   TH1F* ratio = nullptr;
0105 
0106   if (den != nullptr && num != nullptr) {
0107     TH1F* denreb = den;
0108     TH1F* numreb = num;
0109     if (rebin > 0) {
0110       denreb = (TH1F*)den->Rebin(rebin, "denrebinned");
0111       numreb = (TH1F*)num->Rebin(rebin, "numrebinned");
0112     }
0113 
0114     ratio = new TH1F(*numreb);
0115     ratio->SetDirectory(nullptr);
0116     ratio->Reset();
0117     ratio->Sumw2();
0118     ratio->Divide(numreb, denreb, 1, 1, "B");
0119     delete denreb;
0120     delete numreb;
0121   }
0122 
0123   return ratio;
0124 }