CMS 3D CMS Logo

DataROOTDumper2.cc
Go to the documentation of this file.
1 /*
2  * DataROOTDumper2.cc
3  *
4  * Created on: Dec 11, 2019
5  * Author: kbunkow
6  */
7 
9 
12 
18 
19 #include "TFile.h"
20 #include "TTree.h"
21 
23  const OMTFConfiguration* omtfConfig,
24  CandidateSimMuonMatcher* candidateSimMuonMatcher)
25  : EmulationObserverBase(edmCfg, omtfConfig), candidateSimMuonMatcher(candidateSimMuonMatcher) {
26  edm::LogVerbatim("l1tOmtfEventPrint") << " omtfConfig->nTestRefHits() " << omtfConfig->nTestRefHits()
27  << " event.omtfGpResultsPdfSum.num_elements() " << endl;
29 
30  if (edmCfg.exists("dumpKilledOmtfCands"))
31  if (edmCfg.getParameter<bool>("dumpKilledOmtfCands"))
32  dumpKilledOmtfCands = true;
33 
34  edm::LogVerbatim("l1tOmtfEventPrint") << " DataROOTDumper2 created. dumpKilledOmtfCands " << dumpKilledOmtfCands
35  << std::endl;
36 }
37 
39 
42 
43  rootTree = fs->make<TTree>("OMTFHitsTree", "");
44 
45  rootTree->Branch("eventNum", &omtfEvent.eventNum);
46  rootTree->Branch("muonEvent", &omtfEvent.muonEvent);
47 
48  rootTree->Branch("muonPt", &omtfEvent.muonPt);
49  rootTree->Branch("muonEta", &omtfEvent.muonEta);
50  rootTree->Branch("muonPhi", &omtfEvent.muonPhi);
51  rootTree->Branch("muonPropEta", &omtfEvent.muonPropEta);
52  rootTree->Branch("muonPropPhi", &omtfEvent.muonPropPhi);
53  rootTree->Branch("muonCharge", &omtfEvent.muonCharge);
54 
55  rootTree->Branch("muonDxy", &omtfEvent.muonDxy);
56  rootTree->Branch("muonRho", &omtfEvent.muonRho);
57 
58  rootTree->Branch("omtfPt", &omtfEvent.omtfPt);
59  rootTree->Branch("omtfUPt", &omtfEvent.omtfUPt);
60  rootTree->Branch("omtfEta", &omtfEvent.omtfEta);
61  rootTree->Branch("omtfPhi", &omtfEvent.omtfPhi);
62  rootTree->Branch("omtfCharge", &omtfEvent.omtfCharge);
63 
64  rootTree->Branch("omtfHwEta", &omtfEvent.omtfHwEta);
65 
66  rootTree->Branch("omtfProcessor", &omtfEvent.omtfProcessor);
67  rootTree->Branch("omtfScore", &omtfEvent.omtfScore);
68  rootTree->Branch("omtfQuality", &omtfEvent.omtfQuality);
69  rootTree->Branch("omtfRefLayer", &omtfEvent.omtfRefLayer);
70  rootTree->Branch("omtfRefHitNum", &omtfEvent.omtfRefHitNum);
71 
72  rootTree->Branch("omtfFiredLayers", &omtfEvent.omtfFiredLayers); //<<<<<<<<<<<<<<<<<<<<<<!!!!TODOO
73 
74  rootTree->Branch("killed", &omtfEvent.killed);
75 
76  rootTree->Branch("hits", &omtfEvent.hits);
77 
78  rootTree->Branch("deltaEta", &omtfEvent.deltaEta);
79  rootTree->Branch("deltaPhi", &omtfEvent.deltaPhi);
80 
81  ptGenPos = fs->make<TH1I>("ptGenPos", "ptGenPos, eta at vertex 0.8 - 1.24", 400, 0, 200); //TODO
82  ptGenNeg = fs->make<TH1I>("ptGenNeg", "ptGenNeg, eta at vertex 0.8 - 1.24", 400, 0, 200);
83 }
84 
85 void DataROOTDumper2::observeProcesorEmulation(unsigned int iProcessor,
86  l1t::tftype mtfType,
87  const std::shared_ptr<OMTFinput>&,
88  const AlgoMuons& algoCandidates,
89  const AlgoMuons& gbCandidates,
90  const std::vector<l1t::RegionalMuonCand>& candMuons) {}
91 
93  std::unique_ptr<l1t::RegionalMuonCandBxCollection>& finalCandidates) {
94  /*
95  int muonCharge = 0;
96  if (simMuon) {
97  if (std::abs(simMuon->momentum().eta()) < 0.8 || std::abs(simMuon->momentum().eta()) > 1.24)
98  return;
99 
100  muonCharge = (std::abs(simMuon->type()) == 13) ? simMuon->type() / -13 : 0;
101  if (muonCharge > 0)
102  ptGenPos->Fill(simMuon->momentum().pt());
103  else
104  ptGenNeg->Fill(simMuon->momentum().pt());
105  }
106 
107  if (simMuon == nullptr || !omtfCand->isValid()) //no sim muon or empty candidate
108  return;
109 
110  omtfEvent.muonPt = simMuon->momentum().pt();
111  omtfEvent.muonEta = simMuon->momentum().eta();
112 
113  //TODO add cut on ete if needed
114  if(std::abs(event.muonEta) < 0.8 || std::abs(event.muonEta) > 1.24)
115  return;
116 
117  omtfEvent.muonPhi = simMuon->momentum().phi();
118  omtfEvent.muonCharge = muonCharge; //TODO
119  */
120 
121  std::vector<MatchingResult> matchingResults = candidateSimMuonMatcher->getMatchingResults();
122  LogTrace("l1tOmtfEventPrint") << "\nDataROOTDumper2::observeEventEnd matchingResults.size() "
123  << matchingResults.size() << std::endl;
124 
125  //candidateSimMuonMatcher should use the trackingParticles, because the simTracks are not stored for the pile-up events
126  for (auto& matchingResult : matchingResults) {
127  omtfEvent.eventNum = iEvent.id().event();
128 
129  if (matchingResult.trackingParticle) {
130  auto trackingParticle = matchingResult.trackingParticle;
131 
132  omtfEvent.muonEvent = trackingParticle->eventId().event();
133 
134  omtfEvent.muonPt = trackingParticle->pt();
135  omtfEvent.muonEta = trackingParticle->momentum().eta();
136  omtfEvent.muonPhi = trackingParticle->momentum().phi();
137  omtfEvent.muonPropEta = matchingResult.propagatedEta;
138  omtfEvent.muonPropPhi = matchingResult.propagatedPhi;
139  omtfEvent.muonCharge = (std::abs(trackingParticle->pdgId()) == 13) ? trackingParticle->pdgId() / -13 : 0;
140 
141  if (trackingParticle->parentVertex().isNonnull()) {
142  omtfEvent.muonDxy = trackingParticle->dxy();
143  omtfEvent.muonRho = trackingParticle->parentVertex()->position().Rho();
144  }
145 
146  omtfEvent.deltaEta = matchingResult.deltaEta;
147  omtfEvent.deltaPhi = matchingResult.deltaPhi;
148 
149  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd trackingParticle: eventId "
150  << trackingParticle->eventId().event() << " pdgId " << std::setw(3)
151  << trackingParticle->pdgId() << " trackId "
152  << trackingParticle->g4Tracks().at(0).trackId() << " pt " << std::setw(9)
153  << trackingParticle->pt() //<<" Beta "<<simMuon->momentum().Beta()
154  << " eta " << std::setw(9) << trackingParticle->momentum().eta() << " phi "
155  << std::setw(9) << trackingParticle->momentum().phi() << std::endl;
156 
157  if (std::abs(omtfEvent.muonEta) > 0.8 && std::abs(omtfEvent.muonEta) < 1.24) {
158  if (omtfEvent.muonCharge > 0)
159  ptGenPos->Fill(omtfEvent.muonPt);
160  else
161  ptGenNeg->Fill(omtfEvent.muonPt);
162  }
163  } else if (matchingResult.simTrack) {
164  auto simTrack = matchingResult.simTrack;
165 
166  omtfEvent.muonEvent = simTrack->eventId().event();
167 
168  omtfEvent.muonPt = simTrack->momentum().pt();
169  omtfEvent.muonEta = simTrack->momentum().eta();
170  omtfEvent.muonPhi = simTrack->momentum().phi();
171  omtfEvent.muonPropEta = matchingResult.propagatedEta;
172  omtfEvent.muonPropPhi = matchingResult.propagatedPhi;
173  omtfEvent.muonCharge = simTrack->charge();
174 
175  if (!simTrack->noVertex() && matchingResult.simVertex) {
176  const math::XYZTLorentzVectorD& vtxPos = matchingResult.simVertex->position();
177  omtfEvent.muonDxy = (-vtxPos.X() * simTrack->momentum().py() + vtxPos.Y() * simTrack->momentum().px()) /
178  simTrack->momentum().pt();
179  omtfEvent.muonRho = vtxPos.Rho();
180  }
181 
182  omtfEvent.deltaEta = matchingResult.deltaEta;
183  omtfEvent.deltaPhi = matchingResult.deltaPhi;
184 
185  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd simTrack: eventId "
186  << simTrack->eventId().event() << " pdgId " << std::setw(3)
187  << simTrack->type() //<< " trackId " << simTrack->g4Tracks().at(0).trackId()
188  << " pt " << std::setw(9)
189  << simTrack->momentum().pt() //<<" Beta "<<simMuon->momentum().Beta()
190  << " eta " << std::setw(9) << simTrack->momentum().eta() << " phi " << std::setw(9)
191  << simTrack->momentum().phi() << std::endl;
192 
193  if (std::abs(omtfEvent.muonEta) > 0.8 && std::abs(omtfEvent.muonEta) < 1.24) {
194  if (omtfEvent.muonCharge > 0)
195  ptGenPos->Fill(omtfEvent.muonPt);
196  else
197  ptGenNeg->Fill(omtfEvent.muonPt);
198  }
199  } else {
200  omtfEvent.muonEvent = -1;
201 
202  omtfEvent.muonPt = 0;
203 
204  omtfEvent.muonEta = 0;
205  omtfEvent.muonPhi = 0;
206 
209 
210  omtfEvent.muonCharge = 0; //TODO
211 
212  omtfEvent.muonDxy = 0;
213  omtfEvent.muonRho = 0;
214  }
215 
216  auto addOmtfCand = [&](AlgoMuonPtr& procMuon) {
217  omtfEvent.omtfPt = omtfConfig->hwPtToGev(procMuon->getPtConstr());
218  omtfEvent.omtfUPt = omtfConfig->hwUPtToGev(procMuon->getPtUnconstr());
219  omtfEvent.omtfEta = omtfConfig->hwEtaToEta(procMuon->getEtaHw());
220  omtfEvent.omtfPhi = procMuon->getPhi();
221  omtfEvent.omtfCharge = procMuon->getChargeConstr();
222  omtfEvent.omtfScore = procMuon->getPdfSum();
223 
224  omtfEvent.omtfHwEta = procMuon->getEtaHw();
225 
226  omtfEvent.omtfFiredLayers = procMuon->getFiredLayerBits();
227  omtfEvent.omtfRefLayer = procMuon->getRefLayer();
228  omtfEvent.omtfRefHitNum = procMuon->getRefHitNumber();
229 
230  omtfEvent.hits.clear();
231 
232  //TODO choose, which gpResult should be dumped
233  //auto& gpResult = procMuon->getGpResultConstr();
234  auto& gpResult = (procMuon->getGpResultUnconstr().getPdfSumUnconstr() > procMuon->getGpResultConstr().getPdfSum())
235  ? procMuon->getGpResultUnconstr()
236  : procMuon->getGpResultConstr();
237 
238  /*
239  edm::LogVerbatim("l1tOmtfEventPrint")<<"DataROOTDumper2:;observeEventEnd muonPt "<<event.muonPt<<" muonCharge "<<event.muonCharge
240  <<" omtfPt "<<event.omtfPt<<" RefLayer "<<event.omtfRefLayer<<" omtfPtCont "<<event.omtfPtCont
241  <<std::endl; */
242 
243  for (unsigned int iLogicLayer = 0; iLogicLayer < gpResult.getStubResults().size(); ++iLogicLayer) {
244  auto& stubResult = gpResult.getStubResults()[iLogicLayer];
245 
246  //TODO it is to have the hit if it is below the quality cut
247  /*if (omtfConfig->isBendingLayer(iLogicLayer) && !stubResult.getMuonStub()) {
248  auto& stubResult = gpResult.getStubResults()[iLogicLayer-1];
249  }*/
250 
251  if (stubResult.getMuonStub()) { //&& stubResult.getValid() //TODO!!!!!!!!!!!!!!!!1
253  hit.layer = iLogicLayer;
254  hit.quality = stubResult.getMuonStub()->qualityHw;
255  hit.eta = stubResult.getMuonStub()->etaHw; //in which scale?
256  hit.valid = stubResult.getValid();
257 
258  int hitPhi = stubResult.getMuonStub()->phiHw;
259  unsigned int refLayerLogicNum = omtfConfig->getRefToLogicNumber()[procMuon->getRefLayer()];
260  int phiRefHit = gpResult.getStubResults()[refLayerLogicNum].getMuonStub()->phiHw;
261 
262  if (omtfConfig->isBendingLayer(iLogicLayer)) {
263  hitPhi = stubResult.getMuonStub()->phiBHw;
264  phiRefHit = 0; //phi ref hit for the bending layer set to 0, since it should not be included in the phiDist
265  }
266 
267  //phiDist = hitPhi - phiRefHit;
268  hit.phiDist = hitPhi - phiRefHit;
269 
270  /* LogTrace("l1tOmtfEventPrint")<<" muonPt "<<event.muonPt<<" omtfPt "<<event.omtfPt<<" RefLayer "<<event.omtfRefLayer
271  <<" layer "<<int(hit.layer)<<" PdfBin "<<stubResult.getPdfBin()<<" hit.phiDist "<<hit.phiDist<<" valid "<<stubResult.getValid()<<" " //<<" phiDist "<<phiDist
272  <<" getDistPhiBitShift "<<procMuon->getGoldenPatern()->getDistPhiBitShift(iLogicLayer, procMuon->getRefLayer())
273  <<" meanDistPhiValue "<<procMuon->getGoldenPatern()->meanDistPhiValue(iLogicLayer, procMuon->getRefLayer())//<<(phiDist != hit.phiDist? "!!!!!!!<<<<<" : "")
274  <<endl;*/
275 
276  if (hit.phiDist > 504 || hit.phiDist < -512) {
277  edm::LogVerbatim("l1tOmtfEventPrint")
278  << " muonPt " << omtfEvent.muonPt << " omtfPt " << omtfEvent.omtfPt << " RefLayer "
279  << (int)omtfEvent.omtfRefLayer << " layer " << int(hit.layer) << " hit.phiDist " << hit.phiDist
280  << " valid " << stubResult.getValid() << " !!!!!!!!!!!!!!!!!!!!!!!!" << endl;
281  }
282 
283  DetId detId(stubResult.getMuonStub()->detId);
284  if (detId.subdetId() == MuonSubdetId::CSC) {
285  CSCDetId cscId(detId);
286  hit.z = cscId.chamber() % 2;
287  }
288 
289  omtfEvent.hits.push_back(hit.rawData);
290  }
291  }
292 
293  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd adding omtfCand : " << std::endl;
294  auto finalCandidate = matchingResult.muonCand;
295  LogTrace("l1tOmtfEventPrint") << " hwPt " << finalCandidate->hwPt() << " hwSign " << finalCandidate->hwSign()
296  << " hwQual " << finalCandidate->hwQual() << " hwEta " << std::setw(4)
297  << finalCandidate->hwEta() << std::setw(4) << " hwPhi " << finalCandidate->hwPhi()
298  << " eta " << std::setw(9) << (finalCandidate->hwEta() * 0.010875)
299  << " isKilled " << procMuon->isKilled() << " tRefLayer " << procMuon->getRefLayer()
300  << " RefHitNumber " << procMuon->getRefHitNumber() << std::endl;
301  };
302 
303  if (matchingResult.muonCand && matchingResult.procMuon->getPtConstr() >= 0 &&
304  matchingResult.muonCand->hwQual() >= 1) {
305  //TODO set the quality, quality 0 has the candidates with eta > 1.3(?) EtaHw >= 121
306  //&& matchingResult.genPt < 20
307 
308  omtfEvent.omtfQuality = matchingResult.muonCand->hwQual(); //procMuon->getQ();
309  omtfEvent.killed = false;
310  omtfEvent.omtfProcessor = matchingResult.muonCand->processor();
311 
312  if (matchingResult.muonCand->trackFinderType() == l1t::omtf_neg) {
313  omtfEvent.omtfProcessor *= -1;
314  }
315 
316  addOmtfCand(matchingResult.procMuon);
317  rootTree->Fill();
318 
319  if (dumpKilledOmtfCands) {
320  for (auto& killedCand : matchingResult.procMuon->getKilledMuons()) {
322  omtfEvent.killed = true;
323  if (killedCand->isKilled() == false) {
324  edm::LogVerbatim("l1tOmtfEventPrint") << " killedCand->isKilled() == false !!!!!!!!";
325  }
326  addOmtfCand(killedCand);
327  rootTree->Fill();
328  }
329  }
330  } else if (omtfEvent.muonPt > 0) { //checking if there was a simMuon
331  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd no matching omtfCand" << std::endl;
332 
333  omtfEvent.omtfPt = 0;
334  omtfEvent.omtfUPt = 0;
335  omtfEvent.omtfEta = 0;
336  omtfEvent.omtfPhi = 0;
337  omtfEvent.omtfCharge = 0;
338  omtfEvent.omtfScore = 0;
339 
340  omtfEvent.omtfHwEta = 0;
341 
346 
348  omtfEvent.killed = false;
349 
350  omtfEvent.hits.clear();
351 
352  rootTree->Fill();
353  }
354  }
355  evntCnt++;
356 }
357 
358 void DataROOTDumper2::endJob() { edm::LogVerbatim("l1tOmtfEventPrint") << " evntCnt " << evntCnt << endl; }
short muonEvent
Log< level::Info, true > LogVerbatim
float muonPropEta
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void endJob() override
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
CandidateSimMuonMatcher * candidateSimMuonMatcher
bool exists(std::string const &parameterName) const
checks if a parameter exists
char omtfRefHitNum
char omtfQuality
virtual double hwEtaToEta(int hwEta) const
center of eta bin
unsigned int omtfFiredLayers
#define LogTrace(id)
float deltaPhi
DataROOTDumper2(const edm::ParameterSet &edmCfg, const OMTFConfiguration *omtfConfig, CandidateSimMuonMatcher *candidateSimMuonMatcher)
unsigned int evntCnt
int iEvent
Definition: GenABIO.cc:224
double hwUPtToGev(int hwPt) const override
unsigned int eventNum
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int chamber() const
Definition: CSCDetId.h:62
float muonPropPhi
const std::vector< int > & getRefToLogicNumber() const
double hwPtToGev(int hwPt) const override
uGMT pt scale conversion
short omtfScore
unsigned int nTestRefHits() const
std::vector< MatchingResult > getMatchingResults()
float deltaEta
Definition: DetId.h:17
char omtfRefLayer
void observeProcesorEmulation(unsigned int iProcessor, l1t::tftype mtfType, const std::shared_ptr< OMTFinput > &, const AlgoMuons &algoCandidates, const AlgoMuons &gbCandidates, const std::vector< l1t::RegionalMuonCand > &candMuons) override
std::vector< AlgoMuonPtr > AlgoMuons
Definition: AlgoMuon.h:183
std::vector< unsigned long > hits
short omtfHwEta
std::shared_ptr< AlgoMuon > AlgoMuonPtr
Definition: AlgoMuon.h:182
static constexpr int CSC
Definition: MuonSubdetId.h:12
char omtfProcessor
const OMTFConfiguration * omtfConfig
void observeEventEnd(const edm::Event &iEvent, std::unique_ptr< l1t::RegionalMuonCandBxCollection > &finalCandidates) override
~DataROOTDumper2() override
bool isBendingLayer(unsigned int iLayer) const override