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 "TStyle.h"
28 #include <cstdlib>
29 #include <iostream>
30 #include <memory>
31 #include <sstream>
32 #include <string>
33 #include <vector>
34 
35 double PatternOptimizerBase::vxMuRate(double pt_GeV) {
36  if (pt_GeV == 0)
37  return 0.0;
38  const double lum = 2.0e34; //defoult is 1.0e34;
39  const double dabseta = 1.0;
40  const double dpt = 1.0;
41  const double afactor = 1.0e-34 * lum * dabseta * dpt;
42  const double a = 2 * 1.3084E6;
43  const double mu = -0.725;
44  const double sigma = 0.4333;
45  const double s2 = 2 * sigma * sigma;
46 
47  double ptlog10;
48  ptlog10 = log10(pt_GeV);
49  double ex = (ptlog10 - mu) * (ptlog10 - mu) / s2;
50  double rate = (a * exp(-ex) * afactor);
51  //edm::LogError("l1tOmtfEventPrint")<<ptCode<<" "<<rate;//<<<<<<<<<<<<<<<<<<<<<<<<
52  return rate;
53 }
54 
55 double PatternOptimizerBase::vxIntegMuRate(double pt_GeV, double dpt, double etaFrom, double etaTo) {
56  //integration using trapeze method - not exact but good enough
57  double rate = 0.5 * (vxMuRate(pt_GeV) + vxMuRate(pt_GeV + dpt)) * dpt;
58 
59  rate = rate * (etaTo - etaFrom);
60  //edm::LogError("l1tOmtfEventPrint")<<ptCode<<" "<<rate;//<<<<<<<<<<<<<<<<<<<<<<<<
61  return rate;
62 }
63 
65  const OMTFConfiguration* omtfConfig,
67  : EmulationObserverBase(edmCfg, omtfConfig), goldenPatterns(gps) {
68  // TODO Auto-generated constructor stub
69 
70  simMuPt = new TH1I("simMuPt", "simMuPt", goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5);
72  new TH1I("simMuFoundByOmtfPt", "simMuFoundByOmtfPt", goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5);
73 
74  simMuPtSpectrum = new TH1F("simMuPtSpectrum", "simMuPtSpectrum", 800, 0, 400);
75 
76  if (edmCfg.exists("simTracksTag") == false)
77  edm::LogError("l1tOmtfEventPrint") << "simTracksTag not found !!!" << std::endl;
78 }
79 
81 
83  edm::LogVerbatim("l1tOmtfEventPrint") << __FUNCTION__ << ": " << __LINE__ << " called!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
84  << std::endl;
85  for (int patNum = goldenPatterns.size() - 1; patNum >= 0; patNum--) {
86  double pt = omtfConfig->getPatternPtRange(patNum).ptFrom;
87  if (pt > 0) {
88  edm::LogVerbatim("l1tOmtfEventPrint")
89  << "cmsRun runThresholdCalc.py " << patNum << " " << (patNum + 1) << " _" << RPCConst::iptFromPt(pt) << "_";
90  if (goldenPatterns[patNum]->key().theCharge == -1)
91  edm::LogVerbatim("l1tOmtfEventPrint") << "m_";
92  else
93  edm::LogVerbatim("l1tOmtfEventPrint") << "p_";
94 
95  edm::LogVerbatim("l1tOmtfEventPrint") << " > out" << patNum << ".txt" << std::endl;
96  }
97  }
98 }
99 
101  std::unique_ptr<l1t::RegionalMuonCandBxCollection>& finalCandidates) {
102  if (simMuon == nullptr || omtfCand->getGoldenPatern() == nullptr) //no sim muon or empty candidate
103  return;
104 
105  double ptSim = simMuon->momentum().pt();
106  int chargeSim = (abs(simMuon->type()) == 13) ? simMuon->type() / -13 : 0;
107 
108  unsigned int exptPatNum = omtfConfig->getPatternNum(ptSim, chargeSim);
109  GoldenPatternWithStat* exptCandGp = goldenPatterns.at(exptPatNum).get(); // expected pattern
110  simMuFoundByOmtfPt->Fill(exptCandGp->key().theNumber); //TODO add weight of the muons pt spectrum
111 
112  simMuPtSpectrum->Fill(ptSim, getEventRateWeight(ptSim));
113 }
114 
116  std::string fName = edmCfg.getParameter<std::string>("optimisedPatsXmlFile");
117  edm::LogImportant("PatternOptimizer") << " Writing optimized patterns to " << fName << std::endl;
118  XMLConfigWriter xmlWriter(omtfConfig, false, false);
119  xmlWriter.writeGPs(goldenPatterns, fName);
120 
121  fName.replace(fName.find('.'), fName.length(), ".root");
123 }
124 
126  gStyle->SetOptStat(111111);
127  TFile outfile(rootFileName.c_str(), "RECREATE");
128  edm::LogVerbatim("l1tOmtfEventPrint") << __FUNCTION__ << ": " << __LINE__ << " out fileName " << rootFileName
129  << " outfile->GetName() " << outfile.GetName() << " writeLayerStat "
130  << writeLayerStat << endl;
131 
132  outfile.cd();
133  simMuFoundByOmtfPt->Write();
134 
135  simMuPtSpectrum->Write();
136 
137  outfile.mkdir("patternsPdfs")->cd();
138  outfile.mkdir("patternsPdfs/canvases");
139  outfile.mkdir("layerStats");
140  ostringstream ostrName;
141  ostringstream ostrTtle;
142  vector<TH1F*> classProbHists;
143  for (unsigned int iRefLayer = 0; iRefLayer < goldenPatterns[0]->getPdf()[0].size(); ++iRefLayer) {
144  ostrName.str("");
145  ostrName << "Neg_RefLayer_" << iRefLayer;
146  ostrTtle.str("");
147  ostrTtle << "Neg_RefLayer_" << iRefLayer;
148  classProbHists.push_back(new TH1F(
149  ostrName.str().c_str(), ostrTtle.str().c_str(), goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5));
150 
151  ostrName.str("");
152  ostrName << "Pos_RefLayer_" << iRefLayer;
153  ostrTtle.str("");
154  ostrTtle << "Pos_RefLayer_" << iRefLayer;
155  classProbHists.push_back(new TH1F(
156  ostrName.str().c_str(), ostrTtle.str().c_str(), goldenPatterns.size(), -0.5, goldenPatterns.size() - 0.5));
157  }
158 
159  for (auto& gp : goldenPatterns) {
160  OMTFConfiguration::PatternPt patternPt = omtfConfig->getPatternPtRange(gp->key().theNumber);
161  if (gp->key().thePt == 0)
162  continue;
163  //edm::LogVerbatim("l1tOmtfEventPrint") <<__FUNCTION__<<": "<<__LINE__<<" "<<gp->key()<<std::endl;
164  ostrName.str("");
165  ostrName << "PatNum_" << gp->key().theNumber;
166  ostrTtle.str("");
167  ostrTtle << "PatNum_" << gp->key().theNumber << "_ptCode_" << gp->key().thePt << "_Pt_" << patternPt.ptFrom << "_"
168  << patternPt.ptTo << "_GeV";
169  TCanvas* canvas = new TCanvas(ostrName.str().c_str(), ostrTtle.str().c_str(), 1200, 1000);
170  canvas->Divide(gp->getPdf().size(), gp->getPdf()[0].size(), 0, 0);
171 
172  for (unsigned int iLayer = 0; iLayer < gp->getPdf().size(); ++iLayer) {
173  for (unsigned int iRefLayer = 0; iRefLayer < gp->getPdf()[iLayer].size(); ++iRefLayer) {
174  canvas->cd(1 + iLayer + iRefLayer * gp->getPdf().size());
175  ostrName.str("");
176  ostrName << "PatNum_" << gp->key().theNumber << "_refLayer_" << iRefLayer << "_Layer_" << iLayer;
177  ostrTtle.str("");
178  ostrTtle << "PatNum " << gp->key().theNumber << " ptCode " << gp->key().thePt << " refLayer " << iRefLayer
179  << " Layer " << iLayer << " meanDistPhi " << gp->meanDistPhi[iLayer][iRefLayer][0]
180  << " distPhiBitShift "
181  << gp->getDistPhiBitShift(iLayer, iRefLayer); //"_Pt_"<<patternPt.ptFrom<<"_"<<patternPt.ptTo<<"_GeV
182  //edm::LogVerbatim("l1tOmtfEventPrint") <<__FUNCTION__<<": "<<__LINE__<<" creating hist "<<ostrTtle.str()<<std::endl;
183  TH1F* hist = new TH1F(
184  ostrName.str().c_str(), ostrTtle.str().c_str(), omtfConfig->nPdfBins(), -0.5, omtfConfig->nPdfBins() - 0.5);
185  for (unsigned int iPdf = 0; iPdf < gp->getPdf()[iLayer][iRefLayer].size(); iPdf++) {
186  hist->Fill(iPdf, gp->pdfAllRef[iLayer][iRefLayer][iPdf]);
187  }
188  if ((int)iLayer == (omtfConfig->getRefToLogicNumber()[iRefLayer]))
189  hist->SetLineColor(kGreen);
190 
191  hist->GetYaxis()->SetRangeUser(0, omtfConfig->pdfMaxValue() + 1);
192  hist->Draw("hist");
193 
194  outfile.cd("patternsPdfs");
195  hist->Write();
196 
198  if (writeLayerStat) {
199  string histName = "histLayerStat_" + ostrName.str();
200  unsigned int binCnt = gp->getStatistics()[iLayer][iRefLayer].size();
201  TH1I* histLayerStat = new TH1I(histName.c_str(), histName.c_str(), binCnt, -0.5, binCnt - 0.5);
202  for (unsigned int iBin = 0; iBin < binCnt; iBin++) {
203  histLayerStat->Fill(iBin, gp->getStatistics()[iLayer][iRefLayer][iBin][0]);
204  }
205 
206  outfile.cd("layerStats");
207  histLayerStat->Write();
208  }
209  }
210  }
211  outfile.cd("patternsPdfs/canvases");
212  canvas->Write();
213  delete canvas;
214 
215  unsigned int iPdf = omtfConfig->nPdfBins() / 2;
216  for (unsigned int iRefLayer = 0; iRefLayer < gp->getPdf()[0].size(); ++iRefLayer) {
217  unsigned int refLayerLogicNumber = omtfConfig->getRefToLogicNumber()[iRefLayer];
218  if (gp->key().theCharge == -1) {
219  classProbHists[2 * iRefLayer]->Fill(gp->key().theNumber, gp->pdfAllRef[refLayerLogicNumber][iRefLayer][iPdf]);
220  } else
221  classProbHists[2 * iRefLayer + 1]->Fill(gp->key().theNumber,
222  gp->pdfAllRef[refLayerLogicNumber][iRefLayer][iPdf]);
223  }
224  }
225 
226  outfile.cd();
227  for (auto& classProbHist : classProbHists) {
228  classProbHist->Write();
229  }
230 
232 
233  outfile.Close();
234 }
Log< level::Info, true > LogVerbatim
unsigned int theNumber
Definition: GoldenPattern.h:37
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
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
const std::vector< int > & getRefToLogicNumber() const
__shared__ Hist hist
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:119
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
void savePatternsInRoot(std::string rootFileName)
static int iptFromPt(const double pt)
Definition: RPCConst.cc:10
unsigned int nPdfBins() const
const OMTFConfiguration * omtfConfig