CMS 3D CMS Logo

PixelToLNKAssociateFromAscii.cc
Go to the documentation of this file.
2 
7 
8 #include <ostream>
9 #include <fstream>
11 
12 using namespace std;
13 
15  phase1_ = phase;
16  init(fn);
17 }
18 std::string PixelToLNKAssociateFromAscii::version() const { return theVersion; }
19 
22  typedef std::vector<std::pair<DetectorRocId, CablingRocId> >::const_iterator IM;
23  for (IM im = theConnection.begin(); im != theConnection.end(); im++) {
24  if ((*(im->first.module) == *roc.module) && (im->first.rocDetId == roc.rocDetId)) {
25  return &(im->second);
26  }
27  }
28  return nullptr;
29 }
30 
31 // This is where the reading and interpretation of the ascci cabling input file is
32 void PixelToLNKAssociateFromAscii::init(const string& cfg_name) {
33  edm::LogInfo(" init, input file: ") << cfg_name;
34 
35  std::ifstream file(cfg_name.c_str());
36  if (!file) {
37  edm::LogError(" ** PixelToLNKAssociateFromAscii,init ** ") << " cant open data file: " << cfg_name;
38  return;
39  } else {
40  edm::LogInfo("PixelToLNKAssociateFromAscii, read data from: ") << cfg_name;
41  }
42 
43  string line;
44  int fedId = -1;
45  int linkId = -1;
46 
47  try {
48  while (getline(file, line)) {
49  //
50  // treat # lines
51  //
52  string::size_type pos = line.find('#');
53  if (pos != string::npos)
54  line = line.erase(pos);
55 
56  string::size_type posF = line.find("FED:");
57  string::size_type posL = line.find("LNK:");
58  string::size_type posM = line.find("MOD:");
59  string::size_type posR = line.find("ROC:");
60 
61  edm::LogInfo("") << " read line: " << line;
62 
63  //
64  // treat version lines, reset date
65  //
66  if (line.compare(0, 3, "VER") == 0) {
67  edm::LogInfo("version: ") << line;
68  theVersion = line;
69  }
70 
71  //
72  // fed id line
73  //
74  else if (posF != string::npos) {
75  line = line.substr(posF + 4);
76  fedId = atoi(line.c_str());
77  }
78 
79  //
80  // link id linke
81  //
82  else if (posL != string::npos) {
83  string srtL = line.substr(posL + 4);
84  linkId = atoi(srtL.c_str());
85  }
86 
87  //
88  // module description
89  //
90  if (posM != string::npos) {
91  if (posR != string::npos) {
92  string strM = line.substr(posM + 4, posR - posM - 5);
93  string::size_type pos = strM.find(' ');
94  if (pos != string::npos)
95  strM = strM.substr(pos + 1);
96  string strR = line.substr(posR + 4);
97  Range range = readRange(strR);
98  //cout<<" range find "<<strR<<" "<<strR.size()<<" "<<range.min()<<" "<<range.max()<<endl;
99  addConnections(fedId, linkId, strM, range);
100  } else {
101  string strM = line.substr(posM + 4);
102  string::size_type pos = strM.find(' ');
103  if (pos != string::npos)
104  strM = strM.substr(pos + 1);
105  addConnections(fedId, linkId, strM, Range(0, 0));
106  }
107  }
108  }
109  } catch (exception& err) {
110  edm::LogError("**PixelToLNKAssociateFromAscii** exception") << err.what();
111  }
112 
113  //
114  // for debug
115  //
116  std::ostringstream str;
117  str << " **PixelToLNKAssociateFromAscii ** CONNECTIONS: " << endl;
118  typedef vector<pair<DetectorRocId, CablingRocId> >::const_iterator ICON;
119  for (ICON ic = theConnection.begin(); ic != theConnection.end(); ic++) {
120  str << (*ic).first.module->name() << ", rocDetId=" << (*ic).first.rocDetId << ", fedId=" << ic->second.fedId
121  << ", linkId=" << ic->second.linkId << ", rocLinkId=" << ic->second.rocLinkId << endl;
122  }
123  edm::LogInfo("PixelToLNKAssociateFromAscii") << str.str();
124 }
125 
128 
129  // check for Barrel modules
130  pos = module.find("BPix");
131  if (pos != string::npos) {
132  string module0 = module;
133  // strip the trailing spaces
134  string::size_type p = module0.find(' ');
135  //string::size_type p1 = module0.find_first_of(" ");
136  //string::size_type p2 = module0.find_last_not_of(" ");
137  //cout<<p<<" "<<p1<<" "<<p2<<endl;
138  if (p != string::npos)
139  module0 = module0.substr(0, p);
140  PixelBarrelName* name = new PixelBarrelName(module0, phase1_);
141 
142  // shell
143  string strP = module.substr(pos + 6, 2);
145  if (strP == "mO")
147  else if (strP == "mI")
149  else if (strP == "pO")
151  else
153 
154  // // all this can be skipped -----------------------------------
155  // module = module.substr(pos+9);
156  // // sector
157  // pos = module.find("_");
158  // if (pos == string::npos) throw cms::Exception("problem with sector formatting");
159  // // int sector = atoi( module.substr(3,pos-3).c_str());
160  // module = module.substr(pos+1);
161  // // layer
162  // pos = module.find("_");
163  // if (pos == string::npos) throw cms::Exception("problem with layer formatting");
164  // int layer = atoi( module.substr(3,pos-3).c_str());
165  // module = module.substr(pos+1);
166  // // ladder
167  // pos = module.find("_");
168  // if (pos == string::npos) throw cms::Exception("problem with ladder formatting");
169  // int ladder = atoi( module.substr(3,pos-3).c_str());
170  // module = module.substr(pos+1);
171  // // z-module
172  // int zmodule = atoi( module.substr(3,pos-3).c_str());
173  // // place modules in connections
174  // PixelBarrelName * name0 = new PixelBarrelName(part, layer, zmodule, ladder, phase1_);
175  // if(name->name() != module0) cout<<" wrong name "<<fedId<<" "<<linkId<<" "
176  // <<module0<<" "<<name->name()<<" "<<name0->name()<<endl;
177  // if(name->name() != name0->name()) cout<<" wrong name "<<fedId<<" "<<linkId<<" "
178  // <<module0<<" "<<name->name()<<" "<<name0->name()<<endl;
179  // //edm::LogInfo(" module ")<<fedId<<" "<<linkId<<" "<<module0<<" "
180  // // <<name0->name()<<" "<<rocDetIds.max()<<endl;
181  // // until here
182 
183  int rocLnkId = 0;
184  bool loopExecuted = false;
185  for (int rocDetId = rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
186  loopExecuted = true;
187  rocLnkId++;
188  DetectorRocId detectorRocId;
189  //detectorRocId.module = name0;
190  detectorRocId.module = name;
191  detectorRocId.rocDetId = rocDetId;
192 
193  CablingRocId cablingRocId;
194  cablingRocId.fedId = fedId;
195  cablingRocId.linkId = linkId;
196  cablingRocId.rocLinkId = rocLnkId;
197  // fix for type-B modules in barrel
198  edm::LogInfo(" roc ") << rocDetId << " " << rocLnkId << " " << name->isHalfModule() << endl;
199  if (name->isHalfModule() && (rocDetIds.min() > 7) &&
201  //cablingRocId.rocLinkId = 9-rocLnkId;
202  // rocDetId=8,...,15
203  edm::LogInfo(" special for half modules ");
204  cablingRocId.rocLinkId = rocLnkId; // 1...8 19/11/08 d.k.
205  detectorRocId.rocDetId = rocDetId - 8; // 0...7
206  }
207  theConnection.push_back(make_pair(detectorRocId, cablingRocId));
208  }
209  if (!loopExecuted)
210  delete name;
211  }
212 
213  // check for endcap modules
214  pos = module.find("FPix");
215  if (pos != string::npos) {
216  string strH = module.substr(pos + 6, 2);
218  if (strH == "mO")
220  else if (strH == "mI")
222  else if (strH == "pO")
224  else
226  module = module.substr(pos + 9);
227 
228  // disk
229  pos = module.find('_');
230  if (pos == string::npos)
231  throw cms::Exception("problem with disk formatting");
232  int disk = atoi(module.substr(1, pos - 1).c_str());
233  module = module.substr(pos + 1);
234 
235  // blade
236  pos = module.find('_');
237  if (pos == string::npos)
238  throw cms::Exception("problem with blade formatting");
239  int blade = atoi(module.substr(3, pos - 3).c_str());
240  module = module.substr(pos + 1);
241 
242  //pannel
243  pos = module.find('_');
244  if (pos == string::npos)
245  throw cms::Exception("problem with pannel formatting");
246  int pannel = atoi(module.substr(3, pos - 3).c_str());
247  module = module.substr(pos + 1);
248 
249  // plaquete
250  // pos = module.find("_");
251  // if (pos == string::npos) throw cms::Exception("problem with plaquette formatting");
252  // int plaq = atoi( module.substr(3,pos-3).c_str());
253 
254  int ring = 1; // preset to 1 so it is ok for pilot blades
255  // pannel type
256  PixelPannelType::PannelType pannelType;
257  if (phase1_) {
258  pannelType = PixelPannelType::p2x8; // only 1 type for phase1
259 
260  // this is not really needed, just for testing
261  // ring
262  pos = module.find("RNG");
263  if (pos == string::npos)
264  throw cms::Exception("problem with ring formatting");
265  ring = atoi(module.substr(pos + 3, 1).c_str()); //
266  //cout<<" ring "<<ring<<" "<<module<<endl;
267 
268  } else { // phase0
269  pos = module.find("TYP:");
270  if (pos == string::npos)
271  throw cms::Exception("problem with pannel type formatting");
272  string strT = module.substr(pos + 5, 3);
273  string strT4 = module.substr(pos + 5, 4);
274  ring = 1;
275  if (strT == "P3R")
276  pannelType = PixelPannelType::p3R;
277  else if (strT == "P3L")
278  pannelType = PixelPannelType::p3L;
279  else if (strT == "P4R")
280  pannelType = PixelPannelType::p4R;
281  else if (strT == "P4L")
282  pannelType = PixelPannelType::p4L;
283  else if (strT4 == "P2X8")
284  pannelType = PixelPannelType::p2x8; // for pilot blades
285  else
286  throw cms::Exception("problem with pannel type formatting (unrecoginzed word)");
287  }
288 
289  // Cabling accoring to the panle type
290  if (pannelType == PixelPannelType::p4L) {
291  // cout <<"----------- p4L"<<endl;
292  int rocLnkId = 0;
293  for (int plaq = 1; plaq <= 4; plaq++) {
294  Range rocs;
295  int firstRoc = 0;
296  int step = 0;
297  if (plaq == 1) {
298  rocs = Range(0, 1);
299  firstRoc = 1;
300  step = -1;
301  }
302  if (plaq == 2) {
303  rocs = Range(0, 5);
304  firstRoc = 0;
305  step = +1;
306  }
307  if (plaq == 3) {
308  rocs = Range(0, 7);
309  firstRoc = 0;
310  step = +1;
311  }
312  if (plaq == 4) {
313  rocs = Range(0, 4);
314  firstRoc = 0;
315  step = +1;
316  }
317  PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
318  for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
319  rocLnkId++;
320  int rocDetId = firstRoc + step * iroc;
321 
322  DetectorRocId detectorRocId;
323  //detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
324  detectorRocId.module = name;
325  detectorRocId.rocDetId = rocDetId;
326 
327  CablingRocId cablingRocId;
328  cablingRocId.fedId = fedId;
329  cablingRocId.linkId = linkId;
330  cablingRocId.rocLinkId = rocLnkId;
331 
332  theConnection.push_back(make_pair(detectorRocId, cablingRocId));
333  // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
334  }
335  }
336  } else if (pannelType == PixelPannelType::p4R) {
337  // cout <<"----------- p4R"<<endl;
338  int rocLnkId = 0;
339  for (int plaq = 4; plaq >= 1; plaq--) {
340  Range rocs;
341  int firstRoc = 0;
342  int step = 0;
343  if (plaq == 1) {
344  rocs = Range(0, 1);
345  firstRoc = 1;
346  step = -1;
347  }
348  if (plaq == 2) {
349  rocs = Range(0, 5);
350  firstRoc = 3;
351  step = +1;
352  }
353  if (plaq == 3) {
354  rocs = Range(0, 7);
355  firstRoc = 4;
356  step = +1;
357  }
358  if (plaq == 4) {
359  rocs = Range(0, 4);
360  firstRoc = 0;
361  step = +1;
362  }
363  PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
364  for (int iroc = rocs.min(); iroc - rocs.max() <= 0; iroc++) {
365  rocLnkId++;
366  int rocDetId = firstRoc + step * iroc;
367  if (rocDetId > rocs.max())
368  rocDetId = (rocDetId - 1) % rocs.max();
369 
370  DetectorRocId detectorRocId;
371  //detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
372  detectorRocId.module = name;
373  detectorRocId.rocDetId = rocDetId;
374  CablingRocId cablingRocId;
375  cablingRocId.fedId = fedId;
376  cablingRocId.linkId = linkId;
377  cablingRocId.rocLinkId = rocLnkId;
378 
379  theConnection.push_back(make_pair(detectorRocId, cablingRocId));
380  // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
381  }
382  }
383  } else if (pannelType == PixelPannelType::p3L) {
384  // cout <<"----------- p3L"<<endl;
385  int rocLnkId = 0;
386  for (int plaq = 1; plaq <= 3; plaq++) {
387  Range rocs;
388  int firstRoc = 0;
389  int step = 0;
390  if (plaq == 1) {
391  rocs = Range(0, 5);
392  firstRoc = 0;
393  step = 1;
394  }
395  if (plaq == 2) {
396  rocs = Range(0, 7);
397  firstRoc = 0;
398  step = 1;
399  }
400  if (plaq == 3) {
401  rocs = Range(0, 9);
402  firstRoc = 0;
403  step = 1;
404  }
405  PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
406  for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
407  rocLnkId++;
408  int rocDetId = firstRoc + step * iroc;
409 
410  DetectorRocId detectorRocId;
411  detectorRocId.module = name;
412  detectorRocId.rocDetId = rocDetId;
413 
414  CablingRocId cablingRocId;
415  cablingRocId.fedId = fedId;
416  cablingRocId.linkId = linkId;
417  cablingRocId.rocLinkId = rocLnkId;
418 
419  theConnection.push_back(make_pair(detectorRocId, cablingRocId));
420  // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
421  }
422  }
423  } else if (pannelType == PixelPannelType::p3R) {
424  // cout <<"----------- p3R"<<endl;
425  int rocLnkId = 0;
426  for (int plaq = 3; plaq >= 1; plaq--) {
427  Range rocs;
428  int firstRoc = 0;
429  int step = 0;
430  if (plaq == 1) {
431  rocs = Range(0, 5);
432  firstRoc = 3;
433  step = 1;
434  }
435  if (plaq == 2) {
436  rocs = Range(0, 7);
437  firstRoc = 4;
438  step = 1;
439  }
440  if (plaq == 3) {
441  rocs = Range(0, 9);
442  firstRoc = 5;
443  step = 1;
444  }
445  PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
446  for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
447  rocLnkId++;
448  int rocDetId = firstRoc + step * iroc;
449  if (rocDetId > rocs.max())
450  rocDetId = (rocDetId - 1) % rocs.max();
451 
452  DetectorRocId detectorRocId;
453  detectorRocId.module = name;
454  detectorRocId.rocDetId = rocDetId;
455 
456  CablingRocId cablingRocId;
457  cablingRocId.fedId = fedId;
458  cablingRocId.linkId = linkId;
459  cablingRocId.rocLinkId = rocLnkId;
460 
461  theConnection.push_back(make_pair(detectorRocId, cablingRocId));
462  // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
463  } // for
464  } // for
465 
466  } else if (pannelType == PixelPannelType::p2x8) { // phase-1 blades
467  // cout <<"----------- p2x8"<<endl;
468  int rocLnkId = 0;
469  // Range rocs = Range(0, 15);
470  // for (int rocDetId=rocs.min(); rocDetId <= rocs.max(); rocDetId++) {
471  PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, ring, phase1_);
472  bool loopExecuted = false;
473  for (int rocDetId = rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
474  loopExecuted = true;
475  rocLnkId++;
476  DetectorRocId detectorRocId;
477  detectorRocId.module = name;
478  detectorRocId.rocDetId = rocDetId;
479  CablingRocId cablingRocId;
480  cablingRocId.fedId = fedId;
481  cablingRocId.linkId = linkId;
482  cablingRocId.rocLinkId = rocLnkId;
483  theConnection.push_back(make_pair(detectorRocId, cablingRocId));
484  edm::LogInfo("PixelToLNKAssociateFromAscii FPix ")
485  << " rocDetId: " << rocDetId << " rocLnkId:" << rocLnkId << " fedId = " << fedId << " linkId = " << linkId
486  << " name = " << name->name();
487  // cout << " rocDetId: " << rocDetId
488  // << " rocLnkId:" << rocLnkId
489  // << " fedId = " << fedId
490  // << " linkId = " << linkId
491  // << " name = " << name0->name()
492  // << endl;
493  } // end for
494  if (!loopExecuted) {
495  delete name;
496  }
497 
498  } // end of type
499  }
500 }
501 
503  //cout<<l<<" in range "<<l.size()<<endl;
504  string l1, l2;
505  int i1 = -1, i2 = -1;
506  int len = l.size();
507  //for(int i=0; i<len;i++) {
508  // cout<<i<<" "<<l[i]<<endl;
509  //}
510  string::size_type p = l.find(',');
511  if (p != string::npos) {
512  //cout<<p<<" "<<len<<endl;
513  l1 = l.substr(0, p - 1 + 1);
514  l2 = l.substr(p + 1, len - 1 - p);
515  i1 = stoi(l1);
516  i2 = stoi(l2);
517  //cout<<l1<<" "<<l2<<" "<<i1<<" "<<i2<<endl;
518  }
519 
520  return Range(i1, i2);
521 
522  // this method is very stupid it relies on a space being present after the last number!
523  // exchange with string opertaions (above)
524  //bool first = true;
525  //int num1 = -1;
526  //int num2 = -1;
527  // const char * line = l.c_str();
528  // int i=0;
529  // while (line) {
530  // i++;
531  // char * evp = 0;
532  // int num = strtol(line, &evp, 10);
533  // //cout<<i<<" "<<num<<" "<<evp<<" "<<line<<endl;
534  // //{ stringstream s; s<<"read from line: "; s<<num; LogTrace("") << s.str(); }
535  // if (evp != line) {
536  // line = evp +1;
537  // //cout<<i<<" "<<num<<" "<<evp<<" "<<line<<endl;
538  // if (first) { num1 = num; first = false;}
539  // num2 = num;
540  // //cout<<" not first "<<num2<<endl;
541  // } else line = 0;
542  // }
543  // if (first) {
544  // string s = "** PixelToLNKAssociateFromAscii, read data, cant intrpret: " ;
545  // edm::LogInfo(s) << endl
546  // << l << endl
547  // <<"=====> send exception " << endl;
548  // s += l;
549  // throw cms::Exception(s);
550  // }
551  //if(i1!=num1) cout<<" something wrong with min range "<<i1<<" "<<num1<<endl;
552  //if(!phase1_ && (i2!=num2)) cout<<" something wrong with max range "<<i2<<" "<<num2<<endl;
553  //cout<<" min max "<<num1<<" "<<num2<<endl;
554  //return Range(num1,num2);
555 }
void init(const std::string &fileName)
initialisatin (read file)
const T & max() const
upper edge of range
Definition: TRange.h:23
PixelRecoRange< float > Range
std::string version() const override
version
int init
Definition: HydjetWrapper.h:64
Log< level::Error, false > LogError
uint16_t size_type
void addConnections(int fedId, int linkId, std::string module, Range rocDetIds)
const T & min() const
lower edge of range
Definition: TRange.h:20
Range readRange(const std::string &) const
Log< level::Info, false > LogInfo
part
Definition: HCALResponse.h:20
step
Definition: StallMonitor.cc:98
#define str(s)
PixelToLNKAssociateFromAscii(const std::string &fileName, const bool phase1=false)
const CablingRocId * operator()(const DetectorRocId &roc) const override
LNK id for module.