CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/L1Trigger/HardwareValidation/interface/DEcompare.h

Go to the documentation of this file.
00001 #ifndef DEcompare_H
00002 #define DEcompare_H
00003 
00004 /*\class template DEcompare
00005  *\description data|emulation comparison base
00006  *\author Nuno Leonardo (CERN)
00007  *\date 07.03
00008  */
00009 
00010 /*\note specialization free*/
00011 
00012 #include "L1Trigger/HardwareValidation/interface/DEtrait.h"
00013 #include "L1Trigger/HardwareValidation/interface/DEutils.h"
00014 
00015 template <typename T> struct de_rank;
00016 template <typename T> struct DEutils;
00017 template <typename T> struct DEtrait;
00018 
00019 template <typename T> 
00020 class DEcompare {
00021   
00022   typedef typename T::size_type col_sz;
00023   typedef typename T::const_iterator col_cit;
00024   typedef typename T::iterator col_it;
00025   typedef DEtrait<T> de_trait;
00026   typedef typename de_trait::coll_type coll_type;
00027   typedef typename de_trait::cand_type cand_type;
00028   typedef typename std::vector<cand_type> cand_vec;
00029   typedef typename std::pair<cand_type,cand_type> cand_pair;
00030   typedef typename de_trait::coll_type const* typeT;
00031   
00032  public:
00033   
00034   DEcompare(){};
00035   ~DEcompare(){};
00036   
00037   DEcompare(typeT dt, typeT em) : 
00038     data_(dt), emul_(em), t_match(false){
00039     ncand_[0]=get_ncand(dt);
00040     ncand_[1]=get_ncand(em);
00041     deDigiColl_.clear();
00042     if(debug_)
00043       LogDebug("DEcompare") 
00044         << "DEcompare" 
00045         << ": creating instance of type: " << GetName(0)
00046         << ", data size:" << data_->size() << " ncand:" << ncand_[0] 
00047         << ", emul size:" << emul_->size() << " ncand:" << ncand_[1] 
00048         << ".\n" << std::flush;
00049   };
00050 
00051   //handles constructor (deprecated)
00052   /*
00053   DEcompare(edm::Handle<T> dt, edm::Handle<T> em) : 
00054     data_(dt.product()), emul_(em.product()), t_match(false) {
00055     ncand_[0]=get_ncand(dt.product());
00056     ncand_[1]=get_ncand(em.product());
00057     deDigiColl_.clear();
00058   } 
00059   */
00060 
00061   bool do_compare(std::ofstream&,int dump = 0);
00062   bool CompareCollections(std::ofstream&,int dump = 0);
00063   bool SortCollections(cand_vec& dg, cand_vec& eg, cand_vec& db, cand_vec& eb);
00064   bool DumpCandidate(col_cit itd, col_cit itm, std::ofstream&, int mode = 0);
00065   int get_ncand(typeT) const;
00066   int get_ncand(int i) const {
00067     if(debug_ && (i<0 || i>2) )
00068       LogDebug("DEcompare") 
00069         << "DEcompare illegal number of candidates request flag:"<<i<<"\n";
00070     return ncand_[i];
00071   }
00072 
00073   std::string GetName(int i=0)  const {return de_utils.GetName(i);}
00074   std::string print(col_cit it) const {return de_utils.print(it);}
00075   bool is_empty(col_cit it)     const {return de_utils.is_empty(it);}
00076   inline int de_type()          const {return de_trait::de_type();}
00077   bool get_match()              const {return t_match;}
00078 
00079   L1DEDigiCollection getDEDigis() const {return deDigiColl_;}
00080 
00081   std::string print() const {
00082     std::stringstream ss("");
00083     for(col_cit itd=data_->begin();itd!=data_->end();itd++)
00084       if(!is_empty(itd)) ss << "  data: " << print(itd);      
00085     for(col_cit itm=emul_->begin();itm!=emul_->end();itm++)
00086       if(!is_empty(itm)) ss << "  emul: " << print(itm);      
00087     return ss.str();
00088   }
00089   
00090  public:
00091 
00092   static const int debug_=0;
00093 
00094  private:
00095 
00096   typeT data_;
00097   typeT emul_;
00098   DEutils<T> de_utils;
00099   bool t_match;
00100   int ncand_[2];
00101   L1DEDigiCollection deDigiColl_;
00102   
00103 };
00104 
00105 template <typename T> 
00106 bool DEcompare<T>::do_compare(std::ofstream& os, int dump) {
00107   if(debug_)
00108     std::cout << " DEcompare::do_compare... " 
00109               << GetName() << "\n" << std::flush;
00110   t_match = CompareCollections(os,dump);
00111   char ok[10];
00112   if(t_match) sprintf(ok,"successful");
00113   else        sprintf(ok,"failed");
00114   if(dump==-1 || (dump==1 && !t_match))
00115     os << "  ..." << GetName() 
00116        << " data and emulator comparison: " << ok << std::endl;
00117   return t_match;
00118 }
00119 
00120 template <typename T> 
00121 int DEcompare<T>::get_ncand(typeT col) const {
00122   int ncand=0;
00123   for (col_cit it = col->begin(); it!=col->end(); it++) {
00124     if(!is_empty(it)) {
00125       ncand++;
00126     }
00127   }
00128   return ncand;
00129 }
00130 
00131 template <typename T> 
00132 bool DEcompare<T>::CompareCollections(std::ofstream& os, int dump) {
00133 
00134   if(debug_)
00135     std::cout << " DEcompare::CompareCollections...\n"<< std::flush; 
00136   bool match = true;
00137   int ndata = get_ncand(0);
00138   int nemul = get_ncand(1);
00139   assert (ndata || nemul);
00140 
00141   cand_vec data_good, emul_good, data_bad, emul_bad; 
00142   data_good.reserve(data_->size());
00143   emul_good.reserve(emul_->size());
00144   data_bad .reserve(data_->size());
00145   emul_bad .reserve(emul_->size());
00146 
00147   // find matching candidates --  tbd report order match
00148   match &= SortCollections(data_good,emul_good,data_bad,emul_bad);  
00149 
00150   if(debug_)
00151     std::cout << "\tDEcompare stats2:" 
00152               << " data_bad:"  << data_bad .size()
00153               << " emul_bad:"  << emul_bad .size()
00154               << " data_good:" << data_good.size()
00155               << " emul_good:" << emul_good.size()
00156               << " data_:"     << data_->size()
00157               << " emul_:"     << emul_->size()
00158               << ".\n"         <<std::flush;
00159   
00160   if(dump==-1 || (dump==1 && !match)) {
00161     os << "  number of candidates: " << ndata;
00162     if(ndata!=nemul) {
00163       match &= false;
00164       os << " (data) " << nemul << " (emul) disagree";
00165     }
00166     os << std::endl;
00167   }
00168   
00169 
00170   col_cit itd, itm; 
00171   int prtmode=0;
00172   col_sz ndt=0, nem=0, nde=0;
00173   deDigiColl_.clear();
00174 
00176 
00177   ndt = data_bad.size();
00178   nem = emul_bad.size();
00179   nde = (ndt>nem)?ndt:nem;
00180   
00181   itd = data_bad.begin();
00182   itm = emul_bad.begin();
00183 
00184   if(dump==-1)
00185     os << "   un-matched (" << nde << ")\n";
00186 
00187   for (col_sz i=0; i<nde; i++) {
00188 
00189     if     (i< ndt && i< nem) prtmode=0; //dt+em
00190     else if(i< ndt && i>=nem) prtmode=1; //dt
00191     else if(i>=ndt && i< nem) prtmode=2; //em
00192     else assert(0);
00193 
00194     if(abs(dump)==1)
00195       DumpCandidate(itd,itm,os,prtmode);
00196     else if(prtmode==0) { 
00197       if( (dump==2 && !de_utils.de_equal_loc(*itd,*itm)) ||
00198           (dump==3 &&  de_utils.de_equal_loc(*itd,*itm)) )
00199         DumpCandidate(itd,itm,os,prtmode);
00200     }
00201 
00202     // Fill in DEdigi collection 
00203     if(prtmode==0) {
00204       if(de_utils.de_equal_loc(*itd,*itm)) {
00205         deDigiColl_.push_back( de_utils.DEDigi(itd,itm,1) );
00206       } else {
00207         deDigiColl_.push_back( de_utils.DEDigi(itd,itm,2) );
00208       }
00209     } else if (prtmode==1) {
00210       deDigiColl_.push_back(   de_utils.DEDigi(itd,itd,3) );
00211     } else if (prtmode==2) {
00212       deDigiColl_.push_back(   de_utils.DEDigi(itm,itm,4) );
00213     }
00214 
00215     itd++; itm++;
00216   }
00217   
00219 
00220   itd = data_good.begin();
00221   itm = emul_good.begin();
00222 
00223   assert(data_good.size()==emul_good.size());
00224   if(dump==-1)
00225     os << "   matched (" << data_good.size() << ")\n";
00226 
00227   for(col_sz i=0; i<data_good.size(); i++) {
00228     assert(de_utils.de_equal(*itd,*itm));
00229     if(dump==-1)
00230       DumpCandidate(itd,itm,os,prtmode);
00231     deDigiColl_.push_back( de_utils.DEDigi(itd,itm,0) );
00232     itd++; itm++;
00233   }  
00234   
00235   if(debug_)
00236     std::cout << "DEcompare<T>::CompareCollections end.\n"<< std::flush; 
00237 
00238   return match; 
00239 }
00240 
00241 
00242 template <typename T> 
00243 bool DEcompare<T>::SortCollections(cand_vec& data_good, cand_vec& emul_good,
00244                                    cand_vec& data_bad,  cand_vec& emul_bad ) {
00245   if(debug_)
00246     std::cout << " DEcompare::SortCollections...\n"<< std::flush; 
00247 
00248   bool match = true;
00249   cand_vec data_tmp, emul_tmp;
00250   
00251   data_good.clear();
00252   emul_good.clear();
00253   data_bad .clear();
00254   emul_bad .clear();
00255   data_tmp .clear();
00256   emul_tmp .clear();
00257 
00258   for(col_cit ite = emul_->begin(); ite != emul_->end(); ite++) {
00259     if(!is_empty(ite)) {
00260       emul_tmp.push_back(*ite);
00261     }
00262   }
00263   
00264   for(col_cit itd = data_->begin(); itd != data_->end(); itd++) {
00265     if(is_empty(itd)) continue;
00266     col_it ite = emul_tmp.end();
00267     ite = de_utils.de_find(emul_tmp.begin(),emul_tmp.end(),*itd);
00268     if(ite!=emul_tmp.end()) {
00269       data_good.push_back(*itd);
00270       emul_good.push_back(*ite);
00271       ite=emul_tmp.erase(ite);
00272     } else {
00273       data_tmp.push_back(*itd);
00274       match &= false;
00275     }
00276   }
00277 
00278   for(col_it itd = data_tmp.begin(); itd != data_tmp.end(); itd++) {
00279     for(col_it ite = emul_tmp.begin(); ite != emul_tmp.end(); ite++) {
00280       if(de_utils.de_equal_loc(*itd,*ite)) {
00281         data_bad.push_back(*itd);
00282         emul_bad.push_back(*ite);
00283         itd = data_tmp.erase(itd)-1;
00284         ite = emul_tmp.erase(ite)-1;
00285         break;
00286       }  
00287     }
00288   }
00289 
00290   sort(data_tmp.begin(), data_tmp.end(),de_rank<coll_type>());
00291   sort(emul_tmp.begin(), emul_tmp.end(),de_rank<coll_type>());
00292 
00293   data_bad.insert(data_bad.end(),data_tmp.begin(),data_tmp.end());
00294   emul_bad.insert(emul_bad.end(),emul_tmp.begin(),emul_tmp.end());
00295 
00296   if(debug_)
00297     std::cout << "\tDEcompare stats1:" 
00298               << " data_bad:"  << data_bad .size()
00299               << " emul_bad:"  << emul_bad .size()
00300               << " data_good:" << data_good.size()
00301               << " emul_good:" << emul_good.size()
00302               << " data: "     << get_ncand(data_)
00303               << " emul: "     << get_ncand(emul_)
00304               << "\n" << std::flush;
00305   if(debug_)  
00306     std::cout << "DEcompare<T>::SortCollections end.\n"<< std::flush; 
00307   return match;
00308 }
00309 
00310 template <typename T> 
00311 bool DEcompare<T>::DumpCandidate(col_cit itd, col_cit itm, std::ofstream& os, int mode) {
00312   if(mode!=2)
00313     os << "   data: " << print(itd);
00314   if(mode!=1)
00315     os << "   emul: " << print(itm) << std::endl;
00316   if(mode==0) 
00317     if( !de_utils.de_equal(*itd,*itm) ) 
00318       return false;
00319   return true;
00320 }
00321 
00322 #endif