Newer
Older
TestStandRepository / Software / TbNtupleMaker / Alibava / ChanList.cc
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include "ChanList.h"
  8.  
  9. static char svStr[8192];
  10.  
  11. //////////////////////////////////////////////////////////////////////////
  12. // ChanList class
  13. //
  14. // Created: Mon Sep 7 18:50:38 1998 Author: Carlos Lacasta
  15. // Purpose:
  16. //
  17. //////////////////////////////////////////////////////////////////////////
  18. int cmp(const void *x1, const void *x2)
  19. {
  20. double d1 = *(double *) x1 - *(double *) x2;
  21. return (d1 == 0. ? 0 : (d1 < 0. ? -1 : 1));
  22. }
  23. int icmp(const void *x1, const void *x2)
  24. {
  25. int d1 = *(int *) x1 - *(int *) x2;
  26. return (d1 == 0 ? 0 : (d1 < 0 ? -1 : 1));
  27. }
  28.  
  29. ChanList::ChanList(int i1, int i2) :
  30. nch(0), cm(0.), noise(0.)
  31. {
  32. int i;
  33. for (i = 0; i < nch; i++)
  34. ch.push_back(i1 + i);
  35.  
  36. nch = ch.size();
  37. }
  38. ChanList::ChanList(const char *str) :
  39. cm(0.), noise(0.)
  40. {
  41. if (str == 0)
  42. return;
  43. Set(str);
  44. }
  45.  
  46. ChanList::ChanList(const ChanList &cl)
  47. {
  48. copy(cl);
  49. }
  50.  
  51. ChanList &ChanList::operator=(const ChanList &cl)
  52. {
  53. if (&cl == this)
  54. return *this;
  55. copy(cl);
  56. return *this;
  57. }
  58.  
  59. void ChanList::copy(const ChanList &cl)
  60. {
  61. if ( &cl != this )
  62. {
  63. hits = cl.hits;
  64. ch = cl.ch;
  65. nch = ch.size();
  66. cm = cl.cm;
  67. noise = cl.noise;
  68. }
  69. }
  70. int ChanList::Set(const char *str)
  71. {
  72. char *p, *q;
  73. int i, is;
  74. int ival1, ival2, wRange;
  75.  
  76. ch.clear();
  77. strcpy(svStr, str);
  78. p = q = svStr;
  79. wRange = 0;
  80. while (*p)
  81. {
  82. switch (*p)
  83. {
  84. case ',':
  85. *p = 0;
  86. if (wRange)
  87. {
  88. is = sscanf(q, "%d", &ival2);
  89. for (i = ival1 + 1; i <= ival2; i++)
  90. ch.push_back(i);
  91. }
  92. else
  93. {
  94. is = sscanf(q, "%d", &ival1);
  95. ch.push_back(ival1);
  96. }
  97. wRange = 0;
  98. q = p + 1;
  99. p += is;
  100. break;
  101. case '-':
  102. *p = 0;
  103. wRange = 1;
  104. is = sscanf(q, "%d", &ival1);
  105. ch.push_back(ival1);
  106. q = p + 1;
  107. p += is;
  108. break;
  109. }
  110. p++;
  111. }
  112. if (wRange)
  113. {
  114. is = sscanf(q, "%d", &ival2);
  115. for (i = ival1 + 1; i <= ival2; i++)
  116. ch.push_back(i);
  117. }
  118. else
  119. {
  120. is = sscanf(q, "%d", &ival2);
  121. ch.push_back(ival2);
  122. }
  123. // sort
  124. std::sort(ch.begin(), ch.end());
  125. nch = ch.size();
  126. return nch;
  127. }
  128.  
  129. std::ostream &operator<<(std::ostream &os, ChanList &cl)
  130. {
  131. for (int i = 0; i < cl.Nch(); i++)
  132. {
  133. if (i && !(i % 15))
  134. os << std::endl;
  135. os << std::setw(5) << cl[i];
  136. }
  137. return os;
  138. }
  139.  
  140. int ChanList::ParseChanList(const char *str, ChanList **cl)
  141. {
  142. int ireg, nreg = 0;
  143. std::vector<char *> clst;
  144. char *q, *p, *ptr;
  145.  
  146. if (str == 0)
  147. return 0;
  148. if (cl == 0)
  149. return 0;
  150. p = ptr = strdup(str);
  151. do
  152. {
  153. clst.push_back(p);
  154. q = strchr(p, ';');
  155. if (q == 0)
  156. break;
  157. *q++ = 0;
  158. p = q;
  159. } while (*p);
  160.  
  161. nreg = clst.size();
  162. *cl = new ChanList[nreg];
  163. for (ireg = 0; ireg < nreg; ireg++)
  164. (*cl)[ireg].Set(clst[ireg]);
  165.  
  166. free(ptr);
  167. return nreg;
  168. }