Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
/*
 *  See header file for a description of this class.
 *
 *  \author Jeremy Andrea
 */

#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackFwd.h"
#include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
#include "TrackingTools/Records/interface/TransientTrackRecord.h"
#include "TrackingTools/TransientTrack/interface/TransientTrack.h"
#include "MagneticField/Engine/interface/MagneticField.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DQMServices/Core/interface/DQMStore.h"
#include "DQM/TrackingMonitor/interface/TrackEfficiencyMonitor.h"
#include <string>

// needed to compute the efficiency
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include "DataFormats/GeometrySurface/interface/BoundPlane.h"
#include "DataFormats/GeometrySurface/interface/Cylinder.h"
#include "DataFormats/GeometrySurface/interface/Plane.h"
#include "DataFormats/GeometrySurface/interface/RectangularPlaneBounds.h"
#include "DataFormats/Math/interface/Vector3D.h"
#include "DataFormats/MuonReco/interface/Muon.h"
#include "DataFormats/TrackCandidate/interface/TrackCandidate.h"
#include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
#include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
#include "TrackPropagation/SteppingHelixPropagator/interface/SteppingHelixPropagator.h"
#include "TrackingTools/GeomPropagators/interface/Propagator.h"
#include "TrackingTools/GeomPropagators/interface/SmartPropagator.h"
#include "TrackingTools/PatternTools/interface/TwoTrackMinimumDistance.h"
#include "TrackingTools/Records/interface/TrackingComponentsRecord.h"

//-----------------------------------------------------------------------------------
TrackEfficiencyMonitor::TrackEfficiencyMonitor(const edm::ParameterSet& iConfig)
    : ttbToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))),
      propToken_(esConsumes(edm::ESInputTag("", "SteppingHelixPropagatorAny"))),
      navToken_(esConsumes(edm::ESInputTag("", "CosmicNavigationSchool"))),
      gstToken_(esConsumes()),
      mfToken_(esConsumes()),
      mtToken_(esConsumes())
//-----------------------------------------------------------------------------------
{
  dqmStore_ = edm::Service<DQMStore>().operator->();

  theRadius_ = iConfig.getParameter<double>("theRadius");
  theMaxZ_ = iConfig.getParameter<double>("theMaxZ");
  isBFieldOff_ = iConfig.getParameter<bool>("isBFieldOff");
  trackEfficiency_ = iConfig.getParameter<bool>("trackEfficiency");
  theTKTracksLabel_ = iConfig.getParameter<edm::InputTag>("TKTrackCollection");
  theSTATracksLabel_ = iConfig.getParameter<edm::InputTag>("STATrackCollection");
  muonToken_ = consumes<edm::View<reco::Muon> >(iConfig.getParameter<edm::InputTag>("muoncoll"));

  theTKTracksToken_ = consumes<reco::TrackCollection>(theTKTracksLabel_);
  theSTATracksToken_ = consumes<reco::TrackCollection>(theSTATracksLabel_);

  conf_ = iConfig;
}

//-----------------------------------------------------------------------------------
TrackEfficiencyMonitor::~TrackEfficiencyMonitor()
//-----------------------------------------------------------------------------------
{}

//-----------------------------------------------------------------------------------
void TrackEfficiencyMonitor::bookHistograms(DQMStore::IBooker& ibooker,
                                            edm::Run const& /* iRun */,
                                            edm::EventSetup const& /* iSetup */)
