Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:10

0001 /*
0002  * \file DTLocalTriggerTask.cc
0003  *
0004  * \author M. Zanetti - INFN Padova
0005  *
0006 */
0007 
0008 #include "DQM/DTMonitorModule/src/DTLocalTriggerTask.h"
0009 
0010 // Framework
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 
0013 // DT trigger
0014 #include "DQM/DTMonitorModule/interface/DTTrigGeomUtils.h"
0015 
0016 // Geometry
0017 #include "DataFormats/GeometryVector/interface/Pi.h"
0018 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0019 #include "Geometry/DTGeometry/interface/DTLayer.h"
0020 #include "Geometry/DTGeometry/interface/DTTopology.h"
0021 
0022 //Root
0023 #include "TH1.h"
0024 #include "TAxis.h"
0025 
0026 #include <sstream>
0027 #include <iostream>
0028 #include <fstream>
0029 
0030 using namespace edm;
0031 using namespace std;
0032 
0033 DTLocalTriggerTask::DTLocalTriggerTask(const edm::ParameterSet& ps)
0034     : muonGeomToken_(esConsumes<edm::Transition::BeginRun>()),
0035       trigGeomUtils(nullptr),
0036       isLocalRun(ps.getUntrackedParameter<bool>("localrun", true)) {
0037   if (!isLocalRun) {
0038     ltcDigiCollectionToken_ = consumes<LTCDigiCollection>(ps.getParameter<edm::InputTag>("ltcDigiCollectionTag"));
0039   }
0040 
0041   LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerTask") << "[DTLocalTriggerTask]: Constructor" << endl;
0042 
0043   tpMode = ps.getUntrackedParameter<bool>("testPulseMode", false);
0044   detailedAnalysis = ps.getUntrackedParameter<bool>("detailedAnalysis", false);
0045   doTMTheta = ps.getUntrackedParameter<bool>("enableTMTheta", false);
0046   tm_Token_ = consumes<L1MuDTChambPhContainer>(
0047       ps.getUntrackedParameter<InputTag>("tm_label", InputTag("twinMuxStage2Digis:PhIn")));
0048   tmTh_Token_ = consumes<L1MuDTChambThContainer>(
0049       ps.getUntrackedParameter<edm::InputTag>("tmTh_label", InputTag("twinMuxStage2Digis:ThIn")));
0050 
0051   ros_Token_ =
0052       consumes<DTLocalTriggerCollection>(ps.getUntrackedParameter<InputTag>("ros_label", InputTag("dtunpacker")));
0053   seg_Token_ =
0054       consumes<DTRecSegment4DCollection>(ps.getUntrackedParameter<InputTag>("seg_label", InputTag("dt4DSegments")));
0055 
0056   if (tpMode) {
0057     baseFolderTM = "DT/11-LocalTriggerTP-TM/";
0058   } else {
0059     baseFolderTM = "DT/03-LocalTrigger-TM/";
0060   }
0061 
0062   parameters = ps;
0063 }
0064 
0065 DTLocalTriggerTask::~DTLocalTriggerTask() {
0066   LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerTask")
0067       << "[DTLocalTriggerTask]: analyzed " << nevents << " events" << endl;
0068   if (trigGeomUtils) {
0069     delete trigGeomUtils;
0070   }
0071 }
0072 
0073 void DTLocalTriggerTask::dqmBeginRun(const edm::Run& run, const edm::EventSetup& context) {
0074   nevents = 0;
0075   muonGeom = &context.getData(muonGeomToken_);
0076   trigGeomUtils = new DTTrigGeomUtils(muonGeom);
0077 }
0078 
0079 void DTLocalTriggerTask::bookHistograms(DQMStore::IBooker& ibooker,
0080                                         edm::Run const& iRun,
0081                                         edm::EventSetup const& context) {
0082   LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerTask") << "[DTLocalTriggerTask]: bookHistograms" << endl;
0083 
0084   if (parameters.getUntrackedParameter<bool>("staticBooking", true)) {  // Static histo booking
0085 
0086     vector<string> trigSources;
0087     if (parameters.getUntrackedParameter<bool>("localrun", true)) {
0088       trigSources.push_back("");
0089     } else {
0090       trigSources.push_back("_DTonly");
0091       trigSources.push_back("_NoDT");
0092       trigSources.push_back("_DTalso");
0093     }
0094     vector<string>::const_iterator trigSrcIt = trigSources.begin();
0095     vector<string>::const_iterator trigSrcEnd = trigSources.end();
0096 
0097     if (parameters.getUntrackedParameter<bool>("process_tm", true)) {
0098       bookBarrelHistos(ibooker, "TM_ErrorsChamberID");
0099     }
0100 
0101     if (tpMode) {
0102       for (int stat = 1; stat < 5; ++stat) {
0103         for (int wh = -2; wh < 3; ++wh) {
0104           for (int sect = 1; sect < 13; ++sect) {
0105             DTChamberId dtChId(wh, stat, sect);
0106 
0107             if (parameters.getUntrackedParameter<bool>("process_tm", true)) {  // TM data
0108               bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_BXvsQual" + (*trigSrcIt));
0109               bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_QualvsPhirad" + (*trigSrcIt));
0110             }
0111           }
0112         }
0113       }  // end of loop
0114     } else {
0115       for (; trigSrcIt != trigSrcEnd; ++trigSrcIt) {
0116         for (int wh = -2; wh < 3; ++wh) {
0117           for (int sect = 1; sect < 13; ++sect) {
0118             for (int stat = 1; stat < 5; ++stat) {
0119               DTChamberId dtChId(wh, stat, sect);
0120               if (parameters.getUntrackedParameter<bool>("process_tm", true)) {  // TM data
0121 
0122                 bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_BXvsQual_In" + (*trigSrcIt));
0123                 if (detailedAnalysis) {
0124                   bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_QualvsPhirad_In" + (*trigSrcIt));
0125                   bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_QualvsPhibend_In" + (*trigSrcIt));
0126                 }
0127                 bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_Flag1stvsQual_In" + (*trigSrcIt));
0128                 bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_FlagUpDownvsQual_In" + (*trigSrcIt));
0129 
0130                 bookHistos(ibooker, dtChId, "LocalTriggerPhiIn", "TM_BestQual_In" + (*trigSrcIt));
0131                 if (stat != 4 && doTMTheta) {
0132                   bookHistos(ibooker, dtChId, "LocalTriggerTheta", "TM_PositionvsBX" + (*trigSrcIt));
0133                   bookHistos(ibooker, dtChId, "LocalTriggerTheta", "TM_QualityvsBX" + (*trigSrcIt));
0134 
0135                   bookHistos(ibooker, dtChId, "LocalTriggerTheta", "TM_PositionvsQual" + (*trigSrcIt));
0136 
0137                   bookHistos(ibooker, dtChId, "LocalTriggerTheta", "TM_ThetaBXvsQual" + (*trigSrcIt));
0138                   bookHistos(ibooker, dtChId, "LocalTriggerTheta", "TM_ThetaBestQual" + (*trigSrcIt));
0139                 }
0140 
0141                 if (parameters.getUntrackedParameter<bool>("process_seg", true)) {  // TM + Segemnt
0142                   bookHistos(ibooker, dtChId, "Segment", "TM_PhitkvsPhitrig" + (*trigSrcIt));
0143                   bookHistos(ibooker, dtChId, "Segment", "TM_PhibtkvsPhibtrig" + (*trigSrcIt));
0144                   bookHistos(ibooker, dtChId, "Segment", "TM_PhiResidual" + (*trigSrcIt));
0145                   bookHistos(ibooker, dtChId, "Segment", "TM_PhiResidualvsLUTPhi" + (*trigSrcIt));
0146                   bookHistos(ibooker, dtChId, "Segment", "TM_PhibResidual" + (*trigSrcIt));
0147                   bookHistos(ibooker, dtChId, "Segment", "TM_HitstkvsQualtrig" + (*trigSrcIt));
0148                   bookHistos(ibooker, dtChId, "Segment", "TM_TrackPosvsAngle" + (*trigSrcIt));
0149                   bookHistos(ibooker, dtChId, "Segment", "TM_TrackPosvsAngleandTrig" + (*trigSrcIt));
0150                   bookHistos(ibooker, dtChId, "Segment", "TM_TrackPosvsAngleandTrigHHHL" + (*trigSrcIt));
0151                   if (stat != 4) {
0152                     bookHistos(ibooker, dtChId, "Segment", "TM_TrackThetaPosvsAngle" + (*trigSrcIt));  // theta view
0153                     bookHistos(ibooker, dtChId, "Segment", "TM_TrackThetaPosvsAngleandTrig" + (*trigSrcIt));
0154                     bookHistos(ibooker,
0155                                dtChId,
0156                                "Segment",
0157                                "TM_TrackThetaPosvsAngleandTrigH" + (*trigSrcIt));  // TM theta quality not available!
0158                   }
0159                 }
0160               }
0161             }
0162           }
0163           for (int sect = 13; sect < 15; ++sect) {
0164             DTChamberId dtChId(wh, 4, sect);
0165             if (parameters.getUntrackedParameter<bool>("process_tm", true) &&
0166                 parameters.getUntrackedParameter<bool>("process_seg", true)) {  // TM+SEG LUTs data
0167               bookHistos(ibooker, dtChId, "Segment", "TM_PhitkvsPhitrig" + (*trigSrcIt));
0168               bookHistos(ibooker, dtChId, "Segment", "TM_PhibtkvsPhibtrig" + (*trigSrcIt));
0169               bookHistos(ibooker, dtChId, "Segment", "TM_PhiResidual" + (*trigSrcIt));
0170               bookHistos(ibooker, dtChId, "Segment", "TM_PhiResidualvsLUTPhi" + (*trigSrcIt));
0171               bookHistos(ibooker, dtChId, "Segment", "TM_PhibResidual" + (*trigSrcIt));
0172             }
0173           }
0174         }
0175       }  // end of loop
0176     }
0177   }
0178 }
0179 
0180 void DTLocalTriggerTask::beginLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& context) {
0181   LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerTask") << "[DTLocalTriggerTask]: Begin of LS transition" << endl;
0182 
0183   if (lumiSeg.id().luminosityBlock() % parameters.getUntrackedParameter<int>("ResetCycle", 3) == 0) {
0184     for (map<uint32_t, map<string, MonitorElement*> >::const_iterator histo = digiHistos.begin();
0185          histo != digiHistos.end();
0186          histo++) {
0187       for (map<string, MonitorElement*>::const_iterator ht = (*histo).second.begin(); ht != (*histo).second.end();
0188            ht++) {
0189         (*ht).second->Reset();
0190       }
0191     }
0192   }
0193 }
0194 
0195 void DTLocalTriggerTask::analyze(const edm::Event& e, const edm::EventSetup& c) {
0196   if (!nevents) {
0197     edm::Handle<L1MuDTChambPhContainer> l1DTTPGPh;
0198     e.getByToken(tm_Token_, l1DTTPGPh);
0199     edm::Handle<L1MuDTChambThContainer> l1DTTPGTh;
0200     e.getByToken(tmTh_Token_, l1DTTPGTh);
0201 
0202     useTM = (l1DTTPGPh.isValid() || l1DTTPGTh.isValid()) && parameters.getUntrackedParameter<bool>("process_tm", true);
0203 
0204     Handle<DTRecSegment4DCollection> all4DSegments;
0205     e.getByToken(seg_Token_, all4DSegments);
0206     useSEG = all4DSegments.isValid() && parameters.getUntrackedParameter<bool>("process_seg", true);
0207   }
0208 
0209   nevents++;
0210 
0211   triggerSource(e);
0212 
0213   if (useTM) {
0214     edm::Handle<L1MuDTChambPhContainer> l1DTTPGPh;
0215     e.getByToken(tm_Token_, l1DTTPGPh);
0216     vector<L1MuDTChambPhDigi> const* l1PhTrig = l1DTTPGPh->getContainer();
0217 
0218     edm::Handle<L1MuDTChambThContainer> l1DTTPGTh;
0219     e.getByToken(tmTh_Token_, l1DTTPGTh);
0220 
0221     vector<L1MuDTChambThDigi> const* l1ThTrig = l1DTTPGTh->getContainer();
0222 
0223     runTMAnalysis(l1PhTrig, l1ThTrig);
0224   }
0225   if (!tpMode && useSEG) {
0226     Handle<DTRecSegment4DCollection> segments4D;
0227     e.getByToken(seg_Token_, segments4D);
0228 
0229     runSegmentAnalysis(segments4D);
0230   }
0231 }
0232 
0233 void DTLocalTriggerTask::bookBarrelHistos(DQMStore::IBooker& ibooker, string histoTag) {
0234   ibooker.setCurrentFolder(topFolder());
0235   if (histoTag == "TM_ErrorsChamberID") {
0236     tm_IDDataErrorPlot = ibooker.book1D(histoTag.c_str(), "TM Data ID Error", 5, -2, 3);
0237     tm_IDDataErrorPlot->setAxisTitle("wheel", 1);
0238   }
0239 
0240   return;
0241 }
0242 
0243 void DTLocalTriggerTask::bookHistos(DQMStore::IBooker& ibooker,
0244                                     const DTChamberId& dtCh,
0245                                     string folder,
0246                                     string histoTag) {
0247   int wh = dtCh.wheel();
0248   int sc = dtCh.sector();
0249   stringstream wheel;
0250   wheel << wh;
0251   stringstream station;
0252   station << dtCh.station();
0253   stringstream sector;
0254   sector << sc;
0255 
0256   double minBX = 0;
0257   double maxBX = 0;
0258   int rangeBX = 0;
0259 
0260   string histoType = histoTag.substr(3, histoTag.find('_', 3) - 3);
0261 
0262   ibooker.setCurrentFolder(topFolder() + "Wheel" + wheel.str() + "/Sector" + sector.str() + "/Station" + station.str() +
0263                            "/" + folder);
0264 
0265   string histoName = histoTag + "_W" + wheel.str() + "_Sec" + sector.str() + "_St" + station.str();
0266 
0267   LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerTask")
0268       << "[DTLocalTriggerTask]: booking " << topFolder() << "Wheel" << wheel.str() << "/Sector" << sector.str()
0269       << "/Station" << station.str() << "/" << folder << "/" << histoName << endl;
0270 
0271   if (histoType.find("BX") != string::npos) {
0272     if (histoTag.substr(0, 2) == "TM") {
0273       minBX = parameters.getUntrackedParameter<int>("minBXTM", -2) - 0.5;
0274       maxBX = parameters.getUntrackedParameter<int>("maxBXTM", 2) + 0.5;
0275     }
0276     rangeBX = (int)(maxBX - minBX);
0277   }
0278 
0279   if (folder == "LocalTriggerPhiIn") {
0280     if (histoType == "BXvsQual") {
0281       (digiHistos[dtCh.rawId()])[histoTag] =
0282           ibooker.book2D(histoName, "BX vs trigger quality", 7, -0.5, 6.5, rangeBX, minBX, maxBX);
0283       setQLabels((digiHistos[dtCh.rawId()])[histoTag], 1);
0284       return;
0285     }
0286     if (histoType == "BestQual") {
0287       (digiHistos[dtCh.rawId()])[histoTag] =
0288           ibooker.book1D(histoName, "Trigger quality of best primitives", 7, -0.5, 6.5);
0289       setQLabels((digiHistos[dtCh.rawId()])[histoTag], 1);
0290       return;
0291     }
0292     if (histoType == "QualvsPhirad") {
0293       (digiHistos[dtCh.rawId()])[histoTag] =
0294           ibooker.book2D(histoName, "Trigger quality vs local position", 100, -500., 500., 7, -0.5, 6.5);
0295       setQLabels((digiHistos[dtCh.rawId()])[histoTag], 2);
0296       return;
0297     }
0298     if (histoType == "QualvsPhibend") {
0299       (digiHistos[dtCh.rawId()])[histoTag] =
0300           ibooker.book2D(histoName, "Trigger quality vs local direction", 200, -40., 40., 7, -0.5, 6.5);
0301       setQLabels((digiHistos[dtCh.rawId()])[histoTag], 2);
0302       return;
0303     }
0304     if (histoType == "Flag1stvsQual") {
0305       (digiHistos[dtCh.rawId()])[histoTag] =
0306           ibooker.book2D(histoName, "1st/2nd trig flag vs quality", 7, -0.5, 6.5, 2, -0.5, 1.5);
0307       setQLabels((digiHistos[dtCh.rawId()])[histoTag], 1);
0308       return;
0309     }
0310     if (histoType == "FlagUpDownvsQual") {
0311       (digiHistos[dtCh.rawId()])[histoTag] =
0312           ibooker.book2D(histoName, "Up/Down trig flag vs quality", 7, -0.5, 6.5, 2, -0.5, 1.5);
0313       setQLabels((digiHistos[dtCh.rawId()])[histoTag], 1);
0314       return;
0315     }
0316 
0317   } else if (folder == "LocalTriggerTheta") {
0318     if (histoType == "PositionvsBX") {
0319       (digiHistos[dtCh.rawId()])[histoTag] =
0320           ibooker.book2D(histoName, "Theta trigger position vs BX", rangeBX, minBX, maxBX, 7, -0.5, 6.5);
0321       return;
0322     }
0323     if (histoType == "PositionvsQual") {
0324       (digiHistos[dtCh.rawId()])[histoTag] =
0325           ibooker.book2D(histoName, "Theta trigger position vs quality", 2, 0.5, 2.5, 7, -0.5, 6.5);
0326       setQLabelsTheta((digiHistos[dtCh.rawId()])[histoTag], 1);
0327       return;
0328     }
0329     if (histoType == "ThetaBXvsQual") {
0330       (digiHistos[dtCh.rawId()])[histoTag] =
0331           ibooker.book2D(histoName, "BX vs trigger quality", 2, 0.5, 2.5, rangeBX, minBX, maxBX);
0332       setQLabelsTheta((digiHistos[dtCh.rawId()])[histoTag], 1);
0333     }
0334     if (histoType == "ThetaBestQual") {  //It does not make sense: 7 positions, how to define best?
0335       (digiHistos[dtCh.rawId()])[histoTag] = ibooker.book1D(
0336           histoName, "Trigger quality of best primitives (theta)", 2, 0.5, 2.5);  // 0 = not fired, 1 = L, 2 = H
0337       setQLabelsTheta((digiHistos[dtCh.rawId()])[histoTag], 1);
0338       return;
0339     }
0340 
0341   } else if (folder == "Segment") {
0342     if (histoType.find("TrackThetaPosvsAngle") == 0) {
0343       string histoLabel = "Position vs Angle (theta)";
0344       if (histoType.find("andTrigH") != string::npos)
0345         histoLabel += " for H triggers";
0346       else if (histoType.find("andTrig") != string::npos)
0347         histoLabel += " for triggers";
0348 
0349       float min, max;
0350       int nbins;
0351       trigGeomUtils->thetaRange(dtCh, min, max, nbins);
0352       (digiHistos[dtCh.rawId()])[histoTag] = ibooker.book2D(histoName, histoLabel, 16, -40., 40., nbins, min, max);
0353       return;
0354     }
0355     if (histoType.find("TrackPosvsAngle") == 0) {
0356       float min, max;
0357       int nbins;
0358       trigGeomUtils->phiRange(dtCh, min, max, nbins);
0359 
0360       string histoLabel = "Position vs Angle (phi)";
0361       if (histoType.find("andTrigHHHL") != string::npos)
0362         histoLabel += " for HH/HL triggers";
0363       else if (histoType.find("andTrig") != string::npos)
0364         histoLabel += " for triggers";
0365 
0366       (digiHistos[dtCh.rawId()])[histoTag] = ibooker.book2D(histoName, histoLabel, 16, -40., 40., nbins, min, max);
0367       return;
0368     }
0369     if (histoType == "PhitkvsPhitrig") {
0370       (digiHistos[dtCh.rawId()])[histoTag] =
0371           ibooker.book2D(histoName, "Local position: segment vs trigger", 100, -500., 500., 100, -500., 500.);
0372       return;
0373     }
0374     if (histoType == "PhibtkvsPhibtrig") {
0375       (digiHistos[dtCh.rawId()])[histoTag] =
0376           ibooker.book2D(histoName, "Local direction : segment vs trigger", 200, -40., 40., 200, -40., 40.);
0377       return;
0378     }
0379     if (histoType == "PhiResidual") {
0380       (digiHistos[dtCh.rawId()])[histoTag] = ibooker.book1D(
0381           histoName, "Trigger local position - Segment local position (correlated triggers)", 400, -10., 10.);
0382       return;
0383     }
0384     if (histoType == "PhibResidual") {
0385       (digiHistos[dtCh.rawId()])[histoTag] = ibooker.book1D(
0386           histoName, "Trigger local direction - Segment local direction (correlated triggers)", 500, -10., 10.);
0387       return;
0388     }
0389     if (histoType == "HitstkvsQualtrig") {
0390       (digiHistos[dtCh.rawId()])[histoTag] =
0391           ibooker.book2D(histoName, "Segment hits (phi) vs trigger quality", 7, -0.5, 6.5, 10, 0.5, 10.5);
0392       setQLabels((digiHistos[dtCh.rawId()])[histoTag], 1);
0393       return;
0394     }
0395   }
0396 }
0397 
0398 void DTLocalTriggerTask::bookWheelHistos(DQMStore::IBooker& ibooker, int wh, string histoTag) {
0399   stringstream wheel;
0400   wheel << wh;
0401 
0402   string histoType = histoTag.substr(3, histoTag.find('_', 3) - 3);
0403 
0404   ibooker.setCurrentFolder(topFolder() + "Wheel" + wheel.str() + "/");
0405 
0406   string histoName = histoTag + "_W" + wheel.str();
0407 
0408   LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerTask")
0409       << "[DTLocalTriggerTask]: booking " << topFolder() << "Wheel" << wheel.str() << "/" << histoName << endl;
0410 }
0411 
0412 void DTLocalTriggerTask::runTMAnalysis(std::vector<L1MuDTChambPhDigi> const* phTrigs,
0413                                        std::vector<L1MuDTChambThDigi> const* thTrigs) {
0414   //exit(0);
0415 
0416   string histoType;
0417   string histoTag;
0418 
0419   // define best quality trigger segment (phi and theta)
0420   // in any station start from 1 and zero is kept empty
0421   for (int i = 0; i < 5; ++i) {
0422     for (int j = 0; j < 6; ++j) {
0423       for (int k = 0; k < 13; ++k) {
0424         phcode_best[j][i][k] = -1;
0425         thcode_best[j][i][k] = -1;
0426       }
0427     }
0428   }
0429 
0430   vector<L1MuDTChambPhDigi>::const_iterator iph = phTrigs->begin();
0431   vector<L1MuDTChambPhDigi>::const_iterator iphe = phTrigs->end();
0432   for (; iph != iphe; ++iph) {
0433     int phwheel = iph->whNum();
0434     int phsec = iph->scNum() + 1;  // SM The track finder goes from 0 to 11. I need them from 1 to 12 !!!!!
0435     int phst = iph->stNum();
0436     int phbx = iph->bxNum();
0437     int phcode = iph->code();
0438     int phi1st = iph->Ts2Tag();
0439     int updown = iph->UpDownTag();
0440 
0441     // FIXME: workaround for TM data with station ID
0442     if (phst == 0) {
0443       tm_IDDataErrorPlot->Fill(phwheel);
0444       continue;
0445     }
0446 
0447     if (phcode > phcode_best[phwheel + 3][phst][phsec] && phcode < 7) {
0448       phcode_best[phwheel + 3][phst][phsec] = phcode;
0449       iphbest[phwheel + 3][phst][phsec] = &(*iph);
0450     }
0451 
0452     DTChamberId dtChId(phwheel, phst, phsec);
0453 
0454     float x = trigGeomUtils->trigPos(&(*iph));
0455     float angle = trigGeomUtils->trigDir(&(*iph));
0456     uint32_t indexCh = dtChId.rawId();
0457 
0458     map<string, MonitorElement*>& innerME = digiHistos[indexCh];
0459 
0460     if (tpMode) {
0461       innerME.find("TM_BXvsQual" + trigsrc)->second->Fill(phcode, phbx - phi1st);  // SM BX vs Qual Phi view (1st tracks)
0462       innerME.find("TM_QualvsPhirad" + trigsrc)->second->Fill(x, phcode);          // SM Qual vs radial angle Phi view
0463     } else {
0464       innerME.find("TM_BXvsQual_In" + trigsrc)
0465           ->second->Fill(phcode, phbx - phi1st);  // SM BX vs Qual Phi view (1st tracks)
0466       innerME.find("TM_Flag1stvsQual_In" + trigsrc)
0467           ->second->Fill(phcode, phi1st);  // SM Qual 1st/2nd track flag Phi view
0468       innerME.find("TM_FlagUpDownvsQual_In" + trigsrc)
0469           ->second->Fill(phcode, updown);  // SM Qual Up/Down track flag Phi view
0470       if (detailedAnalysis) {
0471         innerME.find("TM_QualvsPhirad_In" + trigsrc)->second->Fill(x, phcode);       // SM Qual vs radial angle Phi view
0472         innerME.find("TM_QualvsPhibend_In" + trigsrc)->second->Fill(angle, phcode);  // SM Qual vs bending Phi view
0473       }
0474     }
0475   }
0476 
0477   if (doTMTheta) {
0478     int thcode[7];
0479     vector<L1MuDTChambThDigi>::const_iterator ith = thTrigs->begin();
0480     vector<L1MuDTChambThDigi>::const_iterator ithe = thTrigs->end();
0481     for (; ith != ithe; ++ith) {
0482       int thwheel = ith->whNum();
0483       int thsec = ith->scNum() + 1;  // SM The track finder goes from 0 to 11. I need them from 1 to 12 !!!!!
0484       int thst = ith->stNum();
0485       int thbx = ith->bxNum();
0486 
0487       for (int pos = 0; pos < 7; pos++) {
0488         thcode[pos] = ith->code(pos);
0489 
0490         if (thcode[pos] > thcode_best[thwheel + 3][thst][thsec]) {
0491           thcode_best[thwheel + 3][thst][thsec] = thcode[pos];
0492           ithbest[thwheel + 3][thst][thsec] = &(*ith);
0493         }
0494       }
0495 
0496       DTChamberId dtChId(thwheel, thst, thsec);
0497       uint32_t indexCh = dtChId.rawId();
0498 
0499       map<string, MonitorElement*>& innerME = digiHistos[indexCh];
0500 
0501       for (int pos = 0; pos < 7; pos++) {  //SM fill position for non zero position bit in theta view
0502         if (thcode[pos] > 0) {             //Fired
0503           innerME.find("TM_PositionvsBX" + trigsrc)->second->Fill(thbx, pos);  // SM BX vs Position Theta view
0504           innerME.find("TM_PositionvsQual" + trigsrc)
0505               ->second->Fill(thcode[pos], pos);  //code = pos + qual; so 0, 1, 2 for 0, L, H resp.
0506           innerME.find("TM_ThetaBXvsQual" + trigsrc)
0507               ->second->Fill(thcode[pos], thbx);  //code = pos + qual; so 0, 1, 2 for 0, L, H resp.
0508         }
0509       }
0510     }
0511   }
0512 
0513   // Fill Quality plots with best TM triggers in phi & theta
0514   if (!tpMode) {
0515     for (int st = 1; st < 5; ++st) {
0516       for (int wh = -2; wh < 3; ++wh) {
0517         for (int sc = 1; sc < 13; ++sc) {
0518           if (phcode_best[wh + wheelArrayShift][st][sc] > -1 && phcode_best[wh + wheelArrayShift][st][sc] < 7) {
0519             DTChamberId id(wh, st, sc);
0520             uint32_t indexCh = id.rawId();
0521             map<string, MonitorElement*>& innerME = digiHistos[indexCh];
0522 
0523             innerME.find("TM_BestQual_In" + trigsrc)
0524                 ->second->Fill(phcode_best[wh + wheelArrayShift][st][sc]);  // Best Qual Trigger Phi view
0525           }
0526           if (thcode_best[wh + wheelArrayShift][st][sc] > 0 && thcode_best[wh + wheelArrayShift][st][sc] < 3) {
0527             DTChamberId id(wh, st, sc);
0528             uint32_t indexCh = id.rawId();
0529             map<string, MonitorElement*>& innerME = digiHistos[indexCh];
0530 
0531             innerME.find("TM_ThetaBestQual" + trigsrc)
0532                 ->second->Fill(thcode_best[wh + wheelArrayShift][st][sc]);  // Best Qual Trigger Theta view
0533           }
0534         }
0535       }
0536     }
0537   }
0538 }
0539 
0540 void DTLocalTriggerTask::runSegmentAnalysis(Handle<DTRecSegment4DCollection>& segments4D) {
0541   DTRecSegment4DCollection::const_iterator track;
0542 
0543   // Find best tracks & good tracks
0544   memset(track_ok, false, 450 * sizeof(bool));
0545 
0546   DTRecSegment4DCollection::id_iterator chamberId;
0547   vector<const DTRecSegment4D*> best4DSegments;
0548 
0549   // Preliminary loop finds best 4D Segment and high quality ones
0550   for (chamberId = segments4D->id_begin(); chamberId != segments4D->id_end(); ++chamberId) {
0551     DTRecSegment4DCollection::range range = segments4D->get(*chamberId);
0552     const DTRecSegment4D* tmpBest = nullptr;
0553     int tmpdof = 0;
0554     int dof = 0;
0555 
0556     for (track = range.first; track != range.second; ++track) {
0557       if ((*track).hasPhi()) {
0558         dof = (*track).phiSegment()->degreesOfFreedom();
0559         if (dof > tmpdof) {
0560           tmpBest = &(*track);
0561           tmpdof = dof;
0562 
0563           int wheel = (*track).chamberId().wheel();
0564           int sector = (*track).chamberId().sector();
0565           int station = (*track).chamberId().station();
0566           if (sector == 13) {
0567             sector = 4;
0568           } else if (sector == 14) {
0569             sector = 10;
0570           }
0571           track_ok[wheel + 3][station][sector] = (!track_ok[wheel + 3][station][sector] && dof >= 2);
0572         }
0573       }
0574     }
0575     if (tmpBest)
0576       best4DSegments.push_back(tmpBest);
0577   }
0578 
0579   vector<const DTRecSegment4D*>::const_iterator btrack;
0580 
0581   for (btrack = best4DSegments.begin(); btrack != best4DSegments.end(); ++btrack) {
0582     if ((*btrack)->hasPhi()) {  // Phi component
0583 
0584       int wheel = (*btrack)->chamberId().wheel();
0585       int station = (*btrack)->chamberId().station();
0586       int sector = (*btrack)->chamberId().sector();
0587       int scsector = 0;
0588       float x_track, y_track, x_angle, y_angle;
0589       trigGeomUtils->computeSCCoordinates((*btrack), scsector, x_track, x_angle, y_track, y_angle);
0590       int nHitsPhi = (*btrack)->phiSegment()->degreesOfFreedom() + 2;
0591 
0592       DTChamberId dtChId(wheel, station, sector);  // get chamber for LUTs histograms (Sectors 1 to 14)
0593       uint32_t indexCh = dtChId.rawId();
0594       map<string, MonitorElement*>& innerMECh = digiHistos[indexCh];
0595 
0596       DTChamberId dtChIdSC =
0597           DTChamberId(wheel, station, scsector);  // get chamber for histograms SC granularity (sectors 1 to 12)
0598       indexCh = dtChIdSC.rawId();
0599       map<string, MonitorElement*>& innerME = digiHistos[indexCh];
0600 
0601       if (useTM && phcode_best[wheel + 3][station][scsector] > -1 && phcode_best[wheel + 3][station][scsector] < 7) {
0602         innerME.find("TM_HitstkvsQualtrig" + trigsrc)->second->Fill(phcode_best[wheel + 3][station][scsector], nHitsPhi);
0603 
0604         if (phcode_best[wheel + 3][station][scsector] > 3 && nHitsPhi >= 7) {
0605           float x_trigger = trigGeomUtils->trigPos(iphbest[wheel + 3][station][scsector]);
0606           float angle_trigger = trigGeomUtils->trigDir(iphbest[wheel + 3][station][scsector]);
0607           trigGeomUtils->trigToSeg(station, x_trigger, x_angle);
0608 
0609           innerMECh.find("TM_PhitkvsPhitrig" + trigsrc)->second->Fill(x_trigger, x_track);
0610           innerMECh.find("TM_PhibtkvsPhibtrig" + trigsrc)->second->Fill(angle_trigger, x_angle);
0611           innerMECh.find("TM_PhiResidual" + trigsrc)->second->Fill(x_trigger - x_track);
0612           innerMECh.find("TM_PhibResidual" + trigsrc)->second->Fill(angle_trigger - x_angle);
0613         }
0614       }
0615 
0616       if (useTM) {
0617         // check for triggers elsewhere in the sector
0618         bool trigFlagTM = false;
0619         for (int ist = 1; ist < 5; ist++) {
0620           if (ist != station && phcode_best[wheel + 3][ist][scsector] >= 2 &&
0621               phcode_best[wheel + 3][ist][scsector] < 7 && track_ok[wheel + 3][ist][scsector] == true) {
0622             trigFlagTM = true;
0623             break;
0624           }
0625         }
0626 
0627         if (trigFlagTM && fabs(x_angle) < 40. && nHitsPhi >= 7) {
0628           // position vs angle of track for reconstruced tracks (denom. for trigger efficiency)
0629           innerME.find("TM_TrackPosvsAngle" + trigsrc)->second->Fill(x_angle, x_track);
0630           if (phcode_best[wheel + 3][station][scsector] >= 2 && phcode_best[wheel + 3][station][scsector] < 7) {
0631             innerME.find("TM_TrackPosvsAngleandTrig" + trigsrc)->second->Fill(x_angle, x_track);
0632             if (phcode_best[wheel + 3][station][scsector] > 4) {  //HH & HL Triggers
0633               innerME.find("TM_TrackPosvsAngleandTrigHHHL" + trigsrc)->second->Fill(x_angle, x_track);
0634             }
0635           }
0636         }
0637 
0638         if ((*btrack)->hasZed() && trigFlagTM && fabs(y_angle) < 40. &&
0639             (*btrack)->zSegment()->degreesOfFreedom() >= 1) {
0640           // position va angle of track for reconstruced tracks (denom. for trigger efficiency) along theta direction
0641           innerME.find("TM_TrackThetaPosvsAngle" + trigsrc)->second->Fill(y_angle, y_track);
0642           if (thcode_best[wheel + 3][station][scsector] > 0) {
0643             innerME.find("TM_TrackThetaPosvsAngleandTrig" + trigsrc)->second->Fill(y_angle, y_track);
0644             if (thcode_best[wheel + 3][station][scsector] == 2) {
0645               innerME.find("TM_TrackThetaPosvsAngleandTrigH" + trigsrc)->second->Fill(y_angle, y_track);
0646             }
0647           }
0648         }
0649       }
0650     }
0651   }
0652 }
0653 
0654 void DTLocalTriggerTask::setQLabels(MonitorElement* me, short int iaxis) {
0655   TH1* histo = me->getTH1();
0656   if (!histo)
0657     return;
0658 
0659   TAxis* axis = nullptr;
0660   if (iaxis == 1) {
0661     axis = histo->GetXaxis();
0662   } else if (iaxis == 2) {
0663     axis = histo->GetYaxis();
0664   }
0665   if (!axis)
0666     return;
0667 
0668   string labels[7] = {"LI", "LO", "HI", "HO", "LL", "HL", "HH"};
0669   int istart = axis->GetXmin() < -1 ? 2 : 1;
0670   for (int i = 0; i < 7; i++) {
0671     axis->SetBinLabel(i + istart, labels[i].c_str());
0672   }
0673 }
0674 
0675 void DTLocalTriggerTask::setQLabelsTheta(MonitorElement* me, short int iaxis) {
0676   TH1* histo = me->getTH1();
0677   if (!histo)
0678     return;
0679 
0680   TAxis* axis = nullptr;
0681   if (iaxis == 1) {
0682     axis = histo->GetXaxis();
0683   } else if (iaxis == 2) {
0684     axis = histo->GetYaxis();
0685   }
0686   if (!axis)
0687     return;
0688 
0689   string labels[2] = {"L", "H"};
0690   int istart = axis->GetXmin() < -1 ? 2 : 1;
0691   for (int i = 0; i < 2; i++) {
0692     axis->SetBinLabel(i + istart, labels[i].c_str());
0693   }
0694 }
0695 
0696 void DTLocalTriggerTask::triggerSource(const edm::Event& e) {
0697   if (!isLocalRun) {
0698     Handle<LTCDigiCollection> ltcdigis;
0699     e.getByToken(ltcDigiCollectionToken_, ltcdigis);
0700 
0701     for (std::vector<LTCDigi>::const_iterator ltc_it = ltcdigis->begin(); ltc_it != ltcdigis->end(); ltc_it++) {
0702       size_t otherTriggerSum = 0;
0703       for (size_t i = 1; i < 6; i++) {
0704         otherTriggerSum += size_t((*ltc_it).HasTriggered(i));
0705       }
0706       if ((*ltc_it).HasTriggered(0) && otherTriggerSum == 0)
0707         trigsrc = "_DTonly";
0708       else if (!(*ltc_it).HasTriggered(0))
0709         trigsrc = "_NoDT";
0710       else if ((*ltc_it).HasTriggered(0) && otherTriggerSum > 0)
0711         trigsrc = "_DTalso";
0712     }
0713     return;
0714   }
0715 
0716   trigsrc = "";
0717   return;
0718 }
0719 
0720 // Local Variables:
0721 // show-trailing-whitespace: t
0722 // truncate-lines: t
0723 // End: