CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/Validation/RecoJets/plugins/Comparison.h

Go to the documentation of this file.
00001 #ifndef Comparison_h
00002 #define Comparison_h
00003 
00004 #include <memory>
00005 #include <string>
00006 #include <vector>
00007 
00008 #include "TH1F.h"
00009 
00010 #include "FWCore/Framework/interface/Event.h"
00011 #include "FWCore/Utilities/interface/InputTag.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013 #include "Validation/RecoJets/plugins/CaloJetQualifier.h"
00014 
00015 #include "FWCore/Utilities/interface/EDMException.h"
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 #include "Validation/RecoJets/interface/NameScheme.h"
00018 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00019 
00020 
00021 template <typename Ref, typename RefQualifier, typename Rec, typename RecQualifier, typename Alg>
00022 class Comparison {
00023   
00024  public:
00025   Comparison(const edm::ParameterSet&);
00026   ~Comparison(){};
00027   std::map<unsigned int, unsigned int> operator()(const Ref&, const Rec&);
00028   void book();
00029   void book(ofstream&);
00030   void summarize();
00031   
00032  private:
00033 
00034   double maxDR_;
00035   double minPtRef_, maxPtRef_;
00036   double minPtRec_, maxPtRec_;
00037   double minEtaRef_, maxEtaRef_;
00038   double minEtaRec_, maxEtaRec_;
00039 
00040  private:
00041 
00042   std::vector<TH1F*> hDR_;   // deltaR matching
00043   std::vector<TH1F*> hPt_;   // matching efficiency vs pt
00044   std::vector<TH1F*> hEta_;  // matching efficiency vs eta 
00045 
00046   Alg alg_;
00047   RefQualifier refQualifier_;
00048   RecQualifier recQualifier_; 
00049 
00050   unsigned int found_;
00051   unsigned int missed_;
00052   unsigned int failed_; 
00053 };
00054 
00055 template <typename Ref, typename RefQualifier, typename Rec, typename RecQualifier, typename Alg>
00056 Comparison<Ref, RefQualifier, Rec, RecQualifier, Alg>::Comparison(const edm::ParameterSet& cfg):
00057   maxDR_( cfg.getParameter<double>( "maxDR" ) ),
00058   minPtRef_ ( cfg.getParameter<double>( "minPtRef"  ) ),
00059   maxPtRef_ ( cfg.getParameter<double>( "maxPtRef"  ) ),
00060   minPtRec_ ( cfg.getParameter<double>( "minPtRec"  ) ),
00061   maxPtRec_ ( cfg.getParameter<double>( "maxPtRec"  ) ),
00062   minEtaRef_( cfg.getParameter<double>( "minEtaRef" ) ),
00063   maxEtaRef_( cfg.getParameter<double>( "maxEtaRef" ) ),
00064   minEtaRec_( cfg.getParameter<double>( "minEtaRec" ) ),
00065   maxEtaRec_( cfg.getParameter<double>( "maxEtaRec" ) ),
00066   refQualifier_( cfg ), recQualifier_( cfg ),
00067   found_( 0 ), missed_( 0 ), failed_( 0 )
00068 {
00069   std::string hist=cfg.getParameter<std::string>("hist");
00070   if( hist.empty() )
00071     book();
00072   else{
00073     ofstream file(hist.c_str(), std::ios::out);
00074     book(file);
00075   }
00076 }
00077 
00078 template <typename Ref, typename RefQualifier, typename Rec, typename RecQualifier, typename Alg>
00079 std::map<unsigned int, unsigned int> 
00080 Comparison<Ref, RefQualifier, Rec, RecQualifier, Alg>::operator()(const Ref& refs, const Rec& recs)
00081 {
00082   int refIdx=0;
00083   std::map<unsigned int, unsigned int> matches;
00084   for(typename Ref::const_iterator ref=refs.begin(); 
00085       ref!=refs.end(); ++ref, ++refIdx ){
00086     if( !(minEtaRef_<ref->eta() && ref->eta()<maxEtaRef_) ) 
00087       // retrict to visible range in eta
00088       continue;
00089     
00090     if( !(minPtRef_ <ref->pt()  && ref->pt() <maxPtRef_ ) )
00091       // restrict to visible range in pt
00092       continue;
00093 
00094     if( !refQualifier_( *ref ) ) 
00095       // restrtict to properly qualified reference object
00096       continue;
00097 
00098     int jetIdx=0;
00099     int match=-1;
00100     double dist=-1.;    
00101     for(typename Rec::const_iterator rec = recs.begin();
00102         rec!=recs.end(); ++rec, ++jetIdx ){
00103       if( !(minEtaRec_<rec->eta() && rec->eta()<maxEtaRec_) ) 
00104         // retrict to visible range in eta
00105         continue;
00106       
00107       if( !(minPtRec_ <rec->pt()  && rec->pt() <maxPtRec_ ) )
00108         // restrict to visible range in pt
00109         continue;
00110       
00111       if( !recQualifier_( *rec ) ) 
00112         // restrtict to properly qualified CaloJet
00113         continue;
00114       
00115       double dR = alg_(*ref, *rec);
00116       if( dist<0 || dR<dist ){
00117         dist  = dR;
00118         match = jetIdx;
00119       }
00120     }
00121     if( match<0 ) ++failed_;
00122     if( match>=0 ){
00123       if(hDR_ .size()>0) hDR_ [0]->Fill( dist );
00124       if(hPt_ .size()>0) hPt_ [0]->Fill( ref->pt()  );
00125       if(hEta_.size()>0) hEta_[0]->Fill( ref->eta() );
00126     }
00127     if( 0<dist && dist<maxDR_ ){
00128       ++found_;
00129       if( match>=0 ){
00130         if(hDR_ .size()>1) hDR_ [1]->Fill( dist );
00131         if(hPt_ .size()>1) hPt_ [1]->Fill( ref->pt()  );
00132         if(hEta_.size()>1) hEta_[1]->Fill( ref->eta() );
00133       }
00134       if( !matches.insert(std::pair<int, int>(refIdx, match)).second )
00135         edm::LogWarning ( "MapMismatch" ) 
00136           << "Match could not be inserted in map; entry already exited?!";
00137     }
00138     else ++missed_;
00139   }
00140   return matches;
00141 }
00142 
00143 template <typename Ref, typename RefQualifier, typename Rec, typename RecQualifier, typename Alg>
00144 void Comparison<Ref, RefQualifier, Rec, RecQualifier, Alg>::book()
00145 {
00146   edm::Service<TFileService> fs;
00147   if( !fs )
00148     throw edm::Exception( edm::errors::Configuration, "TFile Service is not registered in cfg file" );
00149 
00150   NameScheme match("match");
00151   static const unsigned int MAXHIST=2;
00152   for(unsigned int idx=0; idx<MAXHIST; ++idx){
00153     hDR_ .push_back( fs->make<TH1F>( match.name(      "deltaR", idx), match.name("dR"), 100, 0.,   1.) );
00154     hPt_ .push_back( fs->make<TH1F>( match.name(      "effPt",  idx), match.name("pt"),  30, 0., 300.) );
00155     hEta_.push_back( fs->make<TH1F>( match.name(      "effEta", idx), match.name("eta"), 30,-3.,   3.) );
00156   }
00157 }
00158 
00159 template <typename Ref, typename RefQualifier, typename Rec, typename RecQualifier, typename Alg>
00160 void Comparison<Ref, RefQualifier, Rec, RecQualifier, Alg>::book(ofstream& file)
00161 {
00162   edm::Service<TFileService> fs;
00163   if( !fs )
00164     throw edm::Exception( edm::errors::Configuration, "TFile Service is not registered in cfg file" );
00165 
00166   NameScheme match("match");
00167   static const unsigned int MAXHIST=2;
00168   for(unsigned int idx=0; idx<MAXHIST; ++idx){
00169     hDR_ .push_back( fs->make<TH1F>( match.name(file, "deltaR", idx), match.name("dR"), 100, 0.,   1.) );
00170     hPt_ .push_back( fs->make<TH1F>( match.name(file, "effPt",  idx), match.name("pt"),  30, 0., 300.) );
00171     hEta_.push_back( fs->make<TH1F>( match.name(file, "effEta", idx), match.name("eta"), 30,-3.,   3.) );
00172   }
00173 }
00174 
00175 template <typename Ref, typename RefQualifier, typename Rec, typename RecQualifier, typename Alg>
00176 void Comparison<Ref, RefQualifier, Rec, RecQualifier, Alg>::summarize()
00177 {
00178   unsigned int all=found_+missed_+failed_;
00179   if(all>0){
00180     edm::LogInfo("MatchSummary") << "=============================================";
00181     edm::LogInfo("MatchSummary") << "Reference :";
00182     edm::LogInfo("MatchSummary") << "CaloJet   :";
00183     edm::LogInfo("MatchSummary") << "fraction of found  jets: " << 100*found_ /all << "%";
00184     edm::LogInfo("MatchSummary") << "fraction of missed jets: " << 100*missed_/all << "%";
00185     edm::LogInfo("MatchSummary") << "fraction of failed jets: " << 100*failed_/all << "%";
00186   }
00187   else{
00188     edm::LogWarning ( "MatchOrBalanceFault" ) 
00189       << "No missed, failed nor counts found";    
00190   }
00191 }
00192 
00193 #endif