//-----------------------------------------------------------------------------------
{
  std::string MEFolderName = conf_.getParameter<std::string>("FolderName");
  std::string AlgoName = conf_.getParameter<std::string>("AlgoName");

  ibooker.setCurrentFolder(MEFolderName);

  //
  int muonXBin = conf_.getParameter<int>("muonXBin");
  double muonXMin = conf_.getParameter<double>("muonXMin");
  double muonXMax = conf_.getParameter<double>("muonXMax");

  histname = "muonX_";
  muonX = ibooker.book1D(histname + AlgoName, histname + AlgoName, muonXBin, muonXMin, muonXMax);
  muonX->setAxisTitle("");

  //
  int muonYBin = conf_.getParameter<int>("muonYBin");
  double muonYMin = conf_.getParameter<double>("muonYMin");
  double muonYMax = conf_.getParameter<double>("muonYMax");

  histname = "muonY_";
  muonY = ibooker.book1D(histname + AlgoName, histname + AlgoName, muonYBin, muonYMin, muonYMax);
  muonY->setAxisTitle("");

  //
  int muonZBin = conf_.getParameter<int>("muonZBin");
  double muonZMin = conf_.getParameter<double>("muonZMin");
  double muonZMax = conf_.getParameter<double>("muonZMax");

  histname = "muonZ_";
  muonZ = ibooker.book1D(histname + AlgoName, histname + AlgoName, muonZBin, muonZMin, muonZMax);
  muonZ->setAxisTitle("");

  //
  int muonEtaBin = conf_.getParameter<int>("muonEtaBin");
  double muonEtaMin = conf_.getParameter<double>("muonEtaMin");
  double muonEtaMax = conf_.getParameter<double>("muonEtaMax");

  histname = "muonEta_";
  muonEta = ibooker.book1D(histname + AlgoName, histname + AlgoName, muonEtaBin, muonEtaMin, muonEtaMax);
  muonEta->setAxisTitle("");

  //
  int muonPhiBin = conf_.getParameter<int>("muonPhiBin");
  double muonPhiMin = conf_.getParameter<double>("muonPhiMin");
  double muonPhiMax = conf_.getParameter<double>("muonPhiMax");

  histname = "muonPhi_";
  muonPhi = ibooker.book1D(histname + AlgoName, histname + AlgoName, muonPhiBin, muonPhiMin, muonPhiMax);
  muonPhi->setAxisTitle("");

  //
  int muonD0Bin = conf_.getParameter<int>("muonD0Bin");
  double muonD0Min = conf_.getParameter<double>("muonD0Min");
  double muonD0Max = conf_.getParameter<double>("muonD0Max");

  histname = "muonD0_";
  muonD0 = ibooker.book1D(histname + AlgoName, histname + AlgoName, muonD0Bin, muonD0Min, muonD0Max);
  muonD0->setAxisTitle("");

  //
  int muonCompatibleLayersBin = conf_.getParameter<int>("muonCompatibleLayersBin");
  double muonCompatibleLayersMin = conf_.getParameter<double>("muonCompatibleLayersMin");
  double muonCompatibleLayersMax = conf_.getParameter<double>("muonCompatibleLayersMax");

  histname = "muonCompatibleLayers_";
  muonCompatibleLayers = ibooker.book1D(histname + AlgoName,
                                        histname + AlgoName,
                                        muonCompatibleLayersBin,
                                        muonCompatibleLayersMin,
                                        muonCompatibleLayersMax);
  muonCompatibleLayers->setAxisTitle("");

  //------------------------------------------------------------------------------------

  //
  int trackXBin = conf_.getParameter<int>("trackXBin");
  double trackXMin = conf_.getParameter<double>("trackXMin");
  double trackXMax = conf_.getParameter<double>("trackXMax");

  histname = "trackX_";
  trackX = ibooker.book1D(histname + AlgoName, histname + AlgoName, trackXBin, trackXMin, trackXMax);
  trackX->setAxisTitle("");

  //
  int trackYBin = conf_.getParameter<int>("trackYBin");
  double trackYMin = conf_.getParameter<double>("trackYMin");
  double trackYMax = conf_.getParameter<double>("trackYMax");

  histname = "trackY_";
  trackY = ibooker.book1D(histname + AlgoName, histname + AlgoName, trackYBin, trackYMin, trackYMax);
  trackY->setAxisTitle("");

  //
  int trackZBin = conf_.getParameter<int>("trackZBin");
  double trackZMin = conf_.getParameter<double>("trackZMin");
  double trackZMax = conf_.getParameter<double>("trackZMax");

  histname = "trackZ_";
  trackZ = ibooker.book1D(histname + AlgoName, histname + AlgoName, trackZBin, trackZMin, trackZMax);
  trackZ->setAxisTitle("");

  //
  int trackEtaBin = conf_.getParameter<int>("trackEtaBin");
  double trackEtaMin = conf_.getParameter<double>("trackEtaMin");
  double trackEtaMax = conf_.getParameter<double>("trackEtaMax");

  histname = "trackEta_";
  trackEta = ibooker.book1D(histname + AlgoName, histname + AlgoName, trackEtaBin, trackEtaMin, trackEtaMax);
  trackEta->setAxisTitle("");

  //
  int trackPhiBin = conf_.getParameter<int>("trackPhiBin");
  double trackPhiMin = conf_.getParameter<double>("trackPhiMin");
  double trackPhiMax = conf_.getParameter<double>("trackPhiMax");

  histname = "trackPhi_";
  trackPhi = ibooker.book1D(histname + AlgoName, histname + AlgoName, trackPhiBin, trackPhiMin, trackPhiMax);
  trackPhi->setAxisTitle("");

  //
  int trackD0Bin = conf_.getParameter<int>("trackD0Bin");
  double trackD0Min = conf_.getParameter<double>("trackD0Min");
  double trackD0Max = conf_.getParameter<double>("trackD0Max");

  histname = "trackD0_";
  trackD0 = ibooker.book1D(histname + AlgoName, histname + AlgoName, trackD0Bin, trackD0Min, trackD0Max);
  trackD0->setAxisTitle("");

  //
  int trackCompatibleLayersBin = conf_.getParameter<int>("trackCompatibleLayersBin");
  double trackCompatibleLayersMin = conf_.getParameter<double>("trackCompatibleLayersMin");
  double trackCompatibleLayersMax = conf_.getParameter<double>("trackCompatibleLayersMax");

  histname = "trackCompatibleLayers_";
  trackCompatibleLayers = ibooker.book1D(histname + AlgoName,
                                         histname + AlgoName,
                                         trackCompatibleLayersBin,
                                         trackCompatibleLayersMin,
                                         trackCompatibleLayersMax);
  trackCompatibleLayers->setAxisTitle("");

  //------------------------------------------------------------------------------------

  //
  int deltaXBin = conf_.getParameter<int>("deltaXBin");
  double deltaXMin = conf_.getParameter<double>("deltaXMin");
  double deltaXMax = conf_.getParameter<double>("deltaXMax");

  histname = "deltaX_";
  deltaX = ibooker.book1D(histname + AlgoName, histname + AlgoName, deltaXBin, deltaXMin, deltaXMax);
  deltaX->setAxisTitle("");

  //
  int deltaYBin = conf_.getParameter<int>("deltaYBin");
  double deltaYMin = conf_.getParameter<double>("deltaYMin");
  double deltaYMax = conf_.getParameter<double>("deltaYMax");

  histname = "deltaY_";
  deltaY = ibooker.book1D(histname + AlgoName, histname + AlgoName, deltaYBin, deltaYMin, deltaYMax);
  deltaY->setAxisTitle("");

  //
  int signDeltaXBin = conf_.getParameter<int>("signDeltaXBin");
  double signDeltaXMin = conf_.getParameter<double>("signDeltaXMin");
  double signDeltaXMax = conf_.getParameter<double>("signDeltaXMax");

  histname = "signDeltaX_";
  signDeltaX = ibooker.book1D(histname + AlgoName, histname + AlgoName, signDeltaXBin, signDeltaXMin, signDeltaXMax);
  signDeltaX->setAxisTitle("");

  //
  int signDeltaYBin = conf_.getParameter<int>("signDeltaYBin");
  double signDeltaYMin = conf_.getParameter<double>("signDeltaYMin");
  double signDeltaYMax = conf_.getParameter<double>("signDeltaYMax");

  histname = "signDeltaY_";
  signDeltaY = ibooker.book1D(histname + AlgoName, histname + AlgoName, signDeltaYBin, signDeltaYMin, signDeltaYMax);
  signDeltaY->setAxisTitle("");

  histname = "GlobalMuonPtEtaPhi_LowPt_";
  GlobalMuonPtEtaPhiLowPt = ibooker.book2D(histname + AlgoName, histname + AlgoName, 20, -2.4, 2.4, 20, -3.25, 3.25);
  GlobalMuonPtEtaPhiLowPt->setAxisTitle("");

  histname = "StandaloneMuonPtEtaPhi_LowPt_";
  StandaloneMuonPtEtaPhiLowPt =
      ibooker.book2D(histname + AlgoName, histname + AlgoName, 20, -2.4, 2.4, 20, -3.25, 3.25);
  StandaloneMuonPtEtaPhiLowPt->setAxisTitle("");

  histname = "GlobalMuonPtEtaPhi_HighPt_";
  GlobalMuonPtEtaPhiHighPt = ibooker.book2D(histname + AlgoName, histname + AlgoName, 20, -2.4, 2.4, 20, -3.25, 3.25);
  GlobalMuonPtEtaPhiHighPt->setAxisTitle("");

  histname = "StandaloneMuonPtEtaPhi_HighPt_";
  StandaloneMuonPtEtaPhiHighPt =
      ibooker.book2D(histname + AlgoName, histname + AlgoName, 20, -2.4, 2.4, 20, -3.25, 3.25);
  StandaloneMuonPtEtaPhiHighPt->setAxisTitle("");
}

