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:
33  bool getVectorFromFile(TFile*, std::vector<float>&, const TString&);
34  void tryToMerge(std::map<std::vector<int>, QGLikelihoodCategory>&, std::map<std::vector<int>, TH1*>&, std::vector<int>&, int);
37 };
38 
39 
40 // Constructor
42  inputRootFile = pSet.getParameter<std::string>("src");
43  payloadTag = pSet.getParameter<std::string>("payload");
44 }
45 
46 
47 // Get vector from input file (includes translating TVector to std::vector)
48 bool QGLikelihoodDBWriter::getVectorFromFile(TFile* f, std::vector<float>& vector, const TString& name){
49  TVectorT<float> *tVector = nullptr;
50  f->GetObject(name, tVector);
51  if(!tVector) return false;
52  for(int i = 0; i < tVector->GetNoElements(); ++i) vector.push_back((*tVector)[i]);
53  return true;
54 }
55 
56 
57 // Transform ROOT TH1 to QGLikelihoodObject (same indexing)
59  QGLikelihoodObject::Histogram histogram(th1->GetNbinsX(), th1->GetXaxis()->GetBinLowEdge(1), th1->GetXaxis()->GetBinUpEdge(th1->GetNbinsX()));
60  for(int ibin = 0; ibin <= th1->GetNbinsX() + 1; ++ibin) histogram.setBinContent(ibin, th1->GetBinContent(ibin));
61  return histogram;
62 }
63 
64 
65 // Try to merge bin with neighbouring bin (index = 2,3,4 for eta,pt,rho)
66 void QGLikelihoodDBWriter::tryToMerge(std::map<std::vector<int>, QGLikelihoodCategory>& categories, std::map<std::vector<int>, TH1*>& pdfs, std::vector<int>& binNumbers, int index){
67  if(!pdfs[binNumbers]) return;
68  std::vector<int> neighbour = binNumbers;
69  do {
70  if(--(neighbour[index]) < 0) return;
71  } while (!pdfs[neighbour]);
72  if(TString(pdfs[binNumbers]->GetTitle()) != TString(pdfs[neighbour]->GetTitle())) return;
73  if(index != 4 && categories[neighbour].RhoMax != categories[binNumbers].RhoMax) return;
74  if(index != 4 && categories[neighbour].RhoMin != categories[binNumbers].RhoMin) return;
75  if(index != 3 && categories[neighbour].PtMax != categories[binNumbers].PtMax) return;
76  if(index != 3 && categories[neighbour].PtMin != categories[binNumbers].PtMin) return;
77  if(index != 2 && categories[neighbour].EtaMax != categories[binNumbers].EtaMax) return;
78  if(index != 2 && categories[neighbour].EtaMin != categories[binNumbers].EtaMin) return;
79 
80  if(index == 4) categories[neighbour].RhoMax = categories[binNumbers].RhoMax;
81  if(index == 3) categories[neighbour].PtMax = categories[binNumbers].PtMax;
82  if(index == 2) categories[neighbour].EtaMax = categories[binNumbers].EtaMax;
83  pdfs.erase(binNumbers);
84  categories.erase(binNumbers);
85 }
86 
87 
88 // Begin Job
90  QGLikelihoodObject *payload = new QGLikelihoodObject();
91  payload->data.clear();
92 
93  // Get the ROOT file
94  TFile *f = TFile::Open(edm::FileInPath(inputRootFile.c_str()).fullPath().c_str());
95 
96  // The ROOT file contains the binning for each variable, needed to set up the binning grid
97  std::map<TString, std::vector<float>> gridOfBins;
98  for(TString binVariable : {"eta", "pt", "rho"}){
99  if(!getVectorFromFile(f, gridOfBins[binVariable], binVariable + "Bins")){
100  edm::LogError("NoBins") << "Missing bin information for " << binVariable << " in input file" << std::endl;
101  return;
102  }
103  }
104 
105  // Get pdf's from file and associate them to a QGLikelihoodCategory
106  // Some pdf's in the ROOT-file are copies from each other, with the same title: those are merged bins in pt and rho
107  // Here we do not store the copies, but try to extend the range of the neighbouring category (will result in less comparisons during application phase)
108  std::map<std::vector<int>, TH1*> pdfs;
109  std::map<std::vector<int>, QGLikelihoodCategory> categories;
110  for(TString type : {"gluon","quark"}){
111  int qgIndex = (type == "gluon"); // Keep numbering same as in RecoJets/JetAlgorithms/src/QGLikelihoodCalculator.cc
112  for(TString likelihoodVar : {"mult","ptD","axis2"}){
113  int varIndex = (likelihoodVar == "mult" ? 0 : (likelihoodVar == "ptD" ? 1 : 2)); // Keep order same as in RecoJets/JetProducers/plugins/QGTagger.cc
114  for(int i = 0; i < (int)gridOfBins["eta"].size() - 1; ++i){
115  for(int j = 0; j < (int)gridOfBins["pt"].size() - 1; ++j){
116  for(int k = 0; k < (int)gridOfBins["rho"].size() - 1; ++k){
117 
119  category.EtaMin = gridOfBins["eta"][i];
120  category.EtaMax = gridOfBins["eta"][i+1];
121  category.PtMin = gridOfBins["pt"][j];
122  category.PtMax = gridOfBins["pt"][j+1];
123  category.RhoMin = gridOfBins["rho"][k];
124  category.RhoMax = gridOfBins["rho"][k+1];
125  category.QGIndex = qgIndex;
126  category.VarIndex = varIndex;
127 
128  TString key = TString::Format(likelihoodVar + "/" + likelihoodVar + "_" + type + "_eta%d_pt%d_rho%d", i, j, k);
129  TH1* pdf = (TH1*) f->Get(key);
130  if(!pdf){
131  edm::LogError("NoPDF") << "Could not found pdf with key " << key << " in input file" << std::endl;
132  return;
133  }
134 
135  std::vector<int> binNumbers = {qgIndex, varIndex, i,j,k};
136  pdfs[binNumbers] = pdf;
137  categories[binNumbers] = category;
138 
139  tryToMerge(categories, pdfs, binNumbers, 4);
140  }
141  for(int k = 0; k < (int)gridOfBins["rho"].size() - 1; ++k){
142  std::vector<int> binNumbers = {qgIndex, varIndex, i,j,k};
143  tryToMerge(categories, pdfs, binNumbers, 3);
144  }
145  }
146  for(int j = 0; j < (int)gridOfBins["pt"].size() - 1; ++j){
147  for(int k = 0; k < (int)gridOfBins["rho"].size() - 1; ++k){
148  std::vector<int> binNumbers = {qgIndex, varIndex, i,j,k};
149  tryToMerge(categories, pdfs, binNumbers, 2);
150  }
151  }
152  }
153  }
154  }
155 
156 
157  // Write all categories with their histograms to file
158  int i = 0;
159  for(auto category : categories){
161  entry.category = category.second;
162  entry.histogram = transformToHistogramObject(pdfs[category.first]);
163  entry.mean = 0; // not used by the algorithm, is an old data member used in the past, but DB objects are not allowed to change
164  payload->data.push_back(entry);
165 
166  char buff[1000];
167  sprintf(buff, "%6d) var=%1d\t\tqg=%1d\t\teta={%5.2f,%5.2f}\t\tpt={%8.2f,%8.2f}\t\trho={%6.2f,%8.2f}", i++,
168  category.second.VarIndex, category.second.QGIndex, category.second.EtaMin, category.second.EtaMax,
169  category.second.PtMin, category.second.PtMax, category.second.RhoMin, category.second.RhoMax);
170  edm::LogVerbatim("HistName") << buff << std::endl;
171  }
172 
173  // Define the valid range, if no category is found within these bounds a warning will be thrown
174  payload->qgValidRange.EtaMin = gridOfBins["eta"].front();
175  payload->qgValidRange.EtaMax = gridOfBins["eta"].back();
176  payload->qgValidRange.PtMin = gridOfBins["pt"].front();
177  payload->qgValidRange.PtMax = gridOfBins["pt"].back();
178  payload->qgValidRange.RhoMin = gridOfBins["rho"].front();
179  payload->qgValidRange.RhoMax = gridOfBins["rho"].back();
180  payload->qgValidRange.QGIndex = -1;
181  payload->qgValidRange.VarIndex = -1;
182 
183  // Now write it into the DB
184  edm::LogInfo("UserOutput") << "Opening PoolDBOutputService" << std::endl;
185 
187  if(s.isAvailable()){
188  edm::LogInfo("UserOutput") << "Setting up payload with " << payload->data.size() << " entries and tag " << payloadTag << std::endl;
190  else s->appendSinceTime<QGLikelihoodObject>(payload, 111, payloadTag);
191  }
192  edm::LogInfo("UserOutput") << "Wrote in CondDB QGLikelihood payload label: " << payloadTag << std::endl;
193 }
194 
195 
197 
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
QGLikelihoodCategory category
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
QGLikelihoodCategory qgValidRange
void setBinContent(int bin, Value_t value)
const double EtaMax[kNumberCalorimeter]
void tryToMerge(std::map< std::vector< int >, QGLikelihoodCategory > &, std::map< std::vector< int >, TH1 * > &, std::vector< int > &, int)
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 and histogram (mean is not used a...
bool isNewTagRequest(const std::string &recordName)
bool isAvailable() const
Definition: Service.h:46
int j
Definition: DBlmapReader.cc:9
QGLikelihoodObject::Histogram transformToHistogramObject(TH1 *)
double f[11][100]
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
const double EtaMin[kNumberCalorimeter]
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
bool getVectorFromFile(TFile *, std::vector< float > &, const TString &)
virtual void beginJob() override
virtual void endJob() override
tuple size
Write out results.