Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-04 01:57:18

0001 //////////////////////////////////////////////////////////////////////
0002 //                                                                  //
0003 //  Analyzer for making mini-ntuple for L1 track performance plots  //
0004 //                                                                  //
0005 //////////////////////////////////////////////////////////////////////
0006 
0007 ////////////////////
0008 // FRAMEWORK HEADERS
0009 #include "FWCore/PluginManager/interface/ModuleDef.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/ServiceRegistry/interface/Service.h"
0018 
0019 ///////////////////////
0020 // DATA FORMATS HEADERS
0021 #include "DataFormats/Common/interface/Handle.h"
0022 #include "DataFormats/Common/interface/Ref.h"
0023 
0024 #include "DataFormats/L1TrackTrigger/interface/TTTypes.h"
0025 #include "DataFormats/L1TrackTrigger/interface/TTCluster.h"
0026 #include "DataFormats/L1TrackTrigger/interface/TTStub.h"
0027 #include "DataFormats/L1TrackTrigger/interface/TTTrack.h"
0028 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0029 #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertex.h"
0030 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0031 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0032 #include "SimTracker/TrackTriggerAssociation/interface/TTClusterAssociationMap.h"
0033 #include "SimTracker/TrackTriggerAssociation/interface/TTStubAssociationMap.h"
0034 #include "SimTracker/TrackTriggerAssociation/interface/TTTrackAssociationMap.h"
0035 #include "Geometry/Records/interface/StackedTrackerGeometryRecord.h"
0036 
0037 #include "DataFormats/JetReco/interface/GenJetCollection.h"
0038 #include "DataFormats/JetReco/interface/GenJet.h"
0039 
0040 ////////////////////////////
0041 // DETECTOR GEOMETRY HEADERS
0042 #include "MagneticField/Engine/interface/MagneticField.h"
0043 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0044 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0045 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0046 #include "Geometry/TrackerGeometryBuilder/interface/RectangularPixelTopology.h"
0047 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
0048 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0049 
0050 #include "Geometry/CommonTopologies/interface/PixelGeomDetUnit.h"
0051 #include "Geometry/CommonTopologies/interface/PixelGeomDetType.h"
0052 #include "Geometry/TrackerGeometryBuilder/interface/PixelTopologyBuilder.h"
0053 #include "Geometry/Records/interface/StackedTrackerGeometryRecord.h"
0054 
0055 ////////////////
0056 // PHYSICS TOOLS
0057 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0058 
0059 //My additions
0060 #include "DataFormats/L1TCorrelator/interface/TkJet.h"
0061 #include "DataFormats/L1TCorrelator/interface/TkJetFwd.h"
0062 #include "DataFormats/L1Trigger/interface/TkJetWord.h"
0063 #include "DataFormats/Math/interface/LorentzVector.h"
0064 #include "DataFormats/L1TCorrelator/interface/TkEtMiss.h"
0065 #include "DataFormats/L1TCorrelator/interface/TkEtMissFwd.h"
0066 #include "DataFormats/L1TCorrelator/interface/TkHTMiss.h"
0067 #include "DataFormats/L1TCorrelator/interface/TkHTMissFwd.h"
0068 #include "DataFormats/L1Trigger/interface/Vertex.h"
0069 #include "DataFormats/L1Trigger/interface/VertexWord.h"
0070 #include "DataFormats/L1Trigger/interface/EtSum.h"
0071 #include "L1Trigger/L1TTrackMatch/interface/L1TkEtMissEmuAlgo.h"
0072 #include "L1Trigger/L1TTrackMatch/interface/L1TkHTMissEmulatorProducer.h"
0073 
0074 ///////////////
0075 // ROOT HEADERS
0076 #include <TROOT.h>
0077 #include <TCanvas.h>
0078 #include <TTree.h>
0079 #include <TFile.h>
0080 #include <TF1.h>
0081 #include <TH2F.h>
0082 #include <TH1F.h>
0083 #include <TLorentzVector.h>
0084 
0085 //////////////
0086 // STD HEADERS
0087 #include <memory>
0088 #include <string>
0089 #include <iostream>
0090 
0091 //////////////
0092 // NAMESPACES
0093 using namespace std;
0094 using namespace edm;
0095 
0096 //////////////////////////////
0097 //                          //
0098 //     CLASS DEFINITION     //
0099 //                          //
0100 //////////////////////////////
0101 
0102 class L1TrackObjectNtupleMaker : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0103 private:
0104   // ----------constants, enums and typedefs ---------
0105   typedef TTTrack<Ref_Phase2TrackerDigi_> L1Track;
0106   typedef edm::Ptr<L1Track> L1TrackPtr;
0107   typedef std::vector<L1TrackPtr> L1TrackPtrCollection;
0108   typedef std::vector<L1Track> L1TrackCollection;
0109   typedef edm::Ref<L1TrackCollection> L1TrackRef;
0110   typedef edm::RefVector<L1TrackCollection> L1TrackRefCollection;
0111 
0112 public:
0113   // Constructor/destructor
0114   explicit L1TrackObjectNtupleMaker(const edm::ParameterSet& iConfig);
0115   ~L1TrackObjectNtupleMaker() override;
0116 
0117   // Mandatory methods
0118   void beginJob() override;
0119   void endJob() override;
0120   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0121 
0122   // Other member functions
0123   int getSelectedTrackIndex(const L1TrackRef& trackRef,
0124                             const edm::Handle<L1TrackRefCollection>& selectedTrackRefs) const;
0125 
0126 private:
0127   //-----------------------------------------------------------------------------------------------
0128   // Containers of parameters passed by python configuration file
0129   edm::ParameterSet config;
0130 
0131   int MyProcess;       // 11/13/211 for single electrons/muons/pions, 6/15 for pions from ttbar/taus, 1 for inclusive
0132   bool DebugMode;      // lots of debug printout statements
0133   bool SaveAllTracks;  // store in ntuples not only truth-matched tracks but ALL tracks
0134   bool SaveStubs;      // option to save also stubs in the ntuples (makes them large...)
0135   string Displaced;    // "Prompt", "Displaced", "Both"
0136   int TP_minNStub;  // require TPs to have >= minNStub (defining efficiency denominator) (==0 means to only require >= 1 cluster)
0137   int TP_minNStubLayer;  // require TPs to have stubs in >= minNStubLayer layers/disks (defining efficiency denominator)
0138   double TP_minPt;       // save TPs with pt > minPt
0139   double TP_maxEta;      // save TPs with |eta| < maxEta
0140   double TP_maxZ0;       // save TPs with |z0| < maxZ0
0141   int L1Tk_minNStub;     // require L1 tracks to have >= minNStub (this is mostly for tracklet purposes)
0142   bool SaveTrackJets;
0143   bool SaveTrackSums;
0144 
0145   edm::InputTag L1TrackInputTag;                           // L1 track collection
0146   edm::InputTag MCTruthTrackInputTag;                      // MC truth collection
0147   edm::InputTag L1TrackGTTInputTag;                        // L1 track collection
0148   edm::InputTag L1TrackSelectedInputTag;                   // L1 track collection
0149   edm::InputTag L1TrackSelectedEmulationInputTag;          // L1 track collection
0150   edm::InputTag L1TrackExtendedInputTag;                   // L1 track collection
0151   edm::InputTag MCTruthTrackExtendedInputTag;              // MC truth collection
0152   edm::InputTag L1TrackExtendedGTTInputTag;                // L1 track collection
0153   edm::InputTag L1TrackExtendedSelectedInputTag;           // L1 track collection
0154   edm::InputTag L1TrackExtendedSelectedEmulationInputTag;  // L1 track collection
0155   edm::InputTag MCTruthClusterInputTag;
0156   edm::InputTag L1StubInputTag;
0157   edm::InputTag MCTruthStubInputTag;
0158   edm::InputTag TrackingParticleInputTag;
0159   edm::InputTag TrackingVertexInputTag;
0160   edm::InputTag GenJetInputTag;
0161   edm::InputTag RecoVertexInputTag;
0162   edm::InputTag RecoVertexEmuInputTag;
0163   edm::InputTag GenParticleInputTag;
0164 
0165   edm::InputTag TrackFastJetsInputTag;
0166   edm::InputTag TrackJetsInputTag;
0167   edm::InputTag TrackJetsEmuInputTag;
0168   edm::InputTag TrackMETInputTag;
0169   edm::InputTag TrackMETEmuInputTag;
0170   edm::InputTag TrackMHTInputTag;
0171   edm::InputTag TrackMHTEmuInputTag;
0172 
0173   edm::InputTag TrackFastJetsExtendedInputTag;
0174   edm::InputTag TrackJetsExtendedInputTag;
0175   edm::InputTag TrackJetsExtendedEmuInputTag;
0176   edm::InputTag TrackMETExtendedInputTag;
0177   //edm::InputTag TrackMETEmuExtendedInputTag;
0178   edm::InputTag TrackMHTExtendedInputTag;
0179   edm::InputTag TrackMHTEmuExtendedInputTag;
0180 
0181   edm::EDGetTokenT<edmNew::DetSetVector<TTCluster<Ref_Phase2TrackerDigi_>>> ttClusterToken_;
0182   edm::EDGetTokenT<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>> ttStubToken_;
0183   edm::EDGetTokenT<TTClusterAssociationMap<Ref_Phase2TrackerDigi_>> ttClusterMCTruthToken_;
0184   edm::EDGetTokenT<TTStubAssociationMap<Ref_Phase2TrackerDigi_>> ttStubMCTruthToken_;
0185 
0186   edm::EDGetTokenT<L1TrackCollection> ttTrackToken_;
0187   edm::EDGetTokenT<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>> ttTrackMCTruthToken_;
0188   edm::EDGetTokenT<L1TrackCollection> ttTrackGTTToken_;
0189   edm::EDGetTokenT<L1TrackRefCollection> ttTrackSelectedToken_;
0190   edm::EDGetTokenT<L1TrackRefCollection> ttTrackSelectedEmulationToken_;
0191   edm::EDGetTokenT<L1TrackCollection> ttTrackExtendedToken_;
0192   edm::EDGetTokenT<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>> ttTrackMCTruthExtendedToken_;
0193   edm::EDGetTokenT<L1TrackCollection> ttTrackExtendedGTTToken_;
0194   edm::EDGetTokenT<L1TrackRefCollection> ttTrackExtendedSelectedToken_;
0195   edm::EDGetTokenT<L1TrackRefCollection> ttTrackExtendedSelectedEmulationToken_;
0196 
0197   edm::EDGetTokenT<std::vector<TrackingParticle>> TrackingParticleToken_;
0198   edm::EDGetTokenT<std::vector<TrackingVertex>> TrackingVertexToken_;
0199   edm::EDGetTokenT<std::vector<reco::GenJet>> GenJetToken_;
0200   edm::EDGetTokenT<std::vector<reco::GenParticle>> GenParticleToken_;
0201   edm::EDGetTokenT<l1t::VertexCollection> L1VertexToken_;
0202   edm::EDGetTokenT<l1t::VertexWordCollection> L1VertexEmuToken_;
0203 
0204   edm::EDGetTokenT<std::vector<l1t::TkJet>> TrackFastJetsToken_;
0205   edm::EDGetTokenT<std::vector<l1t::TkJet>> TrackFastJetsExtendedToken_;
0206   edm::EDGetTokenT<std::vector<l1t::TkEtMiss>> TrackMETToken_;
0207   edm::EDGetTokenT<std::vector<l1t::TkEtMiss>> TrackMETExtendedToken_;
0208   edm::EDGetTokenT<std::vector<l1t::EtSum>> TrackMETEmuToken_;
0209   //edm::EDGetTokenT<std::vector<l1t::TkEtMiss>> TrackMETEmuExtendedToken_;
0210   edm::EDGetTokenT<l1t::TkHTMissCollection> TrackMHTToken_;
0211   edm::EDGetTokenT<l1t::TkHTMissCollection> TrackMHTExtendedToken_;
0212   edm::EDGetTokenT<std::vector<l1t::EtSum>> TrackMHTEmuToken_;
0213   edm::EDGetTokenT<std::vector<l1t::EtSum>> TrackMHTEmuExtendedToken_;
0214   edm::EDGetTokenT<l1t::TkJetCollection> TrackJetsToken_;
0215   edm::EDGetTokenT<l1t::TkJetCollection> TrackJetsExtendedToken_;
0216   edm::EDGetTokenT<l1t::TkJetWordCollection> TrackJetsEmuToken_;
0217   edm::EDGetTokenT<l1t::TkJetWordCollection> TrackJetsExtendedEmuToken_;
0218 
0219   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
0220   edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> tGeomToken_;
0221 
0222   //-----------------------------------------------------------------------------------------------
0223   // tree & branches for mini-ntuple
0224   bool available_;  // ROOT file for histograms is open.
0225   TTree* eventTree;
0226 
0227   // primary vertex
0228   std::vector<float>* m_pv_L1reco;
0229   std::vector<float>* m_pv_L1reco_sum;
0230   std::vector<float>* m_pv_L1reco_emu;
0231   std::vector<float>* m_pv_L1reco_sum_emu;
0232   std::vector<float>* m_pv_MC;
0233   std::vector<int>* m_MC_lep;
0234 
0235   //gen particles
0236   std::vector<float>* m_gen_pt;
0237   std::vector<float>* m_gen_phi;
0238   std::vector<float>* m_gen_pdgid;
0239   std::vector<float>* m_gen_z0;
0240 
0241   // all L1 tracks (prompt)
0242   std::vector<float>* m_trk_pt;
0243   std::vector<float>* m_trk_eta;
0244   std::vector<float>* m_trk_phi;
0245   std::vector<float>* m_trk_phi_local;
0246   std::vector<float>* m_trk_d0;  // (filled if nFitPar==5, else 999)
0247   std::vector<float>* m_trk_z0;
0248   std::vector<float>* m_trk_chi2;
0249   std::vector<float>* m_trk_chi2dof;
0250   std::vector<float>* m_trk_chi2rphi;
0251   std::vector<float>* m_trk_chi2rz;
0252   std::vector<float>* m_trk_bendchi2;
0253   std::vector<float>* m_trk_MVA1;
0254   std::vector<int>* m_trk_nstub;
0255   std::vector<int>* m_trk_lhits;
0256   std::vector<int>* m_trk_dhits;
0257   std::vector<int>* m_trk_seed;
0258   std::vector<int>* m_trk_hitpattern;
0259   std::vector<unsigned int>* m_trk_phiSector;
0260   std::vector<int>* m_trk_genuine;
0261   std::vector<int>* m_trk_loose;
0262   std::vector<int>* m_trk_unknown;
0263   std::vector<int>* m_trk_combinatoric;
0264   std::vector<int>* m_trk_fake;  //0 fake, 1 track from primary interaction, 2 secondary track
0265   std::vector<int>* m_trk_matchtp_pdgid;
0266   std::vector<float>* m_trk_matchtp_pt;
0267   std::vector<float>* m_trk_matchtp_eta;
0268   std::vector<float>* m_trk_matchtp_phi;
0269   std::vector<float>* m_trk_matchtp_z0;
0270   std::vector<float>* m_trk_matchtp_dxy;
0271   std::vector<float>* m_trk_gtt_pt;
0272   std::vector<float>* m_trk_gtt_eta;
0273   std::vector<float>* m_trk_gtt_phi;
0274   std::vector<int>* m_trk_selected_index;
0275   std::vector<int>* m_trk_selected_emulation_index;
0276 
0277   // all L1 tracks (extended)
0278   std::vector<float>* m_trkExt_pt;
0279   std::vector<float>* m_trkExt_eta;
0280   std::vector<float>* m_trkExt_phi;
0281   std::vector<float>* m_trkExt_phi_local;
0282   std::vector<float>* m_trkExt_d0;  // (filled if nFitPar==5, else 999)
0283   std::vector<float>* m_trkExt_z0;
0284   std::vector<float>* m_trkExt_chi2;
0285   std::vector<float>* m_trkExt_chi2dof;
0286   std::vector<float>* m_trkExt_chi2rphi;
0287   std::vector<float>* m_trkExt_chi2rz;
0288   std::vector<float>* m_trkExt_bendchi2;
0289   std::vector<float>* m_trkExt_MVA;
0290   std::vector<int>* m_trkExt_nstub;
0291   std::vector<int>* m_trkExt_lhits;
0292   std::vector<int>* m_trkExt_dhits;
0293   std::vector<int>* m_trkExt_seed;
0294   std::vector<int>* m_trkExt_hitpattern;
0295   std::vector<unsigned int>* m_trkExt_phiSector;
0296   std::vector<int>* m_trkExt_genuine;
0297   std::vector<int>* m_trkExt_loose;
0298   std::vector<int>* m_trkExt_unknown;
0299   std::vector<int>* m_trkExt_combinatoric;
0300   std::vector<int>* m_trkExt_fake;  //0 fake, 1 track from primary interaction, 2 secondary track
0301   std::vector<int>* m_trkExt_matchtp_pdgid;
0302   std::vector<float>* m_trkExt_matchtp_pt;
0303   std::vector<float>* m_trkExt_matchtp_eta;
0304   std::vector<float>* m_trkExt_matchtp_phi;
0305   std::vector<float>* m_trkExt_matchtp_z0;
0306   std::vector<float>* m_trkExt_matchtp_dxy;
0307   std::vector<float>* m_trkExt_gtt_pt;
0308   std::vector<float>* m_trkExt_gtt_eta;
0309   std::vector<float>* m_trkExt_gtt_phi;
0310   std::vector<int>* m_trkExt_selected_index;
0311   std::vector<int>* m_trkExt_selected_emulation_index;
0312 
0313   // all tracking particles
0314   std::vector<float>* m_tp_pt;
0315   std::vector<float>* m_tp_eta;
0316   std::vector<float>* m_tp_phi;
0317   std::vector<float>* m_tp_dxy;
0318   std::vector<float>* m_tp_d0;
0319   std::vector<float>* m_tp_z0;
0320   std::vector<float>* m_tp_d0_prod;
0321   std::vector<float>* m_tp_z0_prod;
0322   std::vector<int>* m_tp_pdgid;
0323   std::vector<int>* m_tp_nmatch;
0324   std::vector<int>* m_tp_nstub;
0325   std::vector<int>* m_tp_eventid;
0326   std::vector<int>* m_tp_charge;
0327 
0328   // *L1 track* properties if m_tp_nmatch > 0 (prompt)
0329   std::vector<float>* m_matchtrk_pt;
0330   std::vector<float>* m_matchtrk_eta;
0331   std::vector<float>* m_matchtrk_phi;
0332   std::vector<float>* m_matchtrk_d0;  //this variable is only filled if nFitPar==5
0333   std::vector<float>* m_matchtrk_z0;
0334   std::vector<float>* m_matchtrk_chi2;
0335   std::vector<float>* m_matchtrk_chi2dof;
0336   std::vector<float>* m_matchtrk_chi2rphi;
0337   std::vector<float>* m_matchtrk_chi2rz;
0338   std::vector<float>* m_matchtrk_bendchi2;
0339   std::vector<float>* m_matchtrk_MVA1;
0340   std::vector<int>* m_matchtrk_nstub;
0341   std::vector<int>* m_matchtrk_lhits;
0342   std::vector<int>* m_matchtrk_dhits;
0343   std::vector<int>* m_matchtrk_seed;
0344   std::vector<int>* m_matchtrk_hitpattern;
0345 
0346   // *L1 track* properties if m_tp_nmatch > 0 (extended)
0347   std::vector<float>* m_matchtrkExt_pt;
0348   std::vector<float>* m_matchtrkExt_eta;
0349   std::vector<float>* m_matchtrkExt_phi;
0350   std::vector<float>* m_matchtrkExt_d0;  //this variable is only filled if nFitPar==5
0351   std::vector<float>* m_matchtrkExt_z0;
0352   std::vector<float>* m_matchtrkExt_chi2;
0353   std::vector<float>* m_matchtrkExt_chi2dof;
0354   std::vector<float>* m_matchtrkExt_chi2rphi;
0355   std::vector<float>* m_matchtrkExt_chi2rz;
0356   std::vector<float>* m_matchtrkExt_bendchi2;
0357   std::vector<float>* m_matchtrkExt_MVA;
0358   std::vector<int>* m_matchtrkExt_nstub;
0359   std::vector<int>* m_matchtrkExt_lhits;
0360   std::vector<int>* m_matchtrkExt_dhits;
0361   std::vector<int>* m_matchtrkExt_seed;
0362   std::vector<int>* m_matchtrkExt_hitpattern;
0363 
0364   // ALL stubs
0365   std::vector<float>* m_allstub_x;
0366   std::vector<float>* m_allstub_y;
0367   std::vector<float>* m_allstub_z;
0368   std::vector<int>* m_allstub_isBarrel;  // stub is in barrel (1) or in disk (0)
0369   std::vector<int>* m_allstub_layer;
0370   std::vector<int>* m_allstub_isPSmodule;
0371   std::vector<float>* m_allstub_trigDisplace;
0372   std::vector<float>* m_allstub_trigOffset;
0373   std::vector<float>* m_allstub_trigPos;
0374   std::vector<float>* m_allstub_trigBend;
0375 
0376   // stub associated with tracking particle ?
0377   std::vector<int>* m_allstub_matchTP_pdgid;  // -999 if not matched
0378   std::vector<float>* m_allstub_matchTP_pt;   // -999 if not matched
0379   std::vector<float>* m_allstub_matchTP_eta;  // -999 if not matched
0380   std::vector<float>* m_allstub_matchTP_phi;  // -999 if not matched
0381   std::vector<int>* m_allstub_genuine;
0382 
0383   //prompt
0384   float trueMET = 0;
0385   float trueTkMET = 0;
0386   float trkMET = 0;
0387   float trkMETPhi = 0;
0388   float trkMHT = 0;
0389   float trkHT = 0;
0390   float trkMHTEmu = 0;
0391   float trkMHTEmuPhi = 0;
0392   float trkHTEmu = 0;
0393   float trkMETEmu = 0;
0394   float trkMETEmuPhi = 0;
0395 
0396   //displaced
0397   float trkMETExt = 0;
0398   float trkMETPhiExt = 0;
0399   float trkMHTExt = 0;
0400   float trkHTExt = 0;
0401   float trkMHTEmuExt = 0;
0402   float trkMHTEmuPhiExt = 0;
0403   float trkHTEmuExt = 0;
0404 
0405   //fast track jet
0406   std::vector<float>* m_trkfastjet_vz;
0407   std::vector<float>* m_trkfastjet_p;
0408   std::vector<float>* m_trkfastjet_phi;
0409   std::vector<float>* m_trkfastjet_eta;
0410   std::vector<float>* m_trkfastjet_pt;
0411   std::vector<int>* m_trkfastjet_ntracks;
0412   std::vector<float>* m_trkfastjet_tp_sumpt;
0413   std::vector<float>* m_trkfastjet_truetp_sumpt;
0414 
0415   std::vector<float>* m_trkfastjetExt_vz;
0416   std::vector<float>* m_trkfastjetExt_p;
0417   std::vector<float>* m_trkfastjetExt_phi;
0418   std::vector<float>* m_trkfastjetExt_eta;
0419   std::vector<float>* m_trkfastjetExt_pt;
0420   std::vector<int>* m_trkfastjetExt_ntracks;
0421   std::vector<float>* m_trkfastjetExt_tp_sumpt;
0422   std::vector<float>* m_trkfastjetExt_truetp_sumpt;
0423 
0424   std::vector<float>* m_trkjet_vz;
0425   std::vector<float>* m_trkjet_p;
0426   std::vector<float>* m_trkjet_phi;
0427   std::vector<float>* m_trkjet_eta;
0428   std::vector<float>* m_trkjet_pt;
0429   std::vector<int>* m_trkjet_ntracks;
0430   std::vector<int>* m_trkjet_nDisplaced;
0431   std::vector<int>* m_trkjet_nTight;
0432   std::vector<int>* m_trkjet_nTightDisplaced;
0433   std::vector<int>* m_trkjet_ntdtrk;
0434 
0435   std::vector<float>* m_trkjetem_pt;
0436   std::vector<float>* m_trkjetem_phi;
0437   std::vector<float>* m_trkjetem_eta;
0438   std::vector<float>* m_trkjetem_z;
0439   std::vector<int>* m_trkjetem_ntracks;
0440   std::vector<int>* m_trkjetem_nxtracks;
0441 
0442   std::vector<float>* m_trkjetExt_vz;
0443   std::vector<float>* m_trkjetExt_p;
0444   std::vector<float>* m_trkjetExt_phi;
0445   std::vector<float>* m_trkjetExt_eta;
0446   std::vector<float>* m_trkjetExt_pt;
0447   std::vector<int>* m_trkjetExt_ntracks;
0448   std::vector<int>* m_trkjetExt_nDisplaced;
0449   std::vector<int>* m_trkjetExt_nTight;
0450   std::vector<int>* m_trkjetExt_nTightDisplaced;
0451   std::vector<int>* m_trkjetExt_ntdtrk;
0452 
0453   std::vector<float>* m_trkjetemExt_pt;
0454   std::vector<float>* m_trkjetemExt_phi;
0455   std::vector<float>* m_trkjetemExt_eta;
0456   std::vector<float>* m_trkjetemExt_z;
0457   std::vector<int>* m_trkjetemExt_ntracks;
0458   std::vector<int>* m_trkjetemExt_nxtracks;
0459 };
0460 
0461 //////////////////////////////////
0462 //                              //
0463 //     CLASS IMPLEMENTATION     //
0464 //                              //
0465 //////////////////////////////////
0466 
0467 //////////////
0468 // CONSTRUCTOR
0469 L1TrackObjectNtupleMaker::L1TrackObjectNtupleMaker(edm::ParameterSet const& iConfig) : config(iConfig) {
0470   MyProcess = iConfig.getParameter<int>("MyProcess");
0471   DebugMode = iConfig.getParameter<bool>("DebugMode");
0472   SaveAllTracks = iConfig.getParameter<bool>("SaveAllTracks");
0473   SaveStubs = iConfig.getParameter<bool>("SaveStubs");
0474   Displaced = iConfig.getParameter<string>("Displaced");
0475   TP_minNStub = iConfig.getParameter<int>("TP_minNStub");
0476   TP_minNStubLayer = iConfig.getParameter<int>("TP_minNStubLayer");
0477   TP_minPt = iConfig.getParameter<double>("TP_minPt");
0478   TP_maxEta = iConfig.getParameter<double>("TP_maxEta");
0479   TP_maxZ0 = iConfig.getParameter<double>("TP_maxZ0");
0480   L1Tk_minNStub = iConfig.getParameter<int>("L1Tk_minNStub");
0481 
0482   SaveTrackJets = iConfig.getParameter<bool>("SaveTrackJets");
0483   SaveTrackSums = iConfig.getParameter<bool>("SaveTrackSums");
0484 
0485   L1StubInputTag = iConfig.getParameter<edm::InputTag>("L1StubInputTag");
0486   MCTruthClusterInputTag = iConfig.getParameter<edm::InputTag>("MCTruthClusterInputTag");
0487   MCTruthStubInputTag = iConfig.getParameter<edm::InputTag>("MCTruthStubInputTag");
0488   TrackingParticleInputTag = iConfig.getParameter<edm::InputTag>("TrackingParticleInputTag");
0489   TrackingVertexInputTag = iConfig.getParameter<edm::InputTag>("TrackingVertexInputTag");
0490   GenJetInputTag = iConfig.getParameter<edm::InputTag>("GenJetInputTag");
0491   RecoVertexInputTag = iConfig.getParameter<InputTag>("RecoVertexInputTag");
0492   RecoVertexEmuInputTag = iConfig.getParameter<InputTag>("RecoVertexEmuInputTag");
0493   GenParticleInputTag = iConfig.getParameter<InputTag>("GenParticleInputTag");
0494 
0495   if (Displaced == "Prompt" || Displaced == "Both") {
0496     L1TrackInputTag = iConfig.getParameter<edm::InputTag>("L1TrackInputTag");
0497     MCTruthTrackInputTag = iConfig.getParameter<edm::InputTag>("MCTruthTrackInputTag");
0498     L1TrackGTTInputTag = iConfig.getParameter<edm::InputTag>("L1TrackGTTInputTag");
0499     L1TrackSelectedInputTag = iConfig.getParameter<edm::InputTag>("L1TrackSelectedInputTag");
0500     L1TrackSelectedEmulationInputTag = iConfig.getParameter<edm::InputTag>("L1TrackSelectedEmulationInputTag");
0501     TrackFastJetsInputTag = iConfig.getParameter<InputTag>("TrackFastJetsInputTag");
0502     TrackJetsInputTag = iConfig.getParameter<InputTag>("TrackJetsInputTag");
0503     TrackJetsEmuInputTag = iConfig.getParameter<InputTag>("TrackJetsEmuInputTag");
0504     TrackMETInputTag = iConfig.getParameter<InputTag>("TrackMETInputTag");
0505     TrackMETEmuInputTag = iConfig.getParameter<InputTag>("TrackMETEmuInputTag");
0506     TrackMHTInputTag = iConfig.getParameter<InputTag>("TrackMHTInputTag");
0507     TrackMHTEmuInputTag = iConfig.getParameter<InputTag>("TrackMHTEmuInputTag");
0508 
0509     ttTrackToken_ = consumes<L1TrackCollection>(L1TrackInputTag);
0510     ttTrackMCTruthToken_ = consumes<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>>(MCTruthTrackInputTag);
0511     ttTrackGTTToken_ = consumes<L1TrackCollection>(L1TrackGTTInputTag);
0512     ttTrackSelectedToken_ = consumes<L1TrackRefCollection>(L1TrackSelectedInputTag);
0513     ttTrackSelectedEmulationToken_ = consumes<L1TrackRefCollection>(L1TrackSelectedEmulationInputTag);
0514     TrackFastJetsToken_ = consumes<std::vector<l1t::TkJet>>(TrackFastJetsInputTag);
0515     TrackJetsToken_ = consumes<l1t::TkJetCollection>(TrackJetsInputTag);
0516     TrackJetsEmuToken_ = consumes<l1t::TkJetWordCollection>(TrackJetsEmuInputTag);
0517     TrackMETToken_ = consumes<std::vector<l1t::TkEtMiss>>(TrackMETInputTag);
0518     TrackMETEmuToken_ = consumes<std::vector<l1t::EtSum>>(TrackMETEmuInputTag);
0519     TrackMHTToken_ = consumes<l1t::TkHTMissCollection>(TrackMHTInputTag);
0520     TrackMHTEmuToken_ = consumes<std::vector<l1t::EtSum>>(TrackMHTEmuInputTag);
0521   }
0522 
0523   if (Displaced == "Displaced" || Displaced == "Both") {
0524     L1TrackExtendedInputTag = iConfig.getParameter<edm::InputTag>("L1TrackExtendedInputTag");
0525     MCTruthTrackExtendedInputTag = iConfig.getParameter<edm::InputTag>("MCTruthTrackExtendedInputTag");
0526     L1TrackExtendedGTTInputTag = iConfig.getParameter<edm::InputTag>("L1TrackExtendedGTTInputTag");
0527     L1TrackExtendedSelectedInputTag = iConfig.getParameter<edm::InputTag>("L1TrackExtendedSelectedInputTag");
0528     L1TrackExtendedSelectedEmulationInputTag =
0529         iConfig.getParameter<edm::InputTag>("L1TrackExtendedSelectedEmulationInputTag");
0530     TrackFastJetsExtendedInputTag = iConfig.getParameter<InputTag>("TrackFastJetsExtendedInputTag");
0531     TrackJetsExtendedInputTag = iConfig.getParameter<InputTag>("TrackJetsExtendedInputTag");
0532     TrackJetsExtendedEmuInputTag = iConfig.getParameter<InputTag>("TrackJetsExtendedEmuInputTag");
0533     TrackMETExtendedInputTag = iConfig.getParameter<InputTag>("TrackMETExtendedInputTag");
0534     TrackMHTExtendedInputTag = iConfig.getParameter<InputTag>("TrackMHTExtendedInputTag");
0535     TrackMHTEmuInputTag = iConfig.getParameter<InputTag>("TrackMHTEmuInputTag");
0536     TrackMHTEmuExtendedInputTag = iConfig.getParameter<InputTag>("TrackMHTEmuExtendedInputTag");
0537 
0538     ttTrackExtendedToken_ = consumes<L1TrackCollection>(L1TrackExtendedInputTag);
0539     ttTrackMCTruthExtendedToken_ =
0540         consumes<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>>(MCTruthTrackExtendedInputTag);
0541     ttTrackExtendedGTTToken_ = consumes<L1TrackCollection>(L1TrackExtendedGTTInputTag);
0542     ttTrackExtendedSelectedToken_ = consumes<L1TrackRefCollection>(L1TrackExtendedSelectedInputTag);
0543     ttTrackExtendedSelectedEmulationToken_ = consumes<L1TrackRefCollection>(L1TrackExtendedSelectedEmulationInputTag);
0544     TrackFastJetsExtendedToken_ = consumes<std::vector<l1t::TkJet>>(TrackFastJetsExtendedInputTag);
0545     TrackJetsExtendedToken_ = consumes<l1t::TkJetCollection>(TrackJetsExtendedInputTag);
0546     TrackJetsExtendedEmuToken_ = consumes<l1t::TkJetWordCollection>(TrackJetsExtendedEmuInputTag);
0547     TrackMETExtendedToken_ = consumes<std::vector<l1t::TkEtMiss>>(TrackMETExtendedInputTag);
0548     TrackMHTExtendedToken_ = consumes<l1t::TkHTMissCollection>(TrackMHTExtendedInputTag);
0549     TrackMHTEmuToken_ = consumes<std::vector<l1t::EtSum>>(TrackMHTEmuInputTag);
0550     TrackMHTEmuExtendedToken_ = consumes<std::vector<l1t::EtSum>>(TrackMHTEmuExtendedInputTag);
0551   }
0552 
0553   ttStubToken_ = consumes<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>>(L1StubInputTag);
0554   ttClusterMCTruthToken_ = consumes<TTClusterAssociationMap<Ref_Phase2TrackerDigi_>>(MCTruthClusterInputTag);
0555   ttStubMCTruthToken_ = consumes<TTStubAssociationMap<Ref_Phase2TrackerDigi_>>(MCTruthStubInputTag);
0556   TrackingParticleToken_ = consumes<std::vector<TrackingParticle>>(TrackingParticleInputTag);
0557   TrackingVertexToken_ = consumes<std::vector<TrackingVertex>>(TrackingVertexInputTag);
0558   GenJetToken_ = consumes<std::vector<reco::GenJet>>(GenJetInputTag);
0559   GenParticleToken_ = consumes<std::vector<reco::GenParticle>>(GenParticleInputTag);
0560   L1VertexToken_ = consumes<l1t::VertexCollection>(RecoVertexInputTag);
0561   L1VertexEmuToken_ = consumes<l1t::VertexWordCollection>(RecoVertexEmuInputTag);
0562   tTopoToken_ = esConsumes<TrackerTopology, TrackerTopologyRcd>(edm::ESInputTag("", ""));
0563   tGeomToken_ = esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>(edm::ESInputTag("", ""));
0564 
0565   usesResource(TFileService::kSharedResource);
0566 }
0567 
0568 /////////////
0569 // DESTRUCTOR
0570 L1TrackObjectNtupleMaker::~L1TrackObjectNtupleMaker() {}
0571 
0572 //////////
0573 // END JOB
0574 void L1TrackObjectNtupleMaker::endJob() {
0575   // things to be done at the exit of the event Loop
0576   //  edm::LogVerbatim("Tracklet") << "L1TrackObjectNtupleMaker::endJob";
0577 }
0578 
0579 ////////////
0580 // BEGIN JOB
0581 void L1TrackObjectNtupleMaker::beginJob() {
0582   // things to be done before entering the event Loop
0583   //  edm::LogVerbatim("Tracklet") << "L1TrackObjectNtupleMaker::beginJob";
0584 
0585   //-----------------------------------------------------------------------------------------------
0586   // book histograms / make ntuple
0587   edm::Service<TFileService> fs;
0588   available_ = fs.isAvailable();
0589   if (not available_)
0590     return;  // No ROOT file open.
0591 
0592   // initilize
0593   m_trk_pt = new std::vector<float>;
0594   m_trk_eta = new std::vector<float>;
0595   m_trk_phi = new std::vector<float>;
0596   m_trk_phi_local = new std::vector<float>;
0597   m_trk_z0 = new std::vector<float>;
0598   m_trk_d0 = new std::vector<float>;
0599   m_trk_chi2 = new std::vector<float>;
0600   m_trk_chi2dof = new std::vector<float>;
0601   m_trk_chi2rphi = new std::vector<float>;
0602   m_trk_chi2rz = new std::vector<float>;
0603   m_trk_bendchi2 = new std::vector<float>;
0604   m_trk_MVA1 = new std::vector<float>;
0605   m_trk_nstub = new std::vector<int>;
0606   m_trk_lhits = new std::vector<int>;
0607   m_trk_dhits = new std::vector<int>;
0608   m_trk_seed = new std::vector<int>;
0609   m_trk_hitpattern = new std::vector<int>;
0610   m_trk_phiSector = new std::vector<unsigned int>;
0611   m_trk_genuine = new std::vector<int>;
0612   m_trk_loose = new std::vector<int>;
0613   m_trk_unknown = new std::vector<int>;
0614   m_trk_combinatoric = new std::vector<int>;
0615   m_trk_fake = new std::vector<int>;
0616   m_trk_matchtp_pdgid = new std::vector<int>;
0617   m_trk_matchtp_pt = new std::vector<float>;
0618   m_trk_matchtp_eta = new std::vector<float>;
0619   m_trk_matchtp_phi = new std::vector<float>;
0620   m_trk_matchtp_z0 = new std::vector<float>;
0621   m_trk_matchtp_dxy = new std::vector<float>;
0622   m_trk_gtt_pt = new std::vector<float>;
0623   m_trk_gtt_eta = new std::vector<float>;
0624   m_trk_gtt_phi = new std::vector<float>;
0625   m_trk_selected_index = new std::vector<int>;
0626   m_trk_selected_emulation_index = new std::vector<int>;
0627 
0628   m_trkExt_pt = new std::vector<float>;
0629   m_trkExt_eta = new std::vector<float>;
0630   m_trkExt_phi = new std::vector<float>;
0631   m_trkExt_phi_local = new std::vector<float>;
0632   m_trkExt_z0 = new std::vector<float>;
0633   m_trkExt_d0 = new std::vector<float>;
0634   m_trkExt_chi2 = new std::vector<float>;
0635   m_trkExt_chi2dof = new std::vector<float>;
0636   m_trkExt_chi2rphi = new std::vector<float>;
0637   m_trkExt_chi2rz = new std::vector<float>;
0638   m_trkExt_bendchi2 = new std::vector<float>;
0639   m_trkExt_MVA = new std::vector<float>;
0640   m_trkExt_nstub = new std::vector<int>;
0641   m_trkExt_lhits = new std::vector<int>;
0642   m_trkExt_dhits = new std::vector<int>;
0643   m_trkExt_seed = new std::vector<int>;
0644   m_trkExt_hitpattern = new std::vector<int>;
0645   m_trkExt_phiSector = new std::vector<unsigned int>;
0646   m_trkExt_genuine = new std::vector<int>;
0647   m_trkExt_loose = new std::vector<int>;
0648   m_trkExt_unknown = new std::vector<int>;
0649   m_trkExt_combinatoric = new std::vector<int>;
0650   m_trkExt_fake = new std::vector<int>;
0651   m_trkExt_matchtp_pdgid = new std::vector<int>;
0652   m_trkExt_matchtp_pt = new std::vector<float>;
0653   m_trkExt_matchtp_eta = new std::vector<float>;
0654   m_trkExt_matchtp_phi = new std::vector<float>;
0655   m_trkExt_matchtp_z0 = new std::vector<float>;
0656   m_trkExt_matchtp_dxy = new std::vector<float>;
0657   m_trkExt_gtt_pt = new std::vector<float>;
0658   m_trkExt_gtt_eta = new std::vector<float>;
0659   m_trkExt_gtt_phi = new std::vector<float>;
0660   m_trkExt_selected_index = new std::vector<int>;
0661   m_trkExt_selected_emulation_index = new std::vector<int>;
0662 
0663   m_tp_pt = new std::vector<float>;
0664   m_tp_eta = new std::vector<float>;
0665   m_tp_phi = new std::vector<float>;
0666   m_tp_dxy = new std::vector<float>;
0667   m_tp_d0 = new std::vector<float>;
0668   m_tp_z0 = new std::vector<float>;
0669   m_tp_d0_prod = new std::vector<float>;
0670   m_tp_z0_prod = new std::vector<float>;
0671   m_tp_pdgid = new std::vector<int>;
0672   m_tp_nmatch = new std::vector<int>;
0673   m_tp_nstub = new std::vector<int>;
0674   m_tp_eventid = new std::vector<int>;
0675   m_tp_charge = new std::vector<int>;
0676 
0677   m_gen_pt = new std::vector<float>;
0678   m_gen_phi = new std::vector<float>;
0679   m_gen_pdgid = new std::vector<float>;
0680   m_gen_z0 = new std::vector<float>;
0681 
0682   m_matchtrk_pt = new std::vector<float>;
0683   m_matchtrk_eta = new std::vector<float>;
0684   m_matchtrk_phi = new std::vector<float>;
0685   m_matchtrk_z0 = new std::vector<float>;
0686   m_matchtrk_d0 = new std::vector<float>;
0687   m_matchtrk_chi2 = new std::vector<float>;
0688   m_matchtrk_chi2dof = new std::vector<float>;
0689   m_matchtrk_chi2rphi = new std::vector<float>;
0690   m_matchtrk_chi2rz = new std::vector<float>;
0691   m_matchtrk_bendchi2 = new std::vector<float>;
0692   m_matchtrk_MVA1 = new std::vector<float>;
0693   m_matchtrk_nstub = new std::vector<int>;
0694   m_matchtrk_dhits = new std::vector<int>;
0695   m_matchtrk_lhits = new std::vector<int>;
0696   m_matchtrk_seed = new std::vector<int>;
0697   m_matchtrk_hitpattern = new std::vector<int>;
0698 
0699   m_matchtrkExt_pt = new std::vector<float>;
0700   m_matchtrkExt_eta = new std::vector<float>;
0701   m_matchtrkExt_phi = new std::vector<float>;
0702   m_matchtrkExt_z0 = new std::vector<float>;
0703   m_matchtrkExt_d0 = new std::vector<float>;
0704   m_matchtrkExt_chi2 = new std::vector<float>;
0705   m_matchtrkExt_chi2dof = new std::vector<float>;
0706   m_matchtrkExt_chi2rphi = new std::vector<float>;
0707   m_matchtrkExt_chi2rz = new std::vector<float>;
0708   m_matchtrkExt_bendchi2 = new std::vector<float>;
0709   m_matchtrkExt_MVA = new std::vector<float>;
0710   m_matchtrkExt_nstub = new std::vector<int>;
0711   m_matchtrkExt_dhits = new std::vector<int>;
0712   m_matchtrkExt_lhits = new std::vector<int>;
0713   m_matchtrkExt_seed = new std::vector<int>;
0714   m_matchtrkExt_hitpattern = new std::vector<int>;
0715 
0716   m_allstub_x = new std::vector<float>;
0717   m_allstub_y = new std::vector<float>;
0718   m_allstub_z = new std::vector<float>;
0719   m_allstub_isBarrel = new std::vector<int>;
0720   m_allstub_layer = new std::vector<int>;
0721   m_allstub_isPSmodule = new std::vector<int>;
0722   m_allstub_trigDisplace = new std::vector<float>;
0723   m_allstub_trigOffset = new std::vector<float>;
0724   m_allstub_trigPos = new std::vector<float>;
0725   m_allstub_trigBend = new std::vector<float>;
0726   m_allstub_matchTP_pdgid = new std::vector<int>;
0727   m_allstub_matchTP_pt = new std::vector<float>;
0728   m_allstub_matchTP_eta = new std::vector<float>;
0729   m_allstub_matchTP_phi = new std::vector<float>;
0730   m_allstub_genuine = new std::vector<int>;
0731 
0732   m_pv_L1reco = new std::vector<float>;
0733   m_pv_L1reco_sum = new std::vector<float>;
0734   m_pv_L1reco_emu = new std::vector<float>;
0735   m_pv_L1reco_sum_emu = new std::vector<float>;
0736   m_pv_MC = new std::vector<float>;
0737   m_MC_lep = new std::vector<int>;
0738 
0739   m_trkjet_eta = new std::vector<float>;
0740   m_trkjet_vz = new std::vector<float>;
0741   m_trkjet_phi = new std::vector<float>;
0742   m_trkjet_p = new std::vector<float>;
0743   m_trkjet_pt = new std::vector<float>;
0744   m_trkjet_ntracks = new std::vector<int>;
0745   m_trkjet_nDisplaced = new std::vector<int>;
0746   m_trkjet_nTight = new std::vector<int>;
0747   m_trkjet_nTightDisplaced = new std::vector<int>;
0748   m_trkjet_ntdtrk = new std::vector<int>;
0749 
0750   m_trkjetem_pt = new std::vector<float>;
0751   m_trkjetem_phi = new std::vector<float>;
0752   m_trkjetem_eta = new std::vector<float>;
0753   m_trkjetem_z = new std::vector<float>;
0754   m_trkjetem_ntracks = new std::vector<int>;
0755   m_trkjetem_nxtracks = new std::vector<int>;
0756 
0757   m_trkfastjet_eta = new std::vector<float>;
0758   m_trkfastjet_vz = new std::vector<float>;
0759   m_trkfastjet_phi = new std::vector<float>;
0760   m_trkfastjet_p = new std::vector<float>;
0761   m_trkfastjet_pt = new std::vector<float>;
0762   m_trkfastjet_ntracks = new std::vector<int>;
0763   m_trkfastjet_tp_sumpt = new std::vector<float>;
0764   m_trkfastjet_truetp_sumpt = new std::vector<float>;
0765 
0766   m_trkjetExt_eta = new std::vector<float>;
0767   m_trkjetExt_vz = new std::vector<float>;
0768   m_trkjetExt_phi = new std::vector<float>;
0769   m_trkjetExt_p = new std::vector<float>;
0770   m_trkjetExt_pt = new std::vector<float>;
0771   m_trkjetExt_ntracks = new std::vector<int>;
0772   m_trkjetExt_nDisplaced = new std::vector<int>;
0773   m_trkjetExt_nTight = new std::vector<int>;
0774   m_trkjetExt_nTightDisplaced = new std::vector<int>;
0775   m_trkjetExt_ntdtrk = new std::vector<int>;
0776 
0777   m_trkjetemExt_pt = new std::vector<float>;
0778   m_trkjetemExt_phi = new std::vector<float>;
0779   m_trkjetemExt_eta = new std::vector<float>;
0780   m_trkjetemExt_z = new std::vector<float>;
0781   m_trkjetemExt_ntracks = new std::vector<int>;
0782   m_trkjetemExt_nxtracks = new std::vector<int>;
0783 
0784   m_trkfastjetExt_eta = new std::vector<float>;
0785   m_trkfastjetExt_vz = new std::vector<float>;
0786   m_trkfastjetExt_phi = new std::vector<float>;
0787   m_trkfastjetExt_p = new std::vector<float>;
0788   m_trkfastjetExt_pt = new std::vector<float>;
0789   m_trkfastjetExt_ntracks = new std::vector<int>;
0790   m_trkfastjetExt_tp_sumpt = new std::vector<float>;
0791   m_trkfastjetExt_truetp_sumpt = new std::vector<float>;
0792 
0793   // ntuple
0794   eventTree = fs->make<TTree>("eventTree", "Event tree");
0795   if (SaveAllTracks && (Displaced == "Prompt" || Displaced == "Both")) {
0796     eventTree->Branch("trk_pt", &m_trk_pt);
0797     eventTree->Branch("trk_eta", &m_trk_eta);
0798     eventTree->Branch("trk_phi", &m_trk_phi);
0799     eventTree->Branch("trk_phi_local", &m_trk_phi_local);
0800     eventTree->Branch("trk_d0", &m_trk_d0);
0801     eventTree->Branch("trk_z0", &m_trk_z0);
0802     eventTree->Branch("trk_chi2", &m_trk_chi2);
0803     eventTree->Branch("trk_chi2dof", &m_trk_chi2dof);
0804     eventTree->Branch("trk_chi2rphi", &m_trk_chi2rphi);
0805     eventTree->Branch("trk_chi2rz", &m_trk_chi2rz);
0806     eventTree->Branch("trk_bendchi2", &m_trk_bendchi2);
0807     eventTree->Branch("trk_MVA1", &m_trk_MVA1);
0808     eventTree->Branch("trk_nstub", &m_trk_nstub);
0809     eventTree->Branch("trk_lhits", &m_trk_lhits);
0810     eventTree->Branch("trk_dhits", &m_trk_dhits);
0811     eventTree->Branch("trk_seed", &m_trk_seed);
0812     eventTree->Branch("trk_hitpattern", &m_trk_hitpattern);
0813     eventTree->Branch("trk_phiSector", &m_trk_phiSector);
0814     eventTree->Branch("trk_genuine", &m_trk_genuine);
0815     eventTree->Branch("trk_loose", &m_trk_loose);
0816     eventTree->Branch("trk_unknown", &m_trk_unknown);
0817     eventTree->Branch("trk_combinatoric", &m_trk_combinatoric);
0818     eventTree->Branch("trk_fake", &m_trk_fake);
0819     eventTree->Branch("trk_matchtp_pdgid", &m_trk_matchtp_pdgid);
0820     eventTree->Branch("trk_matchtp_pt", &m_trk_matchtp_pt);
0821     eventTree->Branch("trk_matchtp_eta", &m_trk_matchtp_eta);
0822     eventTree->Branch("trk_matchtp_phi", &m_trk_matchtp_phi);
0823     eventTree->Branch("trk_matchtp_z0", &m_trk_matchtp_z0);
0824     eventTree->Branch("trk_matchtp_dxy", &m_trk_matchtp_dxy);
0825     eventTree->Branch("trk_gtt_pt", &m_trk_gtt_pt);
0826     eventTree->Branch("trk_gtt_eta", &m_trk_gtt_eta);
0827     eventTree->Branch("trk_gtt_phi", &m_trk_gtt_phi);
0828     eventTree->Branch("trk_gtt_selected_index", &m_trk_selected_index);
0829     eventTree->Branch("trk_gtt_selected_emulation_index", &m_trk_selected_emulation_index);
0830   }
0831 
0832   if (SaveAllTracks && (Displaced == "Displaced" || Displaced == "Both")) {
0833     eventTree->Branch("trkExt_pt", &m_trkExt_pt);
0834     eventTree->Branch("trkExt_eta", &m_trkExt_eta);
0835     eventTree->Branch("trkExt_phi", &m_trkExt_phi);
0836     eventTree->Branch("trkExt_phi_local", &m_trkExt_phi_local);
0837     eventTree->Branch("trkExt_d0", &m_trkExt_d0);
0838     eventTree->Branch("trkExt_z0", &m_trkExt_z0);
0839     eventTree->Branch("trkExt_chi2", &m_trkExt_chi2);
0840     eventTree->Branch("trkExt_chi2dof", &m_trkExt_chi2dof);
0841     eventTree->Branch("trkExt_chi2rphi", &m_trkExt_chi2rphi);
0842     eventTree->Branch("trkExt_chi2rz", &m_trkExt_chi2rz);
0843     eventTree->Branch("trkExt_bendchi2", &m_trkExt_bendchi2);
0844     eventTree->Branch("trkExt_MVA", &m_trkExt_MVA);
0845     eventTree->Branch("trkExt_nstub", &m_trkExt_nstub);
0846     eventTree->Branch("trkExt_lhits", &m_trkExt_lhits);
0847     eventTree->Branch("trkExt_dhits", &m_trkExt_dhits);
0848     eventTree->Branch("trkExt_seed", &m_trkExt_seed);
0849     eventTree->Branch("trkExt_hitpattern", &m_trkExt_hitpattern);
0850     eventTree->Branch("trkExt_phiSector", &m_trkExt_phiSector);
0851     eventTree->Branch("trkExt_genuine", &m_trkExt_genuine);
0852     eventTree->Branch("trkExt_loose", &m_trkExt_loose);
0853     eventTree->Branch("trkExt_unknown", &m_trkExt_unknown);
0854     eventTree->Branch("trkExt_combinatoric", &m_trkExt_combinatoric);
0855     eventTree->Branch("trkExt_fake", &m_trkExt_fake);
0856     eventTree->Branch("trkExt_matchtp_pdgid", &m_trkExt_matchtp_pdgid);
0857     eventTree->Branch("trkExt_matchtp_pt", &m_trkExt_matchtp_pt);
0858     eventTree->Branch("trkExt_matchtp_eta", &m_trkExt_matchtp_eta);
0859     eventTree->Branch("trkExt_matchtp_phi", &m_trkExt_matchtp_phi);
0860     eventTree->Branch("trkExt_matchtp_z0", &m_trkExt_matchtp_z0);
0861     eventTree->Branch("trkExt_matchtp_dxy", &m_trkExt_matchtp_dxy);
0862     eventTree->Branch("trkExt_gtt_pt", &m_trkExt_gtt_pt);
0863     eventTree->Branch("trkExt_gtt_eta", &m_trkExt_gtt_eta);
0864     eventTree->Branch("trkExt_gtt_phi", &m_trkExt_gtt_phi);
0865     eventTree->Branch("trkExt_gtt_selected_index", &m_trkExt_selected_index);
0866     eventTree->Branch("trkExt_gtt_selected_emulation_index", &m_trkExt_selected_emulation_index);
0867   }
0868   eventTree->Branch("tp_pt", &m_tp_pt);
0869   eventTree->Branch("tp_eta", &m_tp_eta);
0870   eventTree->Branch("tp_phi", &m_tp_phi);
0871   eventTree->Branch("tp_dxy", &m_tp_dxy);
0872   eventTree->Branch("tp_d0", &m_tp_d0);
0873   eventTree->Branch("tp_z0", &m_tp_z0);
0874   eventTree->Branch("tp_d0_prod", &m_tp_d0_prod);
0875   eventTree->Branch("tp_z0_prod", &m_tp_z0_prod);
0876   eventTree->Branch("tp_pdgid", &m_tp_pdgid);
0877   eventTree->Branch("tp_nmatch", &m_tp_nmatch);
0878   eventTree->Branch("tp_nstub", &m_tp_nstub);
0879   eventTree->Branch("tp_eventid", &m_tp_eventid);
0880   eventTree->Branch("tp_charge", &m_tp_charge);
0881 
0882   if (Displaced == "Prompt" || Displaced == "Both") {
0883     eventTree->Branch("matchtrk_pt", &m_matchtrk_pt);
0884     eventTree->Branch("matchtrk_eta", &m_matchtrk_eta);
0885     eventTree->Branch("matchtrk_phi", &m_matchtrk_phi);
0886     eventTree->Branch("matchtrk_z0", &m_matchtrk_z0);
0887     eventTree->Branch("matchtrk_d0", &m_matchtrk_d0);
0888     eventTree->Branch("matchtrk_chi2", &m_matchtrk_chi2);
0889     eventTree->Branch("matchtrk_chi2dof", &m_matchtrk_chi2dof);
0890     eventTree->Branch("matchtrk_chi2rphi", &m_matchtrk_chi2rphi);
0891     eventTree->Branch("matchtrk_chi2rz", &m_matchtrk_chi2rz);
0892     eventTree->Branch("matchtrk_bendchi2", &m_matchtrk_bendchi2);
0893     eventTree->Branch("matchtrk_MVA1", &m_matchtrk_MVA1);
0894     eventTree->Branch("matchtrk_nstub", &m_matchtrk_nstub);
0895     eventTree->Branch("matchtrk_lhits", &m_matchtrk_lhits);
0896     eventTree->Branch("matchtrk_dhits", &m_matchtrk_dhits);
0897     eventTree->Branch("matchtrk_seed", &m_matchtrk_seed);
0898     eventTree->Branch("matchtrk_hitpattern", &m_matchtrk_hitpattern);
0899   }
0900 
0901   if (Displaced == "Displaced" || Displaced == "Both") {
0902     eventTree->Branch("matchtrkExt_pt", &m_matchtrkExt_pt);
0903     eventTree->Branch("matchtrkExt_eta", &m_matchtrkExt_eta);
0904     eventTree->Branch("matchtrkExt_phi", &m_matchtrkExt_phi);
0905     eventTree->Branch("matchtrkExt_z0", &m_matchtrkExt_z0);
0906     eventTree->Branch("matchtrkExt_d0", &m_matchtrkExt_d0);
0907     eventTree->Branch("matchtrkExt_chi2", &m_matchtrkExt_chi2);
0908     eventTree->Branch("matchtrkExt_chi2dof", &m_matchtrkExt_chi2dof);
0909     eventTree->Branch("matchtrkExt_chi2rphi", &m_matchtrkExt_chi2rphi);
0910     eventTree->Branch("matchtrkExt_chi2rz", &m_matchtrkExt_chi2rz);
0911     eventTree->Branch("matchtrkExt_bendchi2", &m_matchtrkExt_bendchi2);
0912     eventTree->Branch("matchtrkExt_MVA", &m_matchtrkExt_MVA);
0913     eventTree->Branch("matchtrkExt_nstub", &m_matchtrkExt_nstub);
0914     eventTree->Branch("matchtrkExt_lhits", &m_matchtrkExt_lhits);
0915     eventTree->Branch("matchtrkExt_dhits", &m_matchtrkExt_dhits);
0916     eventTree->Branch("matchtrkExt_seed", &m_matchtrkExt_seed);
0917     eventTree->Branch("matchtrkExt_hitpattern", &m_matchtrkExt_hitpattern);
0918   }
0919 
0920   if (SaveStubs) {
0921     eventTree->Branch("allstub_x", &m_allstub_x);
0922     eventTree->Branch("allstub_y", &m_allstub_y);
0923     eventTree->Branch("allstub_z", &m_allstub_z);
0924     eventTree->Branch("allstub_isBarrel", &m_allstub_isBarrel);
0925     eventTree->Branch("allstub_layer", &m_allstub_layer);
0926     eventTree->Branch("allstub_isPSmodule", &m_allstub_isPSmodule);
0927     eventTree->Branch("allstub_trigDisplace", &m_allstub_trigDisplace);
0928     eventTree->Branch("allstub_trigOffset", &m_allstub_trigOffset);
0929     eventTree->Branch("allstub_trigPos", &m_allstub_trigPos);
0930     eventTree->Branch("allstub_trigBend", &m_allstub_trigBend);
0931     eventTree->Branch("allstub_matchTP_pdgid", &m_allstub_matchTP_pdgid);
0932     eventTree->Branch("allstub_matchTP_pt", &m_allstub_matchTP_pt);
0933     eventTree->Branch("allstub_matchTP_eta", &m_allstub_matchTP_eta);
0934     eventTree->Branch("allstub_matchTP_phi", &m_allstub_matchTP_phi);
0935     eventTree->Branch("allstub_genuine", &m_allstub_genuine);
0936   }
0937 
0938   eventTree->Branch("pv_L1reco", &m_pv_L1reco);
0939   eventTree->Branch("pv_L1reco_sum", &m_pv_L1reco_sum);
0940   eventTree->Branch("pv_L1reco_emu", &m_pv_L1reco_emu);
0941   eventTree->Branch("pv_L1reco_sum_emu", &m_pv_L1reco_sum_emu);
0942   eventTree->Branch("MC_lep", &m_MC_lep);
0943   eventTree->Branch("pv_MC", &m_pv_MC);
0944   eventTree->Branch("gen_pt", &m_gen_pt);
0945   eventTree->Branch("gen_phi", &m_gen_phi);
0946   eventTree->Branch("gen_pdgid", &m_gen_pdgid);
0947   eventTree->Branch("gen_z0", &m_gen_z0);
0948 
0949   if (SaveTrackJets) {
0950     if (Displaced == "Prompt" || Displaced == "Both") {
0951       eventTree->Branch("trkfastjet_eta", &m_trkfastjet_eta);
0952       eventTree->Branch("trkfastjet_vz", &m_trkfastjet_vz);
0953       eventTree->Branch("trkfastjet_p", &m_trkfastjet_p);
0954       eventTree->Branch("trkfastjet_pt", &m_trkfastjet_pt);
0955       eventTree->Branch("trkfastjet_phi", &m_trkfastjet_phi);
0956       eventTree->Branch("trkfastjet_ntracks", &m_trkfastjet_ntracks);
0957       eventTree->Branch("trkfastjet_truetp_sumpt", m_trkfastjet_truetp_sumpt);
0958 
0959       eventTree->Branch("trkjet_eta", &m_trkjet_eta);
0960       eventTree->Branch("trkjet_vz", &m_trkjet_vz);
0961       eventTree->Branch("trkjet_p", &m_trkjet_p);
0962       eventTree->Branch("trkjet_pt", &m_trkjet_pt);
0963       eventTree->Branch("trkjet_phi", &m_trkjet_phi);
0964       eventTree->Branch("trkjet_ntracks", &m_trkjet_ntracks);
0965       eventTree->Branch("trkjet_nDisplaced", &m_trkjet_nDisplaced);
0966       eventTree->Branch("trkjet_nTight", &m_trkjet_nTight);
0967       eventTree->Branch("trkjet_nTightDisplaced", &m_trkjet_nTightDisplaced);
0968 
0969       eventTree->Branch("trkjetem_eta", &m_trkjetem_eta);
0970       eventTree->Branch("trkjetem_pt", &m_trkjetem_pt);
0971       eventTree->Branch("trkjetem_phi", &m_trkjetem_phi);
0972       eventTree->Branch("trkjetem_z", &m_trkjetem_z);
0973       eventTree->Branch("trkjetem_ntracks", &m_trkjetem_ntracks);
0974       eventTree->Branch("trkjetem_nxtracks", &m_trkjetem_nxtracks);
0975     }
0976     if (Displaced == "Displaced" || Displaced == "Both") {
0977       eventTree->Branch("trkfastjetExt_eta", &m_trkfastjetExt_eta);
0978       eventTree->Branch("trkfastjetExt_vz", &m_trkfastjetExt_vz);
0979       eventTree->Branch("trkfastjetExt_p", &m_trkfastjetExt_p);
0980       eventTree->Branch("trkfastjetExt_pt", &m_trkfastjetExt_pt);
0981       eventTree->Branch("trkfastjetExt_phi", &m_trkfastjetExt_phi);
0982       eventTree->Branch("trkfastjetExt_ntracks", &m_trkfastjetExt_ntracks);
0983       eventTree->Branch("trkfastjetExt_truetp_sumpt", m_trkfastjetExt_truetp_sumpt);
0984 
0985       eventTree->Branch("trkjetExt_eta", &m_trkjetExt_eta);
0986       eventTree->Branch("trkjetExt_vz", &m_trkjetExt_vz);
0987       eventTree->Branch("trkjetExt_p", &m_trkjetExt_p);
0988       eventTree->Branch("trkjetExt_pt", &m_trkjetExt_pt);
0989       eventTree->Branch("trkjetExt_phi", &m_trkjetExt_phi);
0990       eventTree->Branch("trkjetExt_ntracks", &m_trkjetExt_ntracks);
0991       eventTree->Branch("trkjetExt_nDisplaced", &m_trkjetExt_nDisplaced);
0992       eventTree->Branch("trkjetExt_nTight", &m_trkjetExt_nTight);
0993       eventTree->Branch("trkjetExt_nTightDisplaced", &m_trkjetExt_nTightDisplaced);
0994 
0995       eventTree->Branch("trkjetemExt_eta", &m_trkjetemExt_eta);
0996       eventTree->Branch("trkjetemExt_pt", &m_trkjetemExt_pt);
0997       eventTree->Branch("trkjetemExt_phi", &m_trkjetemExt_phi);
0998       eventTree->Branch("trkjetemExt_z", &m_trkjetemExt_z);
0999       eventTree->Branch("trkjetemExt_ntracks", &m_trkjetemExt_ntracks);
1000       eventTree->Branch("trkjetemExt_nxtracks", &m_trkjetemExt_nxtracks);
1001     }
1002   }
1003 
1004   if (SaveTrackSums) {
1005     eventTree->Branch("trueMET", &trueMET, "trueMET/F");
1006     eventTree->Branch("trueTkMET", &trueTkMET, "trueTkMET/F");
1007 
1008     if (Displaced == "Prompt" || Displaced == "Both") {
1009       eventTree->Branch("trkMET", &trkMET, "trkMET/F");
1010       eventTree->Branch("trkMETPhi", &trkMETPhi, "trkMETPhi/F");
1011       eventTree->Branch("trkMETEmu", &trkMETEmu, "trkMETEmu/F");
1012       eventTree->Branch("trkMETEmuPhi", &trkMETEmuPhi, "trkMETEmuPhi/F");
1013       eventTree->Branch("trkMHT", &trkMHT, "trkMHT/F");
1014       eventTree->Branch("trkHT", &trkHT, "trkHT/F");
1015       eventTree->Branch("trkMHTEmu", &trkMHTEmu, "trkMHTEmu/F");
1016       eventTree->Branch("trkMHTEmuPhi", &trkMHTEmuPhi, "trkMHTEmuPhi/F");
1017       eventTree->Branch("trkHTEmu", &trkHTEmu, "trkHTEmu/F");
1018     }
1019     if (Displaced == "Displaced" || Displaced == "Both") {
1020       eventTree->Branch("trkMETExt", &trkMETExt, "trkMETExt/F");
1021       eventTree->Branch("trkMETPhiExt", &trkMETPhiExt, "trkMETPhiExt/F");
1022       eventTree->Branch("trkMHTExt", &trkMHTExt, "trkMHTExt/F");
1023       eventTree->Branch("trkHTExt", &trkHTExt, "trkHTExt/F");
1024       eventTree->Branch("trkMHTEmuExt", &trkMHTEmuExt, "trkMHTEmuExt/F");
1025       eventTree->Branch("trkMHTEmuPhiExt", &trkMHTEmuPhiExt, "trkMHTEmuPhiExt/F");
1026       eventTree->Branch("trkHTEmuExt", &trkHTEmuExt, "trkHTEmuExt/F");
1027     }
1028   }
1029 }
1030 
1031 //////////
1032 // ANALYZE
1033 void L1TrackObjectNtupleMaker::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
1034   if (not available_)
1035     return;  // No ROOT file open.
1036 
1037   if (!(MyProcess == 13 || MyProcess == 11 || MyProcess == 211 || MyProcess == 6 || MyProcess == 15 ||
1038         MyProcess == 1)) {
1039     edm::LogVerbatim("Tracklet") << "The specified MyProcess is invalid! Exiting...";
1040     return;
1041   }
1042 
1043   // clear variables
1044   if (SaveAllTracks && (Displaced == "Prompt" || Displaced == "Both")) {
1045     m_trk_pt->clear();
1046     m_trk_eta->clear();
1047     m_trk_phi->clear();
1048     m_trk_phi_local->clear();
1049     m_trk_d0->clear();
1050     m_trk_z0->clear();
1051     m_trk_chi2->clear();
1052     m_trk_chi2dof->clear();
1053     m_trk_chi2rphi->clear();
1054     m_trk_chi2rz->clear();
1055     m_trk_bendchi2->clear();
1056     m_trk_MVA1->clear();
1057     m_trk_nstub->clear();
1058     m_trk_lhits->clear();
1059     m_trk_dhits->clear();
1060     m_trk_seed->clear();
1061     m_trk_hitpattern->clear();
1062     m_trk_phiSector->clear();
1063     m_trk_genuine->clear();
1064     m_trk_loose->clear();
1065     m_trk_unknown->clear();
1066     m_trk_combinatoric->clear();
1067     m_trk_fake->clear();
1068     m_trk_matchtp_pdgid->clear();
1069     m_trk_matchtp_pt->clear();
1070     m_trk_matchtp_eta->clear();
1071     m_trk_matchtp_phi->clear();
1072     m_trk_matchtp_z0->clear();
1073     m_trk_matchtp_dxy->clear();
1074     m_trk_gtt_pt->clear();
1075     m_trk_gtt_eta->clear();
1076     m_trk_gtt_phi->clear();
1077     m_trk_selected_index->clear();
1078     m_trk_selected_emulation_index->clear();
1079   }
1080   if (SaveAllTracks && (Displaced == "Displaced" || Displaced == "Both")) {
1081     m_trkExt_pt->clear();
1082     m_trkExt_eta->clear();
1083     m_trkExt_phi->clear();
1084     m_trkExt_phi_local->clear();
1085     m_trkExt_d0->clear();
1086     m_trkExt_z0->clear();
1087     m_trkExt_chi2->clear();
1088     m_trkExt_chi2dof->clear();
1089     m_trkExt_chi2rphi->clear();
1090     m_trkExt_chi2rz->clear();
1091     m_trkExt_bendchi2->clear();
1092     m_trkExt_MVA->clear();
1093     m_trkExt_nstub->clear();
1094     m_trkExt_lhits->clear();
1095     m_trkExt_dhits->clear();
1096     m_trkExt_seed->clear();
1097     m_trkExt_hitpattern->clear();
1098     m_trkExt_phiSector->clear();
1099     m_trkExt_genuine->clear();
1100     m_trkExt_loose->clear();
1101     m_trkExt_unknown->clear();
1102     m_trkExt_combinatoric->clear();
1103     m_trkExt_fake->clear();
1104     m_trkExt_matchtp_pdgid->clear();
1105     m_trkExt_matchtp_pt->clear();
1106     m_trkExt_matchtp_eta->clear();
1107     m_trkExt_matchtp_phi->clear();
1108     m_trkExt_matchtp_z0->clear();
1109     m_trkExt_matchtp_dxy->clear();
1110     m_trkExt_gtt_pt->clear();
1111     m_trkExt_gtt_eta->clear();
1112     m_trkExt_gtt_phi->clear();
1113     m_trkExt_selected_index->clear();
1114     m_trkExt_selected_emulation_index->clear();
1115   }
1116   m_tp_pt->clear();
1117   m_tp_eta->clear();
1118   m_tp_phi->clear();
1119   m_tp_dxy->clear();
1120   m_tp_d0->clear();
1121   m_tp_z0->clear();
1122   m_tp_d0_prod->clear();
1123   m_tp_z0_prod->clear();
1124   m_tp_pdgid->clear();
1125   m_tp_nmatch->clear();
1126   m_tp_nstub->clear();
1127   m_tp_eventid->clear();
1128   m_tp_charge->clear();
1129 
1130   m_gen_pt->clear();
1131   m_gen_phi->clear();
1132   m_gen_pdgid->clear();
1133   m_gen_z0->clear();
1134 
1135   if (Displaced == "Prompt" || Displaced == "Both") {
1136     m_matchtrk_pt->clear();
1137     m_matchtrk_eta->clear();
1138     m_matchtrk_phi->clear();
1139     m_matchtrk_z0->clear();
1140     m_matchtrk_d0->clear();
1141     m_matchtrk_chi2->clear();
1142     m_matchtrk_chi2dof->clear();
1143     m_matchtrk_chi2rphi->clear();
1144     m_matchtrk_chi2rz->clear();
1145     m_matchtrk_bendchi2->clear();
1146     m_matchtrk_MVA1->clear();
1147     m_matchtrk_nstub->clear();
1148     m_matchtrk_lhits->clear();
1149     m_matchtrk_dhits->clear();
1150     m_matchtrk_seed->clear();
1151     m_matchtrk_hitpattern->clear();
1152   }
1153 
1154   if (Displaced == "Displaced" || Displaced == "Both") {
1155     m_matchtrkExt_pt->clear();
1156     m_matchtrkExt_eta->clear();
1157     m_matchtrkExt_phi->clear();
1158     m_matchtrkExt_z0->clear();
1159     m_matchtrkExt_d0->clear();
1160     m_matchtrkExt_chi2->clear();
1161     m_matchtrkExt_chi2dof->clear();
1162     m_matchtrkExt_chi2rphi->clear();
1163     m_matchtrkExt_chi2rz->clear();
1164     m_matchtrkExt_bendchi2->clear();
1165     m_matchtrkExt_MVA->clear();
1166     m_matchtrkExt_nstub->clear();
1167     m_matchtrkExt_lhits->clear();
1168     m_matchtrkExt_dhits->clear();
1169     m_matchtrkExt_seed->clear();
1170     m_matchtrkExt_hitpattern->clear();
1171   }
1172 
1173   if (SaveStubs) {
1174     m_allstub_x->clear();
1175     m_allstub_y->clear();
1176     m_allstub_z->clear();
1177     m_allstub_isBarrel->clear();
1178     m_allstub_layer->clear();
1179     m_allstub_isPSmodule->clear();
1180     m_allstub_trigDisplace->clear();
1181     m_allstub_trigOffset->clear();
1182     m_allstub_trigPos->clear();
1183     m_allstub_trigBend->clear();
1184     m_allstub_matchTP_pdgid->clear();
1185     m_allstub_matchTP_pt->clear();
1186     m_allstub_matchTP_eta->clear();
1187     m_allstub_matchTP_phi->clear();
1188     m_allstub_genuine->clear();
1189   }
1190 
1191   if (SaveTrackJets) {
1192     if (Displaced == "Prompt" || Displaced == "Both") {
1193       m_trkjet_eta->clear();
1194       m_trkjet_pt->clear();
1195       m_trkjet_vz->clear();
1196       m_trkjet_phi->clear();
1197       m_trkjet_p->clear();
1198       m_trkjet_ntracks->clear();
1199       m_trkjet_nDisplaced->clear();
1200       m_trkjet_nTight->clear();
1201       m_trkjet_nTightDisplaced->clear();
1202       m_trkjet_ntdtrk->clear();
1203       m_trkfastjet_eta->clear();
1204       m_trkfastjet_pt->clear();
1205       m_trkfastjet_vz->clear();
1206       m_trkfastjet_phi->clear();
1207       m_trkfastjet_p->clear();
1208       m_trkfastjet_ntracks->clear();
1209       m_trkfastjet_truetp_sumpt->clear();
1210       m_trkfastjet_tp_sumpt->clear();
1211       m_trkjetem_eta->clear();
1212       m_trkjetem_pt->clear();
1213       m_trkjetem_phi->clear();
1214       m_trkjetem_z->clear();
1215       m_trkjetem_ntracks->clear();
1216       m_trkjetem_nxtracks->clear();
1217     }
1218     if (Displaced == "Displaced" || Displaced == "Both") {
1219       m_trkjetExt_eta->clear();
1220       m_trkjetExt_pt->clear();
1221       m_trkjetExt_vz->clear();
1222       m_trkjetExt_phi->clear();
1223       m_trkjetExt_p->clear();
1224       m_trkjetExt_ntracks->clear();
1225       m_trkjetExt_nDisplaced->clear();
1226       m_trkjetExt_nTight->clear();
1227       m_trkjetExt_nTightDisplaced->clear();
1228       m_trkjetExt_ntdtrk->clear();
1229       m_trkfastjetExt_eta->clear();
1230       m_trkfastjetExt_pt->clear();
1231       m_trkfastjetExt_vz->clear();
1232       m_trkfastjetExt_phi->clear();
1233       m_trkfastjetExt_p->clear();
1234       m_trkfastjetExt_ntracks->clear();
1235       m_trkfastjetExt_truetp_sumpt->clear();
1236       m_trkfastjetExt_tp_sumpt->clear();
1237       m_trkjetemExt_eta->clear();
1238       m_trkjetemExt_pt->clear();
1239       m_trkjetemExt_phi->clear();
1240       m_trkjetemExt_z->clear();
1241       m_trkjetemExt_ntracks->clear();
1242       m_trkjetemExt_nxtracks->clear();
1243     }
1244 
1245     m_pv_L1reco->clear();
1246     m_pv_L1reco_sum->clear();
1247     m_pv_L1reco_emu->clear();
1248     m_pv_L1reco_sum_emu->clear();
1249     m_pv_MC->clear();
1250     m_MC_lep->clear();
1251   }
1252 
1253   // -----------------------------------------------------------------------------------------------
1254   // retrieve various containers
1255   // -----------------------------------------------------------------------------------------------
1256 
1257   // L1 stubs
1258   edm::Handle<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>> TTStubHandle;
1259   if (SaveStubs)
1260     iEvent.getByToken(ttStubToken_, TTStubHandle);
1261 
1262   // MC truth association maps
1263   edm::Handle<TTClusterAssociationMap<Ref_Phase2TrackerDigi_>> MCTruthTTClusterHandle;
1264   iEvent.getByToken(ttClusterMCTruthToken_, MCTruthTTClusterHandle);
1265   edm::Handle<TTStubAssociationMap<Ref_Phase2TrackerDigi_>> MCTruthTTStubHandle;
1266   iEvent.getByToken(ttStubMCTruthToken_, MCTruthTTStubHandle);
1267 
1268   // tracking particles
1269   edm::Handle<std::vector<TrackingParticle>> TrackingParticleHandle;
1270   edm::Handle<std::vector<TrackingVertex>> TrackingVertexHandle;
1271   iEvent.getByToken(TrackingParticleToken_, TrackingParticleHandle);
1272   iEvent.getByToken(TrackingVertexToken_, TrackingVertexHandle);
1273 
1274   // -----------------------------------------------------------------------------------------------
1275   // more for TTStubs
1276   const TrackerTopology& tTopo = iSetup.getData(tTopoToken_);
1277   const TrackerGeometry& tGeom = iSetup.getData(tGeomToken_);
1278 
1279   //Gen particles
1280   edm::Handle<std::vector<reco::GenParticle>> GenParticleHandle;
1281   iEvent.getByToken(GenParticleToken_, GenParticleHandle);
1282 
1283   //Vertex
1284   edm::Handle<l1t::VertexCollection> L1PrimaryVertexHandle;
1285   iEvent.getByToken(L1VertexToken_, L1PrimaryVertexHandle);
1286   std::vector<l1t::Vertex>::const_iterator vtxIter;
1287 
1288   edm::Handle<l1t::VertexWordCollection> L1PrimaryVertexEmuHandle;
1289   iEvent.getByToken(L1VertexEmuToken_, L1PrimaryVertexEmuHandle);
1290   std::vector<l1t::VertexWord>::const_iterator vtxEmuIter;
1291 
1292   // Track jets
1293   edm::Handle<std::vector<l1t::TkJet>> TrackFastJetsHandle;
1294   edm::Handle<std::vector<l1t::TkJet>> TrackFastJetsExtendedHandle;
1295   edm::Handle<l1t::TkJetCollection> TrackJetsHandle;
1296   edm::Handle<l1t::TkJetCollection> TrackJetsExtendedHandle;
1297   edm::Handle<l1t::TkJetWordCollection> TrackJetsEmuHandle;
1298   edm::Handle<l1t::TkJetWordCollection> TrackJetsExtendedEmuHandle;
1299   std::vector<l1t::TkJet>::const_iterator jetIter;
1300   std::vector<l1t::TkJetWord>::const_iterator jetemIter;
1301 
1302   // Track Sums
1303   edm::Handle<std::vector<l1t::TkEtMiss>> L1TkMETHandle;
1304   edm::Handle<std::vector<l1t::TkEtMiss>> L1TkMETExtendedHandle;
1305   edm::Handle<std::vector<l1t::EtSum>> L1TkMETEmuHandle;
1306   edm::Handle<std::vector<l1t::TkHTMiss>> L1TkMHTHandle;
1307   edm::Handle<std::vector<l1t::TkHTMiss>> L1TkMHTExtendedHandle;
1308   edm::Handle<std::vector<l1t::EtSum>> L1TkMHTEmuHandle;
1309   edm::Handle<std::vector<l1t::EtSum>> L1TkMHTEmuExtendedHandle;
1310 
1311   // L1 tracks
1312   edm::Handle<L1TrackCollection> TTTrackHandle;
1313   edm::Handle<L1TrackCollection> TTTrackExtendedHandle;
1314   edm::Handle<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>> MCTruthTTTrackHandle;
1315   edm::Handle<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>> MCTruthTTTrackExtendedHandle;
1316   edm::Handle<L1TrackCollection> TTTrackGTTHandle;
1317   edm::Handle<L1TrackCollection> TTTrackExtendedGTTHandle;
1318   edm::Handle<L1TrackRefCollection> TTTrackSelectedHandle;
1319   edm::Handle<L1TrackRefCollection> TTTrackSelectedEmulationHandle;
1320   edm::Handle<L1TrackRefCollection> TTTrackExtendedSelectedHandle;
1321   edm::Handle<L1TrackRefCollection> TTTrackExtendedSelectedEmulationHandle;
1322   L1TrackCollection::const_iterator iterL1Track;
1323 
1324   if (Displaced == "Prompt" || Displaced == "Both") {
1325     iEvent.getByToken(TrackFastJetsToken_, TrackFastJetsHandle);
1326     iEvent.getByToken(TrackJetsToken_, TrackJetsHandle);
1327     iEvent.getByToken(TrackJetsEmuToken_, TrackJetsEmuHandle);
1328     iEvent.getByToken(TrackMETToken_, L1TkMETHandle);
1329     iEvent.getByToken(TrackMETEmuToken_, L1TkMETEmuHandle);
1330     iEvent.getByToken(TrackMHTToken_, L1TkMHTHandle);
1331     iEvent.getByToken(TrackMHTEmuToken_, L1TkMHTEmuHandle);
1332     iEvent.getByToken(ttTrackToken_, TTTrackHandle);
1333     iEvent.getByToken(ttTrackMCTruthToken_, MCTruthTTTrackHandle);
1334     iEvent.getByToken(ttTrackGTTToken_, TTTrackGTTHandle);
1335     iEvent.getByToken(ttTrackSelectedToken_, TTTrackSelectedHandle);
1336     iEvent.getByToken(ttTrackSelectedEmulationToken_, TTTrackSelectedEmulationHandle);
1337   }
1338   if (Displaced == "Displaced" || Displaced == "Both") {
1339     iEvent.getByToken(TrackFastJetsExtendedToken_, TrackFastJetsExtendedHandle);
1340     iEvent.getByToken(TrackJetsExtendedToken_, TrackJetsExtendedHandle);
1341     iEvent.getByToken(TrackJetsExtendedEmuToken_, TrackJetsExtendedEmuHandle);
1342     iEvent.getByToken(TrackMETExtendedToken_, L1TkMETExtendedHandle);
1343     iEvent.getByToken(TrackMHTExtendedToken_, L1TkMHTExtendedHandle);
1344     iEvent.getByToken(TrackMHTEmuExtendedToken_, L1TkMHTEmuExtendedHandle);
1345     iEvent.getByToken(ttTrackExtendedToken_, TTTrackExtendedHandle);
1346     iEvent.getByToken(ttTrackMCTruthExtendedToken_, MCTruthTTTrackExtendedHandle);
1347     iEvent.getByToken(ttTrackExtendedGTTToken_, TTTrackExtendedGTTHandle);
1348     iEvent.getByToken(ttTrackExtendedSelectedToken_, TTTrackExtendedSelectedHandle);
1349     iEvent.getByToken(ttTrackExtendedSelectedEmulationToken_, TTTrackExtendedSelectedEmulationHandle);
1350   }
1351 
1352   //Loop over gen particles
1353   if (GenParticleHandle.isValid()) {
1354     vector<reco::GenParticle>::const_iterator genpartIter;
1355 
1356     float zvtx_gen = -999;
1357     float trueMETx = 0;
1358     float trueMETy = 0;
1359     trueMET = 0;
1360     for (genpartIter = GenParticleHandle->begin(); genpartIter != GenParticleHandle->end(); ++genpartIter) {
1361       int status = genpartIter->status();
1362       if (status != 1)
1363         continue;
1364       zvtx_gen = genpartIter->vz();  //for gen vertex
1365       int id = genpartIter->pdgId();
1366       bool isNeutrino = false;
1367       if ((std::abs(id) == 12 || std::abs(id) == 14 || std::abs(id) == 16))
1368         isNeutrino = true;
1369       if (isNeutrino || id == 1000022) {
1370         trueMETx += genpartIter->pt() * cos(genpartIter->phi());
1371         trueMETy += genpartIter->pt() * sin(genpartIter->phi());
1372       }
1373 
1374       m_gen_pt->push_back(genpartIter->pt());
1375       m_gen_phi->push_back(genpartIter->phi());
1376       m_gen_pdgid->push_back(genpartIter->pdgId());
1377       m_gen_z0->push_back(zvtx_gen);
1378     }
1379 
1380     trueMET = sqrt(trueMETx * trueMETx + trueMETy * trueMETy);
1381     m_pv_MC->push_back(zvtx_gen);
1382   } else {
1383     edm::LogWarning("DataNotFound") << "\nWarning: GenParticleHandle not found in the event" << std::endl;
1384   }
1385 
1386   // ----------------------------------------------------------------------------------------------
1387   // loop over L1 stubs
1388   // ----------------------------------------------------------------------------------------------
1389   if (SaveStubs) {
1390     for (auto gd = tGeom.dets().begin(); gd != tGeom.dets().end(); gd++) {
1391       DetId detid = (*gd)->geographicalId();
1392       if (detid.subdetId() != StripSubdetector::TOB && detid.subdetId() != StripSubdetector::TID)
1393         continue;
1394       if (!tTopo.isLower(detid))
1395         continue;                             // loop on the stacks: choose the lower arbitrarily
1396       DetId stackDetid = tTopo.stack(detid);  // Stub module detid
1397 
1398       if (TTStubHandle->find(stackDetid) == TTStubHandle->end())
1399         continue;
1400 
1401       // Get the DetSets of the Clusters
1402       edmNew::DetSet<TTStub<Ref_Phase2TrackerDigi_>> stubs = (*TTStubHandle)[stackDetid];
1403       const GeomDetUnit* det0 = tGeom.idToDetUnit(detid);
1404       const auto* theGeomDet = dynamic_cast<const PixelGeomDetUnit*>(det0);
1405       const PixelTopology* topol = dynamic_cast<const PixelTopology*>(&(theGeomDet->specificTopology()));
1406 
1407       // loop over stubs
1408       for (auto stubIter = stubs.begin(); stubIter != stubs.end(); ++stubIter) {
1409         edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>> tempStubPtr =
1410             edmNew::makeRefTo(TTStubHandle, stubIter);
1411 
1412         int isBarrel = 0;
1413         int layer = -999999;
1414         if (detid.subdetId() == StripSubdetector::TOB) {
1415           isBarrel = 1;
1416           layer = static_cast<int>(tTopo.layer(detid));
1417         } else if (detid.subdetId() == StripSubdetector::TID) {
1418           isBarrel = 0;
1419           layer = static_cast<int>(tTopo.layer(detid));
1420         } else {
1421           edm::LogVerbatim("Tracklet") << "WARNING -- neither TOB or TID stub, shouldn't happen...";
1422           layer = -1;
1423         }
1424 
1425         int isPSmodule = 0;
1426         if (topol->nrows() == 960)
1427           isPSmodule = 1;
1428 
1429         MeasurementPoint coords = tempStubPtr->clusterRef(0)->findAverageLocalCoordinatesCentered();
1430         LocalPoint clustlp = topol->localPosition(coords);
1431         GlobalPoint posStub = theGeomDet->surface().toGlobal(clustlp);
1432 
1433         double tmp_stub_x = posStub.x();
1434         double tmp_stub_y = posStub.y();
1435         double tmp_stub_z = posStub.z();
1436 
1437         float trigDisplace = tempStubPtr->rawBend();
1438         float trigOffset = tempStubPtr->bendOffset();
1439         float trigPos = tempStubPtr->innerClusterPosition();
1440         float trigBend = tempStubPtr->bendFE();
1441 
1442         m_allstub_x->push_back(tmp_stub_x);
1443         m_allstub_y->push_back(tmp_stub_y);
1444         m_allstub_z->push_back(tmp_stub_z);
1445         m_allstub_isBarrel->push_back(isBarrel);
1446         m_allstub_layer->push_back(layer);
1447         m_allstub_isPSmodule->push_back(isPSmodule);
1448         m_allstub_trigDisplace->push_back(trigDisplace);
1449         m_allstub_trigOffset->push_back(trigOffset);
1450         m_allstub_trigPos->push_back(trigPos);
1451         m_allstub_trigBend->push_back(trigBend);
1452 
1453         // matched to tracking particle?
1454         edm::Ptr<TrackingParticle> my_tp = MCTruthTTStubHandle->findTrackingParticlePtr(tempStubPtr);
1455 
1456         int myTP_pdgid = -999;
1457         float myTP_pt = -999;
1458         float myTP_eta = -999;
1459         float myTP_phi = -999;
1460 
1461         if (my_tp.isNull() == false) {
1462           int tmp_eventid = my_tp->eventId().event();
1463           if (tmp_eventid > 0)
1464             continue;  // this means stub from pileup track
1465           myTP_pdgid = my_tp->pdgId();
1466           myTP_pt = my_tp->p4().pt();
1467           myTP_eta = my_tp->p4().eta();
1468           myTP_phi = my_tp->p4().phi();
1469         }
1470         int tmp_stub_genuine = 0;
1471         if (MCTruthTTStubHandle->isGenuine(tempStubPtr))
1472           tmp_stub_genuine = 1;
1473 
1474         m_allstub_matchTP_pdgid->push_back(myTP_pdgid);
1475         m_allstub_matchTP_pt->push_back(myTP_pt);
1476         m_allstub_matchTP_eta->push_back(myTP_eta);
1477         m_allstub_matchTP_phi->push_back(myTP_phi);
1478         m_allstub_genuine->push_back(tmp_stub_genuine);
1479       }
1480     }
1481   }
1482 
1483   // ----------------------------------------------------------------------------------------------
1484   // loop over (prompt) L1 tracks
1485   // ----------------------------------------------------------------------------------------------
1486   if (SaveAllTracks && (Displaced == "Prompt" || Displaced == "Both")) {
1487     if (DebugMode) {
1488       edm::LogVerbatim("Tracklet") << "\n Loop over L1 tracks!";
1489       edm::LogVerbatim("Tracklet") << "\n Looking at " << Displaced << " tracks!";
1490     }
1491 
1492     int this_l1track = 0;
1493     for (iterL1Track = TTTrackHandle->begin(); iterL1Track != TTTrackHandle->end(); iterL1Track++) {
1494       L1TrackPtr l1track_ptr(TTTrackHandle, this_l1track);
1495       L1TrackRef l1track_ref(TTTrackGTTHandle, this_l1track);
1496       this_l1track++;
1497 
1498       float tmp_trk_pt = iterL1Track->momentum().perp();
1499       float tmp_trk_eta = iterL1Track->momentum().eta();
1500       float tmp_trk_phi = iterL1Track->momentum().phi();
1501       float tmp_trk_phi_local = iterL1Track->localPhi();
1502       float tmp_trk_z0 = iterL1Track->z0();            //cm
1503       int tmp_trk_nFitPars = iterL1Track->nFitPars();  //4 or 5
1504 
1505       float tmp_trk_d0 = -999;
1506       if (tmp_trk_nFitPars == 5) {
1507         float tmp_trk_x0 = iterL1Track->POCA().x();
1508         float tmp_trk_y0 = iterL1Track->POCA().y();
1509         tmp_trk_d0 = -tmp_trk_x0 * sin(tmp_trk_phi) + tmp_trk_y0 * cos(tmp_trk_phi);
1510         // tmp_trk_d0 = iterL1Track->d0();
1511       }
1512 
1513       float tmp_trk_chi2 = iterL1Track->chi2();
1514       float tmp_trk_chi2dof = iterL1Track->chi2Red();
1515       float tmp_trk_chi2rphi = iterL1Track->chi2XYRed();
1516       float tmp_trk_chi2rz = iterL1Track->chi2ZRed();
1517       float tmp_trk_bendchi2 = iterL1Track->stubPtConsistency();
1518       float tmp_trk_MVA1 = iterL1Track->trkMVA1();
1519 
1520       std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>>
1521           stubRefs = iterL1Track->getStubRefs();
1522       int tmp_trk_nstub = (int)stubRefs.size();
1523       int tmp_trk_seed = 0;
1524       tmp_trk_seed = (int)iterL1Track->trackSeedType();
1525       int tmp_trk_hitpattern = 0;
1526       tmp_trk_hitpattern = (int)iterL1Track->hitPattern();
1527       unsigned int tmp_trk_phiSector = iterL1Track->phiSector();
1528 
1529       // ----------------------------------------------------------------------------------------------
1530       // loop over stubs on tracks
1531       int tmp_trk_dhits = 0;
1532       int tmp_trk_lhits = 0;
1533       if (true) {
1534         // loop over stubs
1535         for (int is = 0; is < tmp_trk_nstub; is++) {
1536           //detID of stub
1537           DetId detIdStub = tGeom.idToDet((stubRefs.at(is)->clusterRef(0))->getDetId())->geographicalId();
1538           MeasurementPoint coords = stubRefs.at(is)->clusterRef(0)->findAverageLocalCoordinatesCentered();
1539           const GeomDet* theGeomDet = tGeom.idToDet(detIdStub);
1540           Global3DPoint posStub = theGeomDet->surface().toGlobal(theGeomDet->topology().localPosition(coords));
1541 
1542           double x = posStub.x();
1543           double y = posStub.y();
1544           double z = posStub.z();
1545 
1546           int layer = -999999;
1547           if (detIdStub.subdetId() == StripSubdetector::TOB) {
1548             layer = static_cast<int>(tTopo.layer(detIdStub));
1549             if (DebugMode)
1550               edm::LogVerbatim("Tracklet")
1551                   << "   stub in layer " << layer << " at position x y z = " << x << " " << y << " " << z;
1552             tmp_trk_lhits += pow(10, layer - 1);
1553           } else if (detIdStub.subdetId() == StripSubdetector::TID) {
1554             layer = static_cast<int>(tTopo.layer(detIdStub));
1555             if (DebugMode)
1556               edm::LogVerbatim("Tracklet")
1557                   << "   stub in disk " << layer << " at position x y z = " << x << " " << y << " " << z;
1558             tmp_trk_dhits += pow(10, layer - 1);
1559           }
1560         }  //end loop over stubs
1561       }
1562       // ----------------------------------------------------------------------------------------------
1563 
1564       int tmp_trk_genuine = 0;
1565       int tmp_trk_loose = 0;
1566       int tmp_trk_unknown = 0;
1567       int tmp_trk_combinatoric = 0;
1568       if (MCTruthTTTrackHandle->isLooselyGenuine(l1track_ptr))
1569         tmp_trk_loose = 1;
1570       if (MCTruthTTTrackHandle->isGenuine(l1track_ptr))
1571         tmp_trk_genuine = 1;
1572       if (MCTruthTTTrackHandle->isUnknown(l1track_ptr))
1573         tmp_trk_unknown = 1;
1574       if (MCTruthTTTrackHandle->isCombinatoric(l1track_ptr))
1575         tmp_trk_combinatoric = 1;
1576 
1577       if (DebugMode) {
1578         edm::LogVerbatim("Tracklet") << "L1 track,"
1579                                      << " pt: " << tmp_trk_pt << " eta: " << tmp_trk_eta << " phi: " << tmp_trk_phi
1580                                      << " z0: " << tmp_trk_z0 << " chi2: " << tmp_trk_chi2
1581                                      << " chi2rphi: " << tmp_trk_chi2rphi << " chi2rz: " << tmp_trk_chi2rz
1582                                      << " nstub: " << tmp_trk_nstub;
1583         if (tmp_trk_genuine)
1584           edm::LogVerbatim("Tracklet") << "    (is genuine)";
1585         if (tmp_trk_unknown)
1586           edm::LogVerbatim("Tracklet") << "    (is unknown)";
1587         if (tmp_trk_combinatoric)
1588           edm::LogVerbatim("Tracklet") << "    (is combinatoric)";
1589       }
1590 
1591       m_trk_pt->push_back(tmp_trk_pt);
1592       m_trk_eta->push_back(tmp_trk_eta);
1593       m_trk_phi->push_back(tmp_trk_phi);
1594       m_trk_phi_local->push_back(tmp_trk_phi_local);
1595       m_trk_z0->push_back(tmp_trk_z0);
1596       if (tmp_trk_nFitPars == 5)
1597         m_trk_d0->push_back(tmp_trk_d0);
1598       else
1599         m_trk_d0->push_back(999.);
1600       m_trk_chi2->push_back(tmp_trk_chi2);
1601       m_trk_chi2dof->push_back(tmp_trk_chi2dof);
1602       m_trk_chi2rphi->push_back(tmp_trk_chi2rphi);
1603       m_trk_chi2rz->push_back(tmp_trk_chi2rz);
1604       m_trk_bendchi2->push_back(tmp_trk_bendchi2);
1605       m_trk_MVA1->push_back(tmp_trk_MVA1);
1606       m_trk_nstub->push_back(tmp_trk_nstub);
1607       m_trk_dhits->push_back(tmp_trk_dhits);
1608       m_trk_lhits->push_back(tmp_trk_lhits);
1609       m_trk_seed->push_back(tmp_trk_seed);
1610       m_trk_hitpattern->push_back(tmp_trk_hitpattern);
1611       m_trk_phiSector->push_back(tmp_trk_phiSector);
1612       m_trk_genuine->push_back(tmp_trk_genuine);
1613       m_trk_loose->push_back(tmp_trk_loose);
1614       m_trk_unknown->push_back(tmp_trk_unknown);
1615       m_trk_combinatoric->push_back(tmp_trk_combinatoric);
1616 
1617       // ----------------------------------------------------------------------------------------------
1618       // for studying the fake rate
1619       // ----------------------------------------------------------------------------------------------
1620       edm::Ptr<TrackingParticle> my_tp = MCTruthTTTrackHandle->findTrackingParticlePtr(l1track_ptr);
1621 
1622       int myFake = 0;
1623       int myTP_pdgid = -999;
1624       float myTP_pt = -999;
1625       float myTP_eta = -999;
1626       float myTP_phi = -999;
1627       float myTP_z0 = -999;
1628       float myTP_dxy = -999;
1629 
1630       if (my_tp.isNull())
1631         myFake = 0;
1632       else {
1633         int tmp_eventid = my_tp->eventId().event();
1634         if (tmp_eventid > 0)
1635           myFake = 2;
1636         else
1637           myFake = 1;
1638 
1639         myTP_pdgid = my_tp->pdgId();
1640         myTP_pt = my_tp->p4().pt();
1641         myTP_eta = my_tp->p4().eta();
1642         myTP_phi = my_tp->p4().phi();
1643         myTP_z0 = my_tp->vertex().z();
1644 
1645         float myTP_x0 = my_tp->vertex().x();
1646         float myTP_y0 = my_tp->vertex().y();
1647         myTP_dxy = sqrt(myTP_x0 * myTP_x0 + myTP_y0 * myTP_y0);
1648 
1649         if (DebugMode) {
1650           edm::LogVerbatim("Tracklet") << "TP matched to track has pt = " << my_tp->p4().pt()
1651                                        << " eta = " << my_tp->momentum().eta() << " phi = " << my_tp->momentum().phi()
1652                                        << " z0 = " << my_tp->vertex().z() << " pdgid = " << my_tp->pdgId()
1653                                        << " dxy = " << myTP_dxy;
1654         }
1655       }
1656 
1657       m_trk_fake->push_back(myFake);
1658       m_trk_matchtp_pdgid->push_back(myTP_pdgid);
1659       m_trk_matchtp_pt->push_back(myTP_pt);
1660       m_trk_matchtp_eta->push_back(myTP_eta);
1661       m_trk_matchtp_phi->push_back(myTP_phi);
1662       m_trk_matchtp_z0->push_back(myTP_z0);
1663       m_trk_matchtp_dxy->push_back(myTP_dxy);
1664 
1665       // ----------------------------------------------------------------------------------------------
1666       // store the index to the selected track or -1 if not selected
1667       // ----------------------------------------------------------------------------------------------
1668       m_trk_gtt_pt->push_back(l1track_ref->momentum().perp());
1669       m_trk_gtt_eta->push_back(l1track_ref->momentum().eta());
1670       m_trk_gtt_phi->push_back(l1track_ref->momentum().phi());
1671       m_trk_selected_index->push_back(getSelectedTrackIndex(l1track_ref, TTTrackSelectedHandle));
1672       m_trk_selected_emulation_index->push_back(getSelectedTrackIndex(l1track_ref, TTTrackSelectedEmulationHandle));
1673     }  //end track loop
1674   }    //end if SaveAllTracks
1675 
1676   // ----------------------------------------------------------------------------------------------
1677   // loop over (extended) L1 tracks
1678   // ----------------------------------------------------------------------------------------------
1679   if (SaveAllTracks && (Displaced == "Displaced" || Displaced == "Both")) {
1680     if (DebugMode) {
1681       edm::LogVerbatim("Tracklet") << "\n Loop over L1 tracks!";
1682       edm::LogVerbatim("Tracklet") << "\n Looking at " << Displaced << " tracks!";
1683     }
1684 
1685     int this_l1track = 0;
1686     for (iterL1Track = TTTrackExtendedHandle->begin(); iterL1Track != TTTrackExtendedHandle->end(); iterL1Track++) {
1687       L1TrackPtr l1track_ptr(TTTrackExtendedHandle, this_l1track);
1688       L1TrackRef l1track_ref(TTTrackExtendedGTTHandle, this_l1track);
1689       this_l1track++;
1690 
1691       float tmp_trk_pt = iterL1Track->momentum().perp();
1692       float tmp_trk_eta = iterL1Track->momentum().eta();
1693       float tmp_trk_phi = iterL1Track->momentum().phi();
1694       float tmp_trk_phi_local = iterL1Track->localPhi();
1695       float tmp_trk_z0 = iterL1Track->z0();            //cm
1696       int tmp_trk_nFitPars = iterL1Track->nFitPars();  //4 or 5
1697 
1698       float tmp_trk_d0 = -999;
1699       if (tmp_trk_nFitPars == 5) {
1700         float tmp_trk_x0 = iterL1Track->POCA().x();
1701         float tmp_trk_y0 = iterL1Track->POCA().y();
1702         tmp_trk_d0 = -tmp_trk_x0 * sin(tmp_trk_phi) + tmp_trk_y0 * cos(tmp_trk_phi);
1703         // tmp_trk_d0 = iterL1Track->d0();
1704       }
1705 
1706       float tmp_trk_chi2 = iterL1Track->chi2();
1707       float tmp_trk_chi2dof = iterL1Track->chi2Red();
1708       float tmp_trk_chi2rphi = iterL1Track->chi2XYRed();
1709       float tmp_trk_chi2rz = iterL1Track->chi2ZRed();
1710       float tmp_trk_bendchi2 = iterL1Track->stubPtConsistency();
1711       float tmp_trk_MVA1 = iterL1Track->trkMVA1();
1712 
1713       std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>>
1714           stubRefs = iterL1Track->getStubRefs();
1715       int tmp_trk_nstub = (int)stubRefs.size();
1716       int tmp_trk_seed = 0;
1717       tmp_trk_seed = (int)iterL1Track->trackSeedType();
1718       int tmp_trk_hitpattern = 0;
1719       tmp_trk_hitpattern = (int)iterL1Track->hitPattern();
1720       unsigned int tmp_trk_phiSector = iterL1Track->phiSector();
1721 
1722       // ----------------------------------------------------------------------------------------------
1723       // loop over stubs on tracks
1724       int tmp_trk_dhits = 0;
1725       int tmp_trk_lhits = 0;
1726       if (true) {
1727         // loop over stubs
1728         for (int is = 0; is < tmp_trk_nstub; is++) {
1729           //detID of stub
1730           DetId detIdStub = tGeom.idToDet((stubRefs.at(is)->clusterRef(0))->getDetId())->geographicalId();
1731           MeasurementPoint coords = stubRefs.at(is)->clusterRef(0)->findAverageLocalCoordinatesCentered();
1732           const GeomDet* theGeomDet = tGeom.idToDet(detIdStub);
1733           Global3DPoint posStub = theGeomDet->surface().toGlobal(theGeomDet->topology().localPosition(coords));
1734 
1735           double x = posStub.x();
1736           double y = posStub.y();
1737           double z = posStub.z();
1738 
1739           int layer = -999999;
1740           if (detIdStub.subdetId() == StripSubdetector::TOB) {
1741             layer = static_cast<int>(tTopo.layer(detIdStub));
1742             if (DebugMode)
1743               edm::LogVerbatim("Tracklet")
1744                   << "   stub in layer " << layer << " at position x y z = " << x << " " << y << " " << z;
1745             tmp_trk_lhits += pow(10, layer - 1);
1746           } else if (detIdStub.subdetId() == StripSubdetector::TID) {
1747             layer = static_cast<int>(tTopo.layer(detIdStub));
1748             if (DebugMode)
1749               edm::LogVerbatim("Tracklet")
1750                   << "   stub in disk " << layer << " at position x y z = " << x << " " << y << " " << z;
1751             tmp_trk_dhits += pow(10, layer - 1);
1752           }
1753         }  //end loop over stubs
1754       }
1755       // ----------------------------------------------------------------------------------------------
1756 
1757       int tmp_trk_genuine = 0;
1758       int tmp_trk_loose = 0;
1759       int tmp_trk_unknown = 0;
1760       int tmp_trk_combinatoric = 0;
1761       if (MCTruthTTTrackExtendedHandle->isLooselyGenuine(l1track_ptr))
1762         tmp_trk_loose = 1;
1763       if (MCTruthTTTrackExtendedHandle->isGenuine(l1track_ptr))
1764         tmp_trk_genuine = 1;
1765       if (MCTruthTTTrackExtendedHandle->isUnknown(l1track_ptr))
1766         tmp_trk_unknown = 1;
1767       if (MCTruthTTTrackExtendedHandle->isCombinatoric(l1track_ptr))
1768         tmp_trk_combinatoric = 1;
1769 
1770       if (DebugMode) {
1771         edm::LogVerbatim("Tracklet") << "L1 track,"
1772                                      << " pt: " << tmp_trk_pt << " eta: " << tmp_trk_eta << " phi: " << tmp_trk_phi
1773                                      << " z0: " << tmp_trk_z0 << " chi2: " << tmp_trk_chi2
1774                                      << " chi2rphi: " << tmp_trk_chi2rphi << " chi2rz: " << tmp_trk_chi2rz
1775                                      << " nstub: " << tmp_trk_nstub;
1776         if (tmp_trk_genuine)
1777           edm::LogVerbatim("Tracklet") << "    (is genuine)";
1778         if (tmp_trk_unknown)
1779           edm::LogVerbatim("Tracklet") << "    (is unknown)";
1780         if (tmp_trk_combinatoric)
1781           edm::LogVerbatim("Tracklet") << "    (is combinatoric)";
1782       }
1783 
1784       m_trkExt_pt->push_back(tmp_trk_pt);
1785       m_trkExt_eta->push_back(tmp_trk_eta);
1786       m_trkExt_phi->push_back(tmp_trk_phi);
1787       m_trkExt_phi_local->push_back(tmp_trk_phi_local);
1788       m_trkExt_z0->push_back(tmp_trk_z0);
1789       if (tmp_trk_nFitPars == 5)
1790         m_trkExt_d0->push_back(tmp_trk_d0);
1791       else
1792         m_trkExt_d0->push_back(999.);
1793       m_trkExt_chi2->push_back(tmp_trk_chi2);
1794       m_trkExt_chi2dof->push_back(tmp_trk_chi2dof);
1795       m_trkExt_chi2rphi->push_back(tmp_trk_chi2rphi);
1796       m_trkExt_chi2rz->push_back(tmp_trk_chi2rz);
1797       m_trkExt_bendchi2->push_back(tmp_trk_bendchi2);
1798       m_trkExt_MVA->push_back(tmp_trk_MVA1);
1799       m_trkExt_nstub->push_back(tmp_trk_nstub);
1800       m_trkExt_dhits->push_back(tmp_trk_dhits);
1801       m_trkExt_lhits->push_back(tmp_trk_lhits);
1802       m_trkExt_seed->push_back(tmp_trk_seed);
1803       m_trkExt_hitpattern->push_back(tmp_trk_hitpattern);
1804       m_trkExt_phiSector->push_back(tmp_trk_phiSector);
1805       m_trkExt_genuine->push_back(tmp_trk_genuine);
1806       m_trkExt_loose->push_back(tmp_trk_loose);
1807       m_trkExt_unknown->push_back(tmp_trk_unknown);
1808       m_trkExt_combinatoric->push_back(tmp_trk_combinatoric);
1809 
1810       // ----------------------------------------------------------------------------------------------
1811       // for studying the fake rate
1812       // ----------------------------------------------------------------------------------------------
1813       edm::Ptr<TrackingParticle> my_tp = MCTruthTTTrackExtendedHandle->findTrackingParticlePtr(l1track_ptr);
1814 
1815       int myFake = 0;
1816       int myTP_pdgid = -999;
1817       float myTP_pt = -999;
1818       float myTP_eta = -999;
1819       float myTP_phi = -999;
1820       float myTP_z0 = -999;
1821       float myTP_dxy = -999;
1822 
1823       if (my_tp.isNull())
1824         myFake = 0;
1825       else {
1826         int tmp_eventid = my_tp->eventId().event();
1827         if (tmp_eventid > 0)
1828           myFake = 2;
1829         else
1830           myFake = 1;
1831 
1832         myTP_pdgid = my_tp->pdgId();
1833         myTP_pt = my_tp->p4().pt();
1834         myTP_eta = my_tp->p4().eta();
1835         myTP_phi = my_tp->p4().phi();
1836         myTP_z0 = my_tp->vertex().z();
1837 
1838         float myTP_x0 = my_tp->vertex().x();
1839         float myTP_y0 = my_tp->vertex().y();
1840         myTP_dxy = sqrt(myTP_x0 * myTP_x0 + myTP_y0 * myTP_y0);
1841 
1842         if (DebugMode) {
1843           edm::LogVerbatim("Tracklet") << "TP matched to track has pt = " << my_tp->p4().pt()
1844                                        << " eta = " << my_tp->momentum().eta() << " phi = " << my_tp->momentum().phi()
1845                                        << " z0 = " << my_tp->vertex().z() << " pdgid = " << my_tp->pdgId()
1846                                        << " dxy = " << myTP_dxy;
1847         }
1848       }
1849 
1850       m_trkExt_fake->push_back(myFake);
1851       m_trkExt_matchtp_pdgid->push_back(myTP_pdgid);
1852       m_trkExt_matchtp_pt->push_back(myTP_pt);
1853       m_trkExt_matchtp_eta->push_back(myTP_eta);
1854       m_trkExt_matchtp_phi->push_back(myTP_phi);
1855       m_trkExt_matchtp_z0->push_back(myTP_z0);
1856       m_trkExt_matchtp_dxy->push_back(myTP_dxy);
1857 
1858       // ----------------------------------------------------------------------------------------------
1859       // store the index to the selected track or -1 if not selected
1860       // ----------------------------------------------------------------------------------------------
1861       m_trkExt_gtt_pt->push_back(l1track_ref->momentum().perp());
1862       m_trkExt_gtt_eta->push_back(l1track_ref->momentum().eta());
1863       m_trkExt_gtt_phi->push_back(l1track_ref->momentum().phi());
1864       m_trkExt_selected_index->push_back(getSelectedTrackIndex(l1track_ref, TTTrackExtendedSelectedHandle));
1865       m_trkExt_selected_emulation_index->push_back(
1866           getSelectedTrackIndex(l1track_ref, TTTrackExtendedSelectedEmulationHandle));
1867     }  //end track loop
1868   }    //end if SaveAllTracks (displaced)
1869 
1870   // ----------------------------------------------------------------------------------------------
1871   // loop over tracking particles
1872   // ----------------------------------------------------------------------------------------------
1873   if (DebugMode)
1874     edm::LogVerbatim("Tracklet") << "\n Loop over tracking particles!";
1875 
1876   trueTkMET = 0;
1877   float trueTkMETx = 0;
1878   float trueTkMETy = 0;
1879 
1880   int this_tp = 0;
1881   std::vector<TrackingParticle>::const_iterator iterTP;
1882   for (iterTP = TrackingParticleHandle->begin(); iterTP != TrackingParticleHandle->end(); ++iterTP) {
1883     edm::Ptr<TrackingParticle> tp_ptr(TrackingParticleHandle, this_tp);
1884     this_tp++;
1885 
1886     int tmp_eventid = iterTP->eventId().event();
1887     if (MyProcess != 1 && tmp_eventid > 0)
1888       continue;  //only care about primary interaction
1889 
1890     float tmp_tp_pt = iterTP->pt();
1891     float tmp_tp_eta = iterTP->eta();
1892     float tmp_tp_phi = iterTP->phi();
1893     float tmp_tp_vz = iterTP->vz();
1894     float tmp_tp_vx = iterTP->vx();
1895     float tmp_tp_vy = iterTP->vy();
1896     int tmp_tp_pdgid = iterTP->pdgId();
1897     float tmp_tp_z0_prod = tmp_tp_vz;
1898     float tmp_tp_d0_prod = tmp_tp_vx * sin(tmp_tp_phi) - tmp_tp_vy * cos(tmp_tp_phi);
1899 
1900     if (MyProcess == 13 && abs(tmp_tp_pdgid) != 13)
1901       continue;
1902     if (MyProcess == 11 && abs(tmp_tp_pdgid) != 11)
1903       continue;
1904     if ((MyProcess == 6 || MyProcess == 15 || MyProcess == 211) && abs(tmp_tp_pdgid) != 211)
1905       continue;
1906 
1907     if (tmp_tp_pt < TP_minPt)
1908       continue;
1909     if (std::abs(tmp_tp_eta) > TP_maxEta)
1910       continue;
1911 
1912     // ----------------------------------------------------------------------------------------------
1913     // get d0/z0 propagated back to the IP
1914     float tmp_tp_t = tan(2.0 * atan(1.0) - 2.0 * atan(exp(-tmp_tp_eta)));
1915     float delx = -tmp_tp_vx;
1916     float dely = -tmp_tp_vy;
1917 
1918     float A = 0.01 * 0.5696;
1919     float Kmagnitude = A / tmp_tp_pt;
1920     float tmp_tp_charge = tp_ptr->charge();
1921     float K = Kmagnitude * tmp_tp_charge;
1922     float d = 0;
1923     float tmp_tp_x0p = delx - (d + 1. / (2. * K) * sin(tmp_tp_phi));
1924     float tmp_tp_y0p = dely + (d + 1. / (2. * K) * cos(tmp_tp_phi));
1925     float tmp_tp_rp = sqrt(tmp_tp_x0p * tmp_tp_x0p + tmp_tp_y0p * tmp_tp_y0p);
1926     float tmp_tp_d0 = tmp_tp_charge * tmp_tp_rp - (1. / (2. * K));
1927     tmp_tp_d0 = tmp_tp_d0 * (-1);  //fix d0 sign
1928     const double pi = 4.0 * atan(1.0);
1929     float delphi = tmp_tp_phi - atan2(-K * tmp_tp_x0p, K * tmp_tp_y0p);
1930     if (delphi < -pi)
1931       delphi += 2.0 * pi;
1932     if (delphi > pi)
1933       delphi -= 2.0 * pi;
1934     float tmp_tp_z0 = tmp_tp_vz + tmp_tp_t * delphi / (2.0 * K);
1935     // ----------------------------------------------------------------------------------------------
1936 
1937     if (std::abs(tmp_tp_z0) > TP_maxZ0)
1938       continue;
1939 
1940     // for pions in ttbar, only consider TPs coming from near the IP!
1941     float dxy = sqrt(tmp_tp_vx * tmp_tp_vx + tmp_tp_vy * tmp_tp_vy);
1942     float tmp_tp_dxy = dxy;
1943     if (MyProcess == 6 && (dxy > 1.0))
1944       continue;
1945 
1946     if (DebugMode && (Displaced == "Prompt" || Displaced == "Both"))
1947       edm::LogVerbatim("Tracklet") << "Tracking particle, pt: " << tmp_tp_pt << " eta: " << tmp_tp_eta
1948                                    << " phi: " << tmp_tp_phi << " z0: " << tmp_tp_z0 << " d0: " << tmp_tp_d0
1949                                    << " z_prod: " << tmp_tp_z0_prod << " d_prod: " << tmp_tp_d0_prod
1950                                    << " pdgid: " << tmp_tp_pdgid << " eventID: " << iterTP->eventId().event()
1951                                    << " ttclusters " << MCTruthTTClusterHandle->findTTClusterRefs(tp_ptr).size()
1952                                    << " ttstubs " << MCTruthTTStubHandle->findTTStubRefs(tp_ptr).size() << " tttracks "
1953                                    << MCTruthTTTrackHandle->findTTTrackPtrs(tp_ptr).size();
1954 
1955     // ----------------------------------------------------------------------------------------------
1956     // only consider TPs associated with >= 1 cluster, or >= X stubs, or have stubs in >= X layers (configurable options)
1957     if (MCTruthTTClusterHandle->findTTClusterRefs(tp_ptr).empty()) {
1958       if (DebugMode)
1959         edm::LogVerbatim("Tracklet") << "No matching TTClusters for TP, continuing...";
1960       continue;
1961     }
1962 
1963     std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>>
1964         theStubRefs = MCTruthTTStubHandle->findTTStubRefs(tp_ptr);
1965     int nStubTP = (int)theStubRefs.size();
1966 
1967     // how many layers/disks have stubs?
1968     int hasStubInLayer[11] = {0};
1969     for (auto& theStubRef : theStubRefs) {
1970       DetId detid(theStubRef->getDetId());
1971 
1972       int layer = -1;
1973       if (detid.subdetId() == StripSubdetector::TOB) {
1974         layer = static_cast<int>(tTopo.layer(detid)) - 1;  //fill in array as entries 0-5
1975       } else if (detid.subdetId() == StripSubdetector::TID) {
1976         layer = static_cast<int>(tTopo.layer(detid)) + 5;  //fill in array as entries 6-10
1977       }
1978 
1979       //treat genuine stubs separately (==2 is genuine, ==1 is not)
1980       if (MCTruthTTStubHandle->findTrackingParticlePtr(theStubRef).isNull() && hasStubInLayer[layer] < 2)
1981         hasStubInLayer[layer] = 1;
1982       else
1983         hasStubInLayer[layer] = 2;
1984     }
1985 
1986     int nStubLayerTP = 0;
1987     int nStubLayerTP_g = 0;
1988     for (int isum : hasStubInLayer) {
1989       if (isum >= 1)
1990         nStubLayerTP += 1;
1991       if (isum == 2)
1992         nStubLayerTP_g += 1;
1993     }
1994 
1995     if (DebugMode)
1996       edm::LogVerbatim("Tracklet") << "TP is associated with " << nStubTP << " stubs, and has stubs in " << nStubLayerTP
1997                                    << " different layers/disks, and has GENUINE stubs in " << nStubLayerTP_g
1998                                    << " layers ";
1999 
2000     if (TP_minNStub > 0) {
2001       if (DebugMode)
2002         edm::LogVerbatim("Tracklet") << "Only consider TPs with >= " << TP_minNStub << " stubs";
2003       if (nStubTP < TP_minNStub) {
2004         if (DebugMode)
2005           edm::LogVerbatim("Tracklet") << "TP fails minimum nbr stubs requirement! Continuing...";
2006         continue;
2007       }
2008     }
2009     if (TP_minNStubLayer > 0) {
2010       if (DebugMode)
2011         edm::LogVerbatim("Tracklet") << "Only consider TPs with stubs in >= " << TP_minNStubLayer << " layers/disks";
2012       if (nStubLayerTP < TP_minNStubLayer) {
2013         if (DebugMode)
2014           edm::LogVerbatim("Tracklet") << "TP fails stubs in minimum nbr of layers/disks requirement! Continuing...";
2015         continue;
2016       }
2017     }
2018 
2019     if (tmp_eventid == 0) {
2020       trueTkMETx += tmp_tp_pt * cos(tmp_tp_phi);
2021       trueTkMETy += tmp_tp_pt * sin(tmp_tp_phi);
2022     }
2023 
2024     m_tp_pt->push_back(tmp_tp_pt);
2025     m_tp_eta->push_back(tmp_tp_eta);
2026     m_tp_phi->push_back(tmp_tp_phi);
2027     m_tp_dxy->push_back(tmp_tp_dxy);
2028     m_tp_z0->push_back(tmp_tp_z0);
2029     m_tp_d0->push_back(tmp_tp_d0);
2030     m_tp_z0_prod->push_back(tmp_tp_z0_prod);
2031     m_tp_d0_prod->push_back(tmp_tp_d0_prod);
2032     m_tp_pdgid->push_back(tmp_tp_pdgid);
2033     m_tp_nstub->push_back(nStubTP);
2034     m_tp_eventid->push_back(tmp_eventid);
2035     m_tp_charge->push_back(tmp_tp_charge);
2036 
2037     // ----------------------------------------------------------------------------------------------
2038     // look for L1 tracks (prompt) matched to the tracking particle
2039     if (Displaced == "Prompt" || Displaced == "Both") {
2040       std::vector<edm::Ptr<TTTrack<Ref_Phase2TrackerDigi_>>> matchedTracks =
2041           MCTruthTTTrackHandle->findTTTrackPtrs(tp_ptr);
2042 
2043       int nMatch = 0;
2044       int i_track = -1;
2045       float i_chi2dof = 99999;
2046 
2047       if (!matchedTracks.empty()) {
2048         if (DebugMode && (matchedTracks.size() > 1))
2049           edm::LogVerbatim("Tracklet") << "TrackingParticle has more than one matched L1 track!";
2050 
2051         // ----------------------------------------------------------------------------------------------
2052         // loop over matched L1 tracks
2053         // here, "match" means tracks that can be associated to a TrackingParticle with at least one hit of at least one of its clusters
2054         // https://twiki.cern.ch/twiki/bin/viewauth/CMS/SLHCTrackerTriggerSWTools#MC_truth_for_TTTrack
2055 
2056         for (int it = 0; it < (int)matchedTracks.size(); it++) {
2057           bool tmp_trk_genuine = false;
2058           bool tmp_trk_loosegenuine = false;
2059           if (MCTruthTTTrackHandle->isGenuine(matchedTracks.at(it)))
2060             tmp_trk_genuine = true;
2061           if (MCTruthTTTrackHandle->isLooselyGenuine(matchedTracks.at(it)))
2062             tmp_trk_loosegenuine = true;
2063           if (!tmp_trk_loosegenuine)
2064             continue;
2065 
2066           if (DebugMode) {
2067             if (MCTruthTTTrackHandle->findTrackingParticlePtr(matchedTracks.at(it)).isNull()) {
2068               edm::LogVerbatim("Tracklet") << "track matched to TP is NOT uniquely matched to a TP";
2069             } else {
2070               edm::Ptr<TrackingParticle> my_tp = MCTruthTTTrackHandle->findTrackingParticlePtr(matchedTracks.at(it));
2071               edm::LogVerbatim("Tracklet") << "TP matched to track matched to TP ... tp pt = " << my_tp->p4().pt()
2072                                            << " eta = " << my_tp->momentum().eta()
2073                                            << " phi = " << my_tp->momentum().phi() << " z0 = " << my_tp->vertex().z();
2074             }
2075             edm::LogVerbatim("Tracklet") << "   ... matched L1 track has pt = "
2076                                          << matchedTracks.at(it)->momentum().perp()
2077                                          << " eta = " << matchedTracks.at(it)->momentum().eta()
2078                                          << " phi = " << matchedTracks.at(it)->momentum().phi()
2079                                          << " chi2 = " << matchedTracks.at(it)->chi2()
2080                                          << " consistency = " << matchedTracks.at(it)->stubPtConsistency()
2081                                          << " z0 = " << matchedTracks.at(it)->z0()
2082                                          << " nstub = " << matchedTracks.at(it)->getStubRefs().size();
2083             if (tmp_trk_genuine)
2084               edm::LogVerbatim("Tracklet") << "    (genuine!) ";
2085             if (tmp_trk_loosegenuine)
2086               edm::LogVerbatim("Tracklet") << "    (loose genuine!) ";
2087           }
2088 
2089           std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>>
2090               stubRefs = matchedTracks.at(it)->getStubRefs();
2091           int tmp_trk_nstub = stubRefs.size();
2092 
2093           if (tmp_trk_nstub < L1Tk_minNStub)
2094             continue;
2095 
2096           float dmatch_pt = 999;
2097           float dmatch_eta = 999;
2098           float dmatch_phi = 999;
2099           int match_id = 999;
2100 
2101           edm::Ptr<TrackingParticle> my_tp = MCTruthTTTrackHandle->findTrackingParticlePtr(matchedTracks.at(it));
2102           dmatch_pt = std::abs(my_tp->p4().pt() - tmp_tp_pt);
2103           dmatch_eta = std::abs(my_tp->p4().eta() - tmp_tp_eta);
2104           dmatch_phi = std::abs(my_tp->p4().phi() - tmp_tp_phi);
2105           match_id = my_tp->pdgId();
2106           float tmp_trk_chi2dof = matchedTracks.at(it)->chi2Red();
2107 
2108           // ensure that track is uniquely matched to the TP we are looking at!
2109           if (dmatch_pt < 0.1 && dmatch_eta < 0.1 && dmatch_phi < 0.1 && tmp_tp_pdgid == match_id && tmp_trk_genuine) {
2110             nMatch++;
2111             if (i_track < 0 || tmp_trk_chi2dof < i_chi2dof) {
2112               i_track = it;
2113               i_chi2dof = tmp_trk_chi2dof;
2114             }
2115           }
2116 
2117         }  // end loop over matched L1 tracks
2118       }    // end has at least 1 matched L1 track
2119       // ----------------------------------------------------------------------------------------------
2120 
2121       float tmp_matchtrk_pt = -999;
2122       float tmp_matchtrk_eta = -999;
2123       float tmp_matchtrk_phi = -999;
2124       float tmp_matchtrk_z0 = -999;
2125       float tmp_matchtrk_d0 = -999;
2126       float tmp_matchtrk_chi2 = -999;
2127       float tmp_matchtrk_chi2dof = -999;
2128       float tmp_matchtrk_chi2rphi = -999;
2129       float tmp_matchtrk_chi2rz = -999;
2130       float tmp_matchtrk_bendchi2 = -999;
2131       float tmp_matchtrk_MVA1 = -999;
2132       int tmp_matchtrk_nstub = -999;
2133       int tmp_matchtrk_dhits = -999;
2134       int tmp_matchtrk_lhits = -999;
2135       int tmp_matchtrk_seed = -999;
2136       int tmp_matchtrk_hitpattern = -999;
2137       int tmp_matchtrk_nFitPars = -999;
2138 
2139       if (nMatch > 1 && DebugMode)
2140         edm::LogVerbatim("Tracklet") << "WARNING *** 2 or more matches to genuine L1 tracks ***";
2141 
2142       if (nMatch > 0) {
2143         tmp_matchtrk_pt = matchedTracks.at(i_track)->momentum().perp();
2144         tmp_matchtrk_eta = matchedTracks.at(i_track)->momentum().eta();
2145         tmp_matchtrk_phi = matchedTracks.at(i_track)->momentum().phi();
2146         tmp_matchtrk_z0 = matchedTracks.at(i_track)->z0();
2147         tmp_matchtrk_nFitPars = matchedTracks.at(i_track)->nFitPars();
2148 
2149         if (tmp_matchtrk_nFitPars == 5) {
2150           float tmp_matchtrk_x0 = matchedTracks.at(i_track)->POCA().x();
2151           float tmp_matchtrk_y0 = matchedTracks.at(i_track)->POCA().y();
2152           tmp_matchtrk_d0 = -tmp_matchtrk_x0 * sin(tmp_matchtrk_phi) + tmp_matchtrk_y0 * cos(tmp_matchtrk_phi);
2153           // tmp_matchtrk_d0 = matchedTracks.at(i_track)->d0();
2154         }
2155 
2156         tmp_matchtrk_chi2 = matchedTracks.at(i_track)->chi2();
2157         tmp_matchtrk_chi2dof = matchedTracks.at(i_track)->chi2Red();
2158         tmp_matchtrk_chi2rphi = matchedTracks.at(i_track)->chi2XYRed();
2159         tmp_matchtrk_chi2rz = matchedTracks.at(i_track)->chi2ZRed();
2160         tmp_matchtrk_bendchi2 = matchedTracks.at(i_track)->stubPtConsistency();
2161         tmp_matchtrk_MVA1 = matchedTracks.at(i_track)->trkMVA1();
2162         tmp_matchtrk_nstub = (int)matchedTracks.at(i_track)->getStubRefs().size();
2163         tmp_matchtrk_seed = (int)matchedTracks.at(i_track)->trackSeedType();
2164         tmp_matchtrk_hitpattern = (int)matchedTracks.at(i_track)->hitPattern();
2165 
2166         // ------------------------------------------------------------------------------------------
2167         tmp_matchtrk_dhits = 0;
2168         tmp_matchtrk_lhits = 0;
2169 
2170         std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>>
2171             stubRefs = matchedTracks.at(i_track)->getStubRefs();
2172         int tmp_nstub = stubRefs.size();
2173 
2174         for (int is = 0; is < tmp_nstub; is++) {
2175           DetId detIdStub = tGeom.idToDet((stubRefs.at(is)->clusterRef(0))->getDetId())->geographicalId();
2176           int layer = -999999;
2177           if (detIdStub.subdetId() == StripSubdetector::TOB) {
2178             layer = static_cast<int>(tTopo.layer(detIdStub));
2179             tmp_matchtrk_lhits += pow(10, layer - 1);
2180           } else if (detIdStub.subdetId() == StripSubdetector::TID) {
2181             layer = static_cast<int>(tTopo.layer(detIdStub));
2182             tmp_matchtrk_dhits += pow(10, layer - 1);
2183           }
2184         }
2185       }
2186 
2187       m_tp_nmatch->push_back(nMatch);
2188 
2189       m_matchtrk_pt->push_back(tmp_matchtrk_pt);
2190       m_matchtrk_eta->push_back(tmp_matchtrk_eta);
2191       m_matchtrk_phi->push_back(tmp_matchtrk_phi);
2192       m_matchtrk_z0->push_back(tmp_matchtrk_z0);
2193       m_matchtrk_d0->push_back(tmp_matchtrk_d0);
2194       m_matchtrk_chi2->push_back(tmp_matchtrk_chi2);
2195       m_matchtrk_chi2dof->push_back(tmp_matchtrk_chi2dof);
2196       m_matchtrk_chi2rphi->push_back(tmp_matchtrk_chi2rphi);
2197       m_matchtrk_chi2rz->push_back(tmp_matchtrk_chi2rz);
2198       m_matchtrk_bendchi2->push_back(tmp_matchtrk_bendchi2);
2199       m_matchtrk_MVA1->push_back(tmp_matchtrk_MVA1);
2200       m_matchtrk_nstub->push_back(tmp_matchtrk_nstub);
2201       m_matchtrk_dhits->push_back(tmp_matchtrk_dhits);
2202       m_matchtrk_lhits->push_back(tmp_matchtrk_lhits);
2203       m_matchtrk_seed->push_back(tmp_matchtrk_seed);
2204       m_matchtrk_hitpattern->push_back(tmp_matchtrk_hitpattern);
2205     }
2206 
2207     // ----------------------------------------------------------------------------------------------
2208     // look for L1 tracks (extended) matched to the tracking particle
2209     if (Displaced == "Displaced" || Displaced == "Both") {
2210       L1TrackPtrCollection matchedTracks = MCTruthTTTrackExtendedHandle->findTTTrackPtrs(tp_ptr);
2211 
2212       int nMatch = 0;
2213       int i_track = -1;
2214       float i_chi2dof = 99999;
2215 
2216       if (!matchedTracks.empty()) {
2217         if (DebugMode && (matchedTracks.size() > 1))
2218           edm::LogVerbatim("Tracklet") << "TrackingParticle has more than one matched L1 track!";
2219 
2220         // ----------------------------------------------------------------------------------------------
2221         // loop over matched L1 tracks
2222         // here, "match" means tracks that can be associated to a TrackingParticle with at least one hit of at least one of its clusters
2223         // https://twiki.cern.ch/twiki/bin/viewauth/CMS/SLHCTrackerTriggerSWTools#MC_truth_for_TTTrack
2224 
2225         for (int it = 0; it < (int)matchedTracks.size(); it++) {
2226           bool tmp_trk_genuine = false;
2227           bool tmp_trk_loosegenuine = false;
2228           if (MCTruthTTTrackExtendedHandle->isGenuine(matchedTracks.at(it)))
2229             tmp_trk_genuine = true;
2230           if (MCTruthTTTrackExtendedHandle->isLooselyGenuine(matchedTracks.at(it)))
2231             tmp_trk_loosegenuine = true;
2232           if (!tmp_trk_loosegenuine)
2233             continue;
2234 
2235           if (DebugMode) {
2236             if (MCTruthTTTrackExtendedHandle->findTrackingParticlePtr(matchedTracks.at(it)).isNull()) {
2237               edm::LogVerbatim("Tracklet") << "track matched to TP is NOT uniquely matched to a TP";
2238             } else {
2239               edm::Ptr<TrackingParticle> my_tp =
2240                   MCTruthTTTrackExtendedHandle->findTrackingParticlePtr(matchedTracks.at(it));
2241               edm::LogVerbatim("Tracklet") << "TP matched to track matched to TP ... tp pt = " << my_tp->p4().pt()
2242                                            << " eta = " << my_tp->momentum().eta()
2243                                            << " phi = " << my_tp->momentum().phi() << " z0 = " << my_tp->vertex().z();
2244             }
2245             edm::LogVerbatim("Tracklet") << "   ... matched L1 track has pt = "
2246                                          << matchedTracks.at(it)->momentum().perp()
2247                                          << " eta = " << matchedTracks.at(it)->momentum().eta()
2248                                          << " phi = " << matchedTracks.at(it)->momentum().phi()
2249                                          << " chi2 = " << matchedTracks.at(it)->chi2()
2250                                          << " consistency = " << matchedTracks.at(it)->stubPtConsistency()
2251                                          << " z0 = " << matchedTracks.at(it)->z0()
2252                                          << " nstub = " << matchedTracks.at(it)->getStubRefs().size();
2253             if (tmp_trk_genuine)
2254               edm::LogVerbatim("Tracklet") << "    (genuine!) ";
2255             if (tmp_trk_loosegenuine)
2256               edm::LogVerbatim("Tracklet") << "    (loose genuine!) ";
2257           }
2258 
2259           std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>>
2260               stubRefs = matchedTracks.at(it)->getStubRefs();
2261           int tmp_trk_nstub = stubRefs.size();
2262 
2263           if (tmp_trk_nstub < L1Tk_minNStub)
2264             continue;
2265 
2266           float dmatch_pt = 999;
2267           float dmatch_eta = 999;
2268           float dmatch_phi = 999;
2269           int match_id = 999;
2270 
2271           edm::Ptr<TrackingParticle> my_tp =
2272               MCTruthTTTrackExtendedHandle->findTrackingParticlePtr(matchedTracks.at(it));
2273           dmatch_pt = std::abs(my_tp->p4().pt() - tmp_tp_pt);
2274           dmatch_eta = std::abs(my_tp->p4().eta() - tmp_tp_eta);
2275           dmatch_phi = std::abs(my_tp->p4().phi() - tmp_tp_phi);
2276           match_id = my_tp->pdgId();
2277           float tmp_trk_chi2dof = matchedTracks.at(it)->chi2Red();
2278 
2279           // ensure that track is uniquely matched to the TP we are looking at!
2280           if (dmatch_pt < 0.1 && dmatch_eta < 0.1 && dmatch_phi < 0.1 && tmp_tp_pdgid == match_id && tmp_trk_genuine) {
2281             nMatch++;
2282             if (i_track < 0 || tmp_trk_chi2dof < i_chi2dof) {
2283               i_track = it;
2284               i_chi2dof = tmp_trk_chi2dof;
2285             }
2286           }
2287 
2288         }  // end loop over matched L1 tracks
2289       }    // end has at least 1 matched L1 track
2290       // ----------------------------------------------------------------------------------------------
2291 
2292       float tmp_matchtrkExt_pt = -999;
2293       float tmp_matchtrkExt_eta = -999;
2294       float tmp_matchtrkExt_phi = -999;
2295       float tmp_matchtrkExt_z0 = -999;
2296       float tmp_matchtrkExt_d0 = -999;
2297       float tmp_matchtrkExt_chi2 = -999;
2298       float tmp_matchtrkExt_chi2dof = -999;
2299       float tmp_matchtrkExt_chi2rphi = -999;
2300       float tmp_matchtrkExt_chi2rz = -999;
2301       float tmp_matchtrkExt_bendchi2 = -999;
2302       float tmp_matchtrkExt_MVA = -999;
2303       int tmp_matchtrkExt_nstub = -999;
2304       int tmp_matchtrkExt_dhits = -999;
2305       int tmp_matchtrkExt_lhits = -999;
2306       int tmp_matchtrkExt_seed = -999;
2307       int tmp_matchtrkExt_hitpattern = -999;
2308       int tmp_matchtrkExt_nFitPars = -999;
2309 
2310       if (nMatch > 1 && DebugMode)
2311         edm::LogVerbatim("Tracklet") << "WARNING *** 2 or more matches to genuine L1 tracks ***";
2312 
2313       if (nMatch > 0) {
2314         tmp_matchtrkExt_pt = matchedTracks.at(i_track)->momentum().perp();
2315         tmp_matchtrkExt_eta = matchedTracks.at(i_track)->momentum().eta();
2316         tmp_matchtrkExt_phi = matchedTracks.at(i_track)->momentum().phi();
2317         tmp_matchtrkExt_z0 = matchedTracks.at(i_track)->z0();
2318         tmp_matchtrkExt_nFitPars = matchedTracks.at(i_track)->nFitPars();
2319 
2320         if (tmp_matchtrkExt_nFitPars == 5) {
2321           float tmp_matchtrkExt_x0 = matchedTracks.at(i_track)->POCA().x();
2322           float tmp_matchtrkExt_y0 = matchedTracks.at(i_track)->POCA().y();
2323           tmp_matchtrkExt_d0 =
2324               -tmp_matchtrkExt_x0 * sin(tmp_matchtrkExt_phi) + tmp_matchtrkExt_y0 * cos(tmp_matchtrkExt_phi);
2325           // tmp_matchtrkExt_d0 = matchedTracks.at(i_track)->d0();
2326         }
2327 
2328         tmp_matchtrkExt_chi2 = matchedTracks.at(i_track)->chi2();
2329         tmp_matchtrkExt_chi2dof = matchedTracks.at(i_track)->chi2Red();
2330         tmp_matchtrkExt_chi2rphi = matchedTracks.at(i_track)->chi2XYRed();
2331         tmp_matchtrkExt_chi2rz = matchedTracks.at(i_track)->chi2ZRed();
2332         tmp_matchtrkExt_bendchi2 = matchedTracks.at(i_track)->stubPtConsistency();
2333         tmp_matchtrkExt_MVA = matchedTracks.at(i_track)->trkMVA1();
2334         tmp_matchtrkExt_nstub = (int)matchedTracks.at(i_track)->getStubRefs().size();
2335         tmp_matchtrkExt_seed = (int)matchedTracks.at(i_track)->trackSeedType();
2336         tmp_matchtrkExt_hitpattern = (int)matchedTracks.at(i_track)->hitPattern();
2337 
2338         // ------------------------------------------------------------------------------------------
2339         tmp_matchtrkExt_dhits = 0;
2340         tmp_matchtrkExt_lhits = 0;
2341 
2342         std::vector<edm::Ref<edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>>, TTStub<Ref_Phase2TrackerDigi_>>>
2343             stubRefs = matchedTracks.at(i_track)->getStubRefs();
2344         int tmp_nstub = stubRefs.size();
2345 
2346         for (int is = 0; is < tmp_nstub; is++) {
2347           DetId detIdStub = tGeom.idToDet((stubRefs.at(is)->clusterRef(0))->getDetId())->geographicalId();
2348           int layer = -999999;
2349           if (detIdStub.subdetId() == StripSubdetector::TOB) {
2350             layer = static_cast<int>(tTopo.layer(detIdStub));
2351             tmp_matchtrkExt_lhits += pow(10, layer - 1);
2352           } else if (detIdStub.subdetId() == StripSubdetector::TID) {
2353             layer = static_cast<int>(tTopo.layer(detIdStub));
2354             tmp_matchtrkExt_dhits += pow(10, layer - 1);
2355           }
2356         }
2357       }
2358 
2359       // m_tp_nmatch->push_back(nMatch); //modify to be matches for ext
2360       m_matchtrkExt_pt->push_back(tmp_matchtrkExt_pt);
2361       m_matchtrkExt_eta->push_back(tmp_matchtrkExt_eta);
2362       m_matchtrkExt_phi->push_back(tmp_matchtrkExt_phi);
2363       m_matchtrkExt_z0->push_back(tmp_matchtrkExt_z0);
2364       m_matchtrkExt_d0->push_back(tmp_matchtrkExt_d0);
2365       m_matchtrkExt_chi2->push_back(tmp_matchtrkExt_chi2);
2366       m_matchtrkExt_chi2dof->push_back(tmp_matchtrkExt_chi2dof);
2367       m_matchtrkExt_chi2rphi->push_back(tmp_matchtrkExt_chi2rphi);
2368       m_matchtrkExt_chi2rz->push_back(tmp_matchtrkExt_chi2rz);
2369       m_matchtrkExt_bendchi2->push_back(tmp_matchtrkExt_bendchi2);
2370       m_matchtrkExt_MVA->push_back(tmp_matchtrkExt_MVA);
2371       m_matchtrkExt_nstub->push_back(tmp_matchtrkExt_nstub);
2372       m_matchtrkExt_dhits->push_back(tmp_matchtrkExt_dhits);
2373       m_matchtrkExt_lhits->push_back(tmp_matchtrkExt_lhits);
2374       m_matchtrkExt_seed->push_back(tmp_matchtrkExt_seed);
2375       m_matchtrkExt_hitpattern->push_back(tmp_matchtrkExt_hitpattern);
2376     }
2377   }  //end loop tracking particles
2378   trueTkMET = sqrt(trueTkMETx * trueTkMETx + trueTkMETy * trueTkMETy);
2379 
2380   if (L1PrimaryVertexHandle.isValid()) {
2381     for (vtxIter = L1PrimaryVertexHandle->begin(); vtxIter != L1PrimaryVertexHandle->end(); ++vtxIter) {
2382       m_pv_L1reco->push_back(vtxIter->z0());
2383       m_pv_L1reco_sum->push_back(vtxIter->pt());
2384     }
2385   } else
2386     edm::LogWarning("DataNotFound") << "\nWarning: L1PrimaryVertexHandle not found" << std::endl;
2387 
2388   if (L1PrimaryVertexEmuHandle.isValid()) {
2389     for (vtxEmuIter = L1PrimaryVertexEmuHandle->begin(); vtxEmuIter != L1PrimaryVertexEmuHandle->end(); ++vtxEmuIter) {
2390       m_pv_L1reco_emu->push_back(vtxEmuIter->z0());
2391       m_pv_L1reco_sum_emu->push_back(vtxEmuIter->pt());
2392     }
2393   } else
2394     edm::LogWarning("DataNotFound") << "\nWarning: L1PrimaryVertexEmuHandle not found" << std::endl;
2395 
2396   if (SaveTrackSums) {
2397     if (Displaced == "Prompt" || Displaced == "Both") {
2398       if (L1TkMETHandle.isValid()) {
2399         trkMET = L1TkMETHandle->begin()->etMiss();
2400         trkMETPhi = L1TkMETHandle->begin()->p4().phi();
2401       } else {
2402         edm::LogWarning("DataNotFound") << "\nWarning: tkMET handle not found" << std::endl;
2403       }
2404 
2405       if (L1TkMETEmuHandle.isValid()) {
2406         trkMETEmu = L1TkMETEmuHandle->begin()->hwPt() * l1tmetemu::kStepMETwordEt;
2407         trkMETEmuPhi = L1TkMETEmuHandle->begin()->hwPhi() * l1tmetemu::kStepMETwordPhi;
2408       } else {
2409         edm::LogWarning("DataNotFound") << "\nWarning: tkMETEmu handle not found" << std::endl;
2410       }
2411 
2412       if (L1TkMHTHandle.isValid()) {
2413         trkMHT = L1TkMHTHandle->begin()->EtMiss();
2414         trkHT = L1TkMHTHandle->begin()->etTotal();
2415       } else
2416         edm::LogWarning("DataNotFound") << "\nWarning: tkMHT handle not found" << std::endl;
2417 
2418       if (L1TkMHTEmuHandle.isValid()) {
2419         trkMHTEmu = L1TkMHTEmuHandle->begin()->p4().energy();
2420         trkHTEmu = L1TkMHTEmuHandle->begin()->hwPt() * l1tmhtemu::kStepMHT;
2421         trkMHTEmuPhi = L1TkMHTEmuHandle->begin()->hwPhi() * l1tmhtemu::kStepMHTPhi - M_PI;
2422       } else
2423         edm::LogWarning("DataNotFound") << "\nWarning: tkMHTEmu handle not found" << std::endl;
2424     }  //end prompt-track quantities
2425 
2426     if (Displaced == "Displaced" || Displaced == "Both") {
2427       if (L1TkMETExtendedHandle.isValid()) {
2428         trkMETExt = L1TkMETExtendedHandle->begin()->etMiss();
2429         trkMETPhiExt = L1TkMETExtendedHandle->begin()->p4().phi();
2430       } else {
2431         edm::LogWarning("DataNotFound") << "\nWarning: tkMETExtended handle not found" << std::endl;
2432       }
2433 
2434       if (L1TkMHTExtendedHandle.isValid()) {
2435         trkMHTExt = L1TkMHTExtendedHandle->begin()->EtMiss();
2436         trkHTExt = L1TkMHTExtendedHandle->begin()->etTotal();
2437       } else {
2438         edm::LogWarning("DataNotFound") << "\nWarning: tkMHTExtended handle not found" << std::endl;
2439       }
2440 
2441       if (L1TkMHTEmuExtendedHandle.isValid()) {
2442         trkMHTEmuExt = L1TkMHTEmuExtendedHandle->begin()->p4().energy();
2443         trkHTEmuExt = L1TkMHTEmuExtendedHandle->begin()->hwPt() * l1tmhtemu::kStepMHT;
2444         trkMHTEmuPhiExt = L1TkMHTEmuExtendedHandle->begin()->hwPhi() * l1tmhtemu::kStepMHTPhi;
2445       } else
2446         edm::LogWarning("DataNotFound") << "\nWarning: tkMHTEmuExtended handle not found" << std::endl;
2447     }  //end displaced-track quantities
2448   }
2449 
2450   if (SaveTrackJets) {
2451     if (TrackFastJetsHandle.isValid() && (Displaced == "Prompt" || Displaced == "Both")) {
2452       for (jetIter = TrackFastJetsHandle->begin(); jetIter != TrackFastJetsHandle->end(); ++jetIter) {
2453         m_trkfastjet_vz->push_back(jetIter->jetVtx());
2454         m_trkfastjet_ntracks->push_back(jetIter->trkPtrs().size());
2455         m_trkfastjet_phi->push_back(jetIter->phi());
2456         m_trkfastjet_eta->push_back(jetIter->eta());
2457         m_trkfastjet_pt->push_back(jetIter->pt());
2458         m_trkfastjet_p->push_back(jetIter->p());
2459       }
2460     }
2461     if (TrackFastJetsExtendedHandle.isValid() && (Displaced == "Displaced" || Displaced == "Both")) {
2462       for (jetIter = TrackFastJetsExtendedHandle->begin(); jetIter != TrackFastJetsExtendedHandle->end(); ++jetIter) {
2463         m_trkfastjetExt_vz->push_back(jetIter->jetVtx());
2464         m_trkfastjetExt_ntracks->push_back(jetIter->trkPtrs().size());
2465         m_trkfastjetExt_phi->push_back(jetIter->phi());
2466         m_trkfastjetExt_eta->push_back(jetIter->eta());
2467         m_trkfastjetExt_pt->push_back(jetIter->pt());
2468         m_trkfastjetExt_p->push_back(jetIter->p());
2469       }
2470     }
2471     if (!TrackJetsHandle.isValid() && (Displaced == "Prompt" || Displaced == "Both"))
2472       edm::LogWarning("DataNotFound") << "\nWarning: TrackJetsHandle not found" << std::endl;
2473     if (!TrackJetsExtendedHandle.isValid() && (Displaced == "Displaced" || Displaced == "Both"))
2474       edm::LogWarning("DataNotFound") << "\nWarning: TrackJetsExtendedHandle not found" << std::endl;
2475     if (TrackJetsHandle.isValid() && (Displaced == "Prompt" || Displaced == "Both")) {
2476       for (jetIter = TrackJetsHandle->begin(); jetIter != TrackJetsHandle->end(); ++jetIter) {
2477         m_trkjet_vz->push_back(jetIter->jetVtx());
2478         m_trkjet_ntracks->push_back(jetIter->ntracks());
2479         m_trkjet_phi->push_back(jetIter->phi());
2480         m_trkjet_eta->push_back(jetIter->eta());
2481         m_trkjet_pt->push_back(jetIter->pt());
2482         m_trkjet_p->push_back(jetIter->p());
2483         m_trkjet_nDisplaced->push_back(jetIter->nDisptracks());
2484         m_trkjet_nTight->push_back(jetIter->nTighttracks());
2485         m_trkjet_nTightDisplaced->push_back(jetIter->nTightDisptracks());
2486       }
2487     }
2488     if (TrackJetsExtendedHandle.isValid() && (Displaced == "Displaced" || Displaced == "Both")) {
2489       for (jetIter = TrackJetsExtendedHandle->begin(); jetIter != TrackJetsExtendedHandle->end(); ++jetIter) {
2490         m_trkjetExt_vz->push_back(jetIter->jetVtx());
2491         m_trkjetExt_ntracks->push_back(jetIter->ntracks());
2492         m_trkjetExt_phi->push_back(jetIter->phi());
2493         m_trkjetExt_eta->push_back(jetIter->eta());
2494         m_trkjetExt_pt->push_back(jetIter->pt());
2495         m_trkjetExt_p->push_back(jetIter->p());
2496         m_trkjetExt_nDisplaced->push_back(jetIter->nDisptracks());
2497         m_trkjetExt_nTight->push_back(jetIter->nTighttracks());
2498         m_trkjetExt_nTightDisplaced->push_back(jetIter->nTightDisptracks());
2499       }
2500     }
2501 
2502     if (!TrackJetsEmuHandle.isValid() && (Displaced == "Prompt" || Displaced == "Both"))
2503       edm::LogWarning("DataNotFound") << "\nWarning: TrackJetsEmuHandle not found" << std::endl;
2504     else if (TrackJetsEmuHandle.isValid() && (Displaced == "Prompt" || Displaced == "Both")) {
2505       for (jetemIter = TrackJetsEmuHandle->begin(); jetemIter != TrackJetsEmuHandle->end(); ++jetemIter) {
2506         m_trkjetem_ntracks->push_back(jetemIter->nt());
2507         m_trkjetem_phi->push_back(jetemIter->glbphi());
2508         m_trkjetem_eta->push_back(jetemIter->glbeta());
2509         m_trkjetem_pt->push_back(jetemIter->pt());
2510         m_trkjetem_z->push_back(jetemIter->z0());
2511         m_trkjetem_nxtracks->push_back(jetemIter->xt());
2512       }
2513     }
2514     if (!TrackJetsExtendedEmuHandle.isValid() && (Displaced == "Displaced" || Displaced == "Both"))
2515       edm::LogWarning("DataNotFound") << "\nWarning: TrackJetsExtendedEmuHandle not found" << std::endl;
2516     else if (TrackJetsExtendedEmuHandle.isValid() && (Displaced == "Displaced" || Displaced == "Both")) {
2517       for (jetemIter = TrackJetsExtendedEmuHandle->begin(); jetemIter != TrackJetsExtendedEmuHandle->end();
2518            ++jetemIter) {
2519         m_trkjetemExt_ntracks->push_back(jetemIter->nt());
2520         m_trkjetemExt_phi->push_back(jetemIter->glbphi());
2521         m_trkjetemExt_eta->push_back(jetemIter->glbeta());
2522         m_trkjetemExt_pt->push_back(jetemIter->pt());
2523         m_trkjetemExt_z->push_back(jetemIter->z0());
2524         m_trkjetemExt_nxtracks->push_back(jetemIter->xt());
2525       }
2526     }
2527   }  // end track jets
2528 
2529   eventTree->Fill();
2530 }  // end of analyze()
2531 
2532 int L1TrackObjectNtupleMaker::getSelectedTrackIndex(const L1TrackRef& trackRef,
2533                                                     const edm::Handle<L1TrackRefCollection>& selectedTrackRefs) const {
2534   auto it = std::find_if(selectedTrackRefs->begin(), selectedTrackRefs->end(), [&trackRef](L1TrackRef const& obj) {
2535     return obj == trackRef;
2536   });
2537   if (it != selectedTrackRefs->end())
2538     return std::distance(selectedTrackRefs->begin(), it);
2539   else
2540     return -1;
2541 }
2542 
2543 ///////////////////////////
2544 // DEFINE THIS AS A PLUG-IN
2545 DEFINE_FWK_MODULE(L1TrackObjectNtupleMaker);