//-----------------------------------------------------------------------------------
void TrackEfficiencyMonitor::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
//-----------------------------------------------------------------------------------
{
  edm::Handle<reco::TrackCollection> tkTracks = iEvent.getHandle(theTKTracksToken_);
  edm::Handle<reco::TrackCollection> staTracks = iEvent.getHandle(theSTATracksToken_);

  //initialize values
  failedToPropagate = 0;
  nCompatibleLayers = 0;
  findDetLayer = false;

  const NavigationSchool& nav = iSetup.getData(navToken_);
  measurementTracker = &iSetup.getData(mtToken_);
  theTTrackBuilder = &iSetup.getData(ttbToken_);
  thePropagator = &iSetup.getData(propToken_);
  bField = &iSetup.getData(mfToken_);
  theTracker = &iSetup.getData(gstToken_);
  theNavigation = new DirectTrackerNavigation(theTracker);

  edm::Handle<edm::View<reco::Muon> > muons = iEvent.getHandle(muonToken_);
  if (!muons.isValid())
    return;
  for (edm::View<reco::Muon>::const_iterator muon = muons->begin(); muon != muons->end(); ++muon) {
    if ((*muon).pt() < 5)
      continue;
    if (fabs((*muon).eta()) > 2.4)
      continue;
    if ((*muon).vertexNormalizedChi2() > 10)
      continue;
    if ((*muon).isStandAloneMuon() and (*muon).isGlobalMuon()) {
      if ((*muon).pt() < 20)
        GlobalMuonPtEtaPhiLowPt->Fill((*muon).eta(), (*muon).phi());
      else
        GlobalMuonPtEtaPhiHighPt->Fill((*muon).eta(), (*muon).phi());
    }
    if ((*muon).isStandAloneMuon()) {
      if ((*muon).pt() < 20)
        StandaloneMuonPtEtaPhiLowPt->Fill((*muon).eta(), (*muon).phi());
      else
        StandaloneMuonPtEtaPhiHighPt->Fill((*muon).eta(), (*muon).phi());
    }
  }
  if (trackEfficiency_) {
    //---------------------------------------------------
    // Select muons with good quality
    // If B field is on, no up-down matching between the muons
    //---------------------------------------------------
    bool isGoodMuon = false;
    double mudd0 = 0., mudphi = 0., muddsz = 0., mudeta = 0.;
    if (isBFieldOff_) {
      if (staTracks->size() == 2) {
        for (unsigned int bindex = 0; bindex < staTracks->size(); ++bindex) {
          if (0 == bindex) {
            mudd0 += (*staTracks)[bindex].d0();
            mudphi += (*staTracks)[bindex].phi();
            muddsz += (*staTracks)[bindex].dsz();
            mudeta += (*staTracks)[bindex].eta();
          }
          if (1 == bindex) {
            mudd0 -= (*staTracks)[bindex].d0();
            mudphi -= (*staTracks)[bindex].phi();
            muddsz -= (*staTracks)[bindex].dsz();
            mudeta -= (*staTracks)[bindex].eta();
          }
        }
        if ((fabs(mudd0) < 15.0) && (fabs(mudphi) < 0.045) && (fabs(muddsz) < 20.0) && (fabs(mudeta) < 0.060))
          isGoodMuon = true;
      }

      if (isGoodMuon)
        testTrackerTracks(tkTracks, staTracks, nav);

    } else if (staTracks->size() == 1 || staTracks->size() == 2)
      testTrackerTracks(tkTracks, staTracks, nav);
  }

  if (!trackEfficiency_ && tkTracks->size() == 1) {
    if ((tkTracks->front()).normalizedChi2() < 5 && (tkTracks->front()).hitPattern().numberOfValidHits() > 8)
      testSTATracks(tkTracks, staTracks);
  }

  delete theNavigation;
}

