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.c_str();
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  for (int rocDetId=rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
194  rocLnkId++;
195  DetectorRocId detectorRocId;
196  //detectorRocId.module = name0;
197  detectorRocId.module = name;
198  detectorRocId.rocDetId = rocDetId;
199 
200  CablingRocId cablingRocId;
201  cablingRocId.fedId = fedId;
202  cablingRocId.linkId = linkId;
203  cablingRocId.rocLinkId = rocLnkId;
204  // fix for type-B modules in barrel
205  edm::LogInfo(" roc ")<<rocDetId<<" "<<rocLnkId<<" "<<name->isHalfModule()<<endl;
206  if (name->isHalfModule() && (rocDetIds.min()>7)
207  && (part==PixelBarrelName::mO || part==PixelBarrelName::mI) ) {
208  //cablingRocId.rocLinkId = 9-rocLnkId;
209  // rocDetId=8,...,15
210  edm::LogInfo(" special for half modules ");
211  cablingRocId.rocLinkId = rocLnkId; // 1...8 19/11/08 d.k.
212  detectorRocId.rocDetId = rocDetId-8; // 0...7
213  }
214  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
215  }
216  }
217 
218  // check for endcap modules
219  pos = module.find("FPix");
220  if (pos != string::npos) {
221 
222  string strH = module.substr(pos+6,2);
224  if (strH=="mO") part = PixelEndcapName::mO;
225  else if(strH=="mI") part = PixelEndcapName::mI;
226  else if(strH=="pO") part = PixelEndcapName::pO;
227  else part = PixelEndcapName::pI;
228  module = module.substr(pos+9);
229 
230  // disk
231  pos = module.find("_");
232  if (pos == string::npos) throw cms::Exception("problem with disk formatting");
233  int disk = atoi( module.substr(1,pos-1).c_str());
234  module = module.substr(pos+1);
235 
236  // blade
237  pos = module.find("_");
238  if (pos == string::npos) 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) throw cms::Exception("problem with pannel formatting");
245  int pannel = atoi( module.substr(3,pos-3).c_str());
246  module = module.substr(pos+1);
247 
248  // plaquete
249 // pos = module.find("_");
250 // if (pos == string::npos) throw cms::Exception("problem with plaquette formatting");
251 // int plaq = atoi( module.substr(3,pos-3).c_str());
252 
253  int ring=1; // preset to 1 so it is ok for pilot blades
254  // pannel type
255  PixelPannelType::PannelType pannelType;
256  if(phase1_) {
257  pannelType=PixelPannelType::p2x8; // only 1 type for phase1
258 
259  // this is not really needed, just for testing
260  // ring
261  pos = module.find("RNG");
262  if (pos == string::npos) throw cms::Exception("problem with ring formatting");
263  ring = atoi( module.substr(pos+3,1).c_str()); //
264  //cout<<" ring "<<ring<<" "<<module<<endl;
265 
266  } else { // phase0
267  pos = module.find("TYP:");
268  if (pos == string::npos) throw cms::Exception("problem with pannel type formatting");
269  string strT = module.substr(pos+5,3);
270  string strT4 = module.substr(pos+5,4);
271  ring=1;
272  if (strT=="P3R") pannelType=PixelPannelType::p3R;
273  else if (strT=="P3L") pannelType=PixelPannelType::p3L;
274  else if (strT=="P4R") pannelType=PixelPannelType::p4R;
275  else if (strT=="P4L") pannelType=PixelPannelType::p4L;
276  else if (strT4=="P2X8") pannelType=PixelPannelType::p2x8; // for pilot blades
277  else throw cms::Exception("problem with pannel type formatting (unrecoginzed word)");
278  }
279 
280 
281  // Cabling accoring to the panle type
282  if ( pannelType==PixelPannelType::p4L) {
283 // cout <<"----------- p4L"<<endl;
284  int rocLnkId =0;
285  for (int plaq = 1; plaq <= 4; plaq++) {
286  Range rocs; int firstRoc=0; int step=0;
287  if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
288  if (plaq==2) { rocs = Range(0,5); firstRoc=0; step=+1; }
289  if (plaq==3) { rocs = Range(0,7); firstRoc=0; step=+1; }
290  if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
291  PixelEndcapName * name = new PixelEndcapName(part,disk,blade,pannel,plaq);
292  for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
293  rocLnkId++;
294  int rocDetId = firstRoc + step*iroc;
295 
296  DetectorRocId detectorRocId;
297  //detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
298  detectorRocId.module = name;
299  detectorRocId.rocDetId = rocDetId;
300 
301  CablingRocId cablingRocId;
302  cablingRocId.fedId = fedId;
303  cablingRocId.linkId = linkId;
304  cablingRocId.rocLinkId = rocLnkId;
305 
306  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
307 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
308  }
309  }
310  }
311  else if ( pannelType==PixelPannelType::p4R) {
312 // cout <<"----------- p4R"<<endl;
313  int rocLnkId =0;
314  for (int plaq = 4; plaq >= 1; plaq--) {
315  Range rocs; int firstRoc=0; int step=0;
316  if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
317  if (plaq==2) { rocs = Range(0,5); firstRoc=3; step=+1; }
318  if (plaq==3) { rocs = Range(0,7); firstRoc=4; step=+1; }
319  if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
320  PixelEndcapName * name = new PixelEndcapName(part,disk,blade,pannel,plaq);
321  for (int iroc =rocs.min(); iroc-rocs.max() <= 0; iroc++) {
322  rocLnkId++;
323  int rocDetId = firstRoc + step*iroc;
324  if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
325 
326  DetectorRocId detectorRocId;
327  //detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
328  detectorRocId.module = name;
329  detectorRocId.rocDetId = rocDetId;
330  CablingRocId cablingRocId;
331  cablingRocId.fedId = fedId;
332  cablingRocId.linkId = linkId;
333  cablingRocId.rocLinkId = rocLnkId;
334 
335  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
336 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
337  }
338  }
339  }
340  else if ( pannelType==PixelPannelType::p3L) {
341 // cout <<"----------- p3L"<<endl;
342  int rocLnkId =0;
343  for (int plaq = 1; plaq <= 3; plaq++) {
344  Range rocs; int firstRoc=0; int step=0;
345  if (plaq==1) { rocs = Range(0,5); firstRoc=0; step=1; }
346  if (plaq==2) { rocs = Range(0,7); firstRoc=0; step=1; }
347  if (plaq==3) { rocs = Range(0,9); firstRoc=0; step=1; }
348  PixelEndcapName * name = new PixelEndcapName(part,disk,blade,pannel,plaq);
349  for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
350  rocLnkId++;
351  int rocDetId = firstRoc + step*iroc;
352 
353  DetectorRocId detectorRocId;
354  detectorRocId.module = name;
355  detectorRocId.rocDetId = rocDetId;
356 
357  CablingRocId cablingRocId;
358  cablingRocId.fedId = fedId;
359  cablingRocId.linkId = linkId;
360  cablingRocId.rocLinkId = rocLnkId;
361 
362  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
363 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
364  }
365  }
366  }
367  else if ( pannelType==PixelPannelType::p3R) {
368 // cout <<"----------- p3R"<<endl;
369  int rocLnkId =0;
370  for (int plaq = 3; plaq >= 1; plaq--) {
371  Range rocs; int firstRoc=0; int step=0;
372  if (plaq==1) { rocs = Range(0,5); firstRoc=3; step=1; }
373  if (plaq==2) { rocs = Range(0,7); firstRoc=4; step=1; }
374  if (plaq==3) { rocs = Range(0,9); firstRoc=5; step=1; }
375  PixelEndcapName * name = new PixelEndcapName(part,disk,blade,pannel,plaq);
376  for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
377  rocLnkId++;
378  int rocDetId = firstRoc + step*iroc;
379  if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
380 
381  DetectorRocId detectorRocId;
382  detectorRocId.module = name;
383  detectorRocId.rocDetId = rocDetId;
384 
385  CablingRocId cablingRocId;
386  cablingRocId.fedId = fedId;
387  cablingRocId.linkId = linkId;
388  cablingRocId.rocLinkId = rocLnkId;
389 
390  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
391 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
392  } // for
393  } // for
394 
395  } else if ( pannelType==PixelPannelType::p2x8) { // phase-1 blades
396  // cout <<"----------- p2x8"<<endl;
397  int rocLnkId = 0;
398  // Range rocs = Range(0, 15);
399  // for (int rocDetId=rocs.min(); rocDetId <= rocs.max(); rocDetId++) {
400  PixelEndcapName * name = new PixelEndcapName(part, disk, blade, pannel, ring, phase1_);
401  for (int rocDetId=rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
402  rocLnkId++;
403  DetectorRocId detectorRocId;
404  detectorRocId.module = name;
405  detectorRocId.rocDetId = rocDetId;
406  CablingRocId cablingRocId;
407  cablingRocId.fedId = fedId;
408  cablingRocId.linkId = linkId;
409  cablingRocId.rocLinkId = rocLnkId;
410  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
411  edm::LogInfo("PixelToLNKAssociateFromAscii FPix ")
412  << " rocDetId: " << rocDetId
413  << " rocLnkId:" << rocLnkId
414  << " fedId = " << fedId
415  << " linkId = " << linkId
416  << " name = " << name->name();
417  // cout << " rocDetId: " << rocDetId
418  // << " rocLnkId:" << rocLnkId
419  // << " fedId = " << fedId
420  // << " linkId = " << linkId
421  // << " name = " << name0->name()
422  // << endl;
423  } // end for
424 
425  } // end of type
426 
427  }
428 }
429 
432  //cout<<l<<" in range "<<l.size()<<endl;
433  string l1,l2;
434  int i1=-1, i2=-1;
435  int len = l.size();
436  //for(int i=0; i<len;i++) {
437  // cout<<i<<" "<<l[i]<<endl;
438  //}
439  string::size_type p = l.find(",");
440  if(p != string::npos) {
441  //cout<<p<<" "<<len<<endl;
442  l1 = l.substr(0,p-1+1);
443  l2 = l.substr(p+1,len-1-p);
444  i1 = stoi(l1);
445  i2 = stoi(l2);
446  //cout<<l1<<" "<<l2<<" "<<i1<<" "<<i2<<endl;
447  }
448 
449  return Range(i1,i2);
450 
451  // this method is very stupid it relies on a space being present after the last number!
452  // exchange with string opertaions (above)
453  //bool first = true;
454  //int num1 = -1;
455  //int num2 = -1;
456  // const char * line = l.c_str();
457  // int i=0;
458  // while (line) {
459  // i++;
460  // char * evp = 0;
461  // int num = strtol(line, &evp, 10);
462  // //cout<<i<<" "<<num<<" "<<evp<<" "<<line<<endl;
463  // //{ stringstream s; s<<"read from line: "; s<<num; LogTrace("") << s.str(); }
464  // if (evp != line) {
465  // line = evp +1;
466  // //cout<<i<<" "<<num<<" "<<evp<<" "<<line<<endl;
467  // if (first) { num1 = num; first = false;}
468  // num2 = num;
469  // //cout<<" not first "<<num2<<endl;
470  // } else line = 0;
471  // }
472  // if (first) {
473  // string s = "** PixelToLNKAssociateFromAscii, read data, cant intrpret: " ;
474  // edm::LogInfo(s) << endl
475  // << l << endl
476  // <<"=====> send exception " << endl;
477  // s += l;
478  // throw cms::Exception(s);
479  // }
480  //if(i1!=num1) cout<<" something wrong with min range "<<i1<<" "<<num1<<endl;
481  //if(!phase1_ && (i2!=num2)) cout<<" something wrong with max range "<<i2<<" "<<num2<<endl;
482  //cout<<" min max "<<num1<<" "<<num2<<endl;
483  //return Range(num1,num2);
484 
485 }
486 
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