CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/MuonAnalysis/MomentumScaleCalibration/plugins/ResolutionAnalyzer.cc

Go to the documentation of this file.
00001 #ifndef RESOLUTIONANALYZER_CC
00002 #define RESOLUTIONANALYZER_CC
00003 
00004 #include "ResolutionAnalyzer.h"
00005 #include "MuonAnalysis/MomentumScaleCalibration/interface/Functions.h"
00006 #include "MuonAnalysis/MomentumScaleCalibration/interface/RootTreeHandler.h"
00007 
00008 //
00009 // constants, enums and typedefs
00010 //
00011 
00012 //
00013 // static data member definitions
00014 //
00015 
00016 //
00017 // constructors and destructor
00018 //
00019 ResolutionAnalyzer::ResolutionAnalyzer(const edm::ParameterSet& iConfig) :
00020   theMuonLabel_( iConfig.getParameter<edm::InputTag>( "MuonLabel" ) ),
00021   theMuonType_( iConfig.getParameter<int>( "MuonType" ) ),
00022   theRootFileName_( iConfig.getUntrackedParameter<std::string>("OutputFileName") ),
00023   theCovariancesRootFileName_( iConfig.getUntrackedParameter<std::string>("InputFileName") ),
00024   debug_( iConfig.getUntrackedParameter<bool>( "Debug" ) ),
00025   resonance_( iConfig.getParameter<bool>("Resonance") ),
00026   readCovariances_( iConfig.getParameter<bool>( "ReadCovariances" ) ),
00027   treeFileName_( iConfig.getParameter<std::string>("InputTreeName") ),
00028   maxEvents_( iConfig.getParameter<int>("MaxEvents") ),
00029   ptMax_( iConfig.getParameter<double>("PtMax") )
00030 {
00031   //now do what ever initialization is needed
00032 
00033   // Initial parameters values
00034   // -------------------------
00035   int resolFitType = iConfig.getParameter<int>("ResolFitType");
00036   MuScleFitUtils::ResolFitType = resolFitType;
00037   // MuScleFitUtils::resolutionFunction = resolutionFunctionArray[resolFitType];
00038   MuScleFitUtils::resolutionFunction = resolutionFunctionService( resolFitType );
00039   // MuScleFitUtils::resolutionFunctionForVec = resolutionFunctionArrayForVec[resolFitType];
00040   MuScleFitUtils::resolutionFunctionForVec = resolutionFunctionVecService( resolFitType );
00041 
00042   MuScleFitUtils::parResol = iConfig.getParameter<std::vector<double> >("parResol");
00043 
00044   MuScleFitUtils::resfind = iConfig.getParameter<std::vector<int> >("ResFind");
00045 
00046   outputFile_ = new TFile(theRootFileName_.c_str(), "RECREATE");
00047   outputFile_->cd();
00048   fillHistoMap();
00049 
00050   eventCounter_ = 0;
00051 }
00052 
00053 
00054 ResolutionAnalyzer::~ResolutionAnalyzer()
00055 {
00056   outputFile_->cd();
00057   writeHistoMap();
00058   outputFile_->Close();
00059   std::cout << "Total analyzed events = " << eventCounter_ << std::endl;
00060 }
00061 
00062 
00063 //
00064 // member functions
00065 //
00066 
00067 // ------------ method called to for each event  ------------
00068 void ResolutionAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
00069   using namespace edm;
00070 
00071   std::cout << "starting" << std::endl;
00072 
00073   lorentzVector nullLorentzVector(0, 0, 0, 0);
00074 
00075   RootTreeHandler rootTreeHandler;
00076   typedef std::vector<std::pair<lorentzVector,lorentzVector> > MuonPairVector;
00077   MuonPairVector savedPairVector;
00078   MuonPairVector genPairVector;
00079   
00080   std::vector<std::pair<int, int> > evtRun;
00081   rootTreeHandler.readTree(maxEvents_, treeFileName_, &savedPairVector, 0, &evtRun, &genPairVector);
00082   MuonPairVector::iterator savedPair = savedPairVector.begin();
00083   MuonPairVector::iterator genPair = genPairVector.begin();
00084   std::cout << "Starting loop on " << savedPairVector.size() << " muons" << std::endl;
00085   for( ; savedPair != savedPairVector.end(); ++savedPair, ++genPair ) {
00086 
00087   ++eventCounter_;
00088 
00089   if( (eventCounter_ % 10000) == 0 ) {
00090     std::cout << "event = " << eventCounter_ << std::endl;
00091   }
00092   
00093   lorentzVector recMu1( savedPair->first );
00094   lorentzVector recMu2( savedPair->second );
00095   
00096   if ( resonance_ ) {
00097 
00098     // Histograms with genParticles characteristics
00099     // --------------------------------------------
00100 
00101     reco::Particle::LorentzVector genMother( genPair->first + genPair->second );
00102   
00103     mapHisto_["GenMother"]->Fill( genMother );
00104     mapHisto_["DeltaGenMotherMuons"]->Fill( genPair->first, genPair->second );
00105     mapHisto_["GenMotherMuons"]->Fill( genPair->first );
00106     mapHisto_["GenMotherMuons"]->Fill( genPair->second );
00107   
00108     // Match the reco muons with the gen and sim tracks
00109     // ------------------------------------------------
00110     if(checkDeltaR(genPair->first,recMu1)){
00111       mapHisto_["PtResolutionGenVSMu"]->Fill(recMu1,(-genPair->first.Pt()+recMu1.Pt())/genPair->first.Pt(),-1);
00112       mapHisto_["ThetaResolutionGenVSMu"]->Fill(recMu1,(-genPair->first.Theta()+recMu1.Theta()),-1);
00113       mapHisto_["CotgThetaResolutionGenVSMu"]->Fill(recMu1,(-cos(genPair->first.Theta())/sin(genPair->first.Theta())
00114                                                             +cos(recMu1.Theta())/sin(recMu1.Theta())),-1);
00115       mapHisto_["EtaResolutionGenVSMu"]->Fill(recMu1,(-genPair->first.Eta()+recMu1.Eta()),-1);
00116       // mapHisto_["PhiResolutionGenVSMu"]->Fill(recMu1,(-genPair->first.Phi()+recMu1.Phi()),-1);
00117       mapHisto_["PhiResolutionGenVSMu"]->Fill(recMu1,MuScleFitUtils::deltaPhiNoFabs(recMu1.Phi(), genPair->first.Phi()),-1);
00118       recoPtVsgenPt_->Fill(genPair->first.Pt(), recMu1.Pt());
00119       deltaPtOverPt_->Fill( (recMu1.Pt() - genPair->first.Pt())/genPair->first.Pt() );
00120       if( fabs(recMu1.Eta()) > 1 && fabs(recMu1.Eta()) < 1.2 ) {
00121         recoPtVsgenPtEta12_->Fill(genPair->first.Pt(), recMu1.Pt());
00122         deltaPtOverPtForEta12_->Fill( (recMu1.Pt() - genPair->first.Pt())/genPair->first.Pt() );
00123       }
00124     }
00125     if(checkDeltaR(genPair->second,recMu2)){
00126       mapHisto_["PtResolutionGenVSMu"]->Fill(recMu2,(-genPair->second.Pt()+recMu2.Pt())/genPair->second.Pt(),+1);
00127       mapHisto_["ThetaResolutionGenVSMu"]->Fill(recMu2,(-genPair->second.Theta()+recMu2.Theta()),+1);
00128       mapHisto_["CotgThetaResolutionGenVSMu"]->Fill(recMu2,(-cos(genPair->second.Theta())/sin(genPair->second.Theta())
00129                                                             +cos(recMu2.Theta())/sin(recMu2.Theta())),+1);
00130       mapHisto_["EtaResolutionGenVSMu"]->Fill(recMu2,(-genPair->second.Eta()+recMu2.Eta()),+1);
00131       // mapHisto_["PhiResolutionGenVSMu"]->Fill(recMu2,(-genPair->second.Phi()+recMu2.Phi()),+1);
00132       mapHisto_["PhiResolutionGenVSMu"]->Fill(recMu2,MuScleFitUtils::deltaPhiNoFabs(recMu2.Phi(), genPair->second.Phi()),+1);
00133       recoPtVsgenPt_->Fill(genPair->second.Pt(), recMu2.Pt());
00134       deltaPtOverPt_->Fill( (recMu2.Pt() - genPair->second.Pt())/genPair->second.Pt() );
00135       if( fabs(recMu2.Eta()) > 1 && fabs(recMu2.Eta()) < 1.2 ) {
00136         recoPtVsgenPtEta12_->Fill(genPair->second.Pt(), recMu2.Pt());
00137         deltaPtOverPtForEta12_->Fill( (recMu2.Pt() - genPair->second.Pt())/genPair->second.Pt() );
00138       }
00139     }  
00140 
00141     // Fill the mass resolution histograms
00142     // -----------------------------------
00143     // check if the recoMuons match the genMuons
00144     // if( MuScleFitUtils::ResFound && checkDeltaR(simMu.first,recMu1) && checkDeltaR(simMu.second,recMu2) ) {
00145     if( genPair->first != nullLorentzVector && genPair->second != nullLorentzVector &&
00146         checkDeltaR(genPair->first,recMu1) && checkDeltaR(genPair->second,recMu2) ) {
00147 
00148       double recoMass = (recMu1+recMu2).mass();
00149       double genMass = (genPair->first + genPair->second).mass();
00150       // first is always mu-, second is always mu+
00151       mapHisto_["MassResolution"]->Fill(recMu1, -1, genPair->first, recMu2, +1, genPair->second, recoMass, genMass);
00152   
00153       // Fill the reconstructed resonance
00154       reco::Particle::LorentzVector recoResonance( recMu1+recMu2 );
00155       mapHisto_["RecoResonance"]->Fill( recoResonance );
00156       mapHisto_["DeltaRecoResonanceMuons"]->Fill( recMu1, recMu2 );
00157       mapHisto_["RecoResonanceMuons"]->Fill( recMu1 );
00158       mapHisto_["RecoResonanceMuons"]->Fill( recMu2 );
00159   
00160       // Fill the mass resolution (computed from MC), we use the covariance class to compute the variance
00161       if( genMass != 0 ) {
00162         // double diffMass = (recoMass - genMass)/genMass;
00163         double diffMass = recoMass - genMass;
00164         // Fill if for both muons
00165         double pt1 = recMu1.pt();
00166         double eta1 = recMu1.eta();
00167         double pt2 = recMu2.pt();
00168         double eta2 = recMu2.eta();
00169         // This is to avoid nan
00170         if( diffMass == diffMass ) {
00171           massResolutionVsPtEta_->Fill(pt1, eta1, diffMass, diffMass);
00172           massResolutionVsPtEta_->Fill(pt2, eta2, diffMass, diffMass);
00173         }
00174         else {
00175           std::cout << "Error, there is a nan: recoMass = " << recoMass << ", genMass = " << genMass << std::endl;
00176         }
00177         // Fill with mass resolution from resolution function
00178         double massRes = MuScleFitUtils::massResolution(recMu1, recMu2, MuScleFitUtils::parResol);
00179         // The value given by massRes is already divided by the mass, since the derivative functions have mass at the denominator.
00180         // We alos take the squared value, since var = sigma^2.
00181         mapHisto_["hFunctionResolMass"]->Fill( recMu1, std::pow(massRes,2), -1 );
00182         mapHisto_["hFunctionResolMass"]->Fill( recMu2, std::pow(massRes,2), +1 );
00183       }
00184   
00185       // Fill resolution functions for the muons (fill the squared value to make it comparable with the variance)
00186       mapHisto_["hFunctionResolPt"]->Fill( recMu1, MuScleFitUtils::resolutionFunctionForVec->sigmaPt(recMu1.Pt(), recMu1.Eta(), MuScleFitUtils::parResol), -1 );
00187       mapHisto_["hFunctionResolCotgTheta"]->Fill( recMu1, MuScleFitUtils::resolutionFunctionForVec->sigmaCotgTh(recMu1.Pt(), recMu1.Eta(), MuScleFitUtils::parResol), -1 );
00188       mapHisto_["hFunctionResolPhi"]->Fill( recMu1, MuScleFitUtils::resolutionFunctionForVec->sigmaPhi(recMu1.Pt(), recMu1.Eta(), MuScleFitUtils::parResol), -1 );
00189       mapHisto_["hFunctionResolPt"]->Fill( recMu2, MuScleFitUtils::resolutionFunctionForVec->sigmaPt(recMu2.Pt(), recMu2.Eta(), MuScleFitUtils::parResol), +1 );
00190       mapHisto_["hFunctionResolCotgTheta"]->Fill( recMu2, MuScleFitUtils::resolutionFunctionForVec->sigmaCotgTh(recMu2.Pt(), recMu2.Eta(), MuScleFitUtils::parResol), +1 );
00191       mapHisto_["hFunctionResolPhi"]->Fill( recMu2, MuScleFitUtils::resolutionFunctionForVec->sigmaPhi(recMu2.Pt(), recMu2.Eta(), MuScleFitUtils::parResol), +1 );
00192 
00193       if( readCovariances_ ) {
00194         // Compute mass error terms
00195         // ------------------------
00196         double mass   = (recMu1+recMu2).mass();
00197         double pt1    = recMu1.Pt();
00198         double phi1   = recMu1.Phi();
00199         double eta1   = recMu1.Eta();
00200         double theta1 = 2*atan(exp(-eta1));
00201         double pt2    = recMu2.Pt();
00202         double phi2   = recMu2.Phi();
00203         double eta2   = recMu2.Eta();
00204         double theta2 = 2*atan(exp(-eta2));
00205         // Derivatives
00206         double mMu2 = MuScleFitUtils::mMu2;
00207         double dmdpt1  = (pt1/std::pow(sin(theta1),2)*sqrt((std::pow(pt2/sin(theta2),2)+mMu2)/(std::pow(pt1/sin(theta1),2)+mMu2))- 
00208                           pt2*(cos(phi1-phi2)+cos(theta1)*cos(theta2)/(sin(theta1)*sin(theta2))))/mass;
00209         double dmdpt2  = (pt2/std::pow(sin(theta2),2)*sqrt((std::pow(pt1/sin(theta1),2)+mMu2)/(std::pow(pt2/sin(theta2),2)+mMu2))- 
00210                           pt1*(cos(phi2-phi1)+cos(theta2)*cos(theta1)/(sin(theta2)*sin(theta1))))/mass;
00211         double dmdphi1 = pt1*pt2/mass*sin(phi1-phi2);
00212         double dmdphi2 = pt2*pt1/mass*sin(phi2-phi1);
00213         double dmdcotgth1 = (pt1*pt1*cos(theta1)/sin(theta1)*
00214                              sqrt((std::pow(pt2/sin(theta2),2)+mMu2)/(std::pow(pt1/sin(theta1),2)+mMu2)) - 
00215                              pt1*pt2*cos(theta2)/sin(theta2))/mass;
00216         double dmdcotgth2 = (pt2*pt2*cos(theta2)/sin(theta2)*
00217                              sqrt((std::pow(pt1/sin(theta1),2)+mMu2)/(std::pow(pt2/sin(theta2),2)+mMu2)) - 
00218                              pt2*pt1*cos(theta1)/sin(theta1))/mass;
00219 
00220         // Multiplied by the pt here
00221         // -------------------------
00222         double dmdpt[2] = {dmdpt1*recMu1.Pt(), dmdpt2*recMu2.Pt()};
00223         double dmdphi[2] = {dmdphi1, dmdphi2};
00224         double dmdcotgth[2] = {dmdcotgth1, dmdcotgth2};
00225 
00226         // Evaluate the single terms in the mass error expression
00227 
00228         reco::Particle::LorentzVector * recMu[2] = { &recMu1, &recMu2 };
00229         int charge[2] = { -1, +1 };
00230 
00231         double fullMassRes = 0.;
00232         double massRes1 = 0.;
00233         double massRes2 = 0.;
00234         double massRes3 = 0.;
00235         double massRes4 = 0.;
00236         double massRes5 = 0.;
00237         double massRes6 = 0.;
00238         double massResPtAndPt12 = 0.;
00239 
00240         for( int i=0; i<2; ++i ) {
00241 
00242           double ptVariance = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Pt");
00243           double cotgThetaVariance = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "CotgTheta");
00244           double phiVariance = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Phi");
00245           double pt_cotgTheta = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Pt-CotgTheta");
00246           double pt_phi = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Pt-Phi");
00247           double cotgTheta_phi = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "CotgTheta-Phi");
00248 
00249           double pt1_pt2 = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Pt1-Pt2");
00250           double cotgTheta1_cotgTheta2 = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "CotgTheta1-CotgTheta2");
00251           double phi1_phi2 = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Phi1-Phi2");
00252           double pt12_cotgTheta21 = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Pt12-CotgTheta21");
00253           double pt12_phi21 = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "Pt12-Phi21");
00254           double cotgTheta12_phi21 = mapHisto_["ReadCovariances"]->Get(*(recMu[i]), "CotgTheta12-Phi21");
00255   
00256           // ATTENTION: Pt covariance terms are multiplied by Pt, since DeltaPt/Pt was used to compute them
00257           mapHisto_["MassResolutionPt"]->Fill( *(recMu[i]), ptVariance*std::pow(dmdpt[i],2), charge[i] );
00258           mapHisto_["MassResolutionCotgTheta"]->Fill( *(recMu[i]), cotgThetaVariance*std::pow(dmdcotgth[i],2), charge[i] );
00259           mapHisto_["MassResolutionPhi"]->Fill( *(recMu[i]), phiVariance*std::pow(dmdphi[i],2), charge[i] );
00260           mapHisto_["MassResolutionPt-CotgTheta"]->Fill( *(recMu[i]), 2*pt_cotgTheta*dmdpt[i]*dmdcotgth[i], charge[i] );
00261           mapHisto_["MassResolutionPt-Phi"]->Fill( *(recMu[i]), 2*pt_phi*dmdpt[i]*dmdphi[i], charge[i] );
00262           mapHisto_["MassResolutionCotgTheta-Phi"]->Fill( *(recMu[i]), 2*cotgTheta_phi*dmdcotgth[i]*dmdphi[i], charge[i] );
00263 
00264           mapHisto_["MassResolutionPt1-Pt2"]->Fill( *(recMu[i]), pt1_pt2*dmdpt[0]*dmdpt[1], charge[i] );
00265           mapHisto_["MassResolutionCotgTheta1-CotgTheta2"]->Fill( *(recMu[i]), cotgTheta1_cotgTheta2*dmdcotgth[0]*dmdcotgth[1], charge[i] );
00266           mapHisto_["MassResolutionPhi1-Phi2"]->Fill( *(recMu[i]), phi1_phi2*dmdphi[0]*dmdphi[1], charge[i] );
00267           // This must be filled for both configurations: 12 and 21
00268           mapHisto_["MassResolutionPt12-CotgTheta21"]->Fill( *(recMu[i]), pt12_cotgTheta21*dmdpt[0]*dmdcotgth[1], charge[i] );
00269           mapHisto_["MassResolutionPt12-CotgTheta21"]->Fill( *(recMu[i]), pt12_cotgTheta21*dmdpt[1]*dmdcotgth[0], charge[i] );
00270           mapHisto_["MassResolutionPt12-Phi21"]->Fill( *(recMu[i]), pt12_phi21*dmdpt[0]*dmdphi[1], charge[i] );
00271           mapHisto_["MassResolutionPt12-Phi21"]->Fill( *(recMu[i]), pt12_phi21*dmdpt[1]*dmdphi[0], charge[i] );
00272           mapHisto_["MassResolutionCotgTheta12-Phi21"]->Fill( *(recMu[i]), cotgTheta12_phi21*dmdcotgth[0]*dmdphi[1], charge[i] );
00273           mapHisto_["MassResolutionCotgTheta12-Phi21"]->Fill( *(recMu[i]), cotgTheta12_phi21*dmdcotgth[1]*dmdphi[0], charge[i] );
00274 
00275           // Sigmas for comparison
00276           mapHisto_["sigmaPtFromVariance"]->Fill( *(recMu[i]), sqrt(ptVariance), charge[i] );
00277           mapHisto_["sigmaCotgThetaFromVariance"]->Fill( *(recMu[i]), sqrt(cotgThetaVariance), charge[i] );
00278           mapHisto_["sigmaPhiFromVariance"]->Fill( *(recMu[i]), sqrt(phiVariance), charge[i] );
00279 
00280           // Pt term from function
00281           mapHisto_["MassResolutionPtFromFunction"]->Fill( *(recMu[i]), ( MuScleFitUtils::resolutionFunctionForVec->sigmaPt((recMu[i])->Pt(), (recMu[i])->Eta(), MuScleFitUtils::parResol) )*std::pow(dmdpt[i],2), charge[i] );
00282 
00283           fullMassRes +=
00284             ptVariance*std::pow(dmdpt[i],2) +
00285             cotgThetaVariance*std::pow(dmdcotgth[i],2) +
00286             phiVariance*std::pow(dmdphi[i],2) +
00287 
00288             // These are worth twice the others since there are: pt1-phi1, phi1-pt1, pt2-phi2, phi2-pt2
00289             2*pt_cotgTheta*dmdpt[i]*dmdcotgth[i] +
00290             2*pt_phi*dmdpt[i]*dmdphi[i] +
00291             2*cotgTheta_phi*dmdcotgth[i]*dmdphi[i] +
00292 
00293             pt1_pt2*dmdpt[0]*dmdpt[1] +
00294             cotgTheta1_cotgTheta2*dmdcotgth[0]*dmdcotgth[1] +
00295             phi1_phi2*dmdphi[0]*dmdphi[1] +
00296 
00297             // These are filled twice, because of the two combinations
00298             pt12_cotgTheta21*dmdpt[0]*dmdcotgth[1] +
00299             pt12_cotgTheta21*dmdpt[1]*dmdcotgth[0] +
00300             pt12_phi21*dmdpt[0]*dmdphi[1] +
00301             pt12_phi21*dmdpt[1]*dmdphi[0] +
00302             cotgTheta12_phi21*dmdcotgth[0]*dmdphi[1] +
00303             cotgTheta12_phi21*dmdcotgth[1]*dmdphi[0];
00304 
00305           massRes1 += ptVariance*std::pow(dmdpt[i],2);
00306           massRes2 += ptVariance*std::pow(dmdpt[i],2) +
00307             cotgThetaVariance*std::pow(dmdcotgth[i],2);
00308           massRes3 += ptVariance*std::pow(dmdpt[i],2) +
00309             cotgThetaVariance*std::pow(dmdcotgth[i],2) +
00310             phiVariance*std::pow(dmdphi[i],2);
00311           massRes4 += ptVariance*std::pow(dmdpt[i],2) +
00312             cotgThetaVariance*std::pow(dmdcotgth[i],2) +
00313             phiVariance*std::pow(dmdphi[i],2) +
00314             pt1_pt2*dmdpt[0]*dmdpt[1] +
00315             2*pt_cotgTheta*dmdpt[i]*dmdcotgth[i] +
00316             2*pt_phi*dmdpt[i]*dmdphi[i] +
00317             2*cotgTheta_phi*dmdcotgth[i]*dmdphi[i];
00318           massRes5 += ptVariance*std::pow(dmdpt[i],2) +
00319             cotgThetaVariance*std::pow(dmdcotgth[i],2) +
00320             phiVariance*std::pow(dmdphi[i],2) +
00321             pt1_pt2*dmdpt[0]*dmdpt[1] +
00322             2*pt_cotgTheta*dmdpt[i]*dmdcotgth[i] +
00323             2*pt_phi*dmdpt[i]*dmdphi[i] +
00324             2*cotgTheta_phi*dmdcotgth[i]*dmdphi[i] +
00325             cotgTheta1_cotgTheta2*dmdcotgth[0]*dmdcotgth[1] +
00326             phi1_phi2*dmdphi[0]*dmdphi[1];
00327           massRes6 += ptVariance*std::pow(dmdpt[i],2) +
00328             cotgThetaVariance*std::pow(dmdcotgth[i],2) +
00329             phiVariance*std::pow(dmdphi[i],2) +
00330             pt1_pt2*dmdpt[0]*dmdpt[1] +
00331             2*pt_cotgTheta*dmdpt[i]*dmdcotgth[i] +
00332             2*pt_phi*dmdpt[i]*dmdphi[i] +
00333             2*cotgTheta_phi*dmdcotgth[i]*dmdphi[i] +
00334             cotgTheta1_cotgTheta2*dmdcotgth[0]*dmdcotgth[1] +
00335             phi1_phi2*dmdphi[0]*dmdphi[1] +
00336             pt12_cotgTheta21*dmdpt[0]*dmdcotgth[1] +
00337             pt12_cotgTheta21*dmdpt[1]*dmdcotgth[0] +
00338             pt12_phi21*dmdpt[0]*dmdphi[1] +
00339             pt12_phi21*dmdpt[1]*dmdphi[0] +
00340             cotgTheta12_phi21*dmdcotgth[0]*dmdphi[1] +
00341             cotgTheta12_phi21*dmdcotgth[1]*dmdphi[0];
00342 
00343           massResPtAndPt12 += ptVariance*std::pow(dmdpt[i],2) + pt1_pt2*dmdpt[0]*dmdpt[1];
00344 
00345           // Derivatives
00346           mapHisto_["DerivativePt"]->Fill( *(recMu[i]), dmdpt[i], charge[i] );
00347           mapHisto_["DerivativeCotgTheta"]->Fill( *(recMu[i]), dmdcotgth[i], charge[i] );
00348           mapHisto_["DerivativePhi"]->Fill( *(recMu[i]), dmdphi[i], charge[i] );
00349         }
00350         // Fill the complete resolution function with covariance terms
00351         mapHisto_["FullMassResolution"]->Fill( *(recMu[0]), fullMassRes, charge[0]);
00352         mapHisto_["FullMassResolution"]->Fill( *(recMu[1]), fullMassRes, charge[1]);
00353 
00354         mapHisto_["MassRes1"]->Fill( *(recMu[0]), massRes1, charge[0] );
00355         mapHisto_["MassRes1"]->Fill( *(recMu[1]), massRes1, charge[1] );
00356         mapHisto_["MassRes2"]->Fill( *(recMu[0]), massRes2, charge[0] );
00357         mapHisto_["MassRes2"]->Fill( *(recMu[1]), massRes2, charge[1] );
00358         mapHisto_["MassRes3"]->Fill( *(recMu[0]), massRes3, charge[0] );
00359         mapHisto_["MassRes3"]->Fill( *(recMu[1]), massRes3, charge[1] );
00360         mapHisto_["MassRes4"]->Fill( *(recMu[0]), massRes4, charge[0] );
00361         mapHisto_["MassRes4"]->Fill( *(recMu[1]), massRes4, charge[1] );
00362         mapHisto_["MassRes5"]->Fill( *(recMu[0]), massRes5, charge[0] );
00363         mapHisto_["MassRes5"]->Fill( *(recMu[1]), massRes5, charge[1] );
00364         mapHisto_["MassRes6"]->Fill( *(recMu[0]), massRes6, charge[0] );
00365         mapHisto_["MassRes6"]->Fill( *(recMu[1]), massRes6, charge[1] );
00366         mapHisto_["MassResPtAndPt12"]->Fill( *(recMu[0]), massResPtAndPt12, charge[0] );
00367         mapHisto_["MassResPtAndPt12"]->Fill( *(recMu[1]), massResPtAndPt12, charge[1] );
00368       }
00369       else {
00370         // Fill the covariances histograms
00371         mapHisto_["Covariances"]->Fill(recMu1, genPair->first, recMu2, genPair->second);
00372       }
00373     }
00374   } // end if resonance
00375   }
00376 //   else {
00377 // 
00378 //     // Loop on the recMuons
00379 //     std::vector<reco::LeafCandidate>::const_iterator recMuon = muons.begin();
00380 //     for ( ; recMuon!=muons.end(); ++recMuon ) {  
00381 //       int charge = recMuon->charge();
00382 // 
00383 //       lorentzVector recMu(recMuon->p4());
00384 // 
00385 //       // Find the matching MC muon
00386 //       const HepMC::GenEvent* Evt = evtMC->GetEvent();
00387 //       //Loop on generated particles
00388 //       std::map<double, lorentzVector> genAssocMap;
00389 //       HepMC::GenEvent::particle_const_iterator part = Evt->particles_begin();
00390 //       for( ; part!=Evt->particles_end(); ++part ) {
00391 //         if (fabs((*part)->pdg_id())==13 && (*part)->status()==1) {
00392 //           lorentzVector genMu = (lorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
00393 //                                                (*part)->momentum().pz(),(*part)->momentum().e()));
00394 // 
00395 //           double deltaR = sqrt(MuScleFitUtils::deltaPhi(recMu.Phi(),genPair->Phi()) * MuScleFitUtils::deltaPhi(recMu.Phi(),genPair->Phi()) +
00396 //                                ((recMu.Eta()-genPair->Eta()) * (recMu.Eta()-genPair->Eta())));
00397 // 
00398 //           // 13 for the muon (-1) and -13 for the antimuon (+1), thus pdg*charge = -13.
00399 //           // Only in this case we consider it matching.
00400 //           if( ((*part)->pdg_id())*charge == -13 ) genAssocMap.insert(std::make_pair(deltaR, genMu));
00401 //         }
00402 //       }
00403 //       // Take the closest in deltaR
00404 //       lorentzVector genMu(genAssocMap.begin()->second);
00405 // 
00406 //       // Histograms with genParticles characteristics
00407 //       // --------------------------------------------
00408 // 
00409 //       if(checkDeltaR(genMu,recMu)){
00410 //         mapHisto_["PtResolutionGenVSMu"]->Fill(genMu,(-genPair->Pt()+recMu.Pt())/genPair->Pt(),charge);
00411 //         mapHisto_["ThetaResolutionGenVSMu"]->Fill(genMu,(-genPair->Theta()+recMu.Theta()),charge);
00412 //         mapHisto_["CotgThetaResolutionGenVSMu"]->Fill(genMu,(-cos(genPair->Theta())/sin(genPair->Theta())
00413 //                                                              +cos(recMu.Theta())/sin(recMu.Theta())),charge);
00414 //         mapHisto_["EtaResolutionGenVSMu"]->Fill(genMu,(-genPair->Eta()+recMu.Eta()),charge);
00415 //         mapHisto_["PhiResolutionGenVSMu"]->Fill(genMu,MuScleFitUtils::deltaPhiNoFabs(recMu.Phi(), genPair->Phi()),charge);
00416 //       }
00417 //     }
00418 }
00419 
00420 void ResolutionAnalyzer::fillHistoMap() {
00421 
00422   outputFile_->cd();
00423 
00424   // Resonances
00425   // If no Z is required, use a smaller mass range.
00426   double minMass = 0.;
00427   double maxMass = 200.;
00428   if( MuScleFitUtils::resfind[0] != 1 ) {
00429     maxMass = 30.;
00430   }
00431   mapHisto_["GenMother"]               = new HParticle(outputFile_, "GenMother", minMass, maxMass);
00432   mapHisto_["SimResonance"]            = new HParticle(outputFile_, "SimResonance", minMass, maxMass);
00433   mapHisto_["RecoResonance"]           = new HParticle(outputFile_, "RecoResonance", minMass, maxMass);
00434   
00435   // Resonance muons
00436   mapHisto_["GenMotherMuons"]          = new HParticle(outputFile_, "GenMotherMuons", minMass, 1.);
00437   mapHisto_["SimResonanceMuons"]       = new HParticle(outputFile_, "SimResonanceMuons", minMass, 1.);
00438   mapHisto_["RecoResonanceMuons"]      = new HParticle(outputFile_, "RecoResonanceMuons", minMass, 1.);
00439   
00440   // Deltas between resonance muons
00441   mapHisto_["DeltaGenMotherMuons"]     = new HDelta (outputFile_, "DeltaGenMotherMuons");
00442   mapHisto_["DeltaSimResonanceMuons"]  = new HDelta (outputFile_, "DeltaSimResonanceMuons");
00443   mapHisto_["DeltaRecoResonanceMuons"] = new HDelta (outputFile_, "DeltaRecoResonanceMuons");
00444   
00445   //   //Reconstructed muon kinematics
00446   //   //-----------------------------
00447   //   mapHisto_["hRecBestMu"]             = new HParticle         ("hRecBestMu");
00448   //   mapHisto_["hRecBestMu_Acc"]         = new HParticle         ("hRecBestMu_Acc"); 
00449   //   mapHisto_["hDeltaRecBestMu"]        = new HDelta            ("hDeltaRecBestMu");
00450   
00451   //   mapHisto_["hRecBestRes"]            = new HParticle         ("hRecBestRes");
00452   //   mapHisto_["hRecBestRes_Acc"]        = new HParticle         ("hRecBestRes_Acc"); 
00453   //   mapHisto_["hRecBestResVSMu"]        = new HMassVSPart       ("hRecBestResVSMu");
00454   
00455   //Resolution VS muon kinematic
00456   //----------------------------
00457   mapHisto_["PtResolutionGenVSMu"]        = new HResolutionVSPart (outputFile_, "PtResolutionGenVSMu");
00458   mapHisto_["PtResolutionSimVSMu"]        = new HResolutionVSPart (outputFile_, "PtResolutionSimVSMu");
00459   mapHisto_["EtaResolutionGenVSMu"]       = new HResolutionVSPart (outputFile_, "EtaResolutionGenVSMu");
00460   mapHisto_["EtaResolutionSimVSMu"]       = new HResolutionVSPart (outputFile_, "EtaResolutionSimVSMu");
00461   mapHisto_["ThetaResolutionGenVSMu"]     = new HResolutionVSPart (outputFile_, "ThetaResolutionGenVSMu");
00462   mapHisto_["ThetaResolutionSimVSMu"]     = new HResolutionVSPart (outputFile_, "ThetaResolutionSimVSMu");
00463   mapHisto_["CotgThetaResolutionGenVSMu"] = new HResolutionVSPart (outputFile_, "CotgThetaResolutionGenVSMu", -0.02, 0.02, -0.02, 0.02);
00464   mapHisto_["CotgThetaResolutionSimVSMu"] = new HResolutionVSPart (outputFile_, "CotgThetaResolutionSimVSMu");
00465   mapHisto_["PhiResolutionGenVSMu"]       = new HResolutionVSPart (outputFile_, "PhiResolutionGenVSMu", -0.002, 0.002, -0.002, 0.002);
00466   mapHisto_["PhiResolutionSimVSMu"]       = new HResolutionVSPart (outputFile_, "PhiResolutionSimVSMu");
00467 
00468   // Covariances between muons kinematic quantities
00469   // ----------------------------------------------
00470   double ptMax = ptMax_;
00471 
00472   // Mass resolution
00473   // ---------------
00474   mapHisto_["MassResolution"] = new HMassResolutionVSPart (outputFile_,"MassResolution");
00475   
00476   //  mapHisto_["hResolRecoMassVSGenMassVSPt"] = new HResolutionVSPart
00477   
00478   // Mass resolution vs (pt, eta) of the muons from MC
00479   massResolutionVsPtEta_ = new HCovarianceVSxy ( "Mass", "Mass", 100, 0., ptMax, 60, -3, 3 );
00480   // Mass resolution vs (pt, eta) of the muons from function
00481   recoPtVsgenPt_ = new TH2D("recoPtVsgenPt", "recoPtVsgenPt", 100, 0, ptMax, 100, 0, ptMax);
00482   recoPtVsgenPtEta12_ = new TH2D("recoPtVsgenPtEta12", "recoPtVsgenPtEta12", 100, 0, ptMax, 100, 0, ptMax);
00483   deltaPtOverPt_ = new TH1D("deltaPtOverPt", "deltaPtOverPt", 100, -0.1, 0.1);
00484   deltaPtOverPtForEta12_ = new TH1D("deltaPtOverPtForEta12", "deltaPtOverPtForEta12", 100, -0.1, 0.1);
00485 
00486   // Muons resolutions from resolution functions
00487   // -------------------------------------------
00488   int totBinsY = 60;
00489   mapHisto_["hFunctionResolMass"]      = new HFunctionResolution (outputFile_, "hFunctionResolMass", ptMax, totBinsY);
00490   mapHisto_["hFunctionResolPt"]        = new HFunctionResolution (outputFile_, "hFunctionResolPt", ptMax, totBinsY);
00491   mapHisto_["hFunctionResolCotgTheta"] = new HFunctionResolution (outputFile_, "hFunctionResolCotgTheta", ptMax, totBinsY);
00492   mapHisto_["hFunctionResolPhi"]       = new HFunctionResolution (outputFile_, "hFunctionResolPhi", ptMax, totBinsY);
00493 
00494   if( readCovariances_ ) {
00495     // Covariances read from file. Used to compare the terms in the expression of mass error
00496     mapHisto_["ReadCovariances"] = new HCovarianceVSParts ( theCovariancesRootFileName_, "Covariance" );
00497 
00498     // Variances
00499     mapHisto_["MassResolutionPt"]                    = new HFunctionResolutionVarianceCheck(outputFile_,"functionResolMassPt", ptMax);
00500     mapHisto_["MassResolutionCotgTheta"]             = new HFunctionResolutionVarianceCheck(outputFile_,"functionResolMassCotgTheta", ptMax);
00501     mapHisto_["MassResolutionPhi"]                   = new HFunctionResolutionVarianceCheck(outputFile_,"functionResolMassPhi", ptMax);
00502     // Covariances
00503     mapHisto_["MassResolutionPt-CotgTheta"]          = new HFunctionResolution(outputFile_,"functionResolMassPt-CotgTheta", ptMax, totBinsY);
00504     mapHisto_["MassResolutionPt-Phi"]                = new HFunctionResolution(outputFile_,"functionResolMassPt-Phi", ptMax, totBinsY);
00505     mapHisto_["MassResolutionCotgTheta-Phi"]         = new HFunctionResolution(outputFile_,"functionResolMassCotgTheta-Phi", ptMax, totBinsY);
00506     mapHisto_["MassResolutionPt1-Pt2"]               = new HFunctionResolution(outputFile_,"functionResolMassPt1-Pt2", ptMax, totBinsY);
00507     mapHisto_["MassResolutionCotgTheta1-CotgTheta2"] = new HFunctionResolution(outputFile_,"functionResolMassCotgTheta1-CotgTheta2", ptMax, totBinsY);
00508     mapHisto_["MassResolutionPhi1-Phi2"]             = new HFunctionResolution(outputFile_,"functionResolMassPhi1-Phi2", ptMax, totBinsY);
00509     mapHisto_["MassResolutionPt12-CotgTheta21"]      = new HFunctionResolution(outputFile_,"functionResolMassPt12-CotgTheta21", ptMax, totBinsY);
00510     mapHisto_["MassResolutionPt12-Phi21"]            = new HFunctionResolution(outputFile_,"functionResolMassPt12-Phi21", ptMax, totBinsY);
00511     mapHisto_["MassResolutionCotgTheta12-Phi21"]     = new HFunctionResolution(outputFile_,"functionResolMassCotgTheta12-Phi21", ptMax, totBinsY);
00512 
00513     mapHisto_["sigmaPtFromVariance"]                 = new HFunctionResolution(outputFile_,"sigmaPtFromVariance", ptMax, totBinsY);
00514     mapHisto_["sigmaCotgThetaFromVariance"]          = new HFunctionResolution(outputFile_,"sigmaCotgThetaFromVariance", ptMax, totBinsY);
00515     mapHisto_["sigmaPhiFromVariance"]                = new HFunctionResolution(outputFile_,"sigmaPhiFromVariance", ptMax, totBinsY);
00516 
00517     // Derivatives
00518     mapHisto_["DerivativePt"]                        = new HFunctionResolution(outputFile_,"derivativePt", ptMax);
00519     mapHisto_["DerivativeCotgTheta"]                 = new HFunctionResolution(outputFile_,"derivativeCotgTheta", ptMax);
00520     mapHisto_["DerivativePhi"]                       = new HFunctionResolution(outputFile_,"derivativePhi", ptMax);
00521 
00522     // Pt term from function
00523     mapHisto_["MassResolutionPtFromFunction"]        = new HFunctionResolutionVarianceCheck(outputFile_,"functionResolMassPtFromFunction", ptMax);
00524 
00525     mapHisto_["FullMassResolution"]                  = new HFunctionResolution(outputFile_, "fullMassResolution", ptMax);
00526     mapHisto_["MassRes1"]                            = new HFunctionResolution(outputFile_, "massRes1", ptMax);
00527     mapHisto_["MassRes2"]                            = new HFunctionResolution(outputFile_, "massRes2", ptMax);
00528     mapHisto_["MassRes3"]                            = new HFunctionResolution(outputFile_, "massRes3", ptMax);
00529     mapHisto_["MassRes4"]                            = new HFunctionResolution(outputFile_, "massRes4", ptMax);
00530     mapHisto_["MassRes5"]                            = new HFunctionResolution(outputFile_, "massRes5", ptMax);
00531     mapHisto_["MassRes6"]                            = new HFunctionResolution(outputFile_, "massRes6", ptMax);
00532     mapHisto_["MassResPtAndPt12"]                    = new HFunctionResolution(outputFile_, "massResPtAndPt12", ptMax);
00533   }
00534   else {
00535     mapHisto_["Covariances"] = new HCovarianceVSParts ( outputFile_, "Covariance", ptMax );
00536   }
00537 }
00538 
00539 void ResolutionAnalyzer::writeHistoMap() {
00540   for (std::map<std::string, Histograms*>::const_iterator histo=mapHisto_.begin(); 
00541        histo!=mapHisto_.end(); histo++) {
00542     (*histo).second->Write();
00543   }
00544   outputFile_->cd();
00545   massResolutionVsPtEta_->Write();
00546   recoPtVsgenPt_->Write();
00547   recoPtVsgenPtEta12_->Write();
00548   deltaPtOverPt_->Write();
00549   deltaPtOverPtForEta12_->Write();
00550 }
00551 
00552 bool ResolutionAnalyzer::checkDeltaR(const reco::Particle::LorentzVector & genMu, const reco::Particle::LorentzVector & recMu){
00553   //first is always mu-, second is always mu+
00554   double deltaR = sqrt(MuScleFitUtils::deltaPhi(recMu.Phi(),genMu.Phi()) * MuScleFitUtils::deltaPhi(recMu.Phi(),genMu.Phi()) +
00555                        ((recMu.Eta()-genMu.Eta()) * (recMu.Eta()-genMu.Eta())));
00556   if(deltaR<0.01)
00557     return true;
00558   else if (debug_>0)
00559     std::cout<<"Reco muon "<<recMu<<" with eta "<<recMu.Eta()<<" and phi "<<recMu.Phi()<<std::endl
00560              <<" DOES NOT MATCH with generated muon from resonance: "<<std::endl
00561              <<genMu<<" with eta "<<genMu.Eta()<<" and phi "<<genMu.Phi()<<std::endl;
00562   return false;
00563 }
00564 
00565 //define this as a plug-in
00566 DEFINE_FWK_MODULE(ResolutionAnalyzer);
00567 
00568 #endif // RESOLUTIONANALYZER_CC