CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Validation/RecoMuon/plugins/MuonTrackValidatorBase.h

Go to the documentation of this file.
00001 #ifndef MuonTrackValidatorBase_h
00002 #define MuonTrackValidatorBase_h
00003 
00011 #include <memory>
00012 
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/Framework/interface/ESHandle.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 
00017 #include "MagneticField/Engine/interface/MagneticField.h" 
00018 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" 
00019 
00020 #include "SimTracker/TrackAssociation/interface/TrackAssociatorByChi2.h"
00021 
00022 #include "DQMServices/Core/interface/DQMStore.h"
00023 #include "DQMServices/Core/interface/MonitorElement.h"
00024 #include "FWCore/ServiceRegistry/interface/Service.h"
00025 
00026 #include "CommonTools/RecoAlgos/interface/RecoTrackSelector.h"
00027 #include "CommonTools/RecoAlgos/interface/TrackingParticleSelector.h"
00028 #include "CommonTools/RecoAlgos/interface/CosmicTrackingParticleSelector.h"
00029 
00030 #include <iostream>
00031 #include <sstream>
00032 #include <string>
00033 #include <TH1F.h>
00034 #include <TH2F.h>
00035 
00036 class MuonTrackValidatorBase {
00037  public:
00039   MuonTrackValidatorBase(const edm::ParameterSet& pset):
00040     label(pset.getParameter< std::vector<edm::InputTag> >("label")),
00041     usetracker(pset.getParameter<bool>("usetracker")),
00042     usemuon(pset.getParameter<bool>("usemuon")),
00043     bsSrc(pset.getParameter< edm::InputTag >("beamSpot")),
00044     label_tp_effic(pset.getParameter< edm::InputTag >("label_tp_effic")),
00045     label_tp_fake(pset.getParameter< edm::InputTag >("label_tp_fake")),
00046     associators(pset.getParameter< std::vector<std::string> >("associators")),
00047     out(pset.getParameter<std::string>("outputFile")),   
00048     parametersDefiner(pset.getParameter<std::string>("parametersDefiner")),
00049     min(pset.getParameter<double>("min")),
00050     max(pset.getParameter<double>("max")),
00051     nint(pset.getParameter<int>("nint")),
00052     useFabs(pset.getParameter<bool>("useFabsEta")),
00053     minpT(pset.getParameter<double>("minpT")),
00054     maxpT(pset.getParameter<double>("maxpT")),
00055     nintpT(pset.getParameter<int>("nintpT")),
00056     minHit(pset.getParameter<double>("minHit")),
00057     maxHit(pset.getParameter<double>("maxHit")),
00058     nintHit(pset.getParameter<int>("nintHit")),
00059     minPhi(pset.getParameter<double>("minPhi")),
00060     maxPhi(pset.getParameter<double>("maxPhi")),
00061     nintPhi(pset.getParameter<int>("nintPhi")),
00062     minDxy(pset.getParameter<double>("minDxy")),
00063     maxDxy(pset.getParameter<double>("maxDxy")),
00064     nintDxy(pset.getParameter<int>("nintDxy")),
00065     minDz(pset.getParameter<double>("minDz")),
00066     maxDz(pset.getParameter<double>("maxDz")),
00067     nintDz(pset.getParameter<int>("nintDz")),
00068     minVertpos(pset.getParameter<double>("minVertpos")),
00069     maxVertpos(pset.getParameter<double>("maxVertpos")),
00070     nintVertpos(pset.getParameter<int>("nintVertpos")),
00071     minZpos(pset.getParameter<double>("minZpos")),
00072     maxZpos(pset.getParameter<double>("maxZpos")),
00073     nintZpos(pset.getParameter<int>("nintZpos")),
00074     useInvPt(pset.getParameter<bool>("useInvPt")),
00075     //
00076     ptRes_rangeMin(pset.getParameter<double>("ptRes_rangeMin")),
00077     ptRes_rangeMax(pset.getParameter<double>("ptRes_rangeMax")),
00078     phiRes_rangeMin(pset.getParameter<double>("phiRes_rangeMin")),
00079     phiRes_rangeMax(pset.getParameter<double>("phiRes_rangeMax")),
00080     cotThetaRes_rangeMin(pset.getParameter<double>("cotThetaRes_rangeMin")),
00081     cotThetaRes_rangeMax(pset.getParameter<double>("cotThetaRes_rangeMax")),
00082     dxyRes_rangeMin(pset.getParameter<double>("dxyRes_rangeMin")),
00083     dxyRes_rangeMax(pset.getParameter<double>("dxyRes_rangeMax")),
00084     dzRes_rangeMin(pset.getParameter<double>("dzRes_rangeMin")),
00085     dzRes_rangeMax(pset.getParameter<double>("dzRes_rangeMax")),
00086     ptRes_nbin(pset.getParameter<int>("ptRes_nbin")),
00087     cotThetaRes_nbin(pset.getParameter<int>("cotThetaRes_nbin")),
00088     phiRes_nbin(pset.getParameter<int>("phiRes_nbin")),
00089     dxyRes_nbin(pset.getParameter<int>("dxyRes_nbin")),
00090     dzRes_nbin(pset.getParameter<int>("dzRes_nbin")),
00091     ignoremissingtkcollection_(pset.getUntrackedParameter<bool>("ignoremissingtrackcollection",false)),
00092     skipHistoFit(pset.getUntrackedParameter<bool>("skipHistoFit",false)),
00093     useLogPt(pset.getUntrackedParameter<bool>("useLogPt",false))
00094     //
00095     {
00096       dbe_ = edm::Service<DQMStore>().operator->();
00097       if(useLogPt){
00098         maxpT=log10(maxpT);
00099         minpT=log10(minpT);
00100       }
00101     }
00102   
00104   virtual ~MuonTrackValidatorBase(){ }
00105   
00106   virtual void doProfileX(TH2 * th2, MonitorElement* me){
00107     if (th2->GetNbinsX()==me->getNbinsX()){
00108       TProfile * p1 = (TProfile*) th2->ProfileX();
00109       p1->Copy(*me->getTProfile());
00110       delete p1;
00111     } else {
00112       throw cms::Exception("MuonTrackValidator") << "Different number of bins!";
00113     }
00114   }
00115 
00116   virtual void doProfileX(MonitorElement * th2m, MonitorElement* me) {
00117     doProfileX(th2m->getTH2F(), me);
00118   }
00119 
00120   virtual double getEta(double eta) {
00121     if (useFabs) return fabs(eta);
00122     else return eta;
00123   }
00124 
00125   virtual double getPt(double pt) {
00126     if (useInvPt && pt!=0) return 1/pt;
00127     else return pt;
00128   }
00129   
00130   void fillPlotFromVector(MonitorElement* h, std::vector<int>& vec) {
00131     for (unsigned int j=0; j<vec.size(); j++){
00132       h->setBinContent(j+1, vec[j]);
00133     }
00134   }
00135 
00136   void fillPlotFromVectors(MonitorElement* h, std::vector<int>& numerator, std::vector<int>& denominator,std::string type){
00137     double value,err;
00138     for (unsigned int j=0; j<numerator.size(); j++){
00139       if (denominator[j]!=0){
00140         if (type=="effic")
00141           value = ((double) numerator[j])/((double) denominator[j]);
00142         else if (type=="fakerate")
00143           value = 1-((double) numerator[j])/((double) denominator[j]);
00144         else return;
00145         err = sqrt( value*(1-value)/(double) denominator[j] );
00146         h->setBinContent(j+1, value);
00147         h->setBinError(j+1,err);
00148       }
00149       else {
00150         h->setBinContent(j+1, 0);
00151       }
00152     }
00153   }
00154 
00155   void BinLogX(TH1*h)
00156   {
00157     
00158     TAxis *axis = h->GetXaxis();
00159     int bins = axis->GetNbins();
00160     
00161     float from = axis->GetXmin();
00162     float to = axis->GetXmax();
00163     float width = (to - from) / bins;
00164     float *new_bins = new float[bins + 1];
00165     
00166     for (int i = 0; i <= bins; i++) {
00167       new_bins[i] = TMath::Power(10, from + i * width);
00168       
00169     }
00170     axis->Set(bins, new_bins);
00171     delete[] new_bins;
00172   }
00173 
00174   void setUpVectors() {
00175     std::vector<double> etaintervalsv;
00176     std::vector<double> phiintervalsv;
00177     std::vector<double> pTintervalsv;
00178     std::vector<double> dxyintervalsv;
00179     std::vector<double> dzintervalsv;
00180     std::vector<double> vertposintervalsv;
00181     std::vector<double> zposintervalsv;
00182     std::vector<int>    totSIMveta,totASSveta,totASS2veta,totRECveta;
00183     std::vector<int>    totSIMvpT,totASSvpT,totASS2vpT,totRECvpT;
00184     std::vector<int>    totSIMv_hit,totASSv_hit,totASS2v_hit,totRECv_hit;
00185     std::vector<int>    totSIMv_phi,totASSv_phi,totASS2v_phi,totRECv_phi;
00186     std::vector<int>    totSIMv_dxy,totASSv_dxy,totASS2v_dxy,totRECv_dxy;
00187     std::vector<int>    totSIMv_dz,totASSv_dz,totASS2v_dz,totRECv_dz;
00188     std::vector<int>    totSIMv_vertpos,totASSv_vertpos,totSIMv_zpos,totASSv_zpos; 
00189 
00190     // for muon Validation
00191     std::vector<int>    totASSveta_Quality05, totASSveta_Quality075;
00192     std::vector<int>    totASSvpT_Quality05, totASSvpT_Quality075;
00193     std::vector<int>    totASSv_phi_Quality05, totASSv_phi_Quality075;
00194 
00195     double step=(max-min)/nint;
00196     std::ostringstream title,name;
00197     etaintervalsv.push_back(min);
00198     for (int k=1;k<nint+1;k++) {
00199       double d=min+k*step;
00200       etaintervalsv.push_back(d);
00201       totSIMveta.push_back(0);
00202       totASSveta.push_back(0);
00203       totASS2veta.push_back(0);
00204       totRECveta.push_back(0);
00205       //
00206       totASSveta_Quality05.push_back(0);
00207       totASSveta_Quality075.push_back(0);
00208     }   
00209     etaintervals.push_back(etaintervalsv);
00210     totSIMeta.push_back(totSIMveta);
00211     totASSeta.push_back(totASSveta);
00212     totASS2eta.push_back(totASS2veta);
00213     totRECeta.push_back(totRECveta);
00214     //
00215     totASSeta_Quality05.push_back(totASSveta_Quality05);
00216     totASSeta_Quality075.push_back(totASSveta_Quality075);
00217   
00218     double steppT = (maxpT-minpT)/nintpT;
00219     pTintervalsv.push_back(minpT);
00220     for (int k=1;k<nintpT+1;k++) {
00221       double d=0;
00222       if(useLogPt)d=pow(10,minpT+k*steppT);
00223       else d=minpT+k*steppT;
00224       pTintervalsv.push_back(d);
00225       totSIMvpT.push_back(0);
00226       totASSvpT.push_back(0);
00227       totASS2vpT.push_back(0);
00228       totRECvpT.push_back(0);
00229       //
00230       totASSvpT_Quality05.push_back(0);
00231       totASSvpT_Quality075.push_back(0);
00232     }
00233     pTintervals.push_back(pTintervalsv);
00234     totSIMpT.push_back(totSIMvpT);
00235     totASSpT.push_back(totASSvpT);
00236     totASS2pT.push_back(totASS2vpT);
00237     totRECpT.push_back(totRECvpT);
00238     //
00239     totASSpT_Quality05.push_back(totASSvpT_Quality05); 
00240     totASSpT_Quality075.push_back(totASSvpT_Quality075);
00241 
00242     for (int k=1;k<nintHit+1;k++) {
00243       totSIMv_hit.push_back(0);
00244       totASSv_hit.push_back(0);
00245       totASS2v_hit.push_back(0);
00246       totRECv_hit.push_back(0);
00247     }
00248     totSIM_hit.push_back(totSIMv_hit);
00249     totASS_hit.push_back(totASSv_hit);
00250     totASS2_hit.push_back(totASS2v_hit);
00251     totREC_hit.push_back(totRECv_hit);
00252 
00253     double stepPhi = (maxPhi-minPhi)/nintPhi;
00254     phiintervalsv.push_back(minPhi);
00255     for (int k=1;k<nintPhi+1;k++) {
00256       double d=minPhi+k*stepPhi;
00257       phiintervalsv.push_back(d);
00258       totSIMv_phi.push_back(0);
00259       totASSv_phi.push_back(0);
00260       totASS2v_phi.push_back(0);
00261       totRECv_phi.push_back(0);
00262       //
00263       totASSv_phi_Quality05.push_back(0);
00264       totASSv_phi_Quality075.push_back(0);
00265     }
00266     phiintervals.push_back(phiintervalsv);
00267     totSIM_phi.push_back(totSIMv_phi);
00268     totASS_phi.push_back(totASSv_phi);
00269     totASS2_phi.push_back(totASS2v_phi);
00270     totREC_phi.push_back(totRECv_phi);
00271     //
00272     totASS_phi_Quality05.push_back(totASSv_phi_Quality05);
00273     totASS_phi_Quality075.push_back(totASSv_phi_Quality075);
00274 
00275     double stepDxy = (maxDxy-minDxy)/nintDxy;
00276     dxyintervalsv.push_back(minDxy);
00277     for (int k=1;k<nintDxy+1;k++) {
00278       double d=minDxy+k*stepDxy;
00279       dxyintervalsv.push_back(d);
00280       totSIMv_dxy.push_back(0);
00281       totASSv_dxy.push_back(0);
00282       totASS2v_dxy.push_back(0);
00283       totRECv_dxy.push_back(0);
00284     }
00285     dxyintervals.push_back(dxyintervalsv);
00286     totSIM_dxy.push_back(totSIMv_dxy);
00287     totASS_dxy.push_back(totASSv_dxy);
00288     totASS2_dxy.push_back(totASS2v_dxy);
00289     totREC_dxy.push_back(totRECv_dxy);
00290 
00291 
00292     double stepDz = (maxDz-minDz)/nintDz;
00293     dzintervalsv.push_back(minDz);
00294     for (int k=1;k<nintDz+1;k++) {
00295       double d=minDz+k*stepDz;
00296       dzintervalsv.push_back(d);
00297       totSIMv_dz.push_back(0);
00298       totASSv_dz.push_back(0);
00299       totASS2v_dz.push_back(0);
00300       totRECv_dz.push_back(0);
00301     }
00302     dzintervals.push_back(dzintervalsv);
00303     totSIM_dz.push_back(totSIMv_dz);
00304     totASS_dz.push_back(totASSv_dz);
00305     totASS2_dz.push_back(totASS2v_dz);
00306     totREC_dz.push_back(totRECv_dz);
00307 
00308     double stepVertpos = (maxVertpos-minVertpos)/nintVertpos;
00309     vertposintervalsv.push_back(minVertpos);
00310     for (int k=1;k<nintVertpos+1;k++) {
00311       double d=minVertpos+k*stepVertpos;
00312       vertposintervalsv.push_back(d);
00313       totSIMv_vertpos.push_back(0);
00314       totASSv_vertpos.push_back(0);
00315     }
00316     vertposintervals.push_back(vertposintervalsv);
00317     totSIM_vertpos.push_back(totSIMv_vertpos);
00318     totASS_vertpos.push_back(totASSv_vertpos);
00319       
00320     double stepZpos = (maxZpos-minZpos)/nintZpos;
00321     zposintervalsv.push_back(minZpos);
00322     for (int k=1;k<nintZpos+1;k++) {
00323       double d=minZpos+k*stepZpos;
00324       zposintervalsv.push_back(d);
00325       totSIMv_zpos.push_back(0);
00326       totASSv_zpos.push_back(0);
00327     }
00328     zposintervals.push_back(zposintervalsv);
00329     totSIM_zpos.push_back(totSIMv_zpos);
00330     totASS_zpos.push_back(totASSv_zpos);
00331 
00332   }
00333 
00334  protected:
00335 
00336   DQMStore* dbe_;
00337 
00338   std::vector<edm::InputTag> label;
00339   bool usetracker;
00340   bool usemuon;
00341   edm::InputTag bsSrc;
00342   edm::InputTag label_tp_effic;
00343   edm::InputTag label_tp_fake;
00344   std::vector<std::string> associators;
00345   std::string out;
00346   std::string parametersDefiner;
00347        
00348   double  min, max;
00349   int nint;
00350   bool useFabs;
00351   double minpT, maxpT;
00352   int nintpT;
00353   double minHit, maxHit;
00354   int nintHit;
00355   double minPhi, maxPhi;
00356   int nintPhi;
00357   double minDxy, maxDxy;
00358   int nintDxy;
00359   double minDz, maxDz;
00360   int nintDz;
00361   double minVertpos, maxVertpos;
00362   int nintVertpos;
00363   double minZpos, maxZpos;
00364   int nintZpos;
00365   bool useInvPt;
00366   //
00367   double ptRes_rangeMin,ptRes_rangeMax,
00368     phiRes_rangeMin,phiRes_rangeMax, cotThetaRes_rangeMin,cotThetaRes_rangeMax,    
00369     dxyRes_rangeMin,dxyRes_rangeMax, dzRes_rangeMin,dzRes_rangeMax;
00370   int ptRes_nbin, cotThetaRes_nbin, phiRes_nbin, dxyRes_nbin, dzRes_nbin;
00371   bool ignoremissingtkcollection_;
00372   bool skipHistoFit;
00373   bool useLogPt;
00374 
00375   edm::ESHandle<MagneticField> theMF;
00376   std::vector<const TrackAssociatorBase*> associator;
00377 
00378   //sim
00379   std::vector<MonitorElement*> h_ptSIM, h_etaSIM, h_tracksSIM, h_vertposSIM;
00380 
00381   //1D
00382   std::vector<MonitorElement*> h_tracks, h_fakes, h_hits, h_charge;
00383   std::vector<MonitorElement*> h_effic,  h_fakerate, h_recoeta, h_assoceta, h_assoc2eta, h_simuleta;
00384   std::vector<MonitorElement*> h_efficPt, h_fakeratePt, h_recopT, h_assocpT, h_assoc2pT, h_simulpT;
00385   std::vector<MonitorElement*> h_effic_vs_hit, h_fake_vs_hit, h_recohit, h_assochit, h_assoc2hit, h_simulhit;
00386   std::vector<MonitorElement*> h_effic_vs_phi, h_fake_vs_phi, h_recophi, h_assocphi, h_assoc2phi, h_simulphi;
00387   std::vector<MonitorElement*> h_effic_vs_dxy, h_fake_vs_dxy, h_recodxy, h_assocdxy, h_assoc2dxy, h_simuldxy;
00388   std::vector<MonitorElement*> h_effic_vs_dz, h_fake_vs_dz, h_recodz, h_assocdz, h_assoc2dz, h_simuldz;
00389   std::vector<MonitorElement*> h_effic_vs_vertpos, h_effic_vs_zpos, h_assocvertpos, h_simulvertpos, h_assoczpos, h_simulzpos;
00390   std::vector<MonitorElement*> h_pt, h_eta, h_pullTheta,h_pullPhi,h_pullDxy,h_pullDz,h_pullQoverp;
00391 
00392   std::vector<MonitorElement*> h_effic_Quality05, h_assoceta_Quality05, h_effic_Quality075, h_assoceta_Quality075;
00393   std::vector<MonitorElement*> h_efficPt_Quality05, h_assocpT_Quality05, h_efficPt_Quality075, h_assocpT_Quality075;
00394   std::vector<MonitorElement*> h_effic_vs_phi_Quality05, h_assocphi_Quality05, h_effic_vs_phi_Quality075, h_assocphi_Quality075;
00395 
00396   //2D  
00397   std::vector<MonitorElement*> nrec_vs_nsim;
00398   std::vector<MonitorElement*> nrecHit_vs_nsimHit_sim2rec;
00399   std::vector<MonitorElement*> nrecHit_vs_nsimHit_rec2sim;
00400 
00401   //assoc hits
00402   std::vector<MonitorElement*> h_assocFraction, h_assocSharedHit;
00403 
00404   //#hit vs eta: to be used with doProfileX
00405   std::vector<MonitorElement*> nhits_vs_eta, 
00406     nDThits_vs_eta,nCSChits_vs_eta,nRPChits_vs_eta;
00407 
00408   std::vector<MonitorElement*> h_hits_eta,
00409     h_DThits_eta,h_CSChits_eta,h_RPChits_eta;
00410     
00411 
00412   std::vector< std::vector<double> > etaintervals;
00413   std::vector< std::vector<double> > pTintervals;
00414   std::vector< std::vector<double> > phiintervals;
00415   std::vector< std::vector<double> > dxyintervals;
00416   std::vector< std::vector<double> > dzintervals;
00417   std::vector< std::vector<double> > vertposintervals;
00418   std::vector< std::vector<double> > zposintervals;
00419   std::vector< std::vector<int> > totSIMeta,totRECeta,totASSeta,totASS2eta;
00420   std::vector< std::vector<int> > totSIMpT,totRECpT,totASSpT,totASS2pT;
00421   std::vector< std::vector<int> > totSIM_hit,totREC_hit,totASS_hit,totASS2_hit;
00422   std::vector< std::vector<int> > totSIM_phi,totREC_phi,totASS_phi,totASS2_phi;
00423   std::vector< std::vector<int> > totSIM_dxy,totREC_dxy,totASS_dxy,totASS2_dxy;
00424   std::vector< std::vector<int> > totSIM_dz,totREC_dz,totASS_dz,totASS2_dz;
00425   std::vector< std::vector<int> > totSIM_vertpos,totASS_vertpos,totSIM_zpos,totASS_zpos;
00426 
00427   // for muon Validation (SimToReco distributions for Quality > 0.5, 0.75)
00428   std::vector<MonitorElement*> h_PurityVsQuality;
00429   std::vector< std::vector<int> > totASSeta_Quality05,totASSeta_Quality075;
00430   std::vector< std::vector<int> > totASSpT_Quality05, totASSpT_Quality075;
00431   std::vector< std::vector<int> > totASS_phi_Quality05, totASS_phi_Quality075;
00432 
00433 };
00434 
00435 
00436 #endif