CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
QGLikelihoodDBWriter.cc
Go to the documentation of this file.
1 // Author: Benedikt Hegner, Tom Cornelis
2 // Email: benedikt.hegner@cern.ch, tom.cornelis@cern.ch
3 
4 #include "TFile.h"
5 #include "TVector.h"
6 #include "TList.h"
7 #include "TKey.h"
8 #include "TH1.h"
9 #include <sstream>
10 #include <stdlib.h>
11 #include <vector>
12 #include <memory>
13 #include <string>
23 
25  public:
27  virtual void beginJob() override;
28  virtual void analyze(const edm::Event&, const edm::EventSetup&) override {}
29  virtual void endJob() override {}
31 
32  private:
36 };
37 
38 // Constructor
40  inputRootFile = pSet.getParameter<std::string>("src");
41  payloadTag = pSet.getParameter<std::string>("payload");
42 }
43 
45  size_t subStringPos = myString.find(mySubstring);
46  if(subStringPos != std::string::npos){
47  myString = myString.substr(subStringPos + mySubstring.length(), std::string::npos);
48  return true;
49  } else return false;
50 }
51 
52 // Begin Job
54 
55  QGLikelihoodObject *payload = new QGLikelihoodObject();
56  payload->data.clear();
57 
58  // Get the ROOT file and the vectors with binning information
59  TFile *f = TFile::Open(edm::FileInPath(inputRootFile.c_str()).fullPath().c_str());
60  TVectorT<float> *etaBins; f->GetObject("etaBins", etaBins);
61  TVectorT<float> *ptBinsC; f->GetObject("ptBinsC", ptBinsC);
62  TVectorT<float> *ptBinsF; f->GetObject("ptBinsF", ptBinsF);
63  TVectorT<float> *rhoBins; f->GetObject("rhoBins", rhoBins);
64 
65  // Get keys to the histograms
66  TList *keys = f->GetListOfKeys();
67  if(!keys){
68  edm::LogError("NoKeys") << "There are no keys in the input file." << std::endl;
69  return;
70  }
71 
72  // Loop over directories/histograms
73  TIter nextdir(keys);
74  TKey *keydir;
75  while((keydir = (TKey*)nextdir())){
76  if(!keydir->IsFolder()) continue;
77  TDirectory *dir = (TDirectory*)keydir->ReadObj() ;
78  TIter nexthist(dir->GetListOfKeys());
79  TKey *keyhist;
80  while((keyhist = (TKey*)nexthist())){
81  std::string histname = keyhist->GetName();
82  int varIndex, qgIndex;
83 
84  // First check the variable name, and use index in same order as RecoJets/JetProducers/plugins/QGTagger.cc:73
85  if(extractString("mult", histname)) varIndex = 0;
86  else if(extractString("ptD", histname)) varIndex = 1;
87  else if(extractString("axis2", histname)) varIndex = 2;
88  else continue;
89 
90  // Check quark or gluon
91  if(extractString("quark", histname)) qgIndex = 0;
92  else if(extractString("gluon", histname)) qgIndex = 1;
93  else continue;
94 
95  // Get eta, pt and rho ranges
96  extractString("eta-", histname);
97  int etaBin = std::atoi(histname.substr(0, histname.find("_")).c_str());
98  extractString("pt-", histname);
99  int ptBin = std::atoi(histname.substr(0, histname.find("_")).c_str());
100  extractString("rho-", histname);
101  int rhoBin = std::atoi(histname.substr(0, histname.find("_")).c_str());
102 
103  float etaMin = (*etaBins)[etaBin];
104  float etaMax = (*etaBins)[etaBin+1];
105  TVectorT<float> *ptBins = (etaBin == 0? ptBinsC : ptBinsF);
106  float ptMin = (*ptBins)[ptBin];
107  float ptMax = (*ptBins)[ptBin+1];
108  float rhoMin = (*rhoBins)[rhoBin];
109  float rhoMax = (*rhoBins)[rhoBin+1];
110 
111  // Print out for debugging
112  char buff[1000];
113  sprintf(buff, "%50s : var=%1d, qg=%1d, etaMin=%6.2f, etaMax=%6.2f, ptMin=%8.2f, ptMax=%8.2f, rhoMin=%6.2f, rhoMax=%6.2f", keyhist->GetName(), varIndex, qgIndex, etaMin, etaMax, ptMin, ptMax, rhoMin, rhoMax );
114  edm::LogVerbatim("HistName") << buff << std::endl;
115 
116  // Define category parameters
118  category.RhoMin = rhoMin;
119  category.RhoMax = rhoMax;
120  category.PtMin = ptMin;
121  category.PtMax = ptMax;
122  category.EtaMin = etaMin;
123  category.EtaMax = etaMax;
124  category.QGIndex = qgIndex;
125  category.VarIndex = varIndex;
126 
127  // Get TH1
128  TH1* th1hist = (TH1*) keyhist->ReadObj();
129 
130  // Transform ROOT TH1 to QGLikelihoodObject (same indexing)
131  QGLikelihoodObject::Histogram histogram(th1hist->GetNbinsX(), th1hist->GetXaxis()->GetBinLowEdge(1), th1hist->GetXaxis()->GetBinUpEdge(th1hist->GetNbinsX()));
132  for(int ibin = 0; ibin <= th1hist->GetNbinsX() + 1; ++ibin){
133  histogram.setBinContent(ibin, th1hist->GetBinContent(ibin));
134  }
135 
136  // Add this entry with its category parameters, histogram and mean
138  entry.category = category;
139  entry.histogram = histogram;
140  entry.mean = th1hist->GetMean();
141  payload->data.push_back(entry);
142  }
143  }
144  // Define the valid range, if no category is found within these bounds a warning will be thrown
145  payload->qgValidRange.RhoMin = rhoBins->Min();
146  payload->qgValidRange.RhoMax = rhoBins->Max();
147  payload->qgValidRange.EtaMin = etaBins->Min();
148  payload->qgValidRange.EtaMax = etaBins->Max();
149  payload->qgValidRange.PtMin = ptBinsC->Min();
150  payload->qgValidRange.PtMax = ptBinsC->Max();
151  payload->qgValidRange.QGIndex = -1;
152  payload->qgValidRange.VarIndex = -1;
153 
154  // Now write it into the DB
155  edm::LogInfo("UserOutput") << "Opening PoolDBOutputService" << std::endl;
156 
158  if(s.isAvailable()){
159  edm::LogInfo("UserOutput") << "Setting up payload with " << payload->data.size() << " entries and tag " << payloadTag << std::endl;
161  else s->appendSinceTime<QGLikelihoodObject>(payload, 111, payloadTag);
162  }
163  edm::LogInfo("UserOutput") << "Wrote in CondDB QGLikelihood payload label: " << payloadTag << std::endl;
164 }
165 
166 
168 
T getParameter(std::string const &) const
QGLikelihoodCategory category
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
QGLikelihoodCategory qgValidRange
void setBinContent(int bin, Value_t value)
void appendSinceTime(T *payloadObj, cond::Time_t sinceTime, const std::string &recordName, bool withlogging=false)
QGLikelihoodDBWriter(const edm::ParameterSet &)
QGLikelihoodObject containing valid range and entries with category, histogram and mean...
bool isNewTagRequest(const std::string &recordName)
bool extractString(std::string, std::string &)
bool isAvailable() const
Definition: Service.h:46
double f[11][100]
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
Category structure: ranges associated with QGLikelihood histograms.
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
std::vector< Entry > data
virtual void beginJob() override
dbl *** dir
Definition: mlp_gen.cc:35
virtual void endJob() override