CMS 3D CMS Logo

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 <cstdlib>
11 #include <vector>
12 #include <memory>
13 #include <string>
23 
25 public:
27  void beginJob() override;
28  void analyze(const edm::Event&, const edm::EventSetup&) override {}
29  void endJob() override {}
30  ~QGLikelihoodDBWriter() override {}
31 
32 private:
33  bool getVectorFromFile(TFile*, std::vector<float>&, const TString&);
34  void tryToMerge(std::map<std::vector<int>, QGLikelihoodCategory>&,
35  std::map<std::vector<int>, TH1*>&,
36  std::vector<int>&,
37  int);
40 };
41 
42 // Constructor
44  inputRootFile = pSet.getParameter<std::string>("src");
45  payloadTag = pSet.getParameter<std::string>("payload");
46 }
47 
48 // Get vector from input file (includes translating TVector to std::vector)
49 bool QGLikelihoodDBWriter::getVectorFromFile(TFile* f, std::vector<float>& vector, const TString& name) {
50  TVectorT<float>* tVector = nullptr;
51  f->GetObject(name, tVector);
52  if (!tVector)
53  return false;
54  for (int i = 0; i < tVector->GetNoElements(); ++i)
55  vector.push_back((*tVector)[i]);
56  return true;
57 }
58 
59 // Transform ROOT TH1 to QGLikelihoodObject (same indexing)
62  th1->GetNbinsX(), th1->GetXaxis()->GetBinLowEdge(1), th1->GetXaxis()->GetBinUpEdge(th1->GetNbinsX()));
63  for (int ibin = 0; ibin <= th1->GetNbinsX() + 1; ++ibin)
64  histogram.setBinContent(ibin, th1->GetBinContent(ibin));
65  return histogram;
66 }
67 
68 // Try to merge bin with neighbouring bin (index = 2,3,4 for eta,pt,rho)
70  std::map<std::vector<int>, TH1*>& pdfs,
71  std::vector<int>& binNumbers,
72  int index) {
73  if (!pdfs[binNumbers])
74  return;
75  std::vector<int> neighbour = binNumbers;
76  do {
77  if (--(neighbour[index]) < 0)
78  return;
79  } while (!pdfs[neighbour]);
80  if (TString(pdfs[binNumbers]->GetTitle()) != TString(pdfs[neighbour]->GetTitle()))
81  return;
82  if (index != 4 && categories[neighbour].RhoMax != categories[binNumbers].RhoMax)
83  return;
84  if (index != 4 && categories[neighbour].RhoMin != categories[binNumbers].RhoMin)
85  return;
86  if (index != 3 && categories[neighbour].PtMax != categories[binNumbers].PtMax)
87  return;
88  if (index != 3 && categories[neighbour].PtMin != categories[binNumbers].PtMin)
89  return;
90  if (index != 2 && categories[neighbour].EtaMax != categories[binNumbers].EtaMax)
91  return;
92  if (index != 2 && categories[neighbour].EtaMin != categories[binNumbers].EtaMin)
93  return;
94 
95  if (index == 4)
96  categories[neighbour].RhoMax = categories[binNumbers].RhoMax;
97  if (index == 3)
98  categories[neighbour].PtMax = categories[binNumbers].PtMax;
99  if (index == 2)
100  categories[neighbour].EtaMax = categories[binNumbers].EtaMax;
101  pdfs.erase(binNumbers);
102  categories.erase(binNumbers);
103 }
104 
105 // Begin Job
108  payload->data.clear();
109 
110  // Get the ROOT file
111  TFile* f = TFile::Open(edm::FileInPath(inputRootFile.c_str()).fullPath().c_str());
112 
113  // The ROOT file contains the binning for each variable, needed to set up the binning grid
114  std::map<TString, std::vector<float>> gridOfBins;
115  for (const TString& binVariable : {"eta", "pt", "rho"}) {
116  if (!getVectorFromFile(f, gridOfBins[binVariable], binVariable + "Bins")) {
117  edm::LogError("NoBins") << "Missing bin information for " << binVariable << " in input file" << std::endl;
118  return;
119  }
120  }
121 
122  // Get pdf's from file and associate them to a QGLikelihoodCategory
123  // Some pdf's in the ROOT-file are copies from each other, with the same title: those are merged bins in pt and rho
124  // 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)
125  std::map<std::vector<int>, TH1*> pdfs;
126  std::map<std::vector<int>, QGLikelihoodCategory> categories;
127  for (const TString& type : {"gluon", "quark"}) {
128  int qgIndex = (type == "gluon"); // Keep numbering same as in RecoJets/JetAlgorithms/src/QGLikelihoodCalculator.cc
129  for (const TString& likelihoodVar : {"mult", "ptD", "axis2"}) {
130  int varIndex =
131  (likelihoodVar == "mult"
132  ? 0
133  : (likelihoodVar == "ptD" ? 1 : 2)); // Keep order same as in RecoJets/JetProducers/plugins/QGTagger.cc
134  for (int i = 0; i < (int)gridOfBins["eta"].size() - 1; ++i) {
135  for (int j = 0; j < (int)gridOfBins["pt"].size() - 1; ++j) {
136  for (int k = 0; k < (int)gridOfBins["rho"].size() - 1; ++k) {
138  category.EtaMin = gridOfBins["eta"][i];
139  category.EtaMax = gridOfBins["eta"][i + 1];
140  category.PtMin = gridOfBins["pt"][j];
141  category.PtMax = gridOfBins["pt"][j + 1];
142  category.RhoMin = gridOfBins["rho"][k];
143  category.RhoMax = gridOfBins["rho"][k + 1];
144  category.QGIndex = qgIndex;
145  category.VarIndex = varIndex;
146 
147  TString key =
148  TString::Format(likelihoodVar + "/" + likelihoodVar + "_" + type + "_eta%d_pt%d_rho%d", i, j, k);
149  TH1* pdf = (TH1*)f->Get(key);
150  if (!pdf) {
151  edm::LogError("NoPDF") << "Could not found pdf with key " << key << " in input file" << std::endl;
152  return;
153  }
154 
155  std::vector<int> binNumbers = {qgIndex, varIndex, i, j, k};
156  pdfs[binNumbers] = pdf;
157  categories[binNumbers] = category;
158 
159  tryToMerge(categories, pdfs, binNumbers, 4);
160  }
161  for (int k = 0; k < (int)gridOfBins["rho"].size() - 1; ++k) {
162  std::vector<int> binNumbers = {qgIndex, varIndex, i, j, k};
163  tryToMerge(categories, pdfs, binNumbers, 3);
164  }
165  }
166  for (int j = 0; j < (int)gridOfBins["pt"].size() - 1; ++j) {
167  for (int k = 0; k < (int)gridOfBins["rho"].size() - 1; ++k) {
168  std::vector<int> binNumbers = {qgIndex, varIndex, i, j, k};
169  tryToMerge(categories, pdfs, binNumbers, 2);
170  }
171  }
172  }
173  }
174  }
175 
176  // Write all categories with their histograms to file
177  int i = 0;
178  for (const auto& category : categories) {
180  entry.category = category.second;
181  entry.histogram = transformToHistogramObject(pdfs[category.first]);
182  entry.mean =
183  0; // not used by the algorithm, is an old data member used in the past, but DB objects are not allowed to change
184  payload->data.push_back(entry);
185 
186  char buff[1000];
187  sprintf(buff,
188  "%6d) var=%1d\t\tqg=%1d\t\teta={%5.2f,%5.2f}\t\tpt={%8.2f,%8.2f}\t\trho={%6.2f,%8.2f}",
189  i++,
190  category.second.VarIndex,
191  category.second.QGIndex,
192  category.second.EtaMin,
193  category.second.EtaMax,
194  category.second.PtMin,
195  category.second.PtMax,
196  category.second.RhoMin,
197  category.second.RhoMax);
198  edm::LogVerbatim("HistName") << buff << std::endl;
199  }
200 
201  // Define the valid range, if no category is found within these bounds a warning will be thrown
202  payload->qgValidRange.EtaMin = gridOfBins["eta"].front();
203  payload->qgValidRange.EtaMax = gridOfBins["eta"].back();
204  payload->qgValidRange.PtMin = gridOfBins["pt"].front();
205  payload->qgValidRange.PtMax = gridOfBins["pt"].back();
206  payload->qgValidRange.RhoMin = gridOfBins["rho"].front();
207  payload->qgValidRange.RhoMax = gridOfBins["rho"].back();
208  payload->qgValidRange.QGIndex = -1;
209  payload->qgValidRange.VarIndex = -1;
210 
211  // Now write it into the DB
212  edm::LogInfo("UserOutput") << "Opening PoolDBOutputService" << std::endl;
213 
215  if (s.isAvailable()) {
216  edm::LogInfo("UserOutput") << "Setting up payload with " << payload->data.size() << " entries and tag "
217  << payloadTag << std::endl;
218  if (s->isNewTagRequest(payloadTag))
219  s->createNewIOV<QGLikelihoodObject>(payload, s->beginOfTime(), s->endOfTime(), payloadTag);
220  else
221  s->appendSinceTime<QGLikelihoodObject>(payload, 111, payloadTag);
222  }
223  edm::LogInfo("UserOutput") << "Wrote in CondDB QGLikelihood payload label: " << payloadTag << std::endl;
224 }
225 
taus_updatedMVAIds_cff.category
category
Definition: taus_updatedMVAIds_cff.py:31
QGLikelihoodDBWriter::QGLikelihoodDBWriter
QGLikelihoodDBWriter(const edm::ParameterSet &)
Definition: QGLikelihoodDBWriter.cc:43
mps_fire.i
i
Definition: mps_fire.py:428
myMessageLogger_cff.categories
categories
Definition: myMessageLogger_cff.py:10
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
contentValuesFiles.fullPath
fullPath
Definition: contentValuesFiles.py:64
QGLikelihoodDBWriter::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: QGLikelihoodDBWriter.cc:28
mps_splice.entry
entry
Definition: mps_splice.py:68
electronDQMIsoDist_cfi.PtMax
PtMax
Definition: electronDQMIsoDist_cfi.py:51
EDAnalyzer.h
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
QGLikelihoodObject::Entry
Definition: QGLikelihoodObject.h:25
QGLikelihoodDBWriter::transformToHistogramObject
QGLikelihoodObject::Histogram transformToHistogramObject(TH1 *)
Definition: QGLikelihoodDBWriter.cc:60
edm::EDAnalyzer
Definition: EDAnalyzer.h:28
FileInPath.h
edm::FileInPath
Definition: FileInPath.h:64
MakerMacros.h
alignCSCRings.s
s
Definition: alignCSCRings.py:92
PoolDBOutputService.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
HLT_FULL_cff.PtMin
PtMin
Definition: HLT_FULL_cff.py:9904
Service.h
jets_cff.payload
payload
Definition: jets_cff.py:32
dqmdumpme.k
k
Definition: dqmdumpme.py:60
QGLikelihoodDBWriter::beginJob
void beginJob() override
Definition: QGLikelihoodDBWriter.cc:106
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
QGLikelihoodDBWriter::tryToMerge
void tryToMerge(std::map< std::vector< int >, QGLikelihoodCategory > &, std::map< std::vector< int >, TH1 * > &, std::vector< int > &, int)
Definition: QGLikelihoodDBWriter.cc:69
QGLikelihoodCategory
Category structure: ranges associated with QGLikelihood histograms.
Definition: QGLikelihoodObject.h:9
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
QGLikelihoodDBWriter::getVectorFromFile
bool getVectorFromFile(TFile *, std::vector< float > &, const TString &)
Definition: QGLikelihoodDBWriter.cc:49
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
PhysicsTools::Calibration::Histogram::setBinContent
void setBinContent(int bin, Value_t value)
edm::Service< cond::service::PoolDBOutputService >
createfilelist.int
int
Definition: createfilelist.py:10
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
PhysicsTools::Calibration::Histogram< float >
QGLikelihoodObject.h
edm::EventSetup
Definition: EventSetup.h:57
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
MonitorTrackInnerTrackMuons_cff.EtaMin
EtaMin
Definition: MonitorTrackInnerTrackMuons_cff.py:64
MonitorTrackInnerTrackMuons_cff.EtaMax
EtaMax
Definition: MonitorTrackInnerTrackMuons_cff.py:64
QGLikelihoodDBWriter::endJob
void endJob() override
Definition: QGLikelihoodDBWriter.cc:29
Frameworkfwd.h
QGLikelihoodDBWriter::inputRootFile
std::string inputRootFile
Definition: QGLikelihoodDBWriter.cc:39
QGLikelihoodDBWriter
Definition: QGLikelihoodDBWriter.cc:24
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
QGLikelihoodObject
QGLikelihoodObject containing valid range and entries with category and histogram (mean is not used a...
Definition: QGLikelihoodObject.h:22
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
genParticles_cff.map
map
Definition: genParticles_cff.py:11
ParameterSet.h
QGLikelihoodDBWriter::payloadTag
std::string payloadTag
Definition: QGLikelihoodDBWriter.cc:39
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::Event
Definition: Event.h:73
crabWrapper.key
key
Definition: crabWrapper.py:19
QGLikelihoodDBWriter::~QGLikelihoodDBWriter
~QGLikelihoodDBWriter() override
Definition: QGLikelihoodDBWriter.cc:30
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
HLT_FULL_cff.RhoMax
RhoMax
Definition: HLT_FULL_cff.py:70848