CMS 3D CMS Logo

DEcompare.h
Go to the documentation of this file.
1 #ifndef DEcompare_H
2 #define DEcompare_H
3 
4 /*\class template DEcompare
5  *\description data|emulation comparison base
6  *\author Nuno Leonardo (CERN)
7  *\date 07.03
8  */
9 
10 /*\note specialization free*/
11 
12 #include <fstream>
13 
16 
17 template <typename T> struct de_rank;
18 template <typename T> struct DEutils;
19 template <typename T> struct DEtrait;
20 
21 template <typename T>
22 class DEcompare {
23 
24  typedef typename T::size_type col_sz;
25  typedef typename T::const_iterator col_cit;
26  typedef typename T::iterator col_it;
28  typedef typename de_trait::coll_type coll_type;
29  typedef typename de_trait::cand_type cand_type;
30  typedef typename std::vector<cand_type> cand_vec;
31  typedef typename std::pair<cand_type,cand_type> cand_pair;
32  typedef typename de_trait::coll_type const* typeT;
33 
34  public:
35 
36  DEcompare(){};
38 
39  DEcompare(typeT dt, typeT em) :
40  data_(dt), emul_(em), t_match(false){
41  ncand_[0]=get_ncand(dt);
42  ncand_[1]=get_ncand(em);
43  deDigiColl_.clear();
44  if(debug_)
45  LogDebug("DEcompare")
46  << "DEcompare"
47  << ": creating instance of type: " << GetName(0)
48  << ", data size:" << data_->size() << " ncand:" << ncand_[0]
49  << ", emul size:" << emul_->size() << " ncand:" << ncand_[1]
50  << ".\n" << std::flush;
51  };
52 
53  //handles constructor (deprecated)
54  /*
55  DEcompare(edm::Handle<T> dt, edm::Handle<T> em) :
56  data_(dt.product()), emul_(em.product()), t_match(false) {
57  ncand_[0]=get_ncand(dt.product());
58  ncand_[1]=get_ncand(em.product());
59  deDigiColl_.clear();
60  }
61  */
62 
63  bool do_compare(std::ofstream&,int dump = 0);
64  bool CompareCollections(std::ofstream&,int dump = 0);
65  bool SortCollections(cand_vec& dg, cand_vec& eg, cand_vec& db, cand_vec& eb);
66  bool DumpCandidate(col_cit itd, col_cit itm, std::ofstream&, int mode = 0);
67  int get_ncand(typeT) const;
68  int get_ncand(int i) const {
69  if(debug_ && (i<0 || i>2) )
70  LogDebug("DEcompare")
71  << "DEcompare illegal number of candidates request flag:"<<i<<"\n";
72  return ncand_[i];
73  }
74 
75  std::string GetName(int i=0) const {return de_utils.GetName(i);}
76  std::string print(col_cit it) const {return de_utils.print(it);}
77  bool is_empty(col_cit it) const {return de_utils.is_empty(it);}
78  inline int de_type() const {return de_trait::de_type();}
79  bool get_match() const {return t_match;}
80 
82 
83  std::string print() const {
84  std::stringstream ss("");
85  for(col_cit itd=data_->begin();itd!=data_->end();itd++)
86  if(!is_empty(itd)) ss << " data: " << print(itd);
87  for(col_cit itm=emul_->begin();itm!=emul_->end();itm++)
88  if(!is_empty(itm)) ss << " emul: " << print(itm);
89  return ss.str();
90  }
91 
92  public:
93 
94  static const int debug_=0;
95 
96  private:
97 
98  typeT data_;
99  typeT emul_;
101  bool t_match;
102  int ncand_[2];
104 
105 };
106 
107 template <typename T>
108 bool DEcompare<T>::do_compare(std::ofstream& os, int dump) {
109  if(debug_)
110  std::cout << " DEcompare::do_compare... "
111  << GetName() << "\n" << std::flush;
112  t_match = CompareCollections(os,dump);
113  std::string ok;
114  if(t_match) ok="successful";
115  else ok="failed";
116  if(dump==-1 || (dump==1 && !t_match))
117  os << " ..." << GetName()
118  << " data and emulator comparison: " << ok.c_str() << std::endl;
119  return t_match;
120 }
121 
122 template <typename T>
124  int ncand=0;
125  for (col_cit it = col->begin(); it!=col->end(); it++) {
126  if(!is_empty(it)) {
127  ncand++;
128  }
129  }
130  return ncand;
131 }
132 
133 template <typename T>
134 bool DEcompare<T>::CompareCollections(std::ofstream& os, int dump) {
135 
136  if(debug_)
137  std::cout << " DEcompare::CompareCollections...\n"<< std::flush;
138  bool match = true;
139  int ndata = get_ncand(0);
140  int nemul = get_ncand(1);
141  assert (ndata || nemul);
142 
143  cand_vec data_good, emul_good, data_bad, emul_bad;
144  data_good.reserve(data_->size());
145  emul_good.reserve(emul_->size());
146  data_bad .reserve(data_->size());
147  emul_bad .reserve(emul_->size());
148 
149  // find matching candidates -- tbd report order match
150  match &= SortCollections(data_good,emul_good,data_bad,emul_bad);
151 
152  if(debug_)
153  std::cout << "\tDEcompare stats2:"
154  << " data_bad:" << data_bad .size()
155  << " emul_bad:" << emul_bad .size()
156  << " data_good:" << data_good.size()
157  << " emul_good:" << emul_good.size()
158  << " data_:" << data_->size()
159  << " emul_:" << emul_->size()
160  << ".\n" <<std::flush;
161 
162  if(dump==-1 || (dump==1 && !match)) {
163  os << " number of candidates: " << ndata;
164  if(ndata!=nemul) {
165  match &= false;
166  os << " (data) " << nemul << " (emul) disagree";
167  }
168  os << std::endl;
169  }
170 
171 
172  col_cit itd, itm;
173  int prtmode=0;
174  col_sz ndt=0, nem=0, nde=0;
175  deDigiColl_.clear();
176 
178 
179  ndt = data_bad.size();
180  nem = emul_bad.size();
181  nde = (ndt>nem)?ndt:nem;
182 
183  itd = data_bad.begin();
184  itm = emul_bad.begin();
185 
186  if(dump==-1)
187  os << " un-matched (" << nde << ")\n";
188 
189  for (col_sz i=0; i<nde; i++) {
190 
191  if (i< ndt && i< nem) prtmode=0; //dt+em
192  else if(i< ndt && i>=nem) prtmode=1; //dt
193  else if(i>=ndt && i< nem) prtmode=2; //em
194  else assert(0);
195 
196  if(abs(dump)==1)
197  DumpCandidate(itd,itm,os,prtmode);
198  else if(prtmode==0) {
199  if( (dump==2 && !de_utils.de_equal_loc(*itd,*itm)) ||
200  (dump==3 && de_utils.de_equal_loc(*itd,*itm)) )
201  DumpCandidate(itd,itm,os,prtmode);
202  }
203 
204  // Fill in DEdigi collection
205  if(prtmode==0) {
206  if(de_utils.de_equal_loc(*itd,*itm)) {
207  deDigiColl_.push_back( de_utils.DEDigi(itd,itm,1) );
208  } else {
209  deDigiColl_.push_back( de_utils.DEDigi(itd,itm,2) );
210  }
211  } else if (prtmode==1) {
212  deDigiColl_.push_back( de_utils.DEDigi(itd,itd,3) );
213  } else if (prtmode==2) {
214  deDigiColl_.push_back( de_utils.DEDigi(itm,itm,4) );
215  }
216 
217  itd++; itm++;
218  }
219 
221 
222  itd = data_good.begin();
223  itm = emul_good.begin();
224 
225  assert(data_good.size()==emul_good.size());
226  if(dump==-1)
227  os << " matched (" << data_good.size() << ")\n";
228 
229  for(col_sz i=0; i<data_good.size(); i++) {
230  assert(de_utils.de_equal(*itd,*itm));
231  if(dump==-1)
232  DumpCandidate(itd,itm,os,prtmode);
233  deDigiColl_.push_back( de_utils.DEDigi(itd,itm,0) );
234  itd++; itm++;
235  }
236 
237  if(debug_)
238  std::cout << "DEcompare<T>::CompareCollections end.\n"<< std::flush;
239 
240  return match;
241 }
242 
243 
244 template <typename T>
245 bool DEcompare<T>::SortCollections(cand_vec& data_good, cand_vec& emul_good,
246  cand_vec& data_bad, cand_vec& emul_bad ) {
247  if(debug_)
248  std::cout << " DEcompare::SortCollections...\n"<< std::flush;
249 
250  bool match = true;
251  cand_vec data_tmp, emul_tmp;
252 
253  data_good.clear();
254  emul_good.clear();
255  data_bad .clear();
256  emul_bad .clear();
257  data_tmp .clear();
258  emul_tmp .clear();
259 
260  for(col_cit ite = emul_->begin(); ite != emul_->end(); ite++) {
261  if(!is_empty(ite)) {
262  emul_tmp.push_back(*ite);
263  }
264  }
265 
266  for(col_cit itd = data_->begin(); itd != data_->end(); itd++) {
267  if(is_empty(itd)) continue;
268  col_it ite = emul_tmp.end();
269  ite = de_utils.de_find(emul_tmp.begin(),emul_tmp.end(),*itd);
270  if(ite!=emul_tmp.end()) {
271  data_good.push_back(*itd);
272  emul_good.push_back(*ite);
273  ite=emul_tmp.erase(ite);
274  } else {
275  data_tmp.push_back(*itd);
276  match &= false;
277  }
278  }
279 
280  for(col_it itd = data_tmp.begin(); itd != data_tmp.end(); itd++) {
281  for(col_it ite = emul_tmp.begin(); ite != emul_tmp.end(); ite++) {
282  if(de_utils.de_equal_loc(*itd,*ite)) {
283  data_bad.push_back(*itd);
284  emul_bad.push_back(*ite);
285  itd = data_tmp.erase(itd)-1;
286  ite = emul_tmp.erase(ite)-1;
287  break;
288  }
289  }
290  }
291 
292  sort(data_tmp.begin(), data_tmp.end(),de_rank<coll_type>());
293  sort(emul_tmp.begin(), emul_tmp.end(),de_rank<coll_type>());
294 
295  data_bad.insert(data_bad.end(),data_tmp.begin(),data_tmp.end());
296  emul_bad.insert(emul_bad.end(),emul_tmp.begin(),emul_tmp.end());
297 
298  if(debug_)
299  std::cout << "\tDEcompare stats1:"
300  << " data_bad:" << data_bad .size()
301  << " emul_bad:" << emul_bad .size()
302  << " data_good:" << data_good.size()
303  << " emul_good:" << emul_good.size()
304  << " data: " << get_ncand(data_)
305  << " emul: " << get_ncand(emul_)
306  << "\n" << std::flush;
307  if(debug_)
308  std::cout << "DEcompare<T>::SortCollections end.\n"<< std::flush;
309  return match;
310 }
311 
312 template <typename T>
313 bool DEcompare<T>::DumpCandidate(col_cit itd, col_cit itm, std::ofstream& os, int mode) {
314  if(mode!=2)
315  os << " data: " << print(itd);
316  if(mode!=1)
317  os << " emul: " << print(itm) << std::endl;
318  if(mode==0)
319  if( !de_utils.de_equal(*itd,*itm) )
320  return false;
321  return true;
322 }
323 
324 #endif
#define LogDebug(id)
float dt
Definition: AMPTWrapper.h:126
~DEcompare()
Definition: DEcompare.h:37
bool CompareCollections(std::ofstream &, int dump=0)
Definition: DEcompare.h:134
std::string print(col_cit it) const
Definition: DEcompare.h:76
— order candidates —
Definition: DEcompare.h:17
static const int debug_
Definition: DEcompare.h:94
de_trait::coll_type const * typeT
Definition: DEcompare.h:32
typeT data_
Definition: DEcompare.h:98
int get_ncand(typeT) const
Definition: DEcompare.h:123
de_trait::cand_type cand_type
Definition: DEcompare.h:29
bool SortCollections(cand_vec &dg, cand_vec &eg, cand_vec &db, cand_vec &eb)
Definition: DEcompare.h:245
std::vector< L1DataEmulDigi > L1DEDigiCollection
Definition: DEtrait.h:80
uint16_t size_type
bool get_match() const
Definition: DEcompare.h:79
L1DEDigiCollection getDEDigis() const
Definition: DEcompare.h:81
DEtrait< T > de_trait
Definition: DEcompare.h:27
int get_ncand(int i) const
Definition: DEcompare.h:68
int de_type() const
Definition: DEcompare.h:78
std::pair< cand_type, cand_type > cand_pair
Definition: DEcompare.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::string print() const
Definition: DEcompare.h:83
std::vector< cand_type > cand_vec
Definition: DEcompare.h:30
std::string GetName(int i=0) const
Definition: DEcompare.h:75
bool do_compare(std::ofstream &, int dump=0)
Definition: DEcompare.h:108
T::size_type col_sz
Definition: DEcompare.h:24
bool DumpCandidate(col_cit itd, col_cit itm, std::ofstream &, int mode=0)
Definition: DEcompare.h:313
int ncand_[2]
Definition: DEcompare.h:102
bool is_empty(col_cit it) const
Definition: DEcompare.h:77
T::const_iterator col_cit
Definition: DEcompare.h:25
col
Definition: cuy.py:1009
bool t_match
Definition: DEcompare.h:101
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
DEcompare()
Definition: DEcompare.h:36
DEutils< T > de_utils
Definition: DEcompare.h:100
T::iterator col_it
Definition: DEcompare.h:26
de_trait::coll_type coll_type
Definition: DEcompare.h:28
typeT emul_
Definition: DEcompare.h:99
DEcompare(typeT dt, typeT em)
Definition: DEcompare.h:39
L1DEDigiCollection deDigiColl_
Definition: DEcompare.h:103