Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-12 23:01:03

0001 // system includes
0002 #include <memory>
0003 #include <cmath>
0004 #include <fmt/printf.h>
0005 
0006 // user include files
0007 #include "Alignment/OfflineValidation/interface/DiLeptonVertexHelpers.h"
0008 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0009 #include "DataFormats/Candidate/interface/Particle.h"
0010 #include "DataFormats/TrackReco/interface/Track.h"
0011 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0012 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Framework/interface/Frameworkfwd.h"
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 #include "FWCore/Utilities/interface/InputTag.h"
0020 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0021 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0022 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0023 
0024 // ROOT includes
0025 #include "TH1D.h"
0026 #include "TH2D.h"
0027 #include "TH3D.h"
0028 
0029 namespace DiMuonValid {
0030   using LV = reco::Particle::LorentzVector;
0031 }
0032 //
0033 // class declaration
0034 //
0035 class DiMuonValidation : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0036 public:
0037   explicit DiMuonValidation(const edm::ParameterSet& pset)
0038       : eBeam_(pset.getParameter<double>("eBeam")),
0039         compressionSettings_(pset.getUntrackedParameter<int>("compressionSettings", -1)),
0040         TkTag_(pset.getParameter<std::string>("TkTag")),
0041         pair_mass_min_(pset.getParameter<double>("Pair_mass_min")),
0042         pair_mass_max_(pset.getParameter<double>("Pair_mass_max")),
0043         pair_mass_nbins_(pset.getParameter<int>("Pair_mass_nbins")),
0044         pair_etaminpos_(pset.getParameter<double>("Pair_etaminpos")),
0045         pair_etamaxpos_(pset.getParameter<double>("Pair_etamaxpos")),
0046         pair_etaminneg_(pset.getParameter<double>("Pair_etaminneg")),
0047         pair_etamaxneg_(pset.getParameter<double>("Pair_etamaxneg")),
0048         variable_CosThetaCS_xmin_(pset.getParameter<double>("Variable_CosThetaCS_xmin")),
0049         variable_CosThetaCS_xmax_(pset.getParameter<double>("Variable_CosThetaCS_xmax")),
0050         variable_CosThetaCS_nbins_(pset.getParameter<int>("Variable_CosThetaCS_nbins")),
0051         variable_DeltaEta_xmin_(pset.getParameter<double>("Variable_DeltaEta_xmin")),
0052         variable_DeltaEta_xmax_(pset.getParameter<double>("Variable_DeltaEta_xmax")),
0053         variable_DeltaEta_nbins_(pset.getParameter<int>("Variable_DeltaEta_nbins")),
0054         variable_EtaMinus_xmin_(pset.getParameter<double>("Variable_EtaMinus_xmin")),
0055         variable_EtaMinus_xmax_(pset.getParameter<double>("Variable_EtaMinus_xmax")),
0056         variable_EtaMinus_nbins_(pset.getParameter<int>("Variable_EtaMinus_nbins")),
0057         variable_EtaPlus_xmin_(pset.getParameter<double>("Variable_EtaPlus_xmin")),
0058         variable_EtaPlus_xmax_(pset.getParameter<double>("Variable_EtaPlus_xmax")),
0059         variable_EtaPlus_nbins_(pset.getParameter<int>("Variable_EtaPlus_nbins")),
0060         variable_PhiCS_xmin_(pset.getParameter<double>("Variable_PhiCS_xmin")),
0061         variable_PhiCS_xmax_(pset.getParameter<double>("Variable_PhiCS_xmax")),
0062         variable_PhiCS_nbins_(pset.getParameter<int>("Variable_PhiCS_nbins")),
0063         variable_PhiMinus_xmin_(pset.getParameter<double>("Variable_PhiMinus_xmin")),
0064         variable_PhiMinus_xmax_(pset.getParameter<double>("Variable_PhiMinus_xmax")),
0065         variable_PhiMinus_nbins_(pset.getParameter<int>("Variable_PhiMinus_nbins")),
0066         variable_PhiPlus_xmin_(pset.getParameter<double>("Variable_PhiPlus_xmin")),
0067         variable_PhiPlus_xmax_(pset.getParameter<double>("Variable_PhiPlus_xmax")),
0068         variable_PhiPlus_nbins_(pset.getParameter<int>("Variable_PhiPlus_nbins")),
0069         variable_PairPt_xmin_(pset.getParameter<double>("Variable_PairPt_xmin")),
0070         variable_PairPt_xmax_(pset.getParameter<double>("Variable_PairPt_xmax")),
0071         variable_PairPt_nbins_(pset.getParameter<int>("Variable_PairPt_nbins")) {
0072     usesResource(TFileService::kSharedResource);
0073     theTrackCollectionToken_ = consumes<reco::TrackCollection>(TkTag_);
0074 
0075     variables_min_[Variable::CosThetaCS] = variable_CosThetaCS_xmin_;
0076     variables_min_[Variable::DeltaEta] = variable_DeltaEta_xmin_;
0077     variables_min_[Variable::EtaMinus] = variable_EtaMinus_xmin_;
0078     variables_min_[Variable::EtaPlus] = variable_EtaPlus_xmin_;
0079     variables_min_[Variable::PhiCS] = variable_PhiCS_xmin_;
0080     variables_min_[Variable::PhiMinus] = variable_PhiMinus_xmin_;
0081     variables_min_[Variable::PhiPlus] = variable_PhiPlus_xmin_;
0082     variables_min_[Variable::Pt] = variable_PairPt_xmin_;
0083 
0084     variables_max_[Variable::CosThetaCS] = variable_CosThetaCS_xmax_;
0085     variables_max_[Variable::DeltaEta] = variable_DeltaEta_xmax_;
0086     variables_max_[Variable::EtaMinus] = variable_EtaMinus_xmax_;
0087     variables_max_[Variable::EtaPlus] = variable_EtaPlus_xmax_;
0088     variables_max_[Variable::PhiCS] = variable_PhiCS_xmax_;
0089     variables_max_[Variable::PhiMinus] = variable_PhiMinus_xmax_;
0090     variables_max_[Variable::PhiPlus] = variable_PhiPlus_xmax_;
0091     variables_max_[Variable::Pt] = variable_PairPt_xmax_;
0092 
0093     variables_bins_number_[Variable::CosThetaCS] = variable_CosThetaCS_nbins_;
0094     variables_bins_number_[Variable::DeltaEta] = variable_DeltaEta_nbins_;
0095     variables_bins_number_[Variable::EtaMinus] = variable_EtaMinus_nbins_;
0096     variables_bins_number_[Variable::EtaPlus] = variable_EtaPlus_nbins_;
0097     variables_bins_number_[Variable::PhiCS] = variable_PhiCS_nbins_;
0098     variables_bins_number_[Variable::PhiMinus] = variable_PhiMinus_nbins_;
0099     variables_bins_number_[Variable::PhiPlus] = variable_PhiPlus_nbins_;
0100     variables_bins_number_[Variable::Pt] = variable_PairPt_nbins_;
0101   }
0102 
0103   ~DiMuonValidation() override = default;
0104 
0105   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0106 
0107   // enumeration to contain the variables to plot
0108   enum Variable {
0109     CosThetaCS = 0,
0110     DeltaEta = 1,
0111     EtaMinus = 2,
0112     EtaPlus = 3,
0113     PhiCS = 4,
0114     PhiMinus = 5,
0115     PhiPlus = 6,
0116     Pt = 7,
0117     VarNumber = 8
0118   };
0119 
0120   static constexpr double mu_mass2_ = 0.105658 * 0.105658;  //The invariant mass of muon 105.658MeV
0121 
0122 private:
0123   void beginJob() override;
0124   void analyze(const edm::Event&, const edm::EventSetup&) override;
0125 
0126   // ----------member data ---------------------------
0127   float eBeam_;
0128   int compressionSettings_;
0129   std::string TkTag_;
0130 
0131   double pair_mass_min_;
0132   double pair_mass_max_;
0133   int pair_mass_nbins_;
0134   double pair_etaminpos_;
0135   double pair_etamaxpos_;
0136   double pair_etaminneg_;
0137   double pair_etamaxneg_;
0138 
0139   double variable_CosThetaCS_xmin_;
0140   double variable_CosThetaCS_xmax_;
0141   int variable_CosThetaCS_nbins_;
0142 
0143   double variable_DeltaEta_xmin_;
0144   double variable_DeltaEta_xmax_;
0145   int variable_DeltaEta_nbins_;
0146 
0147   double variable_EtaMinus_xmin_;
0148   double variable_EtaMinus_xmax_;
0149   int variable_EtaMinus_nbins_;
0150 
0151   double variable_EtaPlus_xmin_;
0152   double variable_EtaPlus_xmax_;
0153   int variable_EtaPlus_nbins_;
0154 
0155   double variable_PhiCS_xmin_;
0156   double variable_PhiCS_xmax_;
0157   int variable_PhiCS_nbins_;
0158 
0159   double variable_PhiMinus_xmin_;
0160   double variable_PhiMinus_xmax_;
0161   int variable_PhiMinus_nbins_;
0162 
0163   double variable_PhiPlus_xmin_;
0164   double variable_PhiPlus_xmax_;
0165   int variable_PhiPlus_nbins_;
0166 
0167   double variable_PairPt_xmin_;
0168   double variable_PairPt_xmax_;
0169   int variable_PairPt_nbins_;
0170 
0171   edm::EDGetTokenT<reco::TrackCollection> theTrackCollectionToken_;
0172 
0173   TH1F* th1f_mass;
0174   TH2D* th2d_mass_variables_[Variable::VarNumber];  // actual histograms
0175 
0176   std::string variables_name_[Variable::VarNumber] = {
0177       "CosThetaCS", "DeltaEta", "EtaMinus", "EtaPlus", "PhiCS", "PhiMinus", "PhiPlus", "Pt"};
0178 
0179   int variables_bins_number_[Variable::VarNumber];  // = {20, 20, 12, 12, 20, 16, 16, 100};
0180   double variables_min_[Variable::VarNumber];       // = {-1, -4.8, -2.4, -2.4, -M_PI / 2, -M_PI, -M_PI, 0};
0181   double variables_max_[Variable::VarNumber];       // = {+1, +4.8, +2.4, +2.4, +M_PI / 2, +M_PI, +M_PI, 100};
0182 
0183   DiLeptonHelp::PlotsVsDiLeptonRegion InvMassInEtaBins = DiLeptonHelp::PlotsVsDiLeptonRegion(1.5);
0184   DiLeptonHelp::PlotsVsDiLeptonRegion InvMassVsPhiPlusInEtaBins = DiLeptonHelp::PlotsVsDiLeptonRegion(1.5);
0185   DiLeptonHelp::PlotsVsDiLeptonRegion InvMassVsPhiMinusInEtaBins = DiLeptonHelp::PlotsVsDiLeptonRegion(1.5);
0186   DiLeptonHelp::PlotsVsDiLeptonRegion InvMassVsCosThetaCSInEtaBins = DiLeptonHelp::PlotsVsDiLeptonRegion(1.5);
0187 };
0188 
0189 //
0190 // member functions
0191 //
0192 
0193 // ------------ method called for each event  ------------
0194 void DiMuonValidation::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0195   using namespace edm;
0196 
0197   const reco::TrackCollection& tC = iEvent.get(theTrackCollectionToken_);
0198 
0199   DiMuonValid::LV LV_mother(0., 0., 0., 0.);
0200   for (reco::TrackCollection::const_iterator track1 = tC.begin(); track1 != tC.end(); track1++) {
0201     DiMuonValid::LV LV_track1(track1->px(),
0202                               track1->py(),
0203                               track1->pz(),
0204                               sqrt((track1->p() * track1->p()) + mu_mass2_));  //old 106
0205 
0206     for (reco::TrackCollection::const_iterator track2 = track1 + 1; track2 != tC.end(); track2++) {
0207       if (track1->charge() == track2->charge()) {
0208         continue;
0209       }  // only reconstruct opposite charge pair
0210 
0211       DiMuonValid::LV LV_track2(
0212           track2->px(), track2->py(), track2->pz(), sqrt((track2->p() * track2->p()) + mu_mass2_));
0213 
0214       LV_mother = LV_track1 + LV_track2;
0215       double mother_mass = LV_mother.M();
0216       th1f_mass->Fill(mother_mass);
0217       double mother_pt = LV_mother.Pt();
0218 
0219       int charge1 = track1->charge();
0220       double etaMu1 = track1->eta();
0221       double phiMu1 = track1->phi();
0222       double ptMu1 = track1->pt();
0223 
0224       int charge2 = track2->charge();
0225       double etaMu2 = track2->eta();
0226       double phiMu2 = track2->phi();
0227       double ptMu2 = track2->pt();
0228 
0229       if (charge1 < 0) {  // use Mu+ for charge1, Mu- for charge2
0230         std::swap(charge1, charge2);
0231         std::swap(etaMu1, etaMu2);
0232         std::swap(phiMu1, phiMu2);
0233         std::swap(ptMu1, ptMu2);
0234       }
0235 
0236       const auto& tplus = track1->charge() > 0 ? track1 : track2;
0237       const auto& tminus = track1->charge() < 0 ? track1 : track2;
0238       TLorentzVector p4_tplus(tplus->px(), tplus->py(), tplus->pz(), sqrt((tplus->p() * tplus->p()) + mu_mass2_));
0239       TLorentzVector p4_tminus(tminus->px(), tminus->py(), tminus->pz(), sqrt((tminus->p() * tminus->p()) + mu_mass2_));
0240       std::pair<TLorentzVector, TLorentzVector> tktk_p4 = std::make_pair(p4_tplus, p4_tminus);
0241       InvMassInEtaBins.fillTH1Plots(mother_mass, tktk_p4);
0242       InvMassVsPhiPlusInEtaBins.fillTH2Plots(mother_mass, phiMu1, tktk_p4);
0243       InvMassVsPhiMinusInEtaBins.fillTH2Plots(mother_mass, phiMu2, tktk_p4);
0244 
0245       //eta cut
0246       if (etaMu1 < pair_etaminpos_ || etaMu1 > pair_etamaxpos_ || etaMu2 < pair_etaminneg_ ||
0247           etaMu2 > pair_etamaxneg_) {
0248         continue;
0249       }
0250 
0251       double delta_eta = etaMu1 - etaMu2;
0252 
0253       double muplus = 1.0 / sqrt(2.0) * (LV_track1.E() + LV_track1.Z());
0254       double muminus = 1.0 / sqrt(2.0) * (LV_track1.E() - LV_track1.Z());
0255       double mubarplus = 1.0 / sqrt(2.0) * (LV_track2.E() + LV_track2.Z());
0256       double mubarminus = 1.0 / sqrt(2.0) * (LV_track2.E() - LV_track2.Z());
0257       //double costheta = 2.0 / Q.mag() / sqrt(pow(Q.mag(), 2) + pow(Q.Pt(), 2)) * (muplus * mubarminus - muminus * mubarplus);
0258       double costhetaCS = 2.0 / LV_mother.mag() / sqrt(pow(LV_mother.mag(), 2) + pow(LV_mother.Pt(), 2)) *
0259                           (muplus * mubarminus - muminus * mubarplus);
0260 
0261       InvMassVsCosThetaCSInEtaBins.fillTH2Plots(mother_mass, costhetaCS, tktk_p4);
0262 
0263       DiMuonValid::LV Pbeam(0., 0., eBeam_, eBeam_);
0264       auto R = Pbeam.Vect().Cross(LV_mother.Vect());
0265       auto Runit = R.Unit();
0266       auto Qt = LV_mother.Vect();
0267       Qt.SetZ(0);
0268       auto Qtunit = Qt.Unit();
0269       DiMuonValid::LV D(LV_track1 - LV_track2);
0270       auto Dt = D.Vect();
0271       Dt.SetZ(0);
0272       double tanphi =
0273           sqrt(pow(LV_mother.mag(), 2) + pow(LV_mother.Pt(), 2)) / LV_mother.mag() * Dt.Dot(Runit) / Dt.Dot(Qtunit);
0274       double phiCS = atan(tanphi);
0275 
0276       if (mother_mass > pair_mass_min_ && mother_mass < pair_mass_max_) {
0277         th2d_mass_variables_[Variable::CosThetaCS]->Fill(mother_mass, costhetaCS, 1);
0278         th2d_mass_variables_[Variable::DeltaEta]->Fill(mother_mass, delta_eta, 1);
0279         th2d_mass_variables_[Variable::EtaMinus]->Fill(mother_mass, etaMu2, 1);
0280         th2d_mass_variables_[Variable::EtaPlus]->Fill(mother_mass, etaMu1, 1);
0281         th2d_mass_variables_[Variable::PhiCS]->Fill(mother_mass, phiCS, 1);
0282         th2d_mass_variables_[Variable::PhiMinus]->Fill(mother_mass, phiMu2, 1);
0283         th2d_mass_variables_[Variable::PhiPlus]->Fill(mother_mass, phiMu1, 1);
0284         th2d_mass_variables_[Variable::Pt]->Fill(mother_mass, mother_pt, 1);
0285       }
0286     }
0287   }
0288 }
0289 
0290 // ------------ method called once each job just before starting event loop  ------------
0291 void DiMuonValidation::beginJob() {
0292   edm::Service<TFileService> fs;
0293   if (compressionSettings_ > 0) {
0294     fs->file().SetCompressionSettings(compressionSettings_);
0295   }
0296 
0297   th1f_mass = fs->make<TH1F>("hMass", "mass;m_{#mu#mu} [GeV];events", 200, 0., 200.);
0298 
0299   for (int i = 0; i < Variable::VarNumber; i++) {
0300     std::string th2d_name = fmt::sprintf("th2d_mass_%s", variables_name_[i].c_str());
0301     th2d_mass_variables_[i] = fs->make<TH2D>(th2d_name.c_str(),
0302                                              th2d_name.c_str(),
0303                                              pair_mass_nbins_,
0304                                              pair_mass_min_,
0305                                              pair_mass_max_,
0306                                              variables_bins_number_[i],
0307                                              variables_min_[i],
0308                                              variables_max_[i]);
0309   }
0310 
0311   // Z-> mm mass in eta bins
0312   TFileDirectory dirResMassEta = fs->mkdir("TkTkMassInEtaBins");
0313   InvMassInEtaBins.bookSet(dirResMassEta, th1f_mass);
0314 
0315   // Z->mm mass vs cos phi Collins-Soper in eta bins
0316   TFileDirectory dirResMassVsCosThetaCSInEta = fs->mkdir("TkTkMassVsCosThetaCSInEtaBins");
0317   InvMassVsCosThetaCSInEtaBins.bookSet(dirResMassVsCosThetaCSInEta, th2d_mass_variables_[Variable::CosThetaCS]);
0318 
0319   // Z->mm mass vs phi minus in eta bins
0320   TFileDirectory dirResMassVsPhiMinusInEta = fs->mkdir("TkTkMassVsPhiMinusInEtaBins");
0321   InvMassVsPhiMinusInEtaBins.bookSet(dirResMassVsPhiMinusInEta, th2d_mass_variables_[Variable::PhiMinus]);
0322 
0323   // Z->mm mass vs phi plus in eta bins
0324   TFileDirectory dirResMassVsPhiPlusInEta = fs->mkdir("TkTkMassVsPhiPlusInEtaBins");
0325   InvMassVsPhiPlusInEtaBins.bookSet(dirResMassVsPhiPlusInEta, th2d_mass_variables_[Variable::PhiPlus]);
0326 }
0327 
0328 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0329 void DiMuonValidation::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0330   edm::ParameterSetDescription desc;
0331   desc.setComment("Validates alignment payloads by evaluating bias in Z->mm mass distributions");
0332   desc.addUntracked<int>("compressionSettings", -1);
0333 
0334   desc.add<double>("eBeam", 3500.)->setComment("beam energy in GeV");
0335   desc.add<std::string>("TkTag", "ALCARECOTkAlZMuMu");
0336 
0337   desc.add<double>("Pair_mass_min", 60);
0338   desc.add<double>("Pair_mass_max", 120);
0339   desc.add<int>("Pair_mass_nbins", 120);
0340   desc.add<double>("Pair_etaminpos", -2.4);
0341   desc.add<double>("Pair_etamaxpos", 2.4);
0342   desc.add<double>("Pair_etaminneg", -2.4);
0343   desc.add<double>("Pair_etamaxneg", 2.4);
0344 
0345   desc.add<double>("Variable_CosThetaCS_xmin", -1.);
0346   desc.add<double>("Variable_CosThetaCS_xmax", 1.);
0347   desc.add<int>("Variable_CosThetaCS_nbins", 20);
0348 
0349   desc.add<double>("Variable_DeltaEta_xmin", -4.8);
0350   desc.add<double>("Variable_DeltaEta_xmax", 4.8);
0351   desc.add<int>("Variable_DeltaEta_nbins", 20);
0352 
0353   desc.add<double>("Variable_EtaMinus_xmin", -2.4);
0354   desc.add<double>("Variable_EtaMinus_xmax", 2.4);
0355   desc.add<int>("Variable_EtaMinus_nbins", 12);
0356 
0357   desc.add<double>("Variable_EtaPlus_xmin", -2.4);
0358   desc.add<double>("Variable_EtaPlus_xmax", 2.4);
0359   desc.add<int>("Variable_EtaPlus_nbins", 12);
0360 
0361   desc.add<double>("Variable_PhiCS_xmin", -M_PI / 2);
0362   desc.add<double>("Variable_PhiCS_xmax", M_PI / 2);
0363   desc.add<int>("Variable_PhiCS_nbins", 20);
0364 
0365   desc.add<double>("Variable_PhiMinus_xmin", -M_PI);
0366   desc.add<double>("Variable_PhiMinus_xmax", M_PI);
0367   desc.add<int>("Variable_PhiMinus_nbins", 16);
0368 
0369   desc.add<double>("Variable_PhiPlus_xmin", -M_PI);
0370   desc.add<double>("Variable_PhiPlus_xmax", M_PI);
0371   desc.add<int>("Variable_PhiPlus_nbins", 16);
0372 
0373   desc.add<double>("Variable_PairPt_xmin", 0.);
0374   desc.add<double>("Variable_PairPt_xmax", 100.);
0375   desc.add<int>("Variable_PairPt_nbins", 100);
0376 
0377   descriptions.addWithDefaultLabel(desc);
0378 }
0379 
0380 //define this as a plug-in
0381 DEFINE_FWK_MODULE(DiMuonValidation);