CMS 3D CMS Logo

PatternOptimizerBase.cc
Go to the documentation of this file.
1 /*
2  * PatternOptimizerBase.cc
3  *
4  * Created on: Oct 17, 2018
5  * Author: kbunkow
6  */
15 
23 
24 #include "TCanvas.h"
25 #include "TFile.h"
26 #include "TH1.h"
27 #include "TH2.h"
28 #include "TH2F.h"
29 #include "TStyle.h"
30 #include <cstdlib>
31 #include <iostream>
32 #include <memory>
33 #include <sstream>
34 #include <string>
35 #include <vector>
36 
37 double PatternOptimizerBase::vxMuRate(double pt_GeV) {
38  if (pt_GeV == 0)
39  return 0.0;
40  const double lum = 2.0e34; //defoult is 1.0e34;
41  const double dabseta = 1.0;
42  const double dpt = 1.0;
43  const double afactor = 1.0e-34 * lum * dabseta * dpt;
44  const double a = 2 * 1.3084E6;
45  const double mu = -0.725;
46  const double sigma = 0.4333;
47  const double s2 = 2 * sigma * sigma;
48 
49  double ptlog10;
50  ptlog10 = log10(pt_GeV);
51  double ex = (ptlog10 - mu) * (ptlog10 - mu) / s2;
52  double rate = (a * exp(-ex) * afactor);
53  //edm::LogError("l1tOmtfEventPrint")<<ptCode<<" "<<rate;//<<<<<<<<<<<<<<<<<<<<<<<<
54  return rate;
55 }
56 
57 double PatternOptimizerBase::vxIntegMuRate(double pt_GeV, double dpt, double etaFrom, double etaTo) {
58  //integration using trapeze method - not exact but good enough
59  double rate = 0.5 * (vxMuRate(pt_GeV) + vxMuRate(pt_GeV + dpt)) * dpt;
60 
61  rate = rate * (etaTo - etaFrom);
62  //edm::LogError("l1tOmtfEventPrint")<<ptCode<<" "<<rate;//<<<<<<<<<<<<<<<<<<<<<<<<
63  return rate;
64 }
65 
67  const OMTFConfiguration* omtfConfig,
69  : EmulationObserverBase(edmCfg, omtfConfig), goldenPatterns(gps) {
70  // TODO Auto-generated constructor stub
71 
72  simMuPt = new TH1I("simMuPt", "simMuPt", goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5);
74  new TH1I("simMuFoundByOmtfPt", "simMuFoundByOmtfPt", goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5);
75 
76  simMuEta = new TH1I("simMuEta", "simMuEta;eta;#events", 100, -2.1, 2.1);
77  candEta = new TH1I("candEta", "candEta;eta;#events", 100, -2.1, 2.1);
78 
79  simMuPtSpectrum = new TH1F("simMuPtSpectrum", "simMuPtSpectrum", 800, 0, 400);
80 
81  simMuPtVsDispl = new TH2I("simMuPtVsDispl", "simMuPtVsDispl;pt [GeV];dxy [cm]", 100, 0, 400, 100, 0, 400);
82  simMuPtVsRho = new TH2I("simMuPtVsRho", "simMuPtVsRho;pt [GeV];rho [cm]", 100, 0, 400, 100, 0, 400);
83 
84  if (edmCfg.exists("simTracksTag") == false)
85  edm::LogError("l1tOmtfEventPrint") << "simTracksTag not found !!!" << std::endl;
86 }
87 
89 
91  edm::LogVerbatim("l1tOmtfEventPrint") << __FUNCTION__ << ": " << __LINE__ << " called!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
92  << std::endl;
93  for (int patNum = goldenPatterns.size() - 1; patNum >= 0; patNum--) {
94  double pt = omtfConfig->getPatternPtRange(patNum).ptFrom;
95  if (pt > 0) {
96  edm::LogVerbatim("l1tOmtfEventPrint")
97  << "cmsRun runThresholdCalc.py " << patNum << " " << (patNum + 1) << " _" << RPCConst::iptFromPt(pt) << "_";
98  if (goldenPatterns[patNum]->key().theCharge == -1)
99  edm::LogVerbatim("l1tOmtfEventPrint") << "m_";
100  else
101  edm::LogVerbatim("l1tOmtfEventPrint") << "p_";
102 
103  edm::LogVerbatim("l1tOmtfEventPrint") << " > out" << patNum << ".txt" << std::endl;
104  }
105  }
106 }
107 
109  std::unique_ptr<l1t::RegionalMuonCandBxCollection>& finalCandidates) {
110  if (simMuon == nullptr || omtfCand->getGoldenPatern() == nullptr) //no sim muon or empty candidate
111  return;
112 
113  double ptSim = simMuon->momentum().pt();
114  int chargeSim = (std::abs(simMuon->type()) == 13) ? simMuon->type() / -13 : 0;
115 
116  //double muDxy = (-1 * genMuon->vx() * genMuon->py() + genMuon->vy() * genMuon->px()) / genMuon->pt();;
117 
118  unsigned int exptPatNum = omtfConfig->getPatternNum(ptSim, chargeSim);
119  GoldenPatternWithStat* exptCandGp = goldenPatterns.at(exptPatNum).get(); // expected pattern
120  simMuFoundByOmtfPt->Fill(exptCandGp->key().theNumber); //TODO add weight of the muons pt spectrum
121 
122  simMuPtSpectrum->Fill(ptSim, getEventRateWeight(ptSim));
123 }
124 
126  std::string fName = edmCfg.getParameter<std::string>("optimisedPatsXmlFile");
127  edm::LogImportant("PatternOptimizer") << " Writing optimized patterns to " << fName << std::endl;
128  XMLConfigWriter xmlWriter(omtfConfig, false, false);
129  xmlWriter.writeGPs(goldenPatterns, fName);
130 
131  fName.replace(fName.find('.'), fName.length(), ".root");
133 }
134 
136  gStyle->SetOptStat(111111);
137  TFile outfile(rootFileName.c_str(), "RECREATE");
138  edm::LogVerbatim("l1tOmtfEventPrint") << __FUNCTION__ << ": " << __LINE__ << " out fileName " << rootFileName
139  << " outfile->GetName() " << outfile.GetName() << " writeLayerStat "
140  << writeLayerStat << endl;
141 
142  outfile.cd();
143  simMuFoundByOmtfPt->Write();
144 
145  simMuEta->Write();
146  candEta->Write();
147 
148  simMuPtSpectrum->Write();
149  simMuPtVsDispl->Write();
150  simMuPtVsRho->Write();
151 
152  outfile.mkdir("patternsPdfs")->cd();
153  outfile.mkdir("patternsPdfs/canvases");
154  outfile.mkdir("patternsPdfs/canvases2");
155  outfile.mkdir("layerStats");
156  ostringstream ostrName;
157  ostringstream ostrTtle;
158  vector<TH1F*> classProbHists;
159  for (unsigned int iRefLayer = 0; iRefLayer < goldenPatterns[0]->getPdf()[0].size(); ++iRefLayer) {
160  ostrName.str("");
161  ostrName << "Neg_RefLayer_" << iRefLayer;
162  ostrTtle.str("");
163  ostrTtle << "Neg_RefLayer_" << iRefLayer;
164  classProbHists.push_back(new TH1F(
165  ostrName.str().c_str(), ostrTtle.str().c_str(), goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5));
166 
167  ostrName.str("");
168  ostrName << "Pos_RefLayer_" << iRefLayer;
169  ostrTtle.str("");
170  ostrTtle << "Pos_RefLayer_" << iRefLayer;
171  classProbHists.push_back(new TH1F(
172  ostrName.str().c_str(), ostrTtle.str().c_str(), goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5));
173  }
174 
175  auto gpsCnt = goldenPatterns.size();
176  auto layerCnt = goldenPatterns[0]->getPdf().size();
177  auto refLayerCnt = goldenPatterns[0]->getPdf().size();
178 
179  vector<vector<TH2F*> > distPhiLayerRefLayer(layerCnt, vector<TH2F*>(refLayerCnt, nullptr));
180  vector<vector<TH2F*> > meanDistPhiLayerRefLayer(layerCnt, vector<TH2F*>(refLayerCnt, nullptr));
181  vector<vector<TH2F*> > pdfsLayerRefLayer(layerCnt, vector<TH2F*>(refLayerCnt, nullptr));
182 
183  for (unsigned int iLayer = 0; iLayer < layerCnt; ++iLayer) {
184  int rangeFactor = 1;
185  if (iLayer == 1 || iLayer == 3 || iLayer == 5)
186  rangeFactor = 2;
187 
188  for (unsigned int iRefLayer = 0; iRefLayer < refLayerCnt; ++iRefLayer) {
189  ostrName.str("");
190  ostrName << "distPhi_refLayer_" << iRefLayer << "_layer_" << iLayer;
191  ostrTtle.str("");
192  ostrTtle << "distPhi refLayer " << iRefLayer << " layer " << iLayer;
193  //edm::LogVerbatim("l1tOmtfEventPrint") <<__FUNCTION__<<": "<<__LINE__<<" creating hist "<<ostrTtle.str()<<std::endl;
194  distPhiLayerRefLayer[iLayer][iRefLayer] = new TH2F(ostrName.str().c_str(),
195  ostrTtle.str().c_str(),
196  gpsCnt,
197  0,
198  gpsCnt,
199  omtfConfig->nPdfBins() * rangeFactor * 2,
200  (int)(omtfConfig->nPdfBins()) * (-rangeFactor) - 0.5,
201  omtfConfig->nPdfBins() * rangeFactor - 0.5);
202 
203  ostrName.str("");
204  ostrName << "meanDistPhi_refLayer_" << iRefLayer << "_Layer_" << iLayer;
205  ostrTtle.str("");
206  ostrTtle << "meanDistPhi refLayer " << iRefLayer << " Layer " << iLayer;
207  //edm::LogVerbatim("l1tOmtfEventPrint") <<__FUNCTION__<<": "<<__LINE__<<" creating hist "<<ostrTtle.str()<<std::endl;
208  meanDistPhiLayerRefLayer[iLayer][iRefLayer] = new TH2F(ostrName.str().c_str(),
209  ostrTtle.str().c_str(),
210  gpsCnt,
211  0,
212  gpsCnt,
213  omtfConfig->nPdfBins() * rangeFactor * 2,
214  (int)(omtfConfig->nPdfBins()) * (-rangeFactor) - 0.5,
215  omtfConfig->nPdfBins() * rangeFactor - 0.5);
216 
217  ostrName.str("");
218  ostrName << "pdfs_refLayer_" << iRefLayer << "_layer_" << iLayer;
219  ostrTtle.str("");
220  ostrTtle << "pdfs refLayer " << iRefLayer << " layer " << iLayer;
221  //edm::LogVerbatim("l1tOmtfEventPrint") <<__FUNCTION__<<": "<<__LINE__<<" creating hist "<<ostrTtle.str()<<std::endl;
222  pdfsLayerRefLayer[iLayer][iRefLayer] = new TH2F(ostrName.str().c_str(),
223  ostrTtle.str().c_str(),
224  gpsCnt,
225  0,
226  gpsCnt,
227  omtfConfig->nPdfBins(),
228  -0.5,
229  omtfConfig->nPdfBins() - 0.5);
230  }
231  }
232 
233  for (auto& gp : goldenPatterns) {
234  OMTFConfiguration::PatternPt patternPt = omtfConfig->getPatternPtRange(gp->key().theNumber);
235  if (gp->key().thePt == 0)
236  continue;
237  //edm::LogVerbatim("l1tOmtfEventPrint") <<__FUNCTION__<<": "<<__LINE__<<" "<<gp->key()<<std::endl;
238  ostrName.str("");
239  ostrName << "PatNum_" << gp->key().theNumber;
240  ostrTtle.str("");
241  ostrTtle << "PatNum_" << gp->key().theNumber << "_ptCode_" << gp->key().thePt << "_Pt_" << patternPt.ptFrom << "_"
242  << patternPt.ptTo << "_GeV";
243  TCanvas* canvas = new TCanvas(ostrName.str().c_str(), ostrTtle.str().c_str(), 1200, 1000);
244  canvas->Divide(gp->getPdf().size(), gp->getPdf()[0].size(), 0, 0);
245 
246  for (unsigned int iRefLayer = 0; iRefLayer < gp->getPdf()[0].size(); ++iRefLayer) {
247  for (unsigned int iLayer = 0; iLayer < gp->getPdf().size(); ++iLayer) {
248  canvas->cd(1 + iLayer + iRefLayer * gp->getPdf().size());
249  ostrName.str("");
250  ostrName << "PatNum_" << gp->key().theNumber << "_refLayer_" << iRefLayer << "_Layer_" << iLayer;
251  ostrTtle.str("");
252  ostrTtle << "PatNum " << gp->key().theNumber << " ptCode " << gp->key().thePt << " refLayer " << iRefLayer
253  << " Layer " << iLayer << " meanDistPhi " << gp->meanDistPhi[iLayer][iRefLayer][0]
254  << " distPhiBitShift "
255  << gp->getDistPhiBitShift(iLayer, iRefLayer); //"_Pt_"<<patternPt.ptFrom<<"_"<<patternPt.ptTo<<"_GeV
256  //edm::LogVerbatim("l1tOmtfEventPrint") <<__FUNCTION__<<": "<<__LINE__<<" creating hist "<<ostrTtle.str()<<std::endl;
257  TH1F* hist = new TH1F(
258  ostrName.str().c_str(), ostrTtle.str().c_str(), omtfConfig->nPdfBins(), -0.5, omtfConfig->nPdfBins() - 0.5);
259 
260  int pdfMiddle = gp->getPdf()[iLayer][iRefLayer].size() / 2;
261  int shift = gp->getDistPhiBitShift(iLayer, iRefLayer);
262 
263  for (unsigned int iPdf = 0; iPdf < gp->getPdf()[iLayer][iRefLayer].size(); iPdf++) {
264  hist->Fill(iPdf, gp->pdfAllRef[iLayer][iRefLayer][iPdf]);
265 
266  distPhiLayerRefLayer[iLayer][iRefLayer]->Fill(
267  gp->key().theNumber,
268  (((int)iPdf - pdfMiddle) << shift) + gp->meanDistPhi[iLayer][iRefLayer][0],
269  gp->pdfAllRef[iLayer][iRefLayer][iPdf]);
270 
271  pdfsLayerRefLayer[iLayer][iRefLayer]->Fill(
272  gp->key().theNumber, (int)iPdf, gp->pdfAllRef[iLayer][iRefLayer][iPdf]);
273  }
274  if ((int)iLayer == (omtfConfig->getRefToLogicNumber()[iRefLayer]))
275  hist->SetLineColor(kGreen);
276 
277  meanDistPhiLayerRefLayer[iLayer][iRefLayer]->Fill(
278  gp->key().theNumber, gp->meanDistPhi[iLayer][iRefLayer][0], 1);
279 
280  hist->GetYaxis()->SetRangeUser(0, omtfConfig->pdfMaxValue() + 1);
281  hist->Draw("hist");
282 
283  outfile.cd("patternsPdfs");
284  hist->Write();
285 
287  if (writeLayerStat) {
288  bool saveTh2 = false;
289  if (gp->getStatistics()[iLayer][iRefLayer][0].size() > 1)
290  saveTh2 = true;
291 
292  outfile.cd("layerStats");
293 
294  string histName = "histLayerStat_" + ostrName.str();
295  unsigned int binCnt1 = gp->getStatistics()[iLayer][iRefLayer].size();
296  if (!saveTh2) {
297  TH1I* histLayerStat = new TH1I(histName.c_str(), histName.c_str(), binCnt1, -0.5, binCnt1 - 0.5);
298  for (unsigned int iBin = 0; iBin < binCnt1; iBin++) {
299  histLayerStat->Fill(iBin, gp->getStatistics()[iLayer][iRefLayer][iBin][0]);
300  }
301  histLayerStat->Write();
302  } else {
303  if (iRefLayer == 0 || iRefLayer == 2) { //TODO!!!!!!!!!!!!!!!!!!!!!!!!
304  unsigned int binCnt2 = gp->getStatistics()[iLayer][iRefLayer][0].size();
305  //TH2I* histLayerStat = new TH2I(histName.c_str(), (histName + ";ref phiB;delta_phi").c_str(), binCnt2, -0.5, binCnt2 - 0.5, binCnt1, -0.5, binCnt1 - 0.5);
306  double xmin = -0.5 - binCnt2 / 2;
307  double xmax = binCnt2 / 2 - 0.5;
308  if (edmCfg.getParameter<string>("patternGenerator") == "deltaPhiVsPhiRef") {
309  xmin = -0.5;
310  xmax = binCnt2 - 0.5;
311  }
312  TH2I* histLayerStat = new TH2I(histName.c_str(),
313  (histName + ";ref phiB;delta_phi").c_str(),
314  binCnt2,
315  xmin,
316  xmax,
317  binCnt1,
318  -0.5 - binCnt1 / 2,
319  binCnt1 / 2 - 0.5);
320 
321  if (edmCfg.getParameter<string>("patternGenerator") == "deltaPhiVsPhiRef")
322  histLayerStat->GetXaxis()->SetTitle("ref phi");
323 
324  for (unsigned int iBin1 = 0; iBin1 < binCnt1; iBin1++) { //deltaPhi
325  for (unsigned int iBin2 = 0; iBin2 < binCnt2; iBin2++) { //phiB
326  //histLayerStat->Fill(iBin2, iBin1, gp->getStatistics()[iLayer][iRefLayer][iBin1][iBin2]); //looks that using Fill leads to huge memory cosumption
327  histLayerStat->SetBinContent(
328  iBin2 + 1, iBin1 + 1, gp->getStatistics()[iLayer][iRefLayer][iBin1][iBin2]);
329  }
330  }
331  histLayerStat->Write();
332  histLayerStat->Delete();
333  }
334  }
335  }
336  }
337  }
338  outfile.cd("patternsPdfs/canvases");
339  canvas->Write();
340  delete canvas;
341 
342  unsigned int iPdf = omtfConfig->nPdfBins() / 2;
343  for (unsigned int iRefLayer = 0; iRefLayer < gp->getPdf()[0].size(); ++iRefLayer) {
344  unsigned int refLayerLogicNumber = omtfConfig->getRefToLogicNumber()[iRefLayer];
345  if (gp->key().theCharge == -1) {
346  classProbHists[2 * iRefLayer]->Fill(gp->key().theNumber, gp->pdfAllRef[refLayerLogicNumber][iRefLayer][iPdf]);
347  } else
348  classProbHists[2 * iRefLayer + 1]->Fill(gp->key().theNumber,
349  gp->pdfAllRef[refLayerLogicNumber][iRefLayer][iPdf]);
350  }
351  }
352 
353  outfile.cd();
354  for (auto& classProbHist : classProbHists) {
355  classProbHist->Write();
356  }
357 
358  outfile.cd("patternsPdfs/canvases2");
359  for (unsigned int iRefLayer = 0; iRefLayer < goldenPatterns[0]->getPdf()[0].size(); ++iRefLayer) {
360  ostrName.str("");
361  ostrName << "distPhiForPatterns_Reflayer_" << iRefLayer;
362  ostrTtle.str("");
363  ostrTtle << "distPhiForPatterns Reflayer " << iRefLayer;
364 
365  TCanvas* canvas = new TCanvas(ostrName.str().c_str(), ostrTtle.str().c_str(), 1200, 1000);
366  canvas->Divide(6, 3, 0, 0);
367 
368  for (unsigned int iLayer = 0; iLayer < distPhiLayerRefLayer.size(); ++iLayer) {
369  canvas->cd(iLayer + 1);
370  canvas->cd(iLayer + 1)->SetGridx();
371  canvas->cd(iLayer + 1)->SetGridy();
372 
373  //distPhiLayerRefLayer[iLayer][iRefLayer]->SetLineColor(kBlack);
374  distPhiLayerRefLayer[iLayer][iRefLayer]->Draw("colz");
375 
376  meanDistPhiLayerRefLayer[iLayer][iRefLayer]->SetLineColor(kRed);
377  meanDistPhiLayerRefLayer[iLayer][iRefLayer]->Draw("boxsame");
378 
379  distPhiLayerRefLayer[iLayer][iRefLayer]->Write();
380  meanDistPhiLayerRefLayer[iLayer][iRefLayer]->Write();
381  pdfsLayerRefLayer[iLayer][iRefLayer]->Write();
382  }
383 
384  canvas->Write();
385 
386  ostrName.str("");
387  ostrName << "pdfsForPatterns_Reflayer_" << iRefLayer;
388  ostrTtle.str("");
389  ostrTtle << "pdfsForPatterns Reflayer " << iRefLayer;
390 
391  canvas = new TCanvas(ostrName.str().c_str(), ostrTtle.str().c_str(), 1200, 1000);
392  canvas->Divide(6, 3, 0, 0);
393  for (unsigned int iLayer = 0; iLayer < distPhiLayerRefLayer.size(); ++iLayer) {
394  canvas->cd(iLayer + 1);
395  canvas->cd(iLayer + 1)->SetGridx();
396  canvas->cd(iLayer + 1)->SetGridy();
397 
398  pdfsLayerRefLayer[iLayer][iRefLayer]->Draw("colz");
399  }
400 
401  canvas->Write();
402  }
403 
405 
406  outfile.Close();
407 }
Log< level::Info, true > LogVerbatim
unsigned int theNumber
Definition: GoldenPattern.h:37
int pdfMaxValue() const
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
AlgoMuons::value_type omtfCand
void writeGPs(const GoldenPatternVec< GoldenPatternType > &goldenPats, std::string fName)
bool exists(std::string const &parameterName) const
checks if a parameter exists
unsigned int getPatternNum(double pt, int charge) const
charge: -1 - negative, +1 - positive
static double vxIntegMuRate(double pt_GeV, double dpt, double etaFrom, double etaTo)
const math::XYZTLorentzVectorD & momentum() const
Definition: CoreSimTrack.h:19
int type() const
particle type (HEP PDT convension)
Definition: CoreSimTrack.h:22
int iEvent
Definition: GenABIO.cc:224
void observeEventEnd(const edm::Event &iEvent, std::unique_ptr< l1t::RegionalMuonCandBxCollection > &finalCandidates) override
Key key() const
Definition: GoldenPattern.h:56
virtual double getEventRateWeight(double pt)
static double vxMuRate(double pt_GeV)
pattern pt range in Gev
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Log< level::Error, true > LogImportant
key
prepare the HTCondor submission files and eventually submit them
const std::vector< int > & getRefToLogicNumber() const
virtual void saveHists(TFile &outfile)
std::vector< std::unique_ptr< GoldenPatternType > > GoldenPatternVec
GoldenPatternVec< GoldenPatternWithStat > & goldenPatterns
double rate(double x)
Definition: Constants.cc:3
double a
Definition: hdecay.h:121
PatternPt getPatternPtRange(unsigned int patNum) const
PatternOptimizerBase(const edm::ParameterSet &edmCfg, const OMTFConfiguration *omtfConfig, GoldenPatternVec< GoldenPatternWithStat > &gps)
def canvas(sub, attr)
Definition: svgfig.py:482
static unsigned int const shift
void savePatternsInRoot(std::string rootFileName)
static int iptFromPt(const double pt)
Definition: RPCConst.cc:10
unsigned int nPdfBins() const
const OMTFConfiguration * omtfConfig