//-----------------------------------------------------------------------------------
void TrackEfficiencyMonitor::testTrackerTracks(edm::Handle<reco::TrackCollection> tkTracks,
                                               edm::Handle<reco::TrackCollection> staTracks,
                                               const NavigationSchool& navigationSchool)
//-----------------------------------------------------------------------------------
{
  const std::string metname = "testStandAloneMuonTracks";

  //---------------------------------------------------
  // get the index of the "up" muon
  // histograms will only be computed for the "up" muon
  //---------------------------------------------------

  int nUpMuon = 0;
  int idxUpMuon = -1;
  for (unsigned int i = 0; i < staTracks->size(); i++) {
    if (checkSemiCylinder((*staTracks)[i]) == TrackEfficiencyMonitor::Up) {
      nUpMuon++;
      idxUpMuon = i;
    }
  }

  if (nUpMuon == 1) {
    //---------------------------------------------------
    //get the muon informations
    //---------------------------------------------------

    reco::TransientTrack staTT = theTTrackBuilder->build((*staTracks)[idxUpMuon]);
    double ipX = staTT.impactPointState().globalPosition().x();
    double ipY = staTT.impactPointState().globalPosition().y();
    double ipZ = staTT.impactPointState().globalPosition().z();
    double eta = staTT.impactPointState().globalDirection().eta();
    double phi = staTT.impactPointState().globalDirection().phi();
    double d0 = (*staTracks)[idxUpMuon].d0();

    TrajectoryStateOnSurface theTSOS = staTT.outermostMeasurementState();
    TrajectoryStateOnSurface theTSOSCompLayers = staTT.outermostMeasurementState();

    //---------------------------------------------------
    //check if the muon is in the tracker acceptance
    //---------------------------------------------------
    bool isInTrackerAcceptance = false;
    isInTrackerAcceptance = trackerAcceptance(theTSOS, theRadius_, theMaxZ_);

    //---------------------------------------------------st
    //count the number of compatible layers
    //---------------------------------------------------
    nCompatibleLayers = compatibleLayers(navigationSchool, theTSOSCompLayers);

    if (isInTrackerAcceptance && (*staTracks)[idxUpMuon].hitPattern().numberOfValidHits() > 28) {
      //---------------------------------------------------
      //count the number of good muon candidates
      //---------------------------------------------------

      TrajectoryStateOnSurface staState;
      LocalVector diffLocal;

      bool isTrack = false;
      if (!tkTracks->empty()) {
        //---------------------------------------------------
        //look for associated tracks
        //---------------------------------------------------
        float DR2min = 1000;
        reco::TrackCollection::const_iterator closestTrk = tkTracks->end();

        for (reco::TrackCollection::const_iterator tkTrack = tkTracks->begin(); tkTrack != tkTracks->end(); ++tkTrack) {
          reco::TransientTrack tkTT = theTTrackBuilder->build(*tkTrack);
          TrajectoryStateOnSurface tkInner = tkTT.innermostMeasurementState();
          staState = thePropagator->propagate(staTT.outermostMeasurementState(), tkInner.surface());
          failedToPropagate = 1;

          if (staState.isValid()) {
            failedToPropagate = 0;
            diffLocal = tkInner.localPosition() - staState.localPosition();
            double DR2 = diffLocal.x() * diffLocal.x() + diffLocal.y() * diffLocal.y();
            if (DR2 < DR2min) {
              DR2min = DR2;
              closestTrk = tkTrack;
            }
            if (pow(DR2, 0.5) < 100.)
              isTrack = true;
          }
        }

        if (DR2min != 1000) {
          reco::TransientTrack tkTT = theTTrackBuilder->build(*closestTrk);
          TrajectoryStateOnSurface tkInner = tkTT.innermostMeasurementState();
          staState = thePropagator->propagate(staTT.outermostMeasurementState(), tkInner.surface());
          deltaX->Fill(diffLocal.x());
          deltaY->Fill(diffLocal.y());
          signDeltaX->Fill(diffLocal.x() /
                           (tkInner.localError().positionError().xx() + staState.localError().positionError().xx()));
          signDeltaY->Fill(diffLocal.y() /
                           (tkInner.localError().positionError().yy() + staState.localError().positionError().yy()));
        }
      }

      if (failedToPropagate == 0) {
        muonX->Fill(ipX);
        muonY->Fill(ipY);
        muonZ->Fill(ipZ);
        muonEta->Fill(eta);
        muonPhi->Fill(phi);
        muonD0->Fill(d0);
        muonCompatibleLayers->Fill(nCompatibleLayers);

        if (isTrack) {
          trackX->Fill(ipX);
          trackY->Fill(ipY);
          trackZ->Fill(ipZ);
          trackEta->Fill(eta);
          trackPhi->Fill(phi);
          trackD0->Fill(d0);
          trackCompatibleLayers->Fill(nCompatibleLayers);
        }
      }
    }
  }
}

