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  if (edmCfg.exists("candidateSimMuonMatcherType")) {
35  if (edmCfg.getParameter<std::string>("candidateSimMuonMatcherType") == "propagation")
36  usePropagation = true;
37  else if (edmCfg.getParameter<std::string>("candidateSimMuonMatcherType") == "matchSimple")
38  usePropagation = false;
39 
40  edm::LogImportant("l1tOmtfEventPrint")
41  << " CandidateSimMuonMatcher: candidateSimMuonMatcherType "
42  << edmCfg.getParameter<std::string>("candidateSimMuonMatcherType") << std::endl;
43  }
44 
45  edm::LogVerbatim("l1tOmtfEventPrint") << " DataROOTDumper2 created. dumpKilledOmtfCands " << dumpKilledOmtfCands
46  << std::endl;
47 }
48 
50 
53 
54  rootTree = fs->make<TTree>("OMTFHitsTree", "");
55 
56  rootTree->Branch("eventNum", &omtfEvent.eventNum);
57  rootTree->Branch("muonEvent", &omtfEvent.muonEvent);
58 
59  rootTree->Branch("muonPt", &omtfEvent.muonPt);
60  rootTree->Branch("muonEta", &omtfEvent.muonEta);
61  rootTree->Branch("muonPhi", &omtfEvent.muonPhi);
62  rootTree->Branch("muonPropEta", &omtfEvent.muonPropEta);
63  rootTree->Branch("muonPropPhi", &omtfEvent.muonPropPhi);
64  rootTree->Branch("muonCharge", &omtfEvent.muonCharge);
65 
66  rootTree->Branch("muonDxy", &omtfEvent.muonDxy);
67  rootTree->Branch("muonRho", &omtfEvent.muonRho);
68 
69  rootTree->Branch("omtfPt", &omtfEvent.omtfPt);
70  rootTree->Branch("omtfUPt", &omtfEvent.omtfUPt);
71  rootTree->Branch("omtfEta", &omtfEvent.omtfEta);
72  rootTree->Branch("omtfPhi", &omtfEvent.omtfPhi);
73  rootTree->Branch("omtfCharge", &omtfEvent.omtfCharge);
74 
75  rootTree->Branch("omtfHwEta", &omtfEvent.omtfHwEta);
76 
77  rootTree->Branch("omtfProcessor", &omtfEvent.omtfProcessor);
78  rootTree->Branch("omtfScore", &omtfEvent.omtfScore);
79  rootTree->Branch("omtfQuality", &omtfEvent.omtfQuality);
80  rootTree->Branch("omtfRefLayer", &omtfEvent.omtfRefLayer);
81  rootTree->Branch("omtfRefHitNum", &omtfEvent.omtfRefHitNum);
82 
83  rootTree->Branch("omtfFiredLayers", &omtfEvent.omtfFiredLayers); //<<<<<<<<<<<<<<<<<<<<<<!!!!TODOO
84 
85  rootTree->Branch("killed", &omtfEvent.killed);
86 
87  rootTree->Branch("hits", &omtfEvent.hits);
88 
89  rootTree->Branch("deltaEta", &omtfEvent.deltaEta);
90  rootTree->Branch("deltaPhi", &omtfEvent.deltaPhi);
91 
92  ptGenPos = fs->make<TH1I>("ptGenPos", "ptGenPos, eta at vertex 0.8 - 1.24", 400, 0, 200); //TODO
93  ptGenNeg = fs->make<TH1I>("ptGenNeg", "ptGenNeg, eta at vertex 0.8 - 1.24", 400, 0, 200);
94 }
95 
96 void DataROOTDumper2::observeProcesorEmulation(unsigned int iProcessor,
97  l1t::tftype mtfType,
98  const std::shared_ptr<OMTFinput>&,
99  const AlgoMuons& algoCandidates,
100  const AlgoMuons& gbCandidates,
101  const std::vector<l1t::RegionalMuonCand>& candMuons) {}
102 
104  std::unique_ptr<l1t::RegionalMuonCandBxCollection>& finalCandidates) {
105  /*
106  int muonCharge = 0;
107  if (simMuon) {
108  if (std::abs(simMuon->momentum().eta()) < 0.8 || std::abs(simMuon->momentum().eta()) > 1.24)
109  return;
110 
111  muonCharge = (std::abs(simMuon->type()) == 13) ? simMuon->type() / -13 : 0;
112  if (muonCharge > 0)
113  ptGenPos->Fill(simMuon->momentum().pt());
114  else
115  ptGenNeg->Fill(simMuon->momentum().pt());
116  }
117 
118  if (simMuon == nullptr || !omtfCand->isValid()) //no sim muon or empty candidate
119  return;
120 
121  omtfEvent.muonPt = simMuon->momentum().pt();
122  omtfEvent.muonEta = simMuon->momentum().eta();
123 
124  //TODO add cut on ete if needed
125  if(std::abs(event.muonEta) < 0.8 || std::abs(event.muonEta) > 1.24)
126  return;
127 
128  omtfEvent.muonPhi = simMuon->momentum().phi();
129  omtfEvent.muonCharge = muonCharge; //TODO
130  */
131 
132  std::vector<MatchingResult> matchingResults = candidateSimMuonMatcher->getMatchingResults();
133  LogTrace("l1tOmtfEventPrint") << "\nDataROOTDumper2::observeEventEnd matchingResults.size() "
134  << matchingResults.size() << std::endl;
135 
136  //candidateSimMuonMatcher should use the trackingParticles, because the simTracks are not stored for the pile-up events
137 
138  //for some events there are more than one matchingResults,
139  //Usually at least one them has genPt 0, which means no simMuon was matched, so candidate is ghost (or fake)
140  //so better is to to drop such event, as it is not sue if the correct simMuon was matched to the candidate.
141  //So we assume here that when the propagation is not used it is a single mu sample and this filter has sense
142  //the propagation is used for multi-muon sample, so then this fitler cannot be used
143  //TODO add a flag to enable this filter? Disable it if not needed
144  if (!usePropagation && matchingResults.size() > 1) { //omtfConfig->cleanStubs() &&
145  edm::LogVerbatim("l1tOmtfEventPrint")
146  << "\nDataROOTDumper2::observeEventEnd matchingResults.size() " << matchingResults.size() << std::endl;
147 
148  for (auto& matchingResult : matchingResults) {
149  edm::LogVerbatim("l1tOmtfEventPrint") << "matchingResult: genPt " << matchingResult.genPt;
150  if (matchingResult.procMuon)
151  edm::LogVerbatim("l1tOmtfEventPrint") << " procMuon.PtConstr " << matchingResult.procMuon->getPtConstr();
152  else
153  edm::LogVerbatim("l1tOmtfEventPrint") << " no procMuon" << std::endl;
154  }
155  edm::LogVerbatim("l1tOmtfEventPrint") << "dropping the event!!!\n" << std::endl;
156  return;
157  }
158 
159  for (auto& matchingResult : matchingResults) {
160  omtfEvent.eventNum = iEvent.id().event();
161 
162  if (matchingResult.trackingParticle) {
163  auto trackingParticle = matchingResult.trackingParticle;
164 
165  omtfEvent.muonEvent = trackingParticle->eventId().event();
166 
167  omtfEvent.muonPt = trackingParticle->pt();
168  omtfEvent.muonEta = trackingParticle->momentum().eta();
169  omtfEvent.muonPhi = trackingParticle->momentum().phi();
170  omtfEvent.muonPropEta = matchingResult.propagatedEta;
171  omtfEvent.muonPropPhi = matchingResult.propagatedPhi;
172  omtfEvent.muonCharge = (std::abs(trackingParticle->pdgId()) == 13) ? trackingParticle->pdgId() / -13 : 0;
173 
174  if (trackingParticle->parentVertex().isNonnull()) {
175  omtfEvent.muonDxy = trackingParticle->dxy();
176  omtfEvent.muonRho = trackingParticle->parentVertex()->position().Rho();
177  }
178 
179  omtfEvent.deltaEta = matchingResult.deltaEta;
180  omtfEvent.deltaPhi = matchingResult.deltaPhi;
181 
182  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd trackingParticle: eventId "
183  << trackingParticle->eventId().event() << " pdgId " << std::setw(3)
184  << trackingParticle->pdgId() << " trackId "
185  << trackingParticle->g4Tracks().at(0).trackId() << " pt " << std::setw(9)
186  << trackingParticle->pt() //<<" Beta "<<simMuon->momentum().Beta()
187  << " eta " << std::setw(9) << trackingParticle->momentum().eta() << " phi "
188  << std::setw(9) << trackingParticle->momentum().phi() << std::endl;
189 
190  if (std::abs(omtfEvent.muonEta) > 0.8 && std::abs(omtfEvent.muonEta) < 1.24) {
191  if (omtfEvent.muonCharge > 0)
192  ptGenPos->Fill(omtfEvent.muonPt);
193  else
194  ptGenNeg->Fill(omtfEvent.muonPt);
195  }
196  } else if (matchingResult.simTrack) {
197  auto simTrack = matchingResult.simTrack;
198 
199  omtfEvent.muonEvent = simTrack->eventId().event();
200 
201  omtfEvent.muonPt = simTrack->momentum().pt();
202  omtfEvent.muonEta = simTrack->momentum().eta();
203  omtfEvent.muonPhi = simTrack->momentum().phi();
204  omtfEvent.muonPropEta = matchingResult.propagatedEta;
205  omtfEvent.muonPropPhi = matchingResult.propagatedPhi;
206  omtfEvent.muonCharge = simTrack->charge();
207 
208  if (!simTrack->noVertex() && matchingResult.simVertex) {
209  const math::XYZTLorentzVectorD& vtxPos = matchingResult.simVertex->position();
210  omtfEvent.muonDxy = (-vtxPos.X() * simTrack->momentum().py() + vtxPos.Y() * simTrack->momentum().px()) /
211  simTrack->momentum().pt();
212  omtfEvent.muonRho = vtxPos.Rho();
213  }
214 
215  omtfEvent.deltaEta = matchingResult.deltaEta;
216  omtfEvent.deltaPhi = matchingResult.deltaPhi;
217 
218  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd simTrack: eventId "
219  << simTrack->eventId().event() << " pdgId " << std::setw(3)
220  << simTrack->type() //<< " trackId " << simTrack->g4Tracks().at(0).trackId()
221  << " pt " << std::setw(9)
222  << simTrack->momentum().pt() //<<" Beta "<<simMuon->momentum().Beta()
223  << " eta " << std::setw(9) << simTrack->momentum().eta() << " phi " << std::setw(9)
224  << simTrack->momentum().phi() << std::endl;
225 
226  if (std::abs(omtfEvent.muonEta) > 0.8 && std::abs(omtfEvent.muonEta) < 1.24) {
227  if (omtfEvent.muonCharge > 0)
228  ptGenPos->Fill(omtfEvent.muonPt);
229  else
230  ptGenNeg->Fill(omtfEvent.muonPt);
231  }
232  } else {
233  omtfEvent.muonEvent = -1;
234 
235  omtfEvent.muonPt = 0;
236 
237  omtfEvent.muonEta = 0;
238  omtfEvent.muonPhi = 0;
239 
242 
243  omtfEvent.muonCharge = 0; //TODO
244 
245  omtfEvent.muonDxy = 0;
246  omtfEvent.muonRho = 0;
247  }
248 
249  auto addOmtfCand = [&](AlgoMuonPtr& procMuon) {
250  omtfEvent.omtfPt = omtfConfig->hwPtToGev(procMuon->getPtConstr());
251  omtfEvent.omtfUPt = omtfConfig->hwUPtToGev(procMuon->getPtUnconstr());
252  omtfEvent.omtfEta = omtfConfig->hwEtaToEta(procMuon->getEtaHw());
253  omtfEvent.omtfPhi = procMuon->getPhi();
254  omtfEvent.omtfCharge = procMuon->getChargeConstr();
255  omtfEvent.omtfScore = procMuon->getPdfSum();
256 
257  omtfEvent.omtfHwEta = procMuon->getEtaHw();
258 
259  omtfEvent.omtfFiredLayers = procMuon->getFiredLayerBits();
260  omtfEvent.omtfRefLayer = procMuon->getRefLayer();
261  omtfEvent.omtfRefHitNum = procMuon->getRefHitNumber();
262 
263  omtfEvent.hits.clear();
264 
265  //TODO choose, which gpResult should be dumped
266  //auto& gpResult = procMuon->getGpResultConstr();
267  auto& gpResult = (procMuon->getGpResultUnconstr().getPdfSumUnconstr() > procMuon->getGpResultConstr().getPdfSum())
268  ? procMuon->getGpResultUnconstr()
269  : procMuon->getGpResultConstr();
270 
271  /*
272  edm::LogVerbatim("l1tOmtfEventPrint")<<"DataROOTDumper2:;observeEventEnd muonPt "<<event.muonPt<<" muonCharge "<<event.muonCharge
273  <<" omtfPt "<<event.omtfPt<<" RefLayer "<<event.omtfRefLayer<<" omtfPtCont "<<event.omtfPtCont
274  <<std::endl; */
275 
276  for (unsigned int iLogicLayer = 0; iLogicLayer < gpResult.getStubResults().size(); ++iLogicLayer) {
277  auto& stubResult = gpResult.getStubResults()[iLogicLayer];
278 
279  //TODO it is to have the hit if it is below the quality cut
280  /*if (omtfConfig->isBendingLayer(iLogicLayer) && !stubResult.getMuonStub()) {
281  auto& stubResult = gpResult.getStubResults()[iLogicLayer-1];
282  }*/
283 
284  if (stubResult.getMuonStub()) { //&& stubResult.getValid() //TODO!!!!!!!!!!!!!!!!1
286  hit.layer = iLogicLayer;
287  hit.quality = stubResult.getMuonStub()->qualityHw;
288  hit.eta = stubResult.getMuonStub()->etaHw; //in which scale?
289  hit.valid = stubResult.getValid();
290 
291  int hitPhi = stubResult.getMuonStub()->phiHw;
292  unsigned int refLayerLogicNum = omtfConfig->getRefToLogicNumber()[procMuon->getRefLayer()];
293  int phiRefHit = gpResult.getStubResults()[refLayerLogicNum].getMuonStub()->phiHw;
294 
295  if (omtfConfig->isBendingLayer(iLogicLayer)) {
296  hitPhi = stubResult.getMuonStub()->phiBHw;
297  phiRefHit = 0; //phi ref hit for the bending layer set to 0, since it should not be included in the phiDist
298  }
299 
300  //phiDist = hitPhi - phiRefHit;
301  hit.phiDist = hitPhi - phiRefHit;
302 
303  /* LogTrace("l1tOmtfEventPrint")<<" muonPt "<<event.muonPt<<" omtfPt "<<event.omtfPt<<" RefLayer "<<event.omtfRefLayer
304  <<" layer "<<int(hit.layer)<<" PdfBin "<<stubResult.getPdfBin()<<" hit.phiDist "<<hit.phiDist<<" valid "<<stubResult.getValid()<<" " //<<" phiDist "<<phiDist
305  <<" getDistPhiBitShift "<<procMuon->getGoldenPatern()->getDistPhiBitShift(iLogicLayer, procMuon->getRefLayer())
306  <<" meanDistPhiValue "<<procMuon->getGoldenPatern()->meanDistPhiValue(iLogicLayer, procMuon->getRefLayer())//<<(phiDist != hit.phiDist? "!!!!!!!<<<<<" : "")
307  <<endl;*/
308 
309  /*if (hit.phiDist > 504 || hit.phiDist < -512) {
310  edm::LogVerbatim("l1tOmtfEventPrint")
311  << " muonPt " << omtfEvent.muonPt << " omtfPt " << omtfEvent.omtfPt << " RefLayer "
312  << (int)omtfEvent.omtfRefLayer << " layer " << int(hit.layer) << " hit.phiDist " << hit.phiDist
313  << " valid " << stubResult.getValid() << " !!!!!!!!!!!!!!!!!!!!!!!!" << endl;
314  }*/
315 
316  DetId detId(stubResult.getMuonStub()->detId);
317  if (detId.subdetId() == MuonSubdetId::CSC) {
318  CSCDetId cscId(detId);
319  hit.z = cscId.chamber() % 2;
320  }
321 
322  omtfEvent.hits.push_back(hit.rawData);
323  }
324  }
325 
326  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd adding omtfCand : " << std::endl;
327  auto finalCandidate = matchingResult.muonCand;
328  LogTrace("l1tOmtfEventPrint") << " hwPt " << finalCandidate->hwPt() << " hwSign " << finalCandidate->hwSign()
329  << " hwQual " << finalCandidate->hwQual() << " hwEta " << std::setw(4)
330  << finalCandidate->hwEta() << std::setw(4) << " hwPhi " << finalCandidate->hwPhi()
331  << " eta " << std::setw(9) << (finalCandidate->hwEta() * 0.010875)
332  << " isKilled " << procMuon->isKilled() << " tRefLayer " << procMuon->getRefLayer()
333  << " RefHitNumber " << procMuon->getRefHitNumber() << std::endl;
334  };
335 
336  if (matchingResult.muonCand && matchingResult.procMuon->getPtConstr() > 0 &&
337  matchingResult.muonCand->hwQual() >= 1) {
338  //TODO set the quality, quality 0 has the candidates with eta > 1.3(?) EtaHw >= 121
339  //&& matchingResult.genPt < 20
340 
341  omtfEvent.omtfQuality = matchingResult.muonCand->hwQual(); //procMuon->getQ();
342  omtfEvent.killed = false;
343  omtfEvent.omtfProcessor = matchingResult.muonCand->processor();
344 
345  if (matchingResult.muonCand->trackFinderType() == l1t::omtf_neg) {
346  omtfEvent.omtfProcessor *= -1;
347  }
348 
349  addOmtfCand(matchingResult.procMuon);
350  rootTree->Fill();
351 
352  if (dumpKilledOmtfCands) {
353  for (auto& killedCand : matchingResult.procMuon->getKilledMuons()) {
355  omtfEvent.killed = true;
356  if (killedCand->isKilled() == false) {
357  edm::LogVerbatim("l1tOmtfEventPrint") << " killedCand->isKilled() == false !!!!!!!!";
358  }
359  addOmtfCand(killedCand);
360  rootTree->Fill();
361  }
362  }
363  } else if (omtfEvent.muonPt > 0) { //checking if there was a simMuon
364  LogTrace("l1tOmtfEventPrint") << "DataROOTDumper2::observeEventEnd no matching omtfCand" << std::endl;
365 
366  omtfEvent.omtfPt = 0;
367  omtfEvent.omtfUPt = 0;
368  omtfEvent.omtfEta = 0;
369  omtfEvent.omtfPhi = 0;
370  omtfEvent.omtfCharge = 0;
371  omtfEvent.omtfScore = 0;
372 
373  omtfEvent.omtfHwEta = 0;
374 
379 
381  omtfEvent.killed = false;
382 
383  omtfEvent.hits.clear();
384 
385  rootTree->Fill();
386  }
387  }
388  evntCnt++;
389 }
390 
391 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
Log< level::Error, true > LogImportant
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:176
std::vector< unsigned long > hits
short omtfHwEta
std::shared_ptr< AlgoMuon > AlgoMuonPtr
Definition: AlgoMuon.h:175
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