Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-06 02:54:12

0001 /*
0002  * DataROOTDumper2.cc
0003  *
0004  *  Created on: Dec 11, 2019
0005  *      Author: kbunkow
0006  */
0007 
0008 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Tools/DataROOTDumper2.h"
0009 
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
0012 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
0013 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
0014 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
0015 
0016 #include "TFile.h"
0017 #include "TTree.h"
0018 #include "TParameter.h"
0019 #include "TObjString.h"
0020 
0021 #include <boost/range/adaptor/reversed.hpp>
0022 #include <boost/timer/timer.hpp>
0023 #include <boost/archive/xml_iarchive.hpp>
0024 #include <boost/archive/xml_oarchive.hpp>
0025 #include <boost/archive/text_oarchive.hpp>
0026 #include <boost/archive/text_iarchive.hpp>
0027 
0028 DataROOTDumper2::DataROOTDumper2(const edm::ParameterSet& edmCfg,
0029                                  const OMTFConfiguration* omtfConfig,
0030                                  std::string rootFileName)
0031     : EmulationObserverBase(edmCfg, omtfConfig) {
0032   edm::LogVerbatim("l1tOmtfEventPrint") << " omtfConfig->nTestRefHits() " << omtfConfig->nTestRefHits()
0033                                         << " event.omtfGpResultsPdfSum.num_elements() " << endl;
0034   initializeTTree(rootFileName);
0035 
0036   edm::LogVerbatim("l1tOmtfEventPrint") << " DataROOTDumper2 created" << std::endl;
0037 }
0038 
0039 DataROOTDumper2::~DataROOTDumper2() { saveTTree(); }
0040 
0041 void DataROOTDumper2::initializeTTree(std::string rootFileName) {
0042   rootFile = new TFile(rootFileName.c_str(), "RECREATE");
0043   rootTree = new TTree("OMTFHitsTree", "");
0044 
0045   rootTree->Branch("muonPt", &omtfEvent.muonPt);
0046   rootTree->Branch("muonEta", &omtfEvent.muonEta);
0047   rootTree->Branch("muonPhi", &omtfEvent.muonPhi);
0048   rootTree->Branch("muonCharge", &omtfEvent.muonCharge);
0049 
0050   rootTree->Branch("omtfPt", &omtfEvent.omtfPt);
0051   rootTree->Branch("omtfEta", &omtfEvent.omtfEta);
0052   rootTree->Branch("omtfPhi", &omtfEvent.omtfPhi);
0053   rootTree->Branch("omtfCharge", &omtfEvent.omtfCharge);
0054 
0055   rootTree->Branch("omtfScore", &omtfEvent.omtfScore);
0056   rootTree->Branch("omtfQuality", &omtfEvent.omtfQuality);
0057   rootTree->Branch("omtfRefLayer", &omtfEvent.omtfRefLayer);
0058   rootTree->Branch("omtfProcessor", &omtfEvent.omtfProcessor);
0059 
0060   rootTree->Branch("omtfFiredLayers", &omtfEvent.omtfFiredLayers);  //<<<<<<<<<<<<<<<<<<<<<<!!!!TODOO
0061 
0062   ptGenPos = new TH1I("ptGenPos", "ptGenPos", 400, 0, 200);
0063   ptGenNeg = new TH1I("ptGenNeg", "ptGenNeg", 400, 0, 200);
0064 }
0065 
0066 void DataROOTDumper2::saveTTree() {
0067   rootFile->Write();
0068   ptGenPos->Write();
0069   ptGenNeg->Write();
0070 
0071   delete rootFile;
0072   delete rootTree;
0073 }
0074 
0075 void DataROOTDumper2::observeEventEnd(const edm::Event& iEvent,
0076                                       std::unique_ptr<l1t::RegionalMuonCandBxCollection>& finalCandidates) {
0077   int muonCharge = 0;
0078   if (simMuon) {
0079     if (abs(simMuon->momentum().eta()) < 0.8 || abs(simMuon->momentum().eta()) > 1.24)
0080       return;
0081 
0082     muonCharge = (abs(simMuon->type()) == 13) ? simMuon->type() / -13 : 0;
0083     if (muonCharge > 0)
0084       ptGenPos->Fill(simMuon->momentum().pt());
0085     else
0086       ptGenNeg->Fill(simMuon->momentum().pt());
0087   }
0088 
0089   if (simMuon == nullptr || !omtfCand->isValid())  //no sim muon or empty candidate
0090     return;
0091 
0092   omtfEvent.muonPt = simMuon->momentum().pt();
0093   omtfEvent.muonEta = simMuon->momentum().eta();
0094 
0095   //TODO add cut on ete if needed
0096   /*  if(abs(event.muonEta) < 0.8 || abs(event.muonEta) > 1.24)
0097     return;*/
0098 
0099   omtfEvent.muonPhi = simMuon->momentum().phi();
0100   omtfEvent.muonCharge = muonCharge;  //TODO
0101 
0102   if (omtfCand->getPt() > 0) {  //&& omtfCand->getFiredLayerCnt() > 3 TODO add to the if needed
0103     omtfEvent.omtfPt = omtfConfig->hwPtToGev(omtfCand->getPt());
0104     omtfEvent.omtfEta = omtfConfig->hwEtaToEta(omtfCand->getEtaHw());
0105     omtfEvent.omtfPhi = omtfCand->getPhi();
0106     omtfEvent.omtfCharge = std::pow(-1, regionalMuonCand.hwSign());
0107     omtfEvent.omtfScore = omtfCand->getPdfSum();
0108     omtfEvent.omtfQuality = regionalMuonCand.hwQual();  //omtfCand->getQ();
0109     omtfEvent.omtfFiredLayers = omtfCand->getFiredLayerBits();
0110     omtfEvent.omtfRefLayer = omtfCand->getRefLayer();
0111     omtfEvent.omtfProcessor = candProcIndx;
0112 
0113     omtfEvent.hits.clear();
0114 
0115     auto& gpResult = omtfCand->getGpResult();
0116 
0117     /*
0118     edm::LogVerbatim("l1tOmtfEventPrint")<<"DataROOTDumper2:;observeEventEnd muonPt "<<event.muonPt<<" muonCharge "<<event.muonCharge
0119         <<" omtfPt "<<event.omtfPt<<" RefLayer "<<event.omtfRefLayer<<" omtfPtCont "<<event.omtfPtCont
0120         <<std::endl;
0121 */
0122 
0123     for (unsigned int iLogicLayer = 0; iLogicLayer < gpResult.getStubResults().size(); ++iLogicLayer) {
0124       auto& stubResult = gpResult.getStubResults()[iLogicLayer];
0125       if (stubResult.getMuonStub()) {  //&& stubResult.getValid() //TODO!!!!!!!!!!!!!!!!1
0126         OmtfEvent::Hit hit;
0127         hit.layer = iLogicLayer;
0128         hit.quality = stubResult.getMuonStub()->qualityHw;
0129         hit.eta = stubResult.getMuonStub()->etaHw;  //in which scale?
0130         hit.valid = stubResult.getValid();
0131 
0132         int hitPhi = stubResult.getMuonStub()->phiHw;
0133         unsigned int refLayerLogicNum = omtfConfig->getRefToLogicNumber()[omtfCand->getRefLayer()];
0134         int phiRefHit = gpResult.getStubResults()[refLayerLogicNum].getMuonStub()->phiHw;
0135 
0136         if (omtfConfig->isBendingLayer(iLogicLayer)) {
0137           hitPhi = stubResult.getMuonStub()->phiBHw;
0138           phiRefHit = 0;  //phi ref hit for the bending layer set to 0, since it should not be included in the phiDist
0139         }
0140 
0141         //phiDist = hitPhi - phiRefHit;
0142         hit.phiDist = hitPhi - phiRefHit;
0143 
0144         /* LogTrace("l1tOmtfEventPrint")<<" muonPt "<<event.muonPt<<" omtfPt "<<event.omtfPt<<" RefLayer "<<event.omtfRefLayer
0145             <<" layer "<<int(hit.layer)<<" PdfBin "<<stubResult.getPdfBin()<<" hit.phiDist "<<hit.phiDist<<" valid "<<stubResult.getValid()<<" " //<<" phiDist "<<phiDist
0146             <<" getDistPhiBitShift "<<omtfCand->getGoldenPatern()->getDistPhiBitShift(iLogicLayer, omtfCand->getRefLayer())
0147             <<" meanDistPhiValue   "<<omtfCand->getGoldenPatern()->meanDistPhiValue(iLogicLayer, omtfCand->getRefLayer())//<<(phiDist != hit.phiDist? "!!!!!!!<<<<<" : "")
0148             <<endl;*/
0149 
0150         if (hit.phiDist > 504 || hit.phiDist < -512) {
0151           edm::LogVerbatim("l1tOmtfEventPrint")
0152               << " muonPt " << omtfEvent.muonPt << " omtfPt " << omtfEvent.omtfPt << " RefLayer "
0153               << omtfEvent.omtfRefLayer << " layer " << int(hit.layer) << " hit.phiDist " << hit.phiDist << " valid "
0154               << stubResult.getValid() << " !!!!!!!!!!!!!!!!!!!!!!!!" << endl;
0155         }
0156 
0157         DetId detId(stubResult.getMuonStub()->detId);
0158         if (detId.subdetId() == MuonSubdetId::CSC) {
0159           CSCDetId cscId(detId);
0160           hit.z = cscId.chamber() % 2;
0161         }
0162 
0163         omtfEvent.hits.push_back(hit.rawData);
0164       }
0165     }
0166 
0167     //debug
0168     /*if( (int)event.hits.size() != omtfCand->getQ()) {
0169       LogTrace("l1tOmtfEventPrint")<<" muonPt "<<event.muonPt<<" omtfPt "<<event.omtfPt<<" RefLayer "<<event.omtfRefLayer
0170                     <<" hits.size "<<event.hits.size()<<" omtfCand->getQ "<<omtfCand->getQ()<<" !!!!!!!!!!!!!!!!!!aaa!!!!!!"<<endl;
0171     }*/
0172 
0173     rootTree->Fill();
0174     evntCnt++;
0175   }
0176 }
0177 
0178 void DataROOTDumper2::endJob() { edm::LogVerbatim("l1tOmtfEventPrint") << " evntCnt " << evntCnt << endl; }