//-----------------------------------------------------------------------------------
void TrackEfficiencyMonitor::testSTATracks(edm::Handle<reco::TrackCollection> tkTracks,
                                           edm::Handle<reco::TrackCollection> staTracks)
//-----------------------------------------------------------------------------------
{
  reco::TransientTrack tkTT = theTTrackBuilder->build(tkTracks->front());
  double ipX = tkTT.impactPointState().globalPosition().x();
  double ipY = tkTT.impactPointState().globalPosition().y();
  double ipZ = tkTT.impactPointState().globalPosition().z();
  double eta = tkTT.impactPointState().globalDirection().eta();
  double phi = tkTT.impactPointState().globalDirection().phi();
  double d0 = (*tkTracks)[0].d0();

  TrajectoryStateOnSurface tkInner = tkTT.innermostMeasurementState();
  LocalVector diffLocal;
  TrajectoryStateOnSurface staState;
  bool isTrack = false;

  if (!staTracks->empty()) {
    //---------------------------------------------------
    //look for associated muons
    //---------------------------------------------------

    float DR2min = 1000;
    reco::TrackCollection::const_iterator closestTrk = staTracks->end();
    //----------------------loop on tracker tracks:
    for (reco::TrackCollection::const_iterator staTrack = staTracks->begin(); staTrack != staTracks->end();
         ++staTrack) {
      if (checkSemiCylinder(*staTrack) == TrackEfficiencyMonitor::Up) {
        reco::TransientTrack staTT = theTTrackBuilder->build(*staTrack);
        failedToPropagate = 1;
        staState = thePropagator->propagate(staTT.outermostMeasurementState(), tkInner.surface());

        if (staState.isValid()) {
          failedToPropagate = 0;
          diffLocal = tkInner.localPosition() - staState.localPosition();

          double DR2 = diffLocal.x() * diffLocal.x() + diffLocal.y() * diffLocal.y();
          if (DR2 < DR2min) {
            DR2min = DR2;
            closestTrk = staTrack;
          }
          if (pow(DR2, 0.5) < 100.)
            isTrack = true;
        }
      }
    }
  }

  if (failedToPropagate == 0) {
    trackX->Fill(ipX);
    trackY->Fill(ipY);
    trackZ->Fill(ipZ);
    trackEta->Fill(eta);
    trackPhi->Fill(phi);
    trackD0->Fill(d0);

    if (isTrack) {
      muonX->Fill(ipX);
      muonY->Fill(ipY);
      muonZ->Fill(ipZ);
      muonEta->Fill(eta);
      muonPhi->Fill(phi);
      muonD0->Fill(d0);
    }
  }
}

