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