| 1 | #include "PixelMap.h" | 
|---|
| 2 | #include <iostream> | 
|---|
| 3 | #include <fstream> | 
|---|
| 4 | #include <cstring> | 
|---|
| 5 | #include <cstdlib> | 
|---|
| 6 | #include <cctype> | 
|---|
| 7 |  | 
|---|
| 8 | PixelMap::PixelMap(std::string file, bool verbose) { | 
|---|
| 9 |  | 
|---|
| 10 | pixelmap.clear(); | 
|---|
| 11 | ReadPixelMap(pixelmap, file, verbose); | 
|---|
| 12 |  | 
|---|
| 13 | } | 
|---|
| 14 |  | 
|---|
| 15 | PixelMap::~PixelMap() { | 
|---|
| 16 |  | 
|---|
| 17 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 18 | delete ((*iter).second); | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | void PixelMap::ReadPixelMap(std::map<std::string, Pixel*>& pixelmap, std::string file, bool verbose) { | 
|---|
| 24 |  | 
|---|
| 25 | //std::string filename("PixelMap.txt"); | 
|---|
| 26 | std::ifstream infile(file.c_str(), std::fstream::in); | 
|---|
| 27 |  | 
|---|
| 28 | if (verbose) { | 
|---|
| 29 | std::cout<<"Reading mapping file: "<<file<<std::endl<<std::endl; | 
|---|
| 30 | if (!infile.good()) { | 
|---|
| 31 | std::cerr<<"ERROR in PixelMap::ReadPixelMap: File "<<file<<" cannot be found."<<std::endl; | 
|---|
| 32 | } | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | while (infile.good()) { | 
|---|
| 36 |  | 
|---|
| 37 | char line[1024]; | 
|---|
| 38 |  | 
|---|
| 39 | infile.getline(line, 1024); | 
|---|
| 40 |  | 
|---|
| 41 | if (line[0] == '#') { | 
|---|
| 42 | if (verbose) { | 
|---|
| 43 | std::cout << "Ignoring comment: " << line << std::endl; | 
|---|
| 44 | } | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | else { | 
|---|
| 48 |  | 
|---|
| 49 | if (strlen(line)!=0) { | 
|---|
| 50 |  | 
|---|
| 51 | char delim[] = ":"; | 
|---|
| 52 | char *name = NULL; //unformated name before : tokens | 
|---|
| 53 | char name2[256] = ""; //formated name without whitespaces | 
|---|
| 54 | char *rest = NULL; | 
|---|
| 55 |  | 
|---|
| 56 | name = strtok(line, delim); | 
|---|
| 57 |  | 
|---|
| 58 | unsigned int count1 = 0; //count non-whitespace characters in name | 
|---|
| 59 | unsigned int count2 = 0; //count number of - in name | 
|---|
| 60 |  | 
|---|
| 61 | for (unsigned int i = 0; i < strlen(name); i++) { | 
|---|
| 62 |  | 
|---|
| 63 | if ( !(isspace(name[i])) ) { | 
|---|
| 64 |  | 
|---|
| 65 | name2[count1] = name[i]; | 
|---|
| 66 | count1+=1; | 
|---|
| 67 |  | 
|---|
| 68 | if ( (name[i] == 45) ) { | 
|---|
| 69 | count2+=1; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | //if (verbose) { | 
|---|
| 77 | //  std::cout << "Formated pixel name: " << name2 << std::endl; | 
|---|
| 78 | //} | 
|---|
| 79 |  | 
|---|
| 80 | if( (count1 > 4) && (count1 < 7) && (count2 == 2) ) { | 
|---|
| 81 |  | 
|---|
| 82 | rest = strtok(NULL, delim); | 
|---|
| 83 |  | 
|---|
| 84 | if(rest != NULL) { | 
|---|
| 85 |  | 
|---|
| 86 | unsigned int DRSboard = (unsigned int)strtod(rest, &rest); | 
|---|
| 87 | unsigned int DRSchip = (unsigned int)strtod(rest, &rest); | 
|---|
| 88 | unsigned int DRSchannel = (unsigned int)strtod(rest, &rest); | 
|---|
| 89 | unsigned int HVboard = (unsigned int)strtod(rest, &rest); | 
|---|
| 90 | unsigned int HVchain = (unsigned int)strtod(rest, &rest); | 
|---|
| 91 | unsigned int HVchannel = (unsigned int)strtod(rest, &rest); | 
|---|
| 92 |  | 
|---|
| 93 | pixelmap[name2] = new Pixel(DRSboard, DRSchip, DRSchannel, | 
|---|
| 94 | HVboard, HVchain, HVchannel); | 
|---|
| 95 |  | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | else { | 
|---|
| 101 |  | 
|---|
| 102 | if (verbose) { | 
|---|
| 103 | std::cout << "ERROR in PixelMap::ReadPixelMap: Wrong pixel name: " << name2 << " -> pixel not initialized" << std::endl; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | else { | 
|---|
| 111 |  | 
|---|
| 112 | if(verbose){ | 
|---|
| 113 | std::cout << "Skipping empty line" << std::endl; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | if (verbose) { | 
|---|
| 123 | std::cout << std::endl << pixelmap.size() << " pixels found" << std::endl << std::endl; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | infile.close(); | 
|---|
| 127 |  | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | void PixelMap::Print() { | 
|---|
| 131 |  | 
|---|
| 132 | std::cout << "Printing entries of pixelmap:" << std::endl << std::endl; | 
|---|
| 133 |  | 
|---|
| 134 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 135 | std::cout << (*iter).first << std::endl << "------------" << std::endl << *((*iter).second) << std::endl; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | std::string PixelMap::DRS_to_Pixel(unsigned int DRSboard, unsigned int DRSchip, unsigned int DRSchannel, bool verbose) { | 
|---|
| 141 |  | 
|---|
| 142 | std::string pixelname = ""; | 
|---|
| 143 |  | 
|---|
| 144 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 145 |  | 
|---|
| 146 | if( ( ((*iter).second)->GetPixelDRSboard() == DRSboard ) && | 
|---|
| 147 | ( ((*iter).second)->GetPixelDRSchip() == DRSchip ) && | 
|---|
| 148 | ( ((*iter).second)->GetPixelDRSchannel() == DRSchannel ) ) { | 
|---|
| 149 |  | 
|---|
| 150 | pixelname = (*iter).first; | 
|---|
| 151 |  | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | if(pixelname == ""){ | 
|---|
| 157 |  | 
|---|
| 158 | if (verbose) { | 
|---|
| 159 | std::cout << "ERROR in PixelMap::DRS_to_Pixel: No pixel with DRS board, chip, channel = " | 
|---|
| 160 | << DRSboard << ", " | 
|---|
| 161 | << DRSchip << ", " | 
|---|
| 162 | << DRSchannel << " found in pixelmap" << std::endl; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | return pixelname; | 
|---|
| 168 |  | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | std::string PixelMap::HV_to_Pixel(unsigned int HVboard, unsigned int HVchain, unsigned int HVchannel, bool verbose) { | 
|---|
| 172 |  | 
|---|
| 173 | std::string pixelname = ""; | 
|---|
| 174 |  | 
|---|
| 175 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 176 |  | 
|---|
| 177 | if( ( ((*iter).second)->GetPixelHVboard() == HVboard ) && | 
|---|
| 178 | ( ((*iter).second)->GetPixelHVchain() == HVchain ) && | 
|---|
| 179 | ( ((*iter).second)->GetPixelHVchannel() == HVchannel ) ) { | 
|---|
| 180 |  | 
|---|
| 181 | pixelname = (*iter).first; | 
|---|
| 182 |  | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | if(pixelname == ""){ | 
|---|
| 188 |  | 
|---|
| 189 | if (verbose) { | 
|---|
| 190 | std::cout << "ERROR in PixelMap::HV_to_Pixel: No pixel with HV board, chain, channel = " | 
|---|
| 191 | << HVboard << ", " | 
|---|
| 192 | << HVchain << ", " | 
|---|
| 193 | << HVchannel << " found in pixelmap" << std::endl; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | return pixelname; | 
|---|
| 199 |  | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | unsigned int PixelMap::Pixel_to_DRSboard(std::string pixelname, bool verbose) { | 
|---|
| 203 |  | 
|---|
| 204 | unsigned int DRSboard = 999999999; | 
|---|
| 205 |  | 
|---|
| 206 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 207 |  | 
|---|
| 208 | if( ((*iter).first) == pixelname ){ | 
|---|
| 209 | DRSboard = ((*iter).second)->GetPixelDRSboard(); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | if (DRSboard == 999999999) { | 
|---|
| 215 |  | 
|---|
| 216 | if (verbose) { | 
|---|
| 217 | std::cout << "ERROR in PixelMap::Pixel_to_DRSboard: No pixel with name = " | 
|---|
| 218 | << pixelname << " found in pixelmap" << std::endl; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | return DRSboard; | 
|---|
| 224 |  | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | unsigned int PixelMap::Pixel_to_DRSchip(std::string pixelname, bool verbose) { | 
|---|
| 228 |  | 
|---|
| 229 | unsigned int DRSchip = 999999999; | 
|---|
| 230 |  | 
|---|
| 231 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 232 |  | 
|---|
| 233 | if( ((*iter).first) == pixelname ){ | 
|---|
| 234 | DRSchip = ((*iter).second)->GetPixelDRSchip(); | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | if (DRSchip == 999999999) { | 
|---|
| 240 |  | 
|---|
| 241 | if (verbose) { | 
|---|
| 242 | std::cout << "ERROR in PixelMap::Pixel_to_DRSchip: No pixel with name = " | 
|---|
| 243 | << pixelname << " found in pixelmap" << std::endl; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | return DRSchip; | 
|---|
| 249 |  | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | unsigned int PixelMap::Pixel_to_DRSchannel(std::string pixelname, bool verbose) { | 
|---|
| 253 |  | 
|---|
| 254 | unsigned int DRSchannel = 999999999; | 
|---|
| 255 |  | 
|---|
| 256 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 257 |  | 
|---|
| 258 | if( ((*iter).first) == pixelname ){ | 
|---|
| 259 | DRSchannel = ((*iter).second)->GetPixelDRSchannel(); | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | if (DRSchannel == 999999999) { | 
|---|
| 265 |  | 
|---|
| 266 | if (verbose) { | 
|---|
| 267 | std::cout << "ERROR in PixelMap::Pixel_to_DRSchannel: No pixel with name = " | 
|---|
| 268 | << pixelname << " found in pixelmap" << std::endl; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | return DRSchannel; | 
|---|
| 274 |  | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | unsigned int PixelMap::Pixel_to_HVboard(std::string pixelname, bool verbose) { | 
|---|
| 278 |  | 
|---|
| 279 | unsigned int HVboard = 999999999; | 
|---|
| 280 |  | 
|---|
| 281 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 282 |  | 
|---|
| 283 | if( ((*iter).first) == pixelname ){ | 
|---|
| 284 | HVboard = ((*iter).second)->GetPixelHVboard(); | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | if (HVboard == 999999999) { | 
|---|
| 290 |  | 
|---|
| 291 | if (verbose) { | 
|---|
| 292 | std::cout << "ERROR in PixelMap::Pixel_to_HVboard: No pixel with name = " | 
|---|
| 293 | << pixelname << " found in pixelmap" << std::endl; | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | return HVboard; | 
|---|
| 299 |  | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | unsigned int PixelMap::Pixel_to_HVchain(std::string pixelname, bool verbose) { | 
|---|
| 303 |  | 
|---|
| 304 | unsigned int HVchain = 999999999; | 
|---|
| 305 |  | 
|---|
| 306 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 307 |  | 
|---|
| 308 | if( ((*iter).first) == pixelname ){ | 
|---|
| 309 | HVchain = ((*iter).second)->GetPixelHVchain(); | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | if (HVchain == 999999999) { | 
|---|
| 315 |  | 
|---|
| 316 | if (verbose) { | 
|---|
| 317 | std::cout << "ERROR in PixelMap::Pixel_to_HVchain: No pixel with name = " | 
|---|
| 318 | << pixelname << " found in pixelmap" << std::endl; | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | return HVchain; | 
|---|
| 324 |  | 
|---|
| 325 | } | 
|---|
| 326 |  | 
|---|
| 327 | unsigned int PixelMap::Pixel_to_HVchannel(std::string pixelname, bool verbose) { | 
|---|
| 328 |  | 
|---|
| 329 | unsigned int HVchannel = 999999999; | 
|---|
| 330 |  | 
|---|
| 331 | for( std::map<const std::string, Pixel*>::iterator iter = pixelmap.begin(); iter != pixelmap.end(); ++iter ) { | 
|---|
| 332 |  | 
|---|
| 333 | if( ((*iter).first) == pixelname ){ | 
|---|
| 334 | HVchannel = ((*iter).second)->GetPixelHVchannel(); | 
|---|
| 335 | } | 
|---|
| 336 |  | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | if (HVchannel == 999999999) { | 
|---|
| 340 |  | 
|---|
| 341 | if (verbose) { | 
|---|
| 342 | std::cout << "ERROR in PixelMap::Pixel_to_HVchannel: No pixel with name = " | 
|---|
| 343 | << pixelname << " found in pixelmap" << std::endl; | 
|---|
| 344 | } | 
|---|
| 345 |  | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | return HVchannel; | 
|---|
| 349 |  | 
|---|
| 350 | } | 
|---|