//-----------------------------------------------------------------------------------
TrackEfficiencyMonitor::SemiCylinder TrackEfficiencyMonitor::checkSemiCylinder(const reco::Track& tk)
//-----------------------------------------------------------------------------------
{
  return tk.innerPosition().phi() > 0 ? TrackEfficiencyMonitor::Up : TrackEfficiencyMonitor::Down;
}

//-----------------------------------------------------------------------------------
bool TrackEfficiencyMonitor::trackerAcceptance(TrajectoryStateOnSurface theTSOS, double theRadius, double theMaxZ)
//-----------------------------------------------------------------------------------
{
  //---------------------------------------------------
  // check if the muon is in the tracker acceptance
  // check the compatibility with a cylinder of radius "theRadius"
  //---------------------------------------------------

  //Propagator*  theTmpPropagator = new SteppingHelixPropagator(&*bField,anyDirection);

  //Propagator*  theTmpPropagator = &*thePropagator;
  Propagator* theTmpPropagator = thePropagator->clone();

  if (theTSOS.globalPosition().y() < 0)
    theTmpPropagator->setPropagationDirection(oppositeToMomentum);
  else
    theTmpPropagator->setPropagationDirection(alongMomentum);

  Cylinder::PositionType pos0;
  Cylinder::RotationType rot0;
  const Cylinder::CylinderPointer cyl = Cylinder::build(pos0, rot0, theRadius);
  TrajectoryStateOnSurface tsosAtCyl = theTmpPropagator->propagate(*theTSOS.freeState(), *cyl);
  double accept = false;
  if (tsosAtCyl.isValid()) {
    if (fabs(tsosAtCyl.globalPosition().z()) < theMaxZ) {
      Cylinder::PositionType pos02;
      Cylinder::RotationType rot02;
      const Cylinder::CylinderPointer cyl2 = Cylinder::build(pos02, rot02, theRadius - 10);
      TrajectoryStateOnSurface tsosAtCyl2 = theTmpPropagator->propagate(*tsosAtCyl.freeState(), *cyl2);
      if (tsosAtCyl2.isValid()) {
        Cylinder::PositionType pos03;
        Cylinder::RotationType rot03;
        const Cylinder::CylinderPointer cyl3 = Cylinder::build(pos03, rot03, theRadius);
        TrajectoryStateOnSurface tsosAtCyl3 = theTmpPropagator->propagate(*tsosAtCyl2.freeState(), *cyl3);
        if (tsosAtCyl3.isValid()) {
          accept = true;
        }
      }
    }
  }
  delete theTmpPropagator;
  //muon propagated to the barrel cylinder
  return accept;
}

