CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/RecoMET/METAlgorithms/src/SignAlgoResolutions.cc

Go to the documentation of this file.
00001 #include "RecoMET/METAlgorithms/interface/SignAlgoResolutions.h"
00002 // -*- C++ -*-
00003 //
00004 // Package:    METAlgorithms
00005 // Class:      SignAlgoResolutions
00006 // 
00014 //
00015 // Original Author:  Kyle Story, Freya Blekman (Cornell University)
00016 //         Created:  Fri Apr 18 11:58:33 CEST 2008
00017 // $Id: SignAlgoResolutions.cc,v 1.7 2010/11/29 10:14:17 akhukhun Exp $
00018 //
00019 //
00020 #include "FWCore/Framework/interface/EventSetup.h"
00021 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00022 #include "DataFormats/TrackReco/interface/Track.h"
00023 
00024 #include <math.h>
00025 
00026 #include <cstdlib>
00027 #include <iostream>
00028 #include <sstream>
00029 #include <string>
00030 #include <sys/stat.h>
00031 
00032 metsig::SignAlgoResolutions::SignAlgoResolutions(const edm::ParameterSet &iConfig):
00033     functionmap_(),
00034     ptResol_(0),
00035     phiResol_(0)
00036 {
00037   addResolutions(iConfig);
00038 }
00039 
00040 
00041 
00042 double metsig::SignAlgoResolutions::eval(const resolutionType & type, const resolutionFunc & func, const double & et, const double & phi, const double & eta) const {
00043   // derive p from et and eta;
00044   double theta = 2*atan(exp(-eta));
00045   double p = et / sin(theta); // rough assumption: take e and p equivalent...
00046   return eval(type,func,et,phi,eta,p);
00047 
00048 }
00049 
00050 double metsig::SignAlgoResolutions::eval(const resolutionType & type, const resolutionFunc & func, const double & et, const double & phi, const double & eta, const double &p) const {
00051 
00052   functionPars x(4);
00053   x[0]=et;
00054   x[1]=phi;
00055   x[2]=eta;
00056   x[3]=p;
00057   //  std::cout << "getting function of type " << type << " " << func << " " << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << std::endl;
00058   return getfunc(type,func,x);
00059 
00060 }
00061 
00062 metsig::SigInputObj  metsig::SignAlgoResolutions::evalPF(const reco::PFCandidate *candidate) const {
00063   double eta = candidate->eta();
00064   double phi = candidate->phi();
00065   double et = candidate->energy()*sin(candidate->theta());
00066   resolutionType thetype;
00067   std::string name;
00068   int type = candidate->particleId();
00069   switch (type) 
00070     {
00071     case 1: 
00072       thetype=PFtype1;name="PFChargedHadron"; break;
00073     case 2: 
00074       thetype=PFtype2;name="PFChargedEM"; break;
00075     case 3: 
00076       thetype=PFtype3;name="PFMuon"; break;
00077     case 4: 
00078       thetype=PFtype4;name="PFNeutralEM"; break;
00079     case 5: 
00080       thetype=PFtype5;name="PFNeutralHadron"; break;
00081     case 6: 
00082       thetype=PFtype6;name="PFtype6"; break;
00083     case 7:
00084       thetype=PFtype7;name="PFtype7"; break;
00085     default:
00086       thetype=PFtype7;name="PFunknown"; break;
00087   }
00088 
00089   double d_et=0, d_phi=0; //d_phi here is the error on phi component of the et
00090   reco::TrackRef trackRef = candidate->trackRef();
00091   if(!trackRef.isNull() && type!=2){
00092     d_et = trackRef->ptError();
00093     d_phi = et*trackRef->phiError();
00094   }
00095   else{
00096     d_et = eval(thetype,ET,et,phi,eta);
00097     d_phi = eval(thetype,PHI,et,phi,eta);
00098   }
00099 
00100   metsig::SigInputObj resultingobj(name,et,phi,d_et,d_phi);
00101   return resultingobj;
00102 }
00103 
00104 
00105 metsig::SigInputObj
00106 metsig::SignAlgoResolutions::evalPFJet(const reco::PFJet *jet) const{
00107 
00108     double jpt  = jet->pt();
00109     double jphi = jet->phi();
00110     double jeta = jet->eta();
00111     double jdeltapt = 999.;
00112     double jdeltapphi = 999.;
00113 
00114     if(jpt<ptResolThreshold_ && jpt<20.){ //use temporary fix for low pT jets
00115         double feta = TMath::Abs(jeta);
00116         int ieta = feta<5.? int(feta/0.5) : 9; //bin size = 0.5 
00117         int ipt  = jpt>3. ? int(jpt-3./2) : 0; //bin size =2, starting from ptmin=3GeV
00118         jdeltapt   = jdpt[ieta][ipt];
00119         jdeltapphi = jpt*jdphi[ieta][ipt];
00120     }
00121     else{
00122         TF1* fPtEta  = ptResol_->parameterEta("sigma",jeta);
00123         TF1* fPhiEta = phiResol_->parameterEta("sigma",jeta);
00124         jdeltapt   = jpt>ptResolThreshold_ ? jpt*fPtEta->Eval(jpt)  : jpt*fPtEta->Eval(ptResolThreshold_);
00125         jdeltapphi = jpt>ptResolThreshold_ ? jpt*fPhiEta->Eval(jpt) : jpt*fPhiEta->Eval(ptResolThreshold_);
00126         delete fPtEta;
00127         delete fPhiEta;
00128     }
00129 
00130     std::string inputtype = "jet";
00131     metsig::SigInputObj obj_jet(inputtype,jpt,jphi,jdeltapt,jdeltapphi);
00132     //std::cout << "RESOLUTIONS JET: " << jpt << "   " << jphi<< "   " <<jdeltapt << "   " << jdeltapphi << std::endl;
00133     return obj_jet;
00134 }
00135 
00136 
00137 void metsig::SignAlgoResolutions::addResolutions(const edm::ParameterSet &iConfig){
00138     using namespace std;
00139 
00140   // Jet Resolutions - for now load from the files. Migrate to EventSetup asap.
00141   metsig::SignAlgoResolutions::initializeJetResolutions( iConfig );
00142   
00143   ptResolThreshold_ = iConfig.getParameter<double>("ptresolthreshold");
00144 
00145 
00146     //get temporary low pT pfjet resolutions
00147     for (int ieta=0; ieta<10; ieta++){
00148       jdpt[ieta] = iConfig.getParameter<std::vector<double> >(Form("jdpt%d", ieta));
00149       jdphi[ieta] = iConfig.getParameter<std::vector<double> >(Form("jdphi%d", ieta));
00150     }
00151 
00152 
00153   // for now: do this by hand - this can obviously also be done via ESSource etc.
00154   functionPars etparameters(3,0);
00155   functionPars phiparameters(1,0);
00156   // set the parameters per function:
00157   // ECAL, BARREL:
00158   std::vector<double> ebet = iConfig.getParameter<std::vector<double> >("EB_EtResPar");
00159   std::vector<double> ebphi = iConfig.getParameter<std::vector<double> >("EB_PhiResPar");
00160 
00161   etparameters[0]=ebet[0];
00162   etparameters[1]=ebet[1];
00163   etparameters[2]=ebet[2];
00164   phiparameters[0]=ebphi[0];
00165   addfunction(caloEB,ET,etparameters);
00166   addfunction(caloEB,PHI,phiparameters);
00167  // ECAL, ENDCAP:
00168   std::vector<double> eeet = iConfig.getParameter<std::vector<double> >("EE_EtResPar");
00169   std::vector<double> eephi = iConfig.getParameter<std::vector<double> >("EE_PhiResPar");
00170 
00171   etparameters[0]=eeet[0];
00172   etparameters[1]=eeet[1];
00173   etparameters[2]=eeet[2];
00174   phiparameters[0]=eephi[0];
00175   addfunction(caloEE,ET,etparameters);
00176   addfunction(caloEE,PHI,phiparameters);
00177  // HCAL, BARREL:
00178   std::vector<double> hbet = iConfig.getParameter<std::vector<double> >("HB_EtResPar");
00179   std::vector<double> hbphi = iConfig.getParameter<std::vector<double> >("HB_PhiResPar");
00180 
00181   etparameters[0]=hbet[0];
00182   etparameters[1]=hbet[1];
00183   etparameters[2]=hbet[2];
00184   phiparameters[0]=hbphi[0];
00185   addfunction(caloHB,ET,etparameters);
00186   addfunction(caloHB,PHI,phiparameters);
00187  // HCAL, ENDCAP:
00188   std::vector<double> heet = iConfig.getParameter<std::vector<double> >("HE_EtResPar");
00189   std::vector<double> hephi = iConfig.getParameter<std::vector<double> >("HE_PhiResPar");
00190 
00191   etparameters[0]=heet[0];
00192   etparameters[1]=heet[1];
00193   etparameters[2]=heet[2];
00194   phiparameters[0]=hephi[0];
00195   addfunction(caloHE,ET,etparameters);
00196   addfunction(caloHE,PHI,phiparameters);
00197  // HCAL, Outer
00198   std::vector<double> hoet = iConfig.getParameter<std::vector<double> >("HO_EtResPar");
00199   std::vector<double> hophi = iConfig.getParameter<std::vector<double> >("HO_PhiResPar");
00200 
00201 
00202   etparameters[0]=hoet[0];
00203   etparameters[1]=hoet[1];
00204   etparameters[2]=hoet[2];
00205   phiparameters[0]=hophi[0];
00206   addfunction(caloHO,ET,etparameters);
00207   addfunction(caloHO,PHI,phiparameters);
00208  // HCAL, Forward
00209   std::vector<double> hfet = iConfig.getParameter<std::vector<double> >("HF_EtResPar");
00210   std::vector<double> hfphi = iConfig.getParameter<std::vector<double> >("HF_PhiResPar");
00211 
00212   etparameters[0]=hfet[0];
00213   etparameters[1]=hfet[1];
00214   etparameters[2]=hfet[2];
00215   phiparameters[0]=hfphi[0];
00216   addfunction(caloHF,ET,etparameters);
00217   addfunction(caloHF,PHI,phiparameters);
00218 
00219 
00220   // PF objects:
00221   // type 1:
00222   std::vector<double> pf1et = iConfig.getParameter<std::vector<double> >("PF_EtResType1");
00223   std::vector<double> pf1phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType1");
00224   etparameters[0]=pf1et[0];
00225   etparameters[1]=pf1et[1];
00226   etparameters[2]=pf1et[2];
00227   phiparameters[0]=pf1phi[0];
00228   addfunction(PFtype1,ET,etparameters);
00229   addfunction(PFtype1,PHI,phiparameters);
00230 
00231   // PF objects:
00232   // type 2:
00233   std::vector<double> pf2et = iConfig.getParameter<std::vector<double> >("PF_EtResType2");
00234   std::vector<double> pf2phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType2");
00235   etparameters[0]=pf2et[0];
00236   etparameters[1]=pf2et[1];
00237   etparameters[2]=pf2et[2];
00238   phiparameters[0]=pf2phi[0];
00239   addfunction(PFtype2,ET,etparameters);
00240   addfunction(PFtype2,PHI,phiparameters);
00241 
00242   // PF objects:
00243   // type 3:
00244   std::vector<double> pf3et = iConfig.getParameter<std::vector<double> >("PF_EtResType3");
00245   std::vector<double> pf3phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType3");
00246   etparameters[0]=pf3et[0];
00247   etparameters[1]=pf3et[1];
00248   etparameters[2]=pf3et[2];
00249   phiparameters[0]=pf3phi[0];
00250   addfunction(PFtype3,ET,etparameters);
00251   addfunction(PFtype3,PHI,phiparameters);
00252 
00253   // PF objects:
00254   // type 4:
00255   std::vector<double> pf4et = iConfig.getParameter<std::vector<double> >("PF_EtResType4");
00256   std::vector<double> pf4phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType4");
00257   etparameters[0]=pf4et[0];
00258   etparameters[1]=pf4et[1];
00259   etparameters[2]=pf4et[2];
00260   //phiparameters[0]=pf4phi[0];
00261   addfunction(PFtype4,ET,etparameters);
00262   addfunction(PFtype4,PHI,pf4phi); //use the same functional form for photon phi error as for pT, pass whole vector
00263 
00264   // PF objects:
00265   // type 5:
00266   std::vector<double> pf5et = iConfig.getParameter<std::vector<double> >("PF_EtResType5");
00267   std::vector<double> pf5phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType5");
00268   etparameters[0]=pf5et[0];
00269   etparameters[1]=pf5et[1];
00270   etparameters[2]=pf5et[2];
00271   phiparameters[0]=pf5phi[0];
00272   addfunction(PFtype5,ET,etparameters);
00273   addfunction(PFtype5,PHI,pf5phi);
00274 
00275   // PF objects:
00276   // type 6:
00277   std::vector<double> pf6et = iConfig.getParameter<std::vector<double> >("PF_EtResType6");
00278   std::vector<double> pf6phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType6");
00279   etparameters[0]=pf6et[0];
00280   etparameters[1]=pf6et[1];
00281   etparameters[2]=pf6et[2];
00282   phiparameters[0]=pf6phi[0];
00283   addfunction(PFtype6,ET,etparameters);
00284   addfunction(PFtype6,PHI,phiparameters);
00285 
00286   
00287   // PF objects:
00288   // type 7:
00289   std::vector<double> pf7et = iConfig.getParameter<std::vector<double> >("PF_EtResType7");
00290   std::vector<double> pf7phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType7");
00291   etparameters[0]=pf7et[0];
00292   etparameters[1]=pf7et[1];
00293   etparameters[2]=pf7et[2];
00294   phiparameters[0]=pf7phi[0];
00295   addfunction(PFtype7,ET,etparameters);
00296   addfunction(PFtype7,PHI,phiparameters);
00297 
00298   return;
00299 }
00300 
00301 void metsig::SignAlgoResolutions::addfunction(resolutionType type, resolutionFunc func, functionPars parameters){
00302 
00303   functionCombo mypair(type,func);
00304   functionmap_[mypair]=parameters;
00305 
00306 }
00307 
00308 double metsig::SignAlgoResolutions::getfunc(const metsig::resolutionType & type,const metsig::resolutionFunc & func, functionPars & x) const{
00309   
00310   double result=0;
00311   functionCombo mypair(type,func);
00312 
00313   if(functionmap_.count(mypair)==0){
00314         return result;
00315   }
00316   
00317   functionPars values = (functionmap_.find(mypair))->second;
00318   switch ( func ){
00319   case metsig::ET :
00320     return EtFunction(x,values);
00321   case metsig::PHI :
00322     return PhiFunction(x,values);
00323   case metsig::TRACKP :
00324     return PFunction(x,values);
00325   case metsig::CONSTPHI :
00326     return PhiConstFunction(x,values);
00327   }
00328   
00329   //  std::cout << "returning function " << type << " " << func << " " << result << " " << x[0] << std::endl; 
00330 
00331   return result;
00332 }
00333 
00334 double metsig::SignAlgoResolutions::EtFunction( const functionPars &x, const functionPars & par) const
00335 {
00336   if(par.size()<3)
00337     return 0.;
00338   if(x.size()<1)
00339     return 0.;
00340   double et=x[0];
00341   if(et<=0.)
00342     return 0.;
00343   double result = et*sqrt((par[2]*par[2])+(par[1]*par[1]/et)+(par[0]*par[0]/(et*et)));
00344   return result;
00345 }
00346 
00347 
00348 double metsig::SignAlgoResolutions::PhiFunction(const functionPars &x,const  functionPars & par) const
00349 {
00350   double et=x[0];
00351   if(et<=0.){
00352     return 0.;
00353   }
00354 
00355   //if 1 parameter is C provided, returns C*pT, if three parameters N, S, C are provided, it returns the usual resolution value, as for sigmaPt
00356   if(par.size()!=1 && par.size()!=3){//only 1 or 3 parameters supported for phi function
00357       return 0.;
00358   }
00359   else if(par.size()==1){
00360     return par[0]*et;
00361   }
00362   else{
00363     return et*sqrt((par[2]*par[2])+(par[1]*par[1]/et)+(par[0]*par[0]/(et*et)));
00364   }
00365 
00366 }
00367 double metsig::SignAlgoResolutions::PFunction(const functionPars &x, const functionPars & par) const
00368 {
00369   // not currently implemented
00370   return 0;
00371 }
00372 
00373 double metsig::SignAlgoResolutions::PhiConstFunction(const functionPars& x, const functionPars &par) const
00374 {
00375   return par[0];
00376 }
00377 
00378 void
00379 metsig::SignAlgoResolutions::initializeJetResolutions( const edm::ParameterSet &iConfig ) {
00380   
00381   using namespace std;
00382   
00383   // only reinitialize the resolutsion if the pointers are zero
00384   if ( ptResol_ == 0 ) {
00385     string resolutionsAlgo  = iConfig.getParameter<std::string>("resolutionsAlgo");     
00386     string resolutionsEra   = iConfig.getParameter<std::string>("resolutionsEra");     
00387 
00388     string cmssw_base(getenv("CMSSW_BASE"));
00389     string cmssw_release_base(getenv("CMSSW_RELEASE_BASE"));
00390     string path = cmssw_base + "/src/CondFormats/JetMETObjects/data";
00391     struct stat st;
00392     if (stat(path.c_str(),&st)!=0) {
00393       path = cmssw_release_base + "/src/CondFormats/JetMETObjects/data";
00394     }
00395     if (stat(path.c_str(),&st)!=0) {
00396       cerr<<"ERROR: tried to set path but failed, abort."<<endl;
00397     }    
00398     string era(resolutionsEra);
00399     string alg(resolutionsAlgo);
00400     string ptFileName  = path + "/" + era + "_PtResolution_" +alg+".txt";
00401     string phiFileName = path + "/" + era + "_PhiResolution_"+alg+".txt";
00402     
00403     ptResol_ = new JetResolution(ptFileName,false);
00404     phiResol_ = new JetResolution(phiFileName,false);
00405   }
00406 }