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 "TList.h"
6 #include "TKey.h"
7 #include "TH1.h"
8 #include <sstream>
9 #include <stdlib.h>
10 #include <vector>
11 #include <memory>
12 #include <string>
22 
24  public:
26  virtual void beginJob() override;
27  virtual void analyze(const edm::Event&, const edm::EventSetup&) override {}
28  virtual void endJob() override {}
30 
31  private:
35 };
36 
37 // Constructor
39  inputRootFile = pSet.getParameter<std::string>("src");
40  payloadTag = pSet.getParameter<std::string>("payload");
41 }
42 
44  size_t subStringPos = myString.find(mySubstring);
45  if(subStringPos != std::string::npos){
46  myString = myString.substr(subStringPos + mySubstring.length(), std::string::npos);
47  return true;
48  } else return false;
49 }
50 
51 // Begin Job
53 
54  QGLikelihoodObject *payload = new QGLikelihoodObject();
55  payload->data.clear();
56 
57  // Get the ROOT files and the keys to the histogram
58  TFile *f = TFile::Open(edm::FileInPath(inputRootFile.c_str()).fullPath().c_str());
59  TList *keys = f->GetListOfKeys();
60  if(!keys){
61  edm::LogError("NoKeys") << "There are no keys in the input file." << std::endl;
62  return;
63  }
64 
65  // Loop over directories/histograms
66  TIter nextdir(keys);
67  TKey *keydir;
68  while((keydir = (TKey*)nextdir())){
69  TDirectory *dir = (TDirectory*)keydir->ReadObj() ;
70  TIter nexthist(dir->GetListOfKeys());
71  TKey *keyhist;
72  while((keyhist = (TKey*)nexthist())){
73 
74  float ptMin, ptMax, rhoMin, rhoMax, etaMin, etaMax;
75  int varIndex, qgIndex;
76 
77  std::string histname = keyhist->GetName();
78  std::string histname_ = keyhist->GetName();
79 
80  // First check the variable name, and use index in same order as RecoJets/JetProducers/plugins/QGTagger.cc:73
81  if(extractString("nPFCand_QC_ptCutJet0", histname)) varIndex = 0;
82  else if(extractString("ptD_QCJet0", histname)) varIndex = 1;
83  else if(extractString("axis2_QCJet0", histname)) varIndex = 2;
84  else continue;
85 
86  // Check pseudorapidity range
87  if(extractString("_F", histname)){ etaMin = 2.5; etaMax = 4.7;}
88  else { etaMin = 0.;etaMax = 2.5;}
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  // Access the pt information
96  extractString("pt", histname);
97  ptMin = std::atof(histname.substr(0, histname.find("_")).c_str());
98  extractString("_", histname);
99  ptMax = std::atof(histname.substr(0, histname.find("rho")).c_str());
100 
101  if(etaMin == 2.5 && ptMin > 128) continue; //In forward use one bin for 127->2000
102  if(etaMin == 2.5 && ptMin == 127) ptMax = 4000;
103 
104  // Access the rho information
105  extractString("rho", histname);
106  rhoMin = std::atof(histname.c_str());
107  rhoMax = rhoMin + 1.; // WARNING: Check if this is still valid when changed to fixedGrid rho (try to move it in the name...)
108 
109  // Print out for debugging
110  char buff[1000];
111  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", histname_.c_str(), varIndex, qgIndex, etaMin, etaMax, ptMin, ptMax, rhoMin, rhoMax );
112  edm::LogVerbatim("HistName") << buff << std::endl;
113 
114  // Define category parameters
116  category.RhoMin = rhoMin;
117  category.RhoMax = rhoMax;
118  category.PtMin = ptMin;
119  category.PtMax = ptMax;
120  category.EtaMin = etaMin;
121  category.EtaMax = etaMax;
122  category.QGIndex = qgIndex;
123  category.VarIndex = varIndex;
124 
125  // Get TH1
126  TH1* th1hist = (TH1*) keyhist->ReadObj();
127 
128  // In the future, this part will (preferably) move to the making of the root files
129  if(th1hist->GetEntries()<50 ) th1hist->Rebin(5); // try to make it more stable
130  else if(th1hist->GetEntries()<500 ) th1hist->Rebin(2); // try to make it more stable
131  th1hist->Scale(1./th1hist->Integral("width"));
132 
133  // Transform ROOT TH1 to QGLikelihoodObject (same indexing)
134  QGLikelihoodObject::Histogram histogram(th1hist->GetNbinsX(), th1hist->GetXaxis()->GetBinLowEdge(1), th1hist->GetXaxis()->GetBinUpEdge(th1hist->GetNbinsX()));
135  for(int ibin = 0; ibin <= th1hist->GetNbinsX() + 1; ++ibin){
136  histogram.setBinContent(ibin, th1hist->GetBinContent(ibin));
137  }
138 
139  // Add this entry with its category parameters, histogram and mean
141  entry.category = category;
142  entry.histogram = histogram;
143  entry.mean = th1hist->GetMean();
144  payload->data.push_back(entry);
145  }
146  }
147 
148  // Define the valid range, if no category is found within these bounds a warning will be thrown
149  payload->qgValidRange.RhoMin = 0;
150  payload->qgValidRange.RhoMax = 46;
151  payload->qgValidRange.EtaMin = 0;
152  payload->qgValidRange.EtaMax = 4.7;
153  payload->qgValidRange.PtMin = 20;
154  payload->qgValidRange.PtMax = 4000;
155  payload->qgValidRange.QGIndex = -1;
156  payload->qgValidRange.VarIndex = -1;
157 
158  // Now write it into the DB
159  edm::LogInfo("UserOutput") << "Opening PoolDBOutputService" << std::endl;
160 
162  if(s.isAvailable()){
163  edm::LogInfo("UserOutput") << "Setting up payload with " << payload->data.size() << " entries and tag " << payloadTag << std::endl;
165  else s->appendSinceTime<QGLikelihoodObject>(payload, 111, payloadTag);
166  }
167  edm::LogInfo("UserOutput") << "Wrote in CondDB QGLikelihood payload label: " << payloadTag << std::endl;
168 }
169 
170 
172 
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