Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-18 00:48:14

0001 #include <string>
0002 
0003 #include "FWCore/Framework/interface/Frameworkfwd.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "DataFormats/Common/interface/ValueMap.h"
0008 
0009 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0010 #include "DQMServices/Core/interface/DQMStore.h"
0011 
0012 #include "DataFormats/Common/interface/ValidHandle.h"
0013 #include "DataFormats/Math/interface/deltaR.h"
0014 #include "DataFormats/Math/interface/GeantUnits.h"
0015 #include "DataFormats/Math/interface/angle_units.h"
0016 #include "DataFormats/ForwardDetId/interface/ETLDetId.h"
0017 #include "DataFormats/ForwardDetId/interface/BTLDetId.h"
0018 
0019 #include "DataFormats/Common/interface/Ptr.h"
0020 #include "DataFormats/Common/interface/PtrVector.h"
0021 #include "DataFormats/Common/interface/RefProd.h"
0022 #include "DataFormats/Common/interface/Ref.h"
0023 #include "DataFormats/Common/interface/RefVector.h"
0024 
0025 #include "DataFormats/TrackReco/interface/Track.h"
0026 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0027 
0028 #include "Geometry/Records/interface/MTDDigiGeometryRecord.h"
0029 #include "Geometry/Records/interface/MTDTopologyRcd.h"
0030 #include "Geometry/MTDNumberingBuilder/interface/MTDTopology.h"
0031 #include "Geometry/MTDCommonData/interface/MTDTopologyMode.h"
0032 #include "Geometry/MTDGeometryBuilder/interface/MTDGeometry.h"
0033 #include "Geometry/MTDGeometryBuilder/interface/ProxyMTDTopology.h"
0034 #include "Geometry/MTDGeometryBuilder/interface/RectangularMTDTopology.h"
0035 
0036 #include "RecoMTD/DetLayers/interface/MTDDetLayerGeometry.h"
0037 #include "RecoMTD/Records/interface/MTDRecoGeometryRecord.h"
0038 #include "MagneticField/Engine/interface/MagneticField.h"
0039 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0040 #include "RecoMTD/DetLayers/interface/MTDDetLayerGeometry.h"
0041 #include "RecoMTD/DetLayers/interface/MTDTrayBarrelLayer.h"
0042 #include "RecoMTD/DetLayers/interface/MTDDetTray.h"
0043 #include "RecoMTD/DetLayers/interface/MTDSectorForwardDoubleLayer.h"
0044 #include "RecoMTD/DetLayers/interface/MTDDetSector.h"
0045 #include "RecoMTD/Records/interface/MTDRecoGeometryRecord.h"
0046 
0047 #include "TrackPropagation/SteppingHelixPropagator/interface/SteppingHelixPropagator.h"
0048 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
0049 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0050 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
0051 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
0052 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
0053 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
0054 #include "DataFormats/GeometryCommonDetAlgo/interface/MeasurementError.h"
0055 #include "DataFormats/GeometryCommonDetAlgo/interface/MeasurementPoint.h"
0056 #include "DataFormats/FTLRecHit/interface/FTLRecHitCollections.h"
0057 
0058 #include "DataFormats/Common/interface/OneToMany.h"
0059 #include "DataFormats/Common/interface/AssociationMap.h"
0060 
0061 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0062 #include "SimDataFormats/Associations/interface/TrackToTrackingParticleAssociator.h"
0063 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
0064 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
0065 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0066 #include "SimDataFormats/Associations/interface/MtdSimLayerClusterToTPAssociatorBaseImpl.h"
0067 
0068 #include "CLHEP/Units/PhysicalConstants.h"
0069 #include "MTDHit.h"
0070 
0071 class MtdTracksValidation : public DQMEDAnalyzer {
0072 public:
0073   explicit MtdTracksValidation(const edm::ParameterSet&);
0074   ~MtdTracksValidation() override;
0075 
0076   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0077 
0078 private:
0079   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0080 
0081   void analyze(const edm::Event&, const edm::EventSetup&) override;
0082 
0083   const std::pair<bool, bool> checkAcceptance(
0084       const reco::Track&, const edm::Event&, const edm::EventSetup&, size_t&, float&, float&, float&, float&);
0085 
0086   const bool trkTPSelLV(const TrackingParticle&);
0087   const bool trkTPSelAll(const TrackingParticle&);
0088   const bool trkRecSel(const reco::TrackBase&);
0089   const bool trkRecSelLowPt(const reco::TrackBase&);
0090   const edm::Ref<std::vector<TrackingParticle>>* getMatchedTP(const reco::TrackBaseRef&);
0091 
0092   const unsigned long int uniqueId(const uint32_t x, const EncodedEventId& y) {
0093     const uint64_t a = static_cast<uint64_t>(x);
0094     const uint64_t b = static_cast<uint64_t>(y.rawId());
0095 
0096     if (x < y.rawId())
0097       return (b << 32) | a;
0098     else
0099       return (a << 32) | b;
0100   }
0101 
0102   bool isETL(const double eta) const { return (std::abs(eta) > trackMinEtlEta_) && (std::abs(eta) < trackMaxEtlEta_); }
0103 
0104   // ------------ member data ------------
0105 
0106   const std::string folder_;
0107   const float trackMaxPt_;
0108   const float trackMaxBtlEta_;
0109   const float trackMinEtlEta_;
0110   const float trackMaxEtlEta_;
0111 
0112   static constexpr double simUnit_ = 1e9;                // sim time in s while reco time in ns
0113   static constexpr double etacutGEN_ = 4.;               // |eta| < 4;
0114   static constexpr double etacutREC_ = 3.;               // |eta| < 3;
0115   static constexpr double pTcutBTL_ = 0.7;               // PT > 0.7 GeV
0116   static constexpr double pTcutETL_ = 0.2;               // PT > 0.2 GeV
0117   static constexpr double depositBTLthreshold_ = 1;      // threshold for energy deposit in BTL cell [MeV]
0118   static constexpr double depositETLthreshold_ = 0.001;  // threshold for energy deposit in ETL cell [MeV]
0119   static constexpr double rBTL_ = 110.0;
0120   static constexpr double zETL_ = 290.0;
0121   static constexpr double etaMatchCut_ = 0.05;
0122   static constexpr double cluDRradius_ = 0.05;  // to cluster rechits around extrapolated track
0123 
0124   const reco::RecoToSimCollection* r2s_;
0125   const reco::SimToRecoCollection* s2r_;
0126 
0127   edm::EDGetTokenT<reco::TrackCollection> GenRecTrackToken_;
0128   edm::EDGetTokenT<reco::TrackCollection> RecTrackToken_;
0129 
0130   edm::EDGetTokenT<TrackingParticleCollection> trackingParticleCollectionToken_;
0131   edm::EDGetTokenT<reco::SimToRecoCollection> simToRecoAssociationToken_;
0132   edm::EDGetTokenT<reco::RecoToSimCollection> recoToSimAssociationToken_;
0133   edm::EDGetTokenT<reco::TPToSimCollectionMtd> tp2SimAssociationMapToken_;
0134   edm::EDGetTokenT<FTLRecHitCollection> btlRecHitsToken_;
0135   edm::EDGetTokenT<FTLRecHitCollection> etlRecHitsToken_;
0136 
0137   edm::EDGetTokenT<edm::ValueMap<int>> trackAssocToken_;
0138   edm::EDGetTokenT<edm::ValueMap<float>> pathLengthToken_;
0139 
0140   edm::EDGetTokenT<edm::ValueMap<float>> tmtdToken_;
0141   edm::EDGetTokenT<edm::ValueMap<float>> SigmatmtdToken_;
0142   edm::EDGetTokenT<edm::ValueMap<float>> t0SrcToken_;
0143   edm::EDGetTokenT<edm::ValueMap<float>> Sigmat0SrcToken_;
0144   edm::EDGetTokenT<edm::ValueMap<float>> t0PidToken_;
0145   edm::EDGetTokenT<edm::ValueMap<float>> Sigmat0PidToken_;
0146   edm::EDGetTokenT<edm::ValueMap<float>> t0SafePidToken_;
0147   edm::EDGetTokenT<edm::ValueMap<float>> Sigmat0SafePidToken_;
0148   edm::EDGetTokenT<edm::ValueMap<float>> SigmaTofPiToken_;
0149   edm::EDGetTokenT<edm::ValueMap<float>> SigmaTofKToken_;
0150   edm::EDGetTokenT<edm::ValueMap<float>> SigmaTofPToken_;
0151   edm::EDGetTokenT<edm::ValueMap<float>> trackMVAQualToken_;
0152   edm::EDGetTokenT<edm::ValueMap<float>> outermostHitPositionToken_;
0153 
0154   edm::ESGetToken<MTDGeometry, MTDDigiGeometryRecord> mtdgeoToken_;
0155   edm::ESGetToken<MTDTopology, MTDTopologyRcd> mtdtopoToken_;
0156   edm::ESGetToken<MTDDetLayerGeometry, MTDRecoGeometryRecord> mtdlayerToken_;
0157   edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magfieldToken_;
0158   edm::ESGetToken<TransientTrackBuilder, TransientTrackRecord> builderToken_;
0159 
0160   MonitorElement* meBTLTrackRPTime_;
0161   MonitorElement* meBTLTrackEffEtaTot_;
0162   MonitorElement* meBTLTrackEffPhiTot_;
0163   MonitorElement* meBTLTrackEffPtTot_;
0164   MonitorElement* meBTLTrackEffEtaMtd_;
0165   MonitorElement* meBTLTrackEffPhiMtd_;
0166   MonitorElement* meBTLTrackEffPtMtd_;
0167   MonitorElement* meBTLTrackPtRes_;
0168 
0169   MonitorElement* meETLTrackRPTime_;
0170   MonitorElement* meETLTrackEffEtaTot_[2];
0171   MonitorElement* meETLTrackEffEtaTotLowPt_[2];
0172   MonitorElement* meETLTrackEffPhiTot_[2];
0173   MonitorElement* meETLTrackEffPtTot_[2];
0174   MonitorElement* meETLTrackEffEtaMtd_[2];
0175   MonitorElement* meETLTrackEffEtaMtdLowPt_[2];
0176   MonitorElement* meETLTrackEffEta2MtdLowPt_[2];
0177   MonitorElement* meETLTrackEffPhiMtd_[2];
0178   MonitorElement* meETLTrackEffPtMtd_[2];
0179   MonitorElement* meETLTrackEffEta2Mtd_[2];
0180   MonitorElement* meETLTrackEffPhi2Mtd_[2];
0181   MonitorElement* meETLTrackEffPt2Mtd_[2];
0182   MonitorElement* meETLTrackPtRes_;
0183 
0184   MonitorElement* meTracktmtd_;
0185   MonitorElement* meTrackt0Src_;
0186   MonitorElement* meTrackSigmat0Src_;
0187   MonitorElement* meTrackt0Pid_;
0188   MonitorElement* meTrackSigmat0Pid_;
0189   MonitorElement* meTrackt0SafePid_;
0190   MonitorElement* meTrackSigmat0SafePid_;
0191   MonitorElement* meTrackNumHits_;
0192   MonitorElement* meTrackNumHitsNT_;
0193   MonitorElement* meTrackMVAQual_;
0194   MonitorElement* meTrackPathLenghtvsEta_;
0195   MonitorElement* meTrackOutermostHitR_;
0196   MonitorElement* meTrackOutermostHitZ_;
0197 
0198   MonitorElement* meTrackSigmaTof_[3];
0199   MonitorElement* meTrackSigmaTofvsP_[3];
0200 
0201   MonitorElement* meTrackPtTot_;
0202   MonitorElement* meExtraPtMtd_;
0203   MonitorElement* meExtraPtEtl2Mtd_;
0204 
0205   MonitorElement* meBTLTrackMatchedTPPtResMtd_;
0206   MonitorElement* meETLTrackMatchedTPPtResMtd_;
0207   MonitorElement* meETLTrackMatchedTP2PtResMtd_;
0208   MonitorElement* meBTLTrackMatchedTPPtRatioGen_;
0209   MonitorElement* meETLTrackMatchedTPPtRatioGen_;
0210   MonitorElement* meETLTrackMatchedTP2PtRatioGen_;
0211   MonitorElement* meBTLTrackMatchedTPPtRatioMtd_;
0212   MonitorElement* meETLTrackMatchedTPPtRatioMtd_;
0213   MonitorElement* meETLTrackMatchedTP2PtRatioMtd_;
0214   MonitorElement* meBTLTrackMatchedTPPtResvsPtMtd_;
0215   MonitorElement* meETLTrackMatchedTPPtResvsPtMtd_;
0216   MonitorElement* meETLTrackMatchedTP2PtResvsPtMtd_;
0217   MonitorElement* meBTLTrackMatchedTPDPtvsPtGen_;
0218   MonitorElement* meETLTrackMatchedTPDPtvsPtGen_;
0219   MonitorElement* meETLTrackMatchedTP2DPtvsPtGen_;
0220   MonitorElement* meBTLTrackMatchedTPDPtvsPtMtd_;
0221   MonitorElement* meETLTrackMatchedTPDPtvsPtMtd_;
0222   MonitorElement* meETLTrackMatchedTP2DPtvsPtMtd_;
0223 
0224   MonitorElement* meTrackMatchedTPEffPtTot_;
0225   MonitorElement* meTrackMatchedTPEffPtTotLV_;
0226   MonitorElement* meTrackMatchedTPEffPtMtd_;
0227   MonitorElement* meTrackMatchedTPEffPtEtl2Mtd_;
0228   MonitorElement* meTrackMatchedTPmtdEffPtTot_;
0229   MonitorElement* meTrackMatchedTPmtdEffPtMtd_;
0230   MonitorElement* meTrackEtaTot_;
0231   MonitorElement* meExtraEtaMtd_;
0232   MonitorElement* meExtraEtaEtl2Mtd_;
0233   MonitorElement* meTrackMatchedTPEffEtaTot_;
0234   MonitorElement* meTrackMatchedTPEffEtaTotLV_;
0235   MonitorElement* meTrackMatchedTPEffEtaMtd_;
0236   MonitorElement* meTrackMatchedTPEffEtaEtl2Mtd_;
0237   MonitorElement* meTrackMatchedTPmtdEffEtaTot_;
0238   MonitorElement* meTrackMatchedTPmtdEffEtaMtd_;
0239   MonitorElement* meTrackResTot_;
0240   MonitorElement* meTrackPullTot_;
0241   MonitorElement* meTrackResTotvsMVAQual_;
0242   MonitorElement* meTrackPullTotvsMVAQual_;
0243 
0244   MonitorElement* meExtraPhiAtBTL_;
0245   MonitorElement* meExtraPhiAtBTLmatched_;
0246   MonitorElement* meExtraBTLeneInCone_;
0247   MonitorElement* meExtraMTDfailExtenderEta_;
0248   MonitorElement* meExtraMTDfailExtenderPt_;
0249 };
0250 
0251 // ------------ constructor and destructor --------------
0252 MtdTracksValidation::MtdTracksValidation(const edm::ParameterSet& iConfig)
0253     : folder_(iConfig.getParameter<std::string>("folder")),
0254       trackMaxPt_(iConfig.getParameter<double>("trackMaximumPt")),
0255       trackMaxBtlEta_(iConfig.getParameter<double>("trackMaximumBtlEta")),
0256       trackMinEtlEta_(iConfig.getParameter<double>("trackMinimumEtlEta")),
0257       trackMaxEtlEta_(iConfig.getParameter<double>("trackMaximumEtlEta")) {
0258   GenRecTrackToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("inputTagG"));
0259   RecTrackToken_ = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("inputTagT"));
0260   trackingParticleCollectionToken_ =
0261       consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("SimTag"));
0262   simToRecoAssociationToken_ =
0263       consumes<reco::SimToRecoCollection>(iConfig.getParameter<edm::InputTag>("TPtoRecoTrackAssoc"));
0264   recoToSimAssociationToken_ =
0265       consumes<reco::RecoToSimCollection>(iConfig.getParameter<edm::InputTag>("TPtoRecoTrackAssoc"));
0266   tp2SimAssociationMapToken_ =
0267       consumes<reco::TPToSimCollectionMtd>(iConfig.getParameter<edm::InputTag>("tp2SimAssociationMapTag"));
0268   btlRecHitsToken_ = consumes<FTLRecHitCollection>(iConfig.getParameter<edm::InputTag>("btlRecHits"));
0269   etlRecHitsToken_ = consumes<FTLRecHitCollection>(iConfig.getParameter<edm::InputTag>("etlRecHits"));
0270   trackAssocToken_ = consumes<edm::ValueMap<int>>(iConfig.getParameter<edm::InputTag>("trackAssocSrc"));
0271   pathLengthToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("pathLengthSrc"));
0272   tmtdToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("tmtd"));
0273   SigmatmtdToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("sigmatmtd"));
0274   t0SrcToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("t0Src"));
0275   Sigmat0SrcToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("sigmat0Src"));
0276   t0PidToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("t0PID"));
0277   Sigmat0PidToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("sigmat0PID"));
0278   t0SafePidToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("t0SafePID"));
0279   Sigmat0SafePidToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("sigmat0SafePID"));
0280   SigmaTofPiToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("sigmaTofPi"));
0281   SigmaTofKToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("sigmaTofK"));
0282   SigmaTofPToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("sigmaTofP"));
0283   trackMVAQualToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("trackMVAQual"));
0284   outermostHitPositionToken_ =
0285       consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("outermostHitPositionSrc"));
0286   mtdgeoToken_ = esConsumes<MTDGeometry, MTDDigiGeometryRecord>();
0287   mtdtopoToken_ = esConsumes<MTDTopology, MTDTopologyRcd>();
0288   mtdlayerToken_ = esConsumes<MTDDetLayerGeometry, MTDRecoGeometryRecord>();
0289   magfieldToken_ = esConsumes<MagneticField, IdealMagneticFieldRecord>();
0290   builderToken_ = esConsumes<TransientTrackBuilder, TransientTrackRecord>(edm::ESInputTag("", "TransientTrackBuilder"));
0291 }
0292 
0293 MtdTracksValidation::~MtdTracksValidation() {}
0294 
0295 // ------------ method called for each event  ------------
0296 void MtdTracksValidation::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0297   using namespace edm;
0298   using namespace geant_units::operators;
0299   using namespace std;
0300 
0301   auto GenRecTrackHandle = makeValid(iEvent.getHandle(GenRecTrackToken_));
0302 
0303   std::unordered_map<uint32_t, MTDHit> m_btlHits;
0304   std::unordered_map<uint32_t, MTDHit> m_etlHits;
0305   std::unordered_map<uint32_t, std::set<unsigned long int>> m_btlTrkPerCell;
0306   std::unordered_map<uint32_t, std::set<unsigned long int>> m_etlTrkPerCell;
0307   const auto& tp2SimAssociationMap = iEvent.get(tp2SimAssociationMapToken_);
0308 
0309   const auto& tMtd = iEvent.get(tmtdToken_);
0310   const auto& SigmatMtd = iEvent.get(SigmatmtdToken_);
0311   const auto& t0Src = iEvent.get(t0SrcToken_);
0312   const auto& Sigmat0Src = iEvent.get(Sigmat0SrcToken_);
0313   const auto& t0Pid = iEvent.get(t0PidToken_);
0314   const auto& Sigmat0Pid = iEvent.get(Sigmat0PidToken_);
0315   const auto& t0Safe = iEvent.get(t0SafePidToken_);
0316   const auto& Sigmat0Safe = iEvent.get(Sigmat0SafePidToken_);
0317   const auto& SigmaTofPi = iEvent.get(SigmaTofPiToken_);
0318   const auto& SigmaTofK = iEvent.get(SigmaTofKToken_);
0319   const auto& SigmaTofP = iEvent.get(SigmaTofPToken_);
0320   const auto& mtdQualMVA = iEvent.get(trackMVAQualToken_);
0321   const auto& trackAssoc = iEvent.get(trackAssocToken_);
0322   const auto& pathLength = iEvent.get(pathLengthToken_);
0323   const auto& outermostHitPosition = iEvent.get(outermostHitPositionToken_);
0324 
0325   auto simToRecoH = makeValid(iEvent.getHandle(simToRecoAssociationToken_));
0326   s2r_ = simToRecoH.product();
0327 
0328   auto recoToSimH = makeValid(iEvent.getHandle(recoToSimAssociationToken_));
0329   r2s_ = recoToSimH.product();
0330 
0331   unsigned int index = 0;
0332 
0333   // --- Loop over all RECO tracks ---
0334   for (const auto& trackGen : *GenRecTrackHandle) {
0335     const reco::TrackRef trackref(iEvent.getHandle(GenRecTrackToken_), index);
0336     index++;
0337 
0338     if (trackAssoc[trackref] == -1) {
0339       LogWarning("mtdTracks") << "Extended track not associated";
0340       continue;
0341     }
0342 
0343     const reco::TrackRef mtdTrackref = reco::TrackRef(iEvent.getHandle(RecTrackToken_), trackAssoc[trackref]);
0344     const reco::Track& track = *mtdTrackref;
0345 
0346     bool isBTL = false;
0347     bool isETL = false;
0348     bool twoETLdiscs = false;
0349     bool noCrack = std::abs(trackGen.eta()) < trackMaxBtlEta_ || std::abs(trackGen.eta()) > trackMinEtlEta_;
0350 
0351     if (trkRecSel(trackGen)) {
0352       meTracktmtd_->Fill(tMtd[trackref]);
0353       if (std::round(SigmatMtd[trackref] - Sigmat0Pid[trackref]) != 0) {
0354         LogWarning("mtdTracks")
0355             << "TimeError associated to refitted track is different from TimeError stored in tofPID "
0356                "sigmat0 ValueMap: this should not happen";
0357       }
0358 
0359       meTrackt0Src_->Fill(t0Src[trackref]);
0360       meTrackSigmat0Src_->Fill(Sigmat0Src[trackref]);
0361 
0362       meTrackt0Pid_->Fill(t0Pid[trackref]);
0363       meTrackSigmat0Pid_->Fill(Sigmat0Pid[trackref]);
0364       meTrackt0SafePid_->Fill(t0Safe[trackref]);
0365       meTrackSigmat0SafePid_->Fill(std::log10(std::max(Sigmat0Safe[trackref], 0.001f)));
0366       meTrackMVAQual_->Fill(mtdQualMVA[trackref]);
0367 
0368       meTrackSigmaTof_[0]->Fill(SigmaTofPi[trackref] * 1e3);  //save as ps
0369       meTrackSigmaTof_[1]->Fill(SigmaTofK[trackref] * 1e3);
0370       meTrackSigmaTof_[2]->Fill(SigmaTofP[trackref] * 1e3);
0371       meTrackSigmaTofvsP_[0]->Fill(trackGen.p(), SigmaTofPi[trackref] * 1e3);
0372       meTrackSigmaTofvsP_[1]->Fill(trackGen.p(), SigmaTofK[trackref] * 1e3);
0373       meTrackSigmaTofvsP_[2]->Fill(trackGen.p(), SigmaTofP[trackref] * 1e3);
0374 
0375       meTrackPathLenghtvsEta_->Fill(std::abs(trackGen.eta()), pathLength[trackref]);
0376 
0377       if (std::abs(trackGen.eta()) < trackMaxBtlEta_) {
0378         // --- all BTL tracks (with and without hit in MTD) ---
0379         meBTLTrackEffEtaTot_->Fill(trackGen.eta());
0380         meBTLTrackEffPhiTot_->Fill(trackGen.phi());
0381         meBTLTrackEffPtTot_->Fill(trackGen.pt());
0382 
0383         bool MTDBtl = false;
0384         int numMTDBtlvalidhits = 0;
0385         for (const auto hit : track.recHits()) {
0386           if (hit->isValid() == false)
0387             continue;
0388           MTDDetId Hit = hit->geographicalId();
0389           if ((Hit.det() == 6) && (Hit.subdetId() == 1) && (Hit.mtdSubDetector() == 1)) {
0390             MTDBtl = true;
0391             numMTDBtlvalidhits++;
0392           }
0393         }
0394         meTrackNumHits_->Fill(numMTDBtlvalidhits);
0395 
0396         // --- keeping only tracks with last hit in MTD ---
0397         if (MTDBtl == true) {
0398           isBTL = true;
0399           meBTLTrackEffEtaMtd_->Fill(trackGen.eta());
0400           meBTLTrackEffPhiMtd_->Fill(trackGen.phi());
0401           meBTLTrackEffPtMtd_->Fill(trackGen.pt());
0402           meBTLTrackRPTime_->Fill(track.t0());
0403           meBTLTrackPtRes_->Fill((trackGen.pt() - track.pt()) / trackGen.pt());
0404         }
0405         if (isBTL && Sigmat0Safe[trackref] < 0.) {
0406           meTrackNumHitsNT_->Fill(numMTDBtlvalidhits);
0407         }
0408       }  //loop over (geometrical) BTL tracks
0409 
0410       else {
0411         // --- all ETL tracks (with and without hit in MTD) ---
0412         if ((trackGen.eta() < -trackMinEtlEta_) && (trackGen.eta() > -trackMaxEtlEta_)) {
0413           meETLTrackEffEtaTot_[0]->Fill(trackGen.eta());
0414           meETLTrackEffPhiTot_[0]->Fill(trackGen.phi());
0415           meETLTrackEffPtTot_[0]->Fill(trackGen.pt());
0416         }
0417 
0418         if ((trackGen.eta() > trackMinEtlEta_) && (trackGen.eta() < trackMaxEtlEta_)) {
0419           meETLTrackEffEtaTot_[1]->Fill(trackGen.eta());
0420           meETLTrackEffPhiTot_[1]->Fill(trackGen.phi());
0421           meETLTrackEffPtTot_[1]->Fill(trackGen.pt());
0422         }
0423 
0424         bool MTDEtlZnegD1 = false;
0425         bool MTDEtlZnegD2 = false;
0426         bool MTDEtlZposD1 = false;
0427         bool MTDEtlZposD2 = false;
0428         int numMTDEtlvalidhits = 0;
0429         for (const auto hit : track.recHits()) {
0430           if (hit->isValid() == false)
0431             continue;
0432           MTDDetId Hit = hit->geographicalId();
0433           if ((Hit.det() == 6) && (Hit.subdetId() == 1) && (Hit.mtdSubDetector() == 2)) {
0434             isETL = true;
0435             ETLDetId ETLHit = hit->geographicalId();
0436 
0437             if ((ETLHit.zside() == -1) && (ETLHit.nDisc() == 1)) {
0438               MTDEtlZnegD1 = true;
0439               meETLTrackRPTime_->Fill(track.t0());
0440               meETLTrackPtRes_->Fill((trackGen.pt() - track.pt()) / trackGen.pt());
0441               numMTDEtlvalidhits++;
0442             }
0443             if ((ETLHit.zside() == -1) && (ETLHit.nDisc() == 2)) {
0444               MTDEtlZnegD2 = true;
0445               meETLTrackRPTime_->Fill(track.t0());
0446               meETLTrackPtRes_->Fill((trackGen.pt() - track.pt()) / trackGen.pt());
0447               numMTDEtlvalidhits++;
0448             }
0449             if ((ETLHit.zside() == 1) && (ETLHit.nDisc() == 1)) {
0450               MTDEtlZposD1 = true;
0451               meETLTrackRPTime_->Fill(track.t0());
0452               meETLTrackPtRes_->Fill((trackGen.pt() - track.pt()) / trackGen.pt());
0453               numMTDEtlvalidhits++;
0454             }
0455             if ((ETLHit.zside() == 1) && (ETLHit.nDisc() == 2)) {
0456               MTDEtlZposD2 = true;
0457               meETLTrackRPTime_->Fill(track.t0());
0458               meETLTrackPtRes_->Fill((trackGen.pt() - track.pt()) / trackGen.pt());
0459               numMTDEtlvalidhits++;
0460             }
0461           }
0462         }
0463         meTrackNumHits_->Fill(-numMTDEtlvalidhits);
0464         if (isETL && Sigmat0Safe[trackref] < 0.) {
0465           meTrackNumHitsNT_->Fill(-numMTDEtlvalidhits);
0466         }
0467 
0468         // --- keeping only tracks with last hit in MTD ---
0469         if ((trackGen.eta() < -trackMinEtlEta_) && (trackGen.eta() > -trackMaxEtlEta_)) {
0470           twoETLdiscs = (MTDEtlZnegD1 == true) && (MTDEtlZnegD2 == true);
0471           if ((MTDEtlZnegD1 == true) || (MTDEtlZnegD2 == true)) {
0472             meETLTrackEffEtaMtd_[0]->Fill(trackGen.eta());
0473             meETLTrackEffPhiMtd_[0]->Fill(trackGen.phi());
0474             meETLTrackEffPtMtd_[0]->Fill(trackGen.pt());
0475             if (twoETLdiscs) {
0476               meETLTrackEffEta2Mtd_[0]->Fill(trackGen.eta());
0477               meETLTrackEffPhi2Mtd_[0]->Fill(trackGen.phi());
0478               meETLTrackEffPt2Mtd_[0]->Fill(trackGen.pt());
0479             }
0480           }
0481         }
0482         if ((trackGen.eta() > trackMinEtlEta_) && (trackGen.eta() < trackMaxEtlEta_)) {
0483           twoETLdiscs = (MTDEtlZposD1 == true) && (MTDEtlZposD2 == true);
0484           if ((MTDEtlZposD1 == true) || (MTDEtlZposD2 == true)) {
0485             meETLTrackEffEtaMtd_[1]->Fill(trackGen.eta());
0486             meETLTrackEffPhiMtd_[1]->Fill(trackGen.phi());
0487             meETLTrackEffPtMtd_[1]->Fill(trackGen.pt());
0488             if (twoETLdiscs) {
0489               meETLTrackEffEta2Mtd_[1]->Fill(trackGen.eta());
0490               meETLTrackEffPhi2Mtd_[1]->Fill(trackGen.phi());
0491               meETLTrackEffPt2Mtd_[1]->Fill(trackGen.pt());
0492             }
0493           }
0494         }
0495       }
0496 
0497       if (isBTL)
0498         meTrackOutermostHitR_->Fill(outermostHitPosition[trackref]);
0499       if (isETL)
0500         meTrackOutermostHitZ_->Fill(std::abs(outermostHitPosition[trackref]));
0501 
0502       LogDebug("MtdTracksValidation") << "Track p/pt = " << trackGen.p() << " " << trackGen.pt() << " eta "
0503                                       << trackGen.eta() << " BTL " << isBTL << " ETL " << isETL << " 2disks "
0504                                       << twoETLdiscs;
0505 
0506       // TrackingParticle based matching
0507 
0508       const reco::TrackBaseRef trkrefb(trackref);
0509       auto tp_info = getMatchedTP(trkrefb);
0510 
0511       meTrackPtTot_->Fill(trackGen.pt());
0512       meTrackEtaTot_->Fill(std::abs(trackGen.eta()));
0513       if (tp_info != nullptr && trkTPSelAll(**tp_info)) {
0514         if (trackGen.pt() < trackMaxPt_) {
0515           if (isBTL) {
0516             meBTLTrackMatchedTPPtResMtd_->Fill(std::abs(track.pt() - (*tp_info)->pt()) /
0517                                                std::abs(trackGen.pt() - (*tp_info)->pt()));
0518             meBTLTrackMatchedTPPtRatioGen_->Fill(trackGen.pt() / (*tp_info)->pt());
0519             meBTLTrackMatchedTPPtRatioMtd_->Fill(track.pt() / (*tp_info)->pt());
0520             meBTLTrackMatchedTPPtResvsPtMtd_->Fill(
0521                 (*tp_info)->pt(), std::abs(track.pt() - (*tp_info)->pt()) / std::abs(trackGen.pt() - (*tp_info)->pt()));
0522             meBTLTrackMatchedTPDPtvsPtGen_->Fill((*tp_info)->pt(),
0523                                                  (trackGen.pt() - (*tp_info)->pt()) / (*tp_info)->pt());
0524             meBTLTrackMatchedTPDPtvsPtMtd_->Fill((*tp_info)->pt(), (track.pt() - (*tp_info)->pt()) / (*tp_info)->pt());
0525           }
0526           if (isETL && !twoETLdiscs && (std::abs(trackGen.eta()) > trackMinEtlEta_) &&
0527               (std::abs(trackGen.eta()) < trackMaxEtlEta_)) {
0528             meETLTrackMatchedTPPtResMtd_->Fill(std::abs(track.pt() - (*tp_info)->pt()) /
0529                                                std::abs(trackGen.pt() - (*tp_info)->pt()));
0530             meETLTrackMatchedTPPtRatioGen_->Fill(trackGen.pt() / (*tp_info)->pt());
0531             meETLTrackMatchedTPPtRatioMtd_->Fill(track.pt() / (*tp_info)->pt());
0532             meETLTrackMatchedTPPtResvsPtMtd_->Fill(
0533                 (*tp_info)->pt(), std::abs(track.pt() - (*tp_info)->pt()) / std::abs(trackGen.pt() - (*tp_info)->pt()));
0534             meETLTrackMatchedTPDPtvsPtGen_->Fill((*tp_info)->pt(),
0535                                                  (trackGen.pt() - (*tp_info)->pt()) / ((*tp_info)->pt()));
0536             meETLTrackMatchedTPDPtvsPtMtd_->Fill((*tp_info)->pt(),
0537                                                  (track.pt() - (*tp_info)->pt()) / ((*tp_info)->pt()));
0538           }
0539           if (isETL && twoETLdiscs) {
0540             meETLTrackMatchedTP2PtResMtd_->Fill(std::abs(track.pt() - (*tp_info)->pt()) /
0541                                                 std::abs(trackGen.pt() - (*tp_info)->pt()));
0542             meETLTrackMatchedTP2PtRatioGen_->Fill(trackGen.pt() / (*tp_info)->pt());
0543             meETLTrackMatchedTP2PtRatioMtd_->Fill(track.pt() / (*tp_info)->pt());
0544             meETLTrackMatchedTP2PtResvsPtMtd_->Fill(
0545                 (*tp_info)->pt(), std::abs(track.pt() - (*tp_info)->pt()) / std::abs(trackGen.pt() - (*tp_info)->pt()));
0546             meETLTrackMatchedTP2DPtvsPtGen_->Fill((*tp_info)->pt(),
0547                                                   (trackGen.pt() - (*tp_info)->pt()) / ((*tp_info)->pt()));
0548             meETLTrackMatchedTP2DPtvsPtMtd_->Fill((*tp_info)->pt(),
0549                                                   (track.pt() - (*tp_info)->pt()) / ((*tp_info)->pt()));
0550           }
0551         }
0552         auto simClustersRefs = tp2SimAssociationMap.find(*tp_info);
0553         const bool withMTD = (simClustersRefs != tp2SimAssociationMap.end());
0554         if (noCrack) {
0555           meTrackMatchedTPEffPtTot_->Fill(trackGen.pt());
0556           if (trkTPSelLV(**tp_info)) {
0557             meTrackMatchedTPEffPtTotLV_->Fill(trackGen.pt());
0558           }
0559           if (withMTD) {
0560             meTrackMatchedTPmtdEffPtTot_->Fill(trackGen.pt());
0561           }
0562         }
0563         meTrackMatchedTPEffEtaTot_->Fill(std::abs(trackGen.eta()));
0564         if (trkTPSelLV(**tp_info)) {
0565           meTrackMatchedTPEffEtaTotLV_->Fill(std::abs(trackGen.eta()));
0566         }
0567         if (withMTD) {
0568           meTrackMatchedTPmtdEffEtaTot_->Fill(std::abs(trackGen.eta()));
0569         }
0570         if (isBTL || isETL) {
0571           if (noCrack) {
0572             meTrackMatchedTPEffPtMtd_->Fill(trackGen.pt());
0573             if (isBTL || twoETLdiscs) {
0574               meTrackMatchedTPEffPtEtl2Mtd_->Fill(trackGen.pt());
0575             }
0576             if (withMTD) {
0577               meTrackMatchedTPmtdEffPtMtd_->Fill(trackGen.pt());
0578             }
0579           }
0580           meTrackMatchedTPEffEtaMtd_->Fill(std::abs(trackGen.eta()));
0581           if (isBTL || twoETLdiscs) {
0582             meTrackMatchedTPEffEtaEtl2Mtd_->Fill(std::abs(trackGen.eta()));
0583           }
0584           if (withMTD) {
0585             meTrackMatchedTPmtdEffEtaMtd_->Fill(std::abs(trackGen.eta()));
0586           }
0587         }
0588 
0589         // time pull and detailed extrapolation check only on tracks associated to TP from signal event
0590         if (!trkTPSelLV(**tp_info)) {
0591           continue;
0592         }
0593         size_t nlayers(0);
0594         float extrho(0.);
0595         float exteta(0.);
0596         float extphi(0.);
0597         float selvar(0.);
0598         auto accept = checkAcceptance(trackGen, iEvent, iSetup, nlayers, extrho, exteta, extphi, selvar);
0599         if (accept.first && std::abs(exteta) < trackMaxBtlEta_) {
0600           meExtraPhiAtBTL_->Fill(angle_units::operators::convertRadToDeg(extphi));
0601           meExtraBTLeneInCone_->Fill(selvar);
0602         }
0603         if (accept.second) {
0604           if (std::abs(exteta) < trackMaxBtlEta_) {
0605             meExtraPhiAtBTLmatched_->Fill(angle_units::operators::convertRadToDeg(extphi));
0606           }
0607           if (noCrack) {
0608             meExtraPtMtd_->Fill(trackGen.pt());
0609             if (nlayers == 2) {
0610               meExtraPtEtl2Mtd_->Fill(trackGen.pt());
0611             }
0612           }
0613           meExtraEtaMtd_->Fill(std::abs(trackGen.eta()));
0614           if (nlayers == 2) {
0615             meExtraEtaEtl2Mtd_->Fill(std::abs(trackGen.eta()));
0616           }
0617           if (accept.first && accept.second && !(isBTL || isETL)) {
0618             edm::LogInfo("MtdTracksValidation")
0619                 << "MtdTracksValidation: extender fail in " << iEvent.id().run() << " " << iEvent.id().event()
0620                 << " pt= " << trackGen.pt() << " eta= " << trackGen.eta();
0621             meExtraMTDfailExtenderEta_->Fill(std::abs(trackGen.eta()));
0622             if (noCrack) {
0623               meExtraMTDfailExtenderPt_->Fill(trackGen.pt());
0624             }
0625           }
0626         }  // detailed extrapolation check
0627 
0628         // time res and time pull
0629         double tsim = (*tp_info)->parentVertex()->position().t() * simUnit_;
0630         double dT(-9999.);
0631         double pullT(-9999.);
0632         if (Sigmat0Safe[trackref] != -1.) {
0633           dT = t0Safe[trackref] - tsim;
0634           pullT = dT / Sigmat0Safe[trackref];
0635           if (isBTL || isETL) {
0636             meTrackResTot_->Fill(dT);
0637             meTrackPullTot_->Fill(pullT);
0638             meTrackResTotvsMVAQual_->Fill(mtdQualMVA[trackref], dT);
0639             meTrackPullTotvsMVAQual_->Fill(mtdQualMVA[trackref], pullT);
0640           }
0641         }  // time res and time pull
0642 
0643       }  // TP matching
0644     }    // trkRecSel
0645 
0646     // ETL tracks with low pt (0.2 < Pt [GeV] < 0.7)
0647     if (trkRecSelLowPt(trackGen)) {
0648       if ((std::abs(trackGen.eta()) > trackMinEtlEta_) && (std::abs(trackGen.eta()) < trackMaxEtlEta_)) {
0649         if (trackGen.pt() < 0.45) {
0650           meETLTrackEffEtaTotLowPt_[0]->Fill(std::abs(trackGen.eta()));
0651         } else {
0652           meETLTrackEffEtaTotLowPt_[1]->Fill(std::abs(trackGen.eta()));
0653         }
0654       }
0655       bool MTDEtlZnegD1 = false;
0656       bool MTDEtlZnegD2 = false;
0657       bool MTDEtlZposD1 = false;
0658       bool MTDEtlZposD2 = false;
0659       for (const auto hit : track.recHits()) {
0660         if (hit->isValid() == false)
0661           continue;
0662         MTDDetId Hit = hit->geographicalId();
0663         if ((Hit.det() == 6) && (Hit.subdetId() == 1) && (Hit.mtdSubDetector() == 2)) {
0664           isETL = true;
0665           ETLDetId ETLHit = hit->geographicalId();
0666           if ((ETLHit.zside() == -1) && (ETLHit.nDisc() == 1)) {
0667             MTDEtlZnegD1 = true;
0668           }
0669           if ((ETLHit.zside() == -1) && (ETLHit.nDisc() == 2)) {
0670             MTDEtlZnegD2 = true;
0671           }
0672           if ((ETLHit.zside() == 1) && (ETLHit.nDisc() == 1)) {
0673             MTDEtlZposD1 = true;
0674           }
0675           if ((ETLHit.zside() == 1) && (ETLHit.nDisc() == 2)) {
0676             MTDEtlZposD2 = true;
0677           }
0678         }
0679       }
0680       if ((trackGen.eta() < -trackMinEtlEta_) && (trackGen.eta() > -trackMaxEtlEta_)) {
0681         twoETLdiscs = (MTDEtlZnegD1 == true) && (MTDEtlZnegD2 == true);
0682       }
0683       if ((trackGen.eta() > trackMinEtlEta_) && (trackGen.eta() < trackMaxEtlEta_)) {
0684         twoETLdiscs = (MTDEtlZposD1 == true) && (MTDEtlZposD2 == true);
0685       }
0686       if (isETL && (std::abs(trackGen.eta()) > trackMinEtlEta_) && (std::abs(trackGen.eta()) < trackMaxEtlEta_)) {
0687         if (trackGen.pt() < 0.45) {
0688           meETLTrackEffEtaMtdLowPt_[0]->Fill(std::abs(trackGen.eta()));
0689         } else {
0690           meETLTrackEffEtaMtdLowPt_[1]->Fill(std::abs(trackGen.eta()));
0691         }
0692       }
0693       if (isETL && twoETLdiscs) {
0694         if (trackGen.pt() < 0.45) {
0695           meETLTrackEffEta2MtdLowPt_[0]->Fill(std::abs(trackGen.eta()));
0696         } else {
0697           meETLTrackEffEta2MtdLowPt_[1]->Fill(std::abs(trackGen.eta()));
0698         }
0699       }
0700     }  // trkRecSelLowPt
0701 
0702   }  // RECO tracks loop
0703 }
0704 
0705 const std::pair<bool, bool> MtdTracksValidation::checkAcceptance(const reco::Track& track,
0706                                                                  const edm::Event& iEvent,
0707                                                                  edm::EventSetup const& iSetup,
0708                                                                  size_t& nlayers,
0709                                                                  float& extrho,
0710                                                                  float& exteta,
0711                                                                  float& extphi,
0712                                                                  float& selvar) {
0713   bool isMatched(false);
0714   nlayers = 0;
0715   extrho = 0.;
0716   exteta = -999.;
0717   extphi = -999.;
0718   selvar = 0.;
0719 
0720   auto geometryHandle = iSetup.getTransientHandle(mtdgeoToken_);
0721   const MTDGeometry* geom = geometryHandle.product();
0722   auto topologyHandle = iSetup.getTransientHandle(mtdtopoToken_);
0723   const MTDTopology* topology = topologyHandle.product();
0724 
0725   auto layerHandle = iSetup.getTransientHandle(mtdlayerToken_);
0726   const MTDDetLayerGeometry* layerGeo = layerHandle.product();
0727 
0728   auto magfieldHandle = iSetup.getTransientHandle(magfieldToken_);
0729   const MagneticField* mfield = magfieldHandle.product();
0730 
0731   auto ttrackBuilder = iSetup.getTransientHandle(builderToken_);
0732 
0733   auto tTrack = ttrackBuilder->build(track);
0734   TrajectoryStateOnSurface tsos = tTrack.outermostMeasurementState();
0735   float theMaxChi2 = 500.;
0736   float theNSigma = 10.;
0737   std::unique_ptr<MeasurementEstimator> theEstimator =
0738       std::make_unique<Chi2MeasurementEstimator>(theMaxChi2, theNSigma);
0739   SteppingHelixPropagator prop(mfield, anyDirection);
0740 
0741   auto btlRecHitsHandle = makeValid(iEvent.getHandle(btlRecHitsToken_));
0742   auto etlRecHitsHandle = makeValid(iEvent.getHandle(etlRecHitsToken_));
0743 
0744   edm::LogVerbatim("MtdTracksValidation")
0745       << "MtdTracksValidation: extrapolating track, pt= " << track.pt() << " eta= " << track.eta();
0746 
0747   //try BTL
0748   bool inBTL = false;
0749   float eneSum(0.);
0750   const std::vector<const DetLayer*>& layersBTL = layerGeo->allBTLLayers();
0751   for (const DetLayer* ilay : layersBTL) {
0752     std::pair<bool, TrajectoryStateOnSurface> comp = ilay->compatible(tsos, prop, *theEstimator);
0753     if (!comp.first)
0754       continue;
0755     if (!inBTL) {
0756       inBTL = true;
0757       extrho = comp.second.globalPosition().perp();
0758       exteta = comp.second.globalPosition().eta();
0759       extphi = comp.second.globalPosition().phi();
0760       edm::LogVerbatim("MtdTracksValidation") << "MtdTracksValidation: extrapolation at BTL surface, rho= " << extrho
0761                                               << " eta= " << exteta << " phi= " << extphi;
0762     }
0763     std::vector<DetLayer::DetWithState> compDets = ilay->compatibleDets(tsos, prop, *theEstimator);
0764     for (const auto& detWithState : compDets) {
0765       const auto& det = detWithState.first;
0766 
0767       // loop on compatible rechits and check energy in a fixed size cone around the extrapolation point
0768 
0769       edm::LogVerbatim("MtdTracksValidation")
0770           << "MtdTracksValidation: DetId= " << det->geographicalId().rawId()
0771           << " gp= " << detWithState.second.globalPosition().x() << " " << detWithState.second.globalPosition().y()
0772           << " " << detWithState.second.globalPosition().z() << " rho= " << detWithState.second.globalPosition().perp()
0773           << " eta= " << detWithState.second.globalPosition().eta()
0774           << " phi= " << detWithState.second.globalPosition().phi();
0775 
0776       for (const auto& recHit : *btlRecHitsHandle) {
0777         BTLDetId detId = recHit.id();
0778         DetId geoId = detId.geographicalId(MTDTopologyMode::crysLayoutFromTopoMode(topology->getMTDTopologyMode()));
0779         const MTDGeomDet* thedet = geom->idToDet(geoId);
0780         if (thedet == nullptr)
0781           throw cms::Exception("MtdTracksValidation") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
0782                                                       << detId.rawId() << ") is invalid!" << std::dec << std::endl;
0783         if (geoId == det->geographicalId()) {
0784           const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
0785           const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
0786 
0787           Local3DPoint local_point(0., 0., 0.);
0788           local_point = topo.pixelToModuleLocalPoint(local_point, detId.row(topo.nrows()), detId.column(topo.nrows()));
0789           const auto& global_point = thedet->toGlobal(local_point);
0790           edm::LogVerbatim("MtdTracksValidation")
0791               << "MtdTracksValidation: Hit id= " << detId.rawId() << " ene= " << recHit.energy()
0792               << " dr= " << reco::deltaR(global_point, detWithState.second.globalPosition());
0793           if (reco::deltaR(global_point, detWithState.second.globalPosition()) < cluDRradius_) {
0794             eneSum += recHit.energy();
0795             //extrho = detWithState.second.globalPosition().perp();
0796             //exteta = detWithState.second.globalPosition().eta();
0797             //extphi = detWithState.second.globalPosition().phi();
0798           }
0799         }
0800       }
0801     }
0802     if (eneSum > depositBTLthreshold_) {
0803       nlayers++;
0804       selvar = eneSum;
0805       isMatched = true;
0806       edm::LogVerbatim("MtdTracksValidation")
0807           << "MtdTracksValidation: BTL matched, energy= " << eneSum << " #layers= " << nlayers;
0808     }
0809   }
0810   if (inBTL) {
0811     return std::make_pair(inBTL, isMatched);
0812   }
0813 
0814   //try ETL
0815   bool inETL = false;
0816   const std::vector<const DetLayer*>& layersETL = layerGeo->allETLLayers();
0817   for (const DetLayer* ilay : layersETL) {
0818     size_t hcount(0);
0819     const BoundDisk& disk = static_cast<const MTDSectorForwardDoubleLayer*>(ilay)->specificSurface();
0820     const double diskZ = disk.position().z();
0821     if (tsos.globalPosition().z() * diskZ < 0)
0822       continue;  // only propagate to the disk that's on the same side
0823     std::pair<bool, TrajectoryStateOnSurface> comp = ilay->compatible(tsos, prop, *theEstimator);
0824     if (!comp.first)
0825       continue;
0826     if (!inETL) {
0827       inETL = true;
0828       extrho = comp.second.globalPosition().perp();
0829       exteta = comp.second.globalPosition().eta();
0830       extphi = comp.second.globalPosition().phi();
0831     }
0832     edm::LogVerbatim("MtdTracksValidation") << "MtdTracksValidation: extrapolation at ETL surface, rho= " << extrho
0833                                             << " eta= " << exteta << " phi= " << extphi;
0834     std::vector<DetLayer::DetWithState> compDets = ilay->compatibleDets(tsos, prop, *theEstimator);
0835     for (const auto& detWithState : compDets) {
0836       const auto& det = detWithState.first;
0837 
0838       // loop on compatible rechits and check hits in a fixed size cone around the extrapolation point
0839 
0840       edm::LogVerbatim("MtdTracksValidation")
0841           << "MtdTracksValidation: DetId= " << det->geographicalId().rawId()
0842           << " gp= " << detWithState.second.globalPosition().x() << " " << detWithState.second.globalPosition().y()
0843           << " " << detWithState.second.globalPosition().z() << " rho= " << detWithState.second.globalPosition().perp()
0844           << " eta= " << detWithState.second.globalPosition().eta()
0845           << " phi= " << detWithState.second.globalPosition().phi();
0846 
0847       for (const auto& recHit : *etlRecHitsHandle) {
0848         ETLDetId detId = recHit.id();
0849         DetId geoId = detId.geographicalId();
0850         const MTDGeomDet* thedet = geom->idToDet(geoId);
0851         if (thedet == nullptr)
0852           throw cms::Exception("MtdTracksValidation") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
0853                                                       << detId.rawId() << ") is invalid!" << std::dec << std::endl;
0854         if (geoId == det->geographicalId()) {
0855           const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
0856           const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
0857 
0858           Local3DPoint local_point(topo.localX(recHit.row()), topo.localY(recHit.column()), 0.);
0859           const auto& global_point = thedet->toGlobal(local_point);
0860           edm::LogVerbatim("MtdTracksValidation")
0861               << "MtdTracksValidation: Hit id= " << detId.rawId() << " time= " << recHit.time()
0862               << " dr= " << reco::deltaR(global_point, detWithState.second.globalPosition());
0863           if (reco::deltaR(global_point, detWithState.second.globalPosition()) < cluDRradius_) {
0864             hcount++;
0865             if (hcount == 1) {
0866               //extrho = detWithState.second.globalPosition().perp();
0867               //exteta = detWithState.second.globalPosition().eta();
0868               //extphi = detWithState.second.globalPosition().phi();
0869             }
0870           }
0871         }
0872       }
0873     }
0874     if (hcount > 0) {
0875       nlayers++;
0876       selvar = (float)hcount;
0877       isMatched = true;
0878       edm::LogVerbatim("MtdTracksValidation")
0879           << "MtdTracksValidation: ETL matched, counts= " << hcount << " #layers= " << nlayers;
0880     }
0881   }
0882 
0883   if (!inBTL && !inETL) {
0884     edm::LogVerbatim("MtdTracksValidation")
0885         << "MtdTracksValidation: track not extrapolating to MTD: pt= " << track.pt() << " eta= " << track.eta()
0886         << " phi= " << track.phi() << " vz= " << track.vz()
0887         << " vxy= " << std::sqrt(track.vx() * track.vx() + track.vy() * track.vy());
0888   }
0889   return std::make_pair(inETL, isMatched);
0890 }
0891 
0892 // ------------ method for histogram booking ------------
0893 void MtdTracksValidation::bookHistograms(DQMStore::IBooker& ibook, edm::Run const& run, edm::EventSetup const& iSetup) {
0894   ibook.setCurrentFolder(folder_);
0895 
0896   // histogram booking
0897   meBTLTrackRPTime_ = ibook.book1D("TrackBTLRPTime", "Track t0 with respect to R.P.;t0 [ns]", 100, -1, 3);
0898   meBTLTrackEffEtaTot_ = ibook.book1D("TrackBTLEffEtaTot", "Eta of tracks (Tot);#eta_{RECO}", 100, -1.6, 1.6);
0899   meBTLTrackEffPhiTot_ = ibook.book1D("TrackBTLEffPhiTot", "Phi of tracks (Tot);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0900   meBTLTrackEffPtTot_ = ibook.book1D("TrackBTLEffPtTot", "Pt of tracks (Tot);pt_{RECO} [GeV]", 50, 0, 10);
0901   meBTLTrackEffEtaMtd_ = ibook.book1D("TrackBTLEffEtaMtd", "Eta of tracks (Mtd);#eta_{RECO}", 100, -1.6, 1.6);
0902   meBTLTrackEffPhiMtd_ = ibook.book1D("TrackBTLEffPhiMtd", "Phi of tracks (Mtd);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0903   meBTLTrackEffPtMtd_ = ibook.book1D("TrackBTLEffPtMtd", "Pt of tracks (Mtd);pt_{RECO} [GeV]", 50, 0, 10);
0904   meBTLTrackPtRes_ =
0905       ibook.book1D("TrackBTLPtRes", "Track pT resolution  ;pT_{Gentrack}-pT_{MTDtrack}/pT_{Gentrack} ", 100, -0.1, 0.1);
0906   meETLTrackRPTime_ = ibook.book1D("TrackETLRPTime", "Track t0 with respect to R.P.;t0 [ns]", 100, -1, 3);
0907   meETLTrackEffEtaTot_[0] =
0908       ibook.book1D("TrackETLEffEtaTotZneg", "Eta of tracks (Tot) (-Z);#eta_{RECO}", 100, -3.2, -1.4);
0909   meETLTrackEffEtaTot_[1] =
0910       ibook.book1D("TrackETLEffEtaTotZpos", "Eta of tracks (Tot) (+Z);#eta_{RECO}", 100, 1.4, 3.2);
0911   meETLTrackEffEtaTotLowPt_[0] =
0912       ibook.book1D("TrackETLEffEtaTotLowPt0", "Eta of tracks, 0.2 < pt < 0.45 (Tot);#eta_{RECO}", 100, 1.4, 3.2);
0913   meETLTrackEffEtaTotLowPt_[1] =
0914       ibook.book1D("TrackETLEffEtaTotLowPt1", "Eta of tracks, 0.45 < pt < 0.7 (Tot);#eta_{RECO}", 100, 1.4, 3.2);
0915   meETLTrackEffPhiTot_[0] =
0916       ibook.book1D("TrackETLEffPhiTotZneg", "Phi of tracks (Tot) (-Z);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0917   meETLTrackEffPhiTot_[1] =
0918       ibook.book1D("TrackETLEffPhiTotZpos", "Phi of tracks (Tot) (+Z);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0919   meETLTrackEffPtTot_[0] = ibook.book1D("TrackETLEffPtTotZneg", "Pt of tracks (Tot) (-Z);pt_{RECO} [GeV]", 50, 0, 10);
0920   meETLTrackEffPtTot_[1] = ibook.book1D("TrackETLEffPtTotZpos", "Pt of tracks (Tot) (+Z);pt_{RECO} [GeV]", 50, 0, 10);
0921   meETLTrackEffEtaMtd_[0] =
0922       ibook.book1D("TrackETLEffEtaMtdZneg", "Eta of tracks (Mtd) (-Z);#eta_{RECO}", 100, -3.2, -1.4);
0923   meETLTrackEffEtaMtd_[1] =
0924       ibook.book1D("TrackETLEffEtaMtdZpos", "Eta of tracks (Mtd) (+Z);#eta_{RECO}", 100, 1.4, 3.2);
0925   meETLTrackEffEtaMtdLowPt_[0] =
0926       ibook.book1D("TrackETLEffEtaMtdLowPt0", "Eta of tracks, 0.2 < pt < 0.45 (Mtd);#eta_{RECO}", 100, 1.4, 3.2);
0927   meETLTrackEffEtaMtdLowPt_[1] =
0928       ibook.book1D("TrackETLEffEtaMtdLowPt1", "Eta of tracks, 0.45 < pt < 0.7 (Mtd);#eta_{RECO}", 100, 1.4, 3.2);
0929   meETLTrackEffEta2MtdLowPt_[0] =
0930       ibook.book1D("TrackETLEffEta2MtdLowPt0", "Eta of tracks, 0.2 < pt < 0.45 (Mtd 2 hit);#eta_{RECO}", 100, 1.4, 3.2);
0931   meETLTrackEffEta2MtdLowPt_[1] =
0932       ibook.book1D("TrackETLEffEta2MtdLowPt1", "Eta of tracks, 0.45 < pt < 0.7 (Mtd 2 hit);#eta_{RECO}", 100, 1.4, 3.2);
0933   meETLTrackEffPhiMtd_[0] =
0934       ibook.book1D("TrackETLEffPhiMtdZneg", "Phi of tracks (Mtd) (-Z);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0935   meETLTrackEffPhiMtd_[1] =
0936       ibook.book1D("TrackETLEffPhiMtdZpos", "Phi of tracks (Mtd) (+Z);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0937   meETLTrackEffPtMtd_[0] = ibook.book1D("TrackETLEffPtMtdZneg", "Pt of tracks (Mtd) (-Z);pt_{RECO} [GeV]", 50, 0, 10);
0938   meETLTrackEffPtMtd_[1] = ibook.book1D("TrackETLEffPtMtdZpos", "Pt of tracks (Mtd) (+Z);pt_{RECO} [GeV]", 50, 0, 10);
0939   meETLTrackEffEta2Mtd_[0] =
0940       ibook.book1D("TrackETLEffEta2MtdZneg", "Eta of tracks (Mtd 2 hit) (-Z);#eta_{RECO}", 100, -3.2, -1.4);
0941   meETLTrackEffEta2Mtd_[1] =
0942       ibook.book1D("TrackETLEffEta2MtdZpos", "Eta of tracks (Mtd 2 hit) (+Z);#eta_{RECO}", 100, 1.4, 3.2);
0943   meETLTrackEffPhi2Mtd_[0] =
0944       ibook.book1D("TrackETLEffPhi2MtdZneg", "Phi of tracks (Mtd 2 hit) (-Z);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0945   meETLTrackEffPhi2Mtd_[1] =
0946       ibook.book1D("TrackETLEffPhi2MtdZpos", "Phi of tracks (Mtd 2 hit) (+Z);#phi_{RECO} [rad]", 100, -3.2, 3.2);
0947   meETLTrackEffPt2Mtd_[0] =
0948       ibook.book1D("TrackETLEffPt2MtdZneg", "Pt of tracks (Mtd 2 hit) (-Z);pt_{RECO} [GeV]", 50, 0, 10);
0949   meETLTrackEffPt2Mtd_[1] =
0950       ibook.book1D("TrackETLEffPt2MtdZpos", "Pt of tracks (Mtd 2 hit) (+Z);pt_{RECO} [GeV]", 50, 0, 10);
0951   meETLTrackPtRes_ =
0952       ibook.book1D("TrackETLPtRes", "Track pT resolution;pT_{Gentrack}-pT_{MTDtrack}/pT_{Gentrack} ", 100, -0.1, 0.1);
0953 
0954   meTracktmtd_ = ibook.book1D("Tracktmtd", "Track time from TrackExtenderWithMTD;tmtd [ns]", 150, 1, 16);
0955   meTrackt0Src_ = ibook.book1D("Trackt0Src", "Track time from TrackExtenderWithMTD;t0Src [ns]", 100, -1.5, 1.5);
0956   meTrackSigmat0Src_ =
0957       ibook.book1D("TrackSigmat0Src", "Time Error from TrackExtenderWithMTD; #sigma_{t0Src} [ns]", 100, 0, 0.1);
0958 
0959   meTrackt0Pid_ = ibook.book1D("Trackt0Pid", "Track t0 as stored in TofPid;t0 [ns]", 100, -1, 1);
0960   meTrackSigmat0Pid_ = ibook.book1D("TrackSigmat0Pid", "Sigmat0 as stored in TofPid; #sigma_{t0} [ns]", 100, 0, 0.1);
0961   meTrackt0SafePid_ = ibook.book1D("Trackt0SafePID", "Track t0 Safe as stored in TofPid;t0 [ns]", 100, -1, 1);
0962   meTrackSigmat0SafePid_ = ibook.book1D(
0963       "TrackSigmat0SafePID", "Log10(Sigmat0 Safe) as stored in TofPid; Log10(#sigma_{t0} [ns])", 80, -3, 1);
0964   meTrackNumHits_ = ibook.book1D("TrackNumHits", "Number of valid MTD hits per track ; Number of hits", 10, -5, 5);
0965   meTrackNumHitsNT_ = ibook.book1D(
0966       "TrackNumHitsNT", "Number of valid MTD hits per track no time associated; Number of hits", 10, -5, 5);
0967   meTrackMVAQual_ = ibook.book1D("TrackMVAQual", "Track MVA Quality as stored in Value Map ; MVAQual", 100, -1, 1);
0968   meTrackPathLenghtvsEta_ = ibook.bookProfile(
0969       "TrackPathLenghtvsEta", "MTD Track pathlength vs MTD track Eta;|#eta|;Pathlength", 100, 0, 3.2, 100.0, 400.0, "S");
0970 
0971   meTrackOutermostHitR_ = ibook.book1D("TrackOutermostHitR", "Track outermost hit position R; R[cm]", 40, 0, 120.);
0972   meTrackOutermostHitZ_ = ibook.book1D("TrackOutermostHitZ", "Track outermost hit position Z; z[cm]", 100, 0, 300.);
0973 
0974   meTrackSigmaTof_[0] =
0975       ibook.book1D("TrackSigmaTof_Pion", "Sigma(TOF) for pion hypothesis; #sigma_{t0} [ps]", 10, 0, 5);
0976   meTrackSigmaTof_[1] =
0977       ibook.book1D("TrackSigmaTof_Kaon", "Sigma(TOF) for kaon hypothesis; #sigma_{t0} [ps]", 25, 0, 25);
0978   meTrackSigmaTof_[2] =
0979       ibook.book1D("TrackSigmaTof_Proton", "Sigma(TOF) for proton hypothesis; #sigma_{t0} [ps]", 50, 0, 50);
0980 
0981   meTrackSigmaTofvsP_[0] = ibook.bookProfile("TrackSigmaTofvsP_Pion",
0982                                              "Sigma(TOF) for pion hypothesis vs p; p [GeV]; #sigma_{t0} [ps]",
0983                                              20,
0984                                              0,
0985                                              10.,
0986                                              0,
0987                                              50.,
0988                                              "S");
0989   meTrackSigmaTofvsP_[1] = ibook.bookProfile("TrackSigmaTofvsP_Kaon",
0990                                              "Sigma(TOF) for kaon hypothesis vs p; p [GeV]; #sigma_{t0} [ps]",
0991                                              20,
0992                                              0,
0993                                              10.,
0994                                              0,
0995                                              50.,
0996                                              "S");
0997   meTrackSigmaTofvsP_[2] = ibook.bookProfile("TrackSigmaTofvsP_Proton",
0998                                              "Sigma(TOF) for proton hypothesis vs p; p [GeV]; #sigma_{t0} [ps]",
0999                                              20,
1000                                              0,
1001                                              10.,
1002                                              0,
1003                                              50.,
1004                                              "S");
1005 
1006   meExtraPtMtd_ =
1007       ibook.book1D("ExtraPtMtd", "Pt of tracks associated to LV extrapolated to hits; track pt [GeV] ", 110, 0., 11.);
1008   meExtraPtEtl2Mtd_ = ibook.book1D("ExtraPtEtl2Mtd",
1009                                    "Pt of tracks associated to LV extrapolated to hits, 2 ETL layers; track pt [GeV] ",
1010                                    110,
1011                                    0.,
1012                                    11.);
1013 
1014   meTrackPtTot_ = ibook.book1D("TrackPtTot", "Pt of tracks ; track pt [GeV] ", 110, 0., 11.);
1015   meTrackMatchedTPEffPtTot_ =
1016       ibook.book1D("MatchedTPEffPtTot", "Pt of tracks matched to TP; track pt [GeV] ", 110, 0., 11.);
1017   meTrackMatchedTPEffPtTotLV_ =
1018       ibook.book1D("MatchedTPEffPtTotLV", "Pt of tracks associated to LV matched to TP; track pt [GeV] ", 110, 0., 11.);
1019   meTrackMatchedTPEffPtMtd_ =
1020       ibook.book1D("MatchedTPEffPtMtd", "Pt of tracks matched to TP with time; track pt [GeV] ", 110, 0., 11.);
1021   meTrackMatchedTPEffPtEtl2Mtd_ = ibook.book1D(
1022       "MatchedTPEffPtEtl2Mtd", "Pt of tracks matched to TP with time, 2 ETL hits; track pt [GeV] ", 110, 0., 11.);
1023 
1024   meBTLTrackMatchedTPPtResMtd_ = ibook.book1D(
1025       "TrackMatchedTPBTLPtResMtd",
1026       "Pt resolution of tracks matched to TP-BTL hit  ;|pT_{MTDtrack}-pT_{truth}|/|pT_{Gentrack}-pT_{truth}| ",
1027       100,
1028       0.,
1029       4.);
1030   meETLTrackMatchedTPPtResMtd_ = ibook.book1D(
1031       "TrackMatchedTPETLPtResMtd",
1032       "Pt resolution of tracks matched to TP-ETL hit  ;|pT_{MTDtrack}-pT_{truth}|/|pT_{Gentrack}-pT_{truth}| ",
1033       100,
1034       0.,
1035       4.);
1036   meETLTrackMatchedTP2PtResMtd_ = ibook.book1D(
1037       "TrackMatchedTPETL2PtResMtd",
1038       "Pt resolution of tracks matched to TP-ETL 2hits  ;|pT_{MTDtrack}-pT_{truth}|/|pT_{Gentrack}-pT_{truth}| ",
1039       100,
1040       0.,
1041       4.);
1042   meBTLTrackMatchedTPPtRatioGen_ = ibook.book1D(
1043       "TrackMatchedTPBTLPtRatioGen", "Pt ratio of Gentracks (BTL)  ;pT_{Gentrack}/pT_{truth} ", 100, 0.9, 1.1);
1044   meETLTrackMatchedTPPtRatioGen_ = ibook.book1D(
1045       "TrackMatchedTPETLPtRatioGen", "Pt ratio of Gentracks (ETL 1hit)  ;pT_{Gentrack}/pT_{truth} ", 100, 0.9, 1.1);
1046   meETLTrackMatchedTP2PtRatioGen_ = ibook.book1D(
1047       "TrackMatchedTPETL2PtRatioGen", "Pt ratio of Gentracks (ETL 2hits)  ;pT_{Gentrack}/pT_{truth} ", 100, 0.9, 1.1);
1048   meBTLTrackMatchedTPPtRatioMtd_ = ibook.book1D("TrackMatchedTPBTLPtRatioMtd",
1049                                                 "Pt ratio of tracks matched to TP-BTL hit  ;pT_{MTDtrack}/pT_{truth} ",
1050                                                 100,
1051                                                 0.9,
1052                                                 1.1);
1053   meETLTrackMatchedTPPtRatioMtd_ = ibook.book1D("TrackMatchedTPETLPtRatioMtd",
1054                                                 "Pt ratio of tracks matched to TP-ETL hit  ;pT_{MTDtrack}/pT_{truth} ",
1055                                                 100,
1056                                                 0.9,
1057                                                 1.1);
1058   meETLTrackMatchedTP2PtRatioMtd_ =
1059       ibook.book1D("TrackMatchedTPETL2PtRatioMtd",
1060                    "Pt ratio of tracks matched to TP-ETL 2hits  ;pT_{MTDtrack}/pT_{truth} ",
1061                    100,
1062                    0.9,
1063                    1.1);
1064   meBTLTrackMatchedTPPtResvsPtMtd_ = ibook.bookProfile("TrackMatchedTPBTLPtResvsPtMtd",
1065                                                        "Pt resolution of tracks matched to TP-BTL hit vs Pt;pT_{truth} "
1066                                                        "[GeV];|pT_{MTDtrack}-pT_{truth}|/|pT_{Gentrack}-pT_{truth}| ",
1067                                                        20,
1068                                                        0.7,
1069                                                        10.,
1070                                                        0.,
1071                                                        4.,
1072                                                        "s");
1073   meETLTrackMatchedTPPtResvsPtMtd_ = ibook.bookProfile("TrackMatchedTPETLPtResvsPtMtd",
1074                                                        "Pt resolution of tracks matched to TP-ETL hit vs Pt;pT_{truth} "
1075                                                        "[GeV];|pT_{MTDtrack}-pT_{truth}|/|pT_{Gentrack}-pT_{truth}| ",
1076                                                        20,
1077                                                        0.7,
1078                                                        10.,
1079                                                        0.,
1080                                                        4.,
1081                                                        "s");
1082   meETLTrackMatchedTP2PtResvsPtMtd_ =
1083       ibook.bookProfile("TrackMatchedTPETL2PtResvsPtMtd",
1084                         "Pt resolution of tracks matched to TP-ETL 2hits Pt pT;pT_{truth} "
1085                         "[GeV];|pT_{MTDtrack}-pT_{truth}|/|pT_{Gentrack}-pT_{truth}| ",
1086                         20,
1087                         0.7,
1088                         10.,
1089                         0.,
1090                         4.,
1091                         "s");
1092   meBTLTrackMatchedTPDPtvsPtGen_ = ibook.bookProfile(
1093       "TrackMatchedTPBTLDPtvsPtGen",
1094       "Pt relative difference of Gentracks (BTL) vs Pt;pT_{truth} [GeV];pT_{Gentrack}-pT_{truth}/pT_{truth} ",
1095       20,
1096       0.7,
1097       10.,
1098       -0.1,
1099       0.1,
1100       "s");
1101   meETLTrackMatchedTPDPtvsPtGen_ = ibook.bookProfile(
1102       "TrackMatchedTPETLDPtvsPtGen",
1103       "Pt relative difference of Gentracks (ETL 1hit) vs Pt;pT_{truth} [GeV];pT_{Gentrack}-pT_{truth}/pT_{truth} ",
1104       20,
1105       0.7,
1106       10.,
1107       -0.1,
1108       0.1,
1109       "s");
1110   meETLTrackMatchedTP2DPtvsPtGen_ = ibook.bookProfile(
1111       "TrackMatchedTPETL2DPtvsPtGen",
1112       "Pt relative difference  of Gentracks (ETL 2hits) vs Pt;pT_{truth} [GeV];pT_{Gentrack}-pT_{truth}/pT_{truth} ",
1113       20,
1114       0.7,
1115       10.,
1116       -0.1,
1117       0.1,
1118       "s");
1119   meBTLTrackMatchedTPDPtvsPtMtd_ = ibook.bookProfile("TrackMatchedTPBTLDPtvsPtMtd",
1120                                                      "Pt relative difference of tracks matched to TP-BTL hits vs "
1121                                                      "Pt;pT_{truth} [GeV];pT_{MTDtrack}-pT_{truth}/pT_{truth} ",
1122                                                      20,
1123                                                      0.7,
1124                                                      10.,
1125                                                      -0.1,
1126                                                      0.1,
1127                                                      "s");
1128   meETLTrackMatchedTPDPtvsPtMtd_ = ibook.bookProfile("TrackMatchedTPETLDPtvsPtMtd",
1129                                                      "Pt relative difference of tracks matched to TP-ETL hits vs "
1130                                                      "Pt;pT_{truth} [GeV];pT_{MTDtrack}-pT_{truth}/pT_{truth} ",
1131                                                      20,
1132                                                      0.7,
1133                                                      10.,
1134                                                      -0.1,
1135                                                      0.1,
1136                                                      "s");
1137   meETLTrackMatchedTP2DPtvsPtMtd_ = ibook.bookProfile("TrackMatchedTPETL2DPtvsPtMtd",
1138                                                       "Pt relative difference of tracks matched to TP-ETL 2hits vs "
1139                                                       "Pt;pT_{truth} [GeV];pT_{MTDtrack}-pT_{truth}/pT_{truth} ",
1140                                                       20,
1141                                                       0.7,
1142                                                       10.,
1143                                                       -0.1,
1144                                                       0.1,
1145                                                       "s");
1146 
1147   meTrackMatchedTPmtdEffPtTot_ =
1148       ibook.book1D("MatchedTPmtdEffPtTot", "Pt of tracks matched to TP-mtd hit; track pt [GeV] ", 110, 0., 11.);
1149   meTrackMatchedTPmtdEffPtMtd_ = ibook.book1D(
1150       "MatchedTPmtdEffPtMtd", "Pt of tracks matched to TP-mtd hit with time; track pt [GeV] ", 110, 0., 11.);
1151 
1152   meExtraEtaMtd_ =
1153       ibook.book1D("ExtraEtaMtd", "Eta of tracks associated to LV extrapolated to hits; track eta ", 66, 0., 3.3);
1154   meExtraEtaEtl2Mtd_ = ibook.book1D(
1155       "ExtraEtaEtl2Mtd", "Eta of tracks associated to LV extrapolated to hits, 2 ETL layers; track eta ", 66, 0., 3.3);
1156 
1157   meTrackEtaTot_ = ibook.book1D("TrackEtaTot", "Eta of tracks ; track eta ", 66, 0., 3.3);
1158   meTrackMatchedTPEffEtaTot_ =
1159       ibook.book1D("MatchedTPEffEtaTot", "Eta of tracks matched to TP; track eta ", 66, 0., 3.3);
1160   meTrackMatchedTPEffEtaTotLV_ =
1161       ibook.book1D("MatchedTPEffEtaTotLV", "Eta of tracks associated to LV matched to TP; track eta ", 66, 0., 3.3);
1162   meTrackMatchedTPEffEtaMtd_ =
1163       ibook.book1D("MatchedTPEffEtaMtd", "Eta of tracks matched to TP with time; track eta ", 66, 0., 3.3);
1164   meTrackMatchedTPEffEtaEtl2Mtd_ = ibook.book1D(
1165       "MatchedTPEffEtaEtl2Mtd", "Eta of tracks matched to TP with time, 2 ETL hits; track eta ", 66, 0., 3.3);
1166 
1167   meTrackMatchedTPmtdEffEtaTot_ =
1168       ibook.book1D("MatchedTPmtdEffEtaTot", "Eta of tracks matched to TP-mtd hit; track eta ", 66, 0., 3.3);
1169   meTrackMatchedTPmtdEffEtaMtd_ =
1170       ibook.book1D("MatchedTPmtdEffEtaMtd", "Eta of tracks matched to TP-mtd hit with time; track eta ", 66, 0., 3.3);
1171 
1172   meTrackResTot_ = ibook.book1D(
1173       "TrackRes", "t_{rec} - t_{sim} for LV associated tracks matched to TP; t_{rec} - t_{sim} [ns] ", 120, -0.15, 0.15);
1174   meTrackPullTot_ = ibook.book1D(
1175       "TrackPull", "Pull for LV associated tracks matched to TP; (t_{rec}-t_{sim})/#sigma_{t}", 50, -5., 5.);
1176   meTrackResTotvsMVAQual_ = ibook.bookProfile(
1177       "TrackResvsMVA",
1178       "t_{rec} - t_{sim} for LV associated tracks matched to TP vs MVA Quality; MVAQual; t_{rec} - t_{sim} [ns] ",
1179       100,
1180       -1.,
1181       1.,
1182       -0.15,
1183       0.15,
1184       "s");
1185   meTrackPullTotvsMVAQual_ = ibook.bookProfile(
1186       "TrackPullvsMVA",
1187       "Pull for LV associated tracks matched to TP vs MVA Quality; MVAQual; (t_{rec}-t_{sim})/#sigma_{t}",
1188       100,
1189       -1.,
1190       1.,
1191       -5.,
1192       5.,
1193       "s");
1194 
1195   meExtraPhiAtBTL_ = ibook.book1D(
1196       "ExtraPhiAtBTL", "Phi at BTL surface of extrapolated tracks associated to LV; phi [deg]", 720, -180., 180.);
1197   meExtraPhiAtBTLmatched_ =
1198       ibook.book1D("ExtraPhiAtBTLmatched",
1199                    "Phi at BTL surface of extrapolated tracks associated to LV matched with BTL hits; phi [deg]",
1200                    720,
1201                    -180.,
1202                    180.);
1203   meExtraBTLeneInCone_ =
1204       ibook.book1D("ExtraBTLeneInCone",
1205                    "BTL reconstructed energy in cone arounnd extrapolated track associated to LV; E [MeV]",
1206                    100,
1207                    0.,
1208                    50.);
1209   meExtraMTDfailExtenderEta_ =
1210       ibook.book1D("ExtraMTDfailExtenderEta",
1211                    "Eta of tracks associated to LV extrapolated to MTD with no track extender match to hits; track eta",
1212                    66,
1213                    0.,
1214                    3.3);
1215   ;
1216   meExtraMTDfailExtenderPt_ = ibook.book1D(
1217       "ExtraMTDfailExtenderPt",
1218       "Pt of tracks associated to LV extrapolated to MTD with no track extender match to hits; track pt [GeV] ",
1219       110,
1220       0.,
1221       11.);
1222 }
1223 
1224 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
1225 
1226 void MtdTracksValidation::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
1227   edm::ParameterSetDescription desc;
1228 
1229   desc.add<std::string>("folder", "MTD/Tracks");
1230   desc.add<edm::InputTag>("inputTagG", edm::InputTag("generalTracks"));
1231   desc.add<edm::InputTag>("inputTagT", edm::InputTag("trackExtenderWithMTD"));
1232   desc.add<edm::InputTag>("inputTagV", edm::InputTag("offlinePrimaryVertices4D"));
1233   desc.add<edm::InputTag>("inputTagH", edm::InputTag("generatorSmeared"));
1234   desc.add<edm::InputTag>("SimTag", edm::InputTag("mix", "MergedTrackTruth"));
1235   desc.add<edm::InputTag>("TPtoRecoTrackAssoc", edm::InputTag("trackingParticleRecoTrackAsssociation"));
1236   desc.add<edm::InputTag>("tp2SimAssociationMapTag", edm::InputTag("mtdSimLayerClusterToTPAssociation"));
1237   desc.add<edm::InputTag>("btlRecHits", edm::InputTag("mtdRecHits", "FTLBarrel"));
1238   desc.add<edm::InputTag>("etlRecHits", edm::InputTag("mtdRecHits", "FTLEndcap"));
1239   desc.add<edm::InputTag>("tmtd", edm::InputTag("trackExtenderWithMTD:generalTracktmtd"));
1240   desc.add<edm::InputTag>("sigmatmtd", edm::InputTag("trackExtenderWithMTD:generalTracksigmatmtd"));
1241   desc.add<edm::InputTag>("t0Src", edm::InputTag("trackExtenderWithMTD:generalTrackt0"));
1242   desc.add<edm::InputTag>("sigmat0Src", edm::InputTag("trackExtenderWithMTD:generalTracksigmat0"));
1243   desc.add<edm::InputTag>("trackAssocSrc", edm::InputTag("trackExtenderWithMTD:generalTrackassoc"))
1244       ->setComment("Association between General and MTD Extended tracks");
1245   desc.add<edm::InputTag>("pathLengthSrc", edm::InputTag("trackExtenderWithMTD:generalTrackPathLength"));
1246   desc.add<edm::InputTag>("t0SafePID", edm::InputTag("tofPID:t0safe"));
1247   desc.add<edm::InputTag>("sigmat0SafePID", edm::InputTag("tofPID:sigmat0safe"));
1248   desc.add<edm::InputTag>("sigmat0PID", edm::InputTag("tofPID:sigmat0"));
1249   desc.add<edm::InputTag>("t0PID", edm::InputTag("tofPID:t0"));
1250   desc.add<edm::InputTag>("sigmaTofPi", edm::InputTag("trackExtenderWithMTD:generalTrackSigmaTofPi"));
1251   desc.add<edm::InputTag>("sigmaTofK", edm::InputTag("trackExtenderWithMTD:generalTrackSigmaTofK"));
1252   desc.add<edm::InputTag>("sigmaTofP", edm::InputTag("trackExtenderWithMTD:generalTrackSigmaTofP"));
1253   desc.add<edm::InputTag>("trackMVAQual", edm::InputTag("mtdTrackQualityMVA:mtdQualMVA"));
1254   desc.add<edm::InputTag>("outermostHitPositionSrc",
1255                           edm::InputTag("trackExtenderWithMTD:generalTrackOutermostHitPosition"));
1256   desc.add<double>("trackMinimumPt", 0.7);  // [GeV]
1257   desc.add<double>("trackMaximumPt", 12.);  // [GeV]
1258   desc.add<double>("trackMaximumBtlEta", 1.5);
1259   desc.add<double>("trackMinimumEtlEta", 1.6);
1260   desc.add<double>("trackMaximumEtlEta", 3.);
1261   desc.addUntracked<bool>("optionalPlots", true);
1262 
1263   descriptions.add("mtdTracksValid", desc);
1264 }
1265 
1266 const bool MtdTracksValidation::trkTPSelLV(const TrackingParticle& tp) {
1267   bool match = (tp.status() != 1) ? false : true;
1268   return match;
1269 }
1270 
1271 const bool MtdTracksValidation::trkTPSelAll(const TrackingParticle& tp) {
1272   bool match = false;
1273 
1274   auto x_pv = tp.parentVertex()->position().x();
1275   auto y_pv = tp.parentVertex()->position().y();
1276   auto z_pv = tp.parentVertex()->position().z();
1277 
1278   auto r_pv = std::sqrt(x_pv * x_pv + y_pv * y_pv);
1279 
1280   match = tp.charge() != 0 && std::abs(tp.eta()) < etacutGEN_ && tp.pt() > pTcutBTL_ && r_pv < rBTL_ &&
1281           std::abs(z_pv) < zETL_;
1282   return match;
1283 }
1284 
1285 const bool MtdTracksValidation::trkRecSel(const reco::TrackBase& trk) {
1286   bool match = false;
1287   match = std::abs(trk.eta()) <= etacutREC_ && trk.pt() > pTcutBTL_;
1288   return match;
1289 }
1290 
1291 const bool MtdTracksValidation::trkRecSelLowPt(const reco::TrackBase& trk) {
1292   bool match = false;
1293   match = std::abs(trk.eta()) <= etacutREC_ && trk.pt() > pTcutETL_ && trk.pt() < pTcutBTL_;
1294   return match;
1295 }
1296 
1297 const edm::Ref<std::vector<TrackingParticle>>* MtdTracksValidation::getMatchedTP(const reco::TrackBaseRef& recoTrack) {
1298   auto found = r2s_->find(recoTrack);
1299 
1300   // reco track not matched to any TP
1301   if (found == r2s_->end())
1302     return nullptr;
1303 
1304   //matched TP equal to any TP associated to in time events
1305   for (const auto& tp : found->val) {
1306     if (tp.first->eventId().bunchCrossing() == 0)
1307       return &tp.first;
1308   }
1309 
1310   // reco track not matched to any TP from vertex
1311   return nullptr;
1312 }
1313 
1314 DEFINE_FWK_MODULE(MtdTracksValidation);