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