//-----------------------------------------------------------------------------------
int TrackEfficiencyMonitor::compatibleLayers(const NavigationSchool& navigationSchool, TrajectoryStateOnSurface theTSOS)
//-----------------------------------------------------------------------------------
{
  //---------------------------------------------------
  // check the number of compatible layers
  //---------------------------------------------------

  std::vector<const BarrelDetLayer*> barrelTOBLayers = measurementTracker->geometricSearchTracker()->tobLayers();

  unsigned int layers = 0;
  for (unsigned int k = 0; k < barrelTOBLayers.size(); k++) {
    const DetLayer* firstLay = barrelTOBLayers[barrelTOBLayers.size() - 1 - k];

    //Propagator*  theTmpPropagator = new SteppingHelixPropagator(&*bField,anyDirection);

    Propagator* theTmpPropagator = thePropagator->clone();
    theTmpPropagator->setPropagationDirection(alongMomentum);

    TrajectoryStateOnSurface startTSOS = theTmpPropagator->propagate(*theTSOS.freeState(), firstLay->surface());

    std::vector<const DetLayer*> trackCompatibleLayers;

    findDetLayer = true;
    bool isUpMuon = false;
    bool firstdtep = true;

    if (startTSOS.isValid()) {
      if (firstdtep)
        layers++;

      int nwhile = 0;

      //for other compatible layers
      while (startTSOS.isValid() && firstLay && findDetLayer) {
        if (firstdtep && startTSOS.globalPosition().y() > 0)
          isUpMuon = true;

        if (firstdtep) {
          std::vector<const DetLayer*> firstCompatibleLayers;
          firstCompatibleLayers.push_back(firstLay);
          std::pair<TrajectoryStateOnSurface, const DetLayer*> nextLayer =
              findNextLayer(theTSOS, firstCompatibleLayers, isUpMuon);
          firstdtep = false;
        } else {
          trackCompatibleLayers = navigationSchool.nextLayers(*firstLay, *(startTSOS.freeState()), alongMomentum);
          if (!trackCompatibleLayers.empty()) {
            std::pair<TrajectoryStateOnSurface, const DetLayer*> nextLayer =
                findNextLayer(startTSOS, trackCompatibleLayers, isUpMuon);
            if (firstLay != nextLayer.second) {
              firstLay = nextLayer.second;
              startTSOS = nextLayer.first;
              layers++;
            } else
              firstLay = nullptr;
          }
        }
        nwhile++;
        if (nwhile > 100)
          break;
      }
      delete theTmpPropagator;
      break;
    }
    delete theTmpPropagator;
  }
  return layers;
}

