CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PixelToLNKAssociateFromAscii.cc
Go to the documentation of this file.
2 
7 
8 #include <ostream>
9 #include <fstream>
11 
12 using namespace std;
13 
15  init(fn);
16 }
18 {
19  return theVersion;
20 }
21 
24 {
25 // bool deb = (roc.module->name()=="BPix_BpI_SEC1_LYR1_LDR1H_MOD1");
26 // if (deb) cout <<"KUKU"<<endl;
27 
28  typedef std::vector< std::pair<DetectorRocId,CablingRocId> >::const_iterator IM;
29  for (IM im = theConnection.begin(); im != theConnection.end(); im++) {
30  if( ( *(im->first.module) == *roc.module ) && (im->first.rocDetId == roc.rocDetId)) {
31  return &(im->second);
32  }
33  }
34  return 0;
35 }
36 
37 void PixelToLNKAssociateFromAscii::init(const string & cfg_name)
38 {
39  LogDebug("init, input file:") << cfg_name.c_str();
40 
41  std::ifstream file( cfg_name.c_str() );
42  if ( !file ) {
43  edm::LogError(" ** PixelToLNKAssociateFromAscii,init ** ")
44  << " cant open data file: " << cfg_name;
45  return;
46  } else {
47  edm::LogInfo("PixelToLNKAssociateFromAscii, read data from: ") <<cfg_name ;
48  }
49 
50  string line;
51  int fedId=-1;
52  int linkId=-1;
53 
54  try {
55  while (getline(file,line)) {
56  //
57  // treat # lines
58  //
59  string::size_type pos = line.find("#");
60  if (pos != string::npos) line = line.erase(pos);
61 
62  string::size_type posF = line.find("FED:");
63  string::size_type posL = line.find("LNK:");
64  string::size_type posM = line.find("MOD:");
65  string::size_type posR = line.find("ROC:");
66 
67  LogTrace("") <<" read line: "<< line;
68 
69 
70  //
71  // treat version lines, reset date
72  //
73  if ( line.compare(0,3,"VER") == 0 ) {
74  edm::LogInfo("version: ")<<line;
75  theVersion = line;
76  }
77 
78  //
79  // fed id line
80  //
81  else if ( posF != string::npos) {
82  line = line.substr(posF+4);
83  fedId = atoi(line.c_str());
84  }
85 
86  //
87  // link id linke
88  //
89  else if ( posL != string::npos) {
90  string srtL = line.substr(posL+4);
91  linkId = atoi(srtL.c_str());
92  }
93 
94  //
95  // module description
96  //
97  if ( posM != string::npos) {
98  if (posR != string::npos) {
99  string strM = line.substr(posM+4, posR-posM-5);
100  string::size_type pos = strM.find(" ");
101  if(pos != string::npos) strM = strM.substr(pos+1);
102  string strR = line.substr(posR+4);
103  Range range = readRange(strR);
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 {
138  string::size_type pos;
139 
140  // check for Barrel modules
141  pos = module.find("BPix");
142  if (pos != string::npos) {
143 
144  // shell
145  string strP = module.substr(pos+6,2);
147  if (strP=="mO") part = PixelBarrelName::mO;
148  else if(strP=="mI") part = PixelBarrelName::mI;
149  else if(strP=="pO") part = PixelBarrelName::pO;
150  else part = PixelBarrelName::pI;
151  module = module.substr(pos+9);
152 
153  // sector
154  pos = module.find("_");
155  if (pos == string::npos) throw cms::Exception("problem with sector formatting");
156  // int sector = atoi( module.substr(3,pos-3).c_str());
157  module = module.substr(pos+1);
158 
159  // layer
160  pos = module.find("_");
161  if (pos == string::npos) throw cms::Exception("problem with layer formatting");
162  int layer = atoi( module.substr(3,pos-3).c_str());
163  module = module.substr(pos+1);
164 
165  // ladder
166  pos = module.find("_");
167  if (pos == string::npos) throw cms::Exception("problem with ladder formatting");
168  int ladder = atoi( module.substr(3,pos-3).c_str());
169  module = module.substr(pos+1);
170 
171  // z-module
172  int zmodule = atoi( module.substr(3,pos-3).c_str());
173 
174  // place modules in connections
175  int rocLnkId = 0;
176  for (int rocDetId=rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
177  rocLnkId++;
178  DetectorRocId detectorRocId;
179  PixelBarrelName * name = new PixelBarrelName(part, layer, zmodule, ladder);
180  detectorRocId.module = name;
181  detectorRocId.rocDetId = rocDetId;
182  CablingRocId cablingRocId;
183  cablingRocId.fedId = fedId;
184  cablingRocId.linkId = linkId;
185  cablingRocId.rocLinkId = rocLnkId;
186  // fix for type-B modules in barrel
187  if (name->isHalfModule() && (rocDetIds.min()>7)
188  && (part==PixelBarrelName::mO || PixelBarrelName::mI) ) {
189  //cablingRocId.rocLinkId = 9-rocLnkId;
190  // rocDetId=8,...,15
191  cablingRocId.rocLinkId = rocLnkId; // 1...8 19/11/08 d.k.
192  detectorRocId.rocDetId = rocDetId-8; // 0...7
193  }
194  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
195  }
196  }
197 
198  // check for endcap modules
199  // check for Barrel modules
200  pos = module.find("FPix");
201  if (pos != string::npos) {
202  string strH = module.substr(pos+6,2);
204  if (strH=="mO") part = PixelEndcapName::mO;
205  else if(strH=="mI") part = PixelEndcapName::mI;
206  else if(strH=="pO") part = PixelEndcapName::pO;
207  else part = PixelEndcapName::pI;
208  module = module.substr(pos+9);
209 
210  // disk
211  pos = module.find("_");
212  if (pos == string::npos) throw cms::Exception("problem with disk formatting");
213  int disk = atoi( module.substr(1,pos-1).c_str());
214  module = module.substr(pos+1);
215 
216  // blade
217  pos = module.find("_");
218  if (pos == string::npos) throw cms::Exception("problem with blade formatting");
219  int blade = atoi( module.substr(3,pos-3).c_str());
220  module = module.substr(pos+1);
221 
222  //pannel
223  pos = module.find("_");
224  if (pos == string::npos) throw cms::Exception("problem with pannel formatting");
225  int pannel = atoi( module.substr(3,pos-3).c_str());
226  module = module.substr(pos+1);
227 
228  // plaquete
229 // pos = module.find("_");
230 // if (pos == string::npos) throw cms::Exception("problem with plaquette formatting");
231 // int plaq = atoi( module.substr(3,pos-3).c_str());
232 
233  // pannel type
234  pos = module.find("TYP:");
235  if (pos == string::npos) throw cms::Exception("problem with pannel type formatting");
236  string strT = module.substr(pos+5,3);
237 
238  PixelPannelType::PannelType pannelType;
239  if (strT=="P3R") pannelType=PixelPannelType::p3R;
240  else if (strT=="P3L") pannelType=PixelPannelType::p3L;
241  else if (strT=="P4R") pannelType=PixelPannelType::p4R;
242  else if (strT=="P4L") pannelType=PixelPannelType::p4L;
243  else throw cms::Exception("problem with pannel type formatting (unrecoginzed word)");
244 
245  if ( pannelType==PixelPannelType::p4L) {
246 // cout <<"----------- p4L"<<endl;
247  int rocLnkId =0;
248  for (int plaq = 1; plaq <= 4; plaq++) {
249  Range rocs; int firstRoc=0; int step=0;
250  if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
251  if (plaq==2) { rocs = Range(0,5); firstRoc=0; step=+1; }
252  if (plaq==3) { rocs = Range(0,7); firstRoc=0; step=+1; }
253  if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
254  for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
255  rocLnkId++;
256  int rocDetId = firstRoc + step*iroc;
257 
258  DetectorRocId detectorRocId;
259  detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
260  detectorRocId.rocDetId = rocDetId;
261 
262  CablingRocId cablingRocId;
263  cablingRocId.fedId = fedId;
264  cablingRocId.linkId = linkId;
265  cablingRocId.rocLinkId = rocLnkId;
266 
267  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
268 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
269  }
270  }
271  }
272  else if ( pannelType==PixelPannelType::p4R) {
273 // cout <<"----------- p4R"<<endl;
274  int rocLnkId =0;
275  for (int plaq = 4; plaq >= 1; plaq--) {
276  Range rocs; int firstRoc=0; int step=0;
277  if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
278  if (plaq==2) { rocs = Range(0,5); firstRoc=3; step=+1; }
279  if (plaq==3) { rocs = Range(0,7); firstRoc=4; step=+1; }
280  if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
281  for (int iroc =rocs.min(); iroc-rocs.max() <= 0; iroc++) {
282  rocLnkId++;
283  int rocDetId = firstRoc + step*iroc;
284  if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
285 
286  DetectorRocId detectorRocId;
287  detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
288  detectorRocId.rocDetId = rocDetId;
289 
290  CablingRocId cablingRocId;
291  cablingRocId.fedId = fedId;
292  cablingRocId.linkId = linkId;
293  cablingRocId.rocLinkId = rocLnkId;
294 
295  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
296 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
297  }
298  }
299  }
300  else if ( pannelType==PixelPannelType::p3L) {
301 // cout <<"----------- p3L"<<endl;
302  int rocLnkId =0;
303  for (int plaq = 1; plaq <= 3; plaq++) {
304  Range rocs; int firstRoc=0; int step=0;
305  if (plaq==1) { rocs = Range(0,5); firstRoc=0; step=1; }
306  if (plaq==2) { rocs = Range(0,7); firstRoc=0; step=1; }
307  if (plaq==3) { rocs = Range(0,9); firstRoc=0; step=1; }
308  for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
309  rocLnkId++;
310  int rocDetId = firstRoc + step*iroc;
311 
312  DetectorRocId detectorRocId;
313  detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
314  detectorRocId.rocDetId = rocDetId;
315 
316  CablingRocId cablingRocId;
317  cablingRocId.fedId = fedId;
318  cablingRocId.linkId = linkId;
319  cablingRocId.rocLinkId = rocLnkId;
320 
321  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
322 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
323  }
324  }
325  }
326  else if ( pannelType==PixelPannelType::p3R) {
327 // cout <<"----------- p3R"<<endl;
328  int rocLnkId =0;
329  for (int plaq = 3; plaq >= 1; plaq--) {
330  Range rocs; int firstRoc=0; int step=0;
331  if (plaq==1) { rocs = Range(0,5); firstRoc=3; step=1; }
332  if (plaq==2) { rocs = Range(0,7); firstRoc=4; step=1; }
333  if (plaq==3) { rocs = Range(0,9); firstRoc=5; step=1; }
334  for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
335  rocLnkId++;
336  int rocDetId = firstRoc + step*iroc;
337  if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
338 
339  DetectorRocId detectorRocId;
340  detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
341  detectorRocId.rocDetId = rocDetId;
342 
343  CablingRocId cablingRocId;
344  cablingRocId.fedId = fedId;
345  cablingRocId.linkId = linkId;
346  cablingRocId.rocLinkId = rocLnkId;
347 
348  theConnection.push_back( make_pair(detectorRocId,cablingRocId));
349 // cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
350  }
351  }
352  }
353 
354  }
355 }
356 
359 {
360  bool first = true;
361  int num1 = -1;
362  int num2 = -1;
363  const char * line = l.c_str();
364  while (line) {
365  char * evp = 0;
366  int num = strtol(line, &evp, 10);
367  { stringstream s; s<<"raad from line: "; s<<num; LogTrace("") << s.str(); }
368  if (evp != line) {
369  line = evp +1;
370  if (first) { num1 = num; first = false; }
371  num2 = num;
372  } else line = 0;
373  }
374  if (first) {
375  string s = "** PixelToLNKAssociateFromAscii, read data, cant intrpret: " ;
376  edm::LogInfo(s) << endl
377  << l << endl
378  <<"=====> send exception " << endl;
379  s += l;
380  throw cms::Exception(s);
381  }
382  return Range(num1,num2);
383 }
384 
#define LogDebug(id)
void init(const std::string &fileName)
initialisatin (read file)
int init
Definition: HydjetWrapper.h:67
PixelToLNKAssociateFromAscii(const std::string &fileName)
uint16_t size_type
const T & min() const
lower edge of range
Definition: TRange.h:23
void addConnections(int fedId, int linkId, std::string module, Range rocDetIds)
virtual const CablingRocId * operator()(const DetectorRocId &roc) const
LNK id for module.
bool isHalfModule() const
full or half module
virtual std::string version() const
version
#define LogTrace(id)
PixelRecoRange< float > Range
part
Definition: HCALResponse.h:20
const T & max() const
upper edge of range
Definition: TRange.h:26
Range readRange(const std::string &) const
Definition: vlib.h:208