//-----------------------------------------------------------------------------------
std::pair<TrajectoryStateOnSurface, const DetLayer*> TrackEfficiencyMonitor::findNextLayer(
    TrajectoryStateOnSurface sTSOS, const std::vector<const DetLayer*>& trackCompatibleLayers, bool isUpMuon)
//-----------------------------------------------------------------------------------
{
  //Propagator*  theTmpPropagator = new SteppingHelixPropagator(&*bField,anyDirection);

  Propagator* theTmpPropagator = thePropagator->clone();
  theTmpPropagator->setPropagationDirection(alongMomentum);

  std::vector<const DetLayer*>::const_iterator itl;
  findDetLayer = false;
  for (itl = trackCompatibleLayers.begin(); itl != trackCompatibleLayers.end(); ++itl) {
    TrajectoryStateOnSurface tsos = theTmpPropagator->propagate(*(sTSOS.freeState()), (**itl).surface());
    if (tsos.isValid()) {
      sTSOS = tsos;
      findDetLayer = true;

      break;
    }
  }
  std::pair<TrajectoryStateOnSurface, const DetLayer*> blabla;
  blabla.first = sTSOS;
  blabla.second = &**itl;
  delete theTmpPropagator;
  return blabla;
}

DEFINE_FWK_MODULE(TrackEfficiencyMonitor);