CMS 3D CMS Logo

PixelToLNKAssociateFromAscii.cc

Go to the documentation of this file.
00001 #include "CalibTracker/SiPixelConnectivity/interface/PixelToLNKAssociateFromAscii.h"
00002 
00003 #include "DataFormats/SiPixelDetId/interface/PixelBarrelName.h"
00004 #include "DataFormats/SiPixelDetId/interface/PixelEndcapName.h"
00005 #include "DataFormats/SiPixelDetId/interface/PixelPannelType.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 #include <ostream>
00009 #include <fstream>
00010 #include "FWCore/Utilities/interface/Exception.h"
00011 
00012 using namespace std;
00013 
00014 PixelToLNKAssociateFromAscii::PixelToLNKAssociateFromAscii(const string & fn) {
00015   init(fn);
00016 }
00017 std::string PixelToLNKAssociateFromAscii::version() const
00018 {
00019   return theVersion; 
00020 }
00021 
00022 const PixelToLNKAssociateFromAscii::CablingRocId * PixelToLNKAssociateFromAscii::operator()( 
00023     const PixelToLNKAssociateFromAscii::DetectorRocId& roc) const
00024 {
00025 //  bool deb = (roc.module->name()=="BPix_BpI_SEC1_LYR1_LDR1H_MOD1");
00026 //  if (deb) cout <<"KUKU"<<endl;
00027 
00028   typedef std::vector< std::pair<DetectorRocId,CablingRocId> >::const_iterator IM;
00029   for (IM im = theConnection.begin(); im != theConnection.end(); im++) {
00030     if( ( *(im->first.module) == *roc.module ) && (im->first.rocDetId == roc.rocDetId)) {
00031       return &(im->second);  
00032     }
00033   }
00034   return 0;
00035 }
00036 
00037 void PixelToLNKAssociateFromAscii::init(const string & cfg_name)
00038 {
00039   LogDebug("init, input file:") << cfg_name.c_str();
00040 
00041   std::ifstream file( cfg_name.c_str() );
00042   if ( !file ) {
00043     edm::LogError(" ** PixelToLNKAssociateFromAscii,init ** ")
00044          << " cant open data file: " << cfg_name;
00045     return;
00046   } else {
00047     edm::LogInfo("PixelToLNKAssociateFromAscii, read data from: ") <<cfg_name ;
00048   }
00049 
00050   string line;
00051   int fedId=-1;
00052   int linkId=-1;
00053 
00054   try {
00055   while (getline(file,line)) {
00056     //
00057     // treat # lines
00058     //
00059     string::size_type pos = line.find("#");
00060     if (pos != string::npos) line = line.erase(pos);
00061 
00062     string::size_type posF = line.find("FED:");
00063     string::size_type posL = line.find("LNK:");
00064     string::size_type posM = line.find("MOD:");
00065     string::size_type posR = line.find("ROC:");
00066 
00067     LogTrace("") <<" read line: "<< line;
00068     
00069 
00070     //
00071     // treat version lines, reset date
00072     //
00073     if (     line.compare(0,3,"VER") == 0 ) { 
00074       edm::LogInfo("version: ")<<line;
00075       theVersion = line;
00076     }
00077 
00078     //
00079     // fed id line
00080     //
00081     else if ( posF != string::npos) { 
00082       line = line.substr(posF+4);
00083       fedId = atoi(line.c_str());
00084     }
00085 
00086     //
00087     // link id linke
00088     //
00089     else if ( posL != string::npos) {
00090       string srtL = line.substr(posL+4);
00091       linkId = atoi(srtL.c_str());
00092     }
00093 
00094     //
00095     // module description
00096     //
00097     if ( posM != string::npos) {
00098       if (posR != string::npos) {
00099         string strM = line.substr(posM+4, posR-posM-5);
00100         string::size_type pos = strM.find(" "); 
00101         if(pos != string::npos) strM = strM.substr(pos+1);
00102         string strR = line.substr(posR+4);
00103         Range range = readRange(strR);
00104         addConnections( fedId, linkId, strM, range);
00105       } else { 
00106         string strM= line.substr(posM+4);
00107         string::size_type pos = strM.find(" "); 
00108         if(pos != string::npos) strM = strM.substr(pos+1);
00109         addConnections( fedId, linkId, strM, Range(0,0));
00110       }
00111     }
00112   }
00113   } 
00114   catch(exception& err) {
00115     edm::LogError("**PixelToLNKAssociateFromAscii**  exception")<<err.what();
00116   }
00117 
00118   //
00119   // for debug
00120   //
00121   std::ostringstream str;
00122   str <<" **PixelToLNKAssociateFromAscii ** CONNECTIONS: "<< endl;
00123   typedef vector< pair<DetectorRocId,CablingRocId> >::const_iterator ICON;
00124   for (ICON ic = theConnection.begin(); ic != theConnection.end(); ic++) {
00125     str<< (*ic).first.module->name()
00126        <<", rocDetId="<<(*ic).first.rocDetId
00127        <<", fedId="<<ic->second.fedId
00128        <<", linkId="<<ic->second.linkId
00129        <<", rocLinkId="<<ic->second.rocLinkId
00130        <<endl;
00131   }
00132   edm::LogInfo("PixelToLNKAssociateFromAscii")<<str.str();
00133 }
00134 
00135 void PixelToLNKAssociateFromAscii::addConnections(
00136     int fedId, int linkId,  std::string module, Range rocDetIds)
00137 {
00138   string::size_type pos;
00139 
00140   // check for Barrel modules
00141   pos = module.find("BPix");
00142   if (pos != string::npos) { 
00143 
00144      // shell
00145      string strP = module.substr(pos+6,2);
00146      PixelBarrelName::Shell part;
00147          if (strP=="mO") part = PixelBarrelName::mO; 
00148      else if(strP=="mI") part = PixelBarrelName::mI;
00149      else if(strP=="pO") part = PixelBarrelName::pO;
00150      else                part = PixelBarrelName::pI;
00151      module = module.substr(pos+9);
00152 
00153      // sector
00154      pos = module.find("_");
00155      if (pos ==  string::npos) throw cms::Exception("problem with sector formatting");
00156      // int sector = atoi( module.substr(3,pos-3).c_str());
00157      module = module.substr(pos+1);
00158 
00159      // layer
00160      pos = module.find("_");
00161      if (pos ==  string::npos) throw cms::Exception("problem with layer formatting");
00162      int layer = atoi( module.substr(3,pos-3).c_str());
00163      module = module.substr(pos+1);
00164 
00165      // ladder
00166      pos = module.find("_");
00167      if (pos ==  string::npos) throw cms::Exception("problem with ladder formatting");
00168      int ladder = atoi( module.substr(3,pos-3).c_str());
00169      module = module.substr(pos+1);
00170 
00171      // z-module
00172      int zmodule = atoi( module.substr(3,pos-3).c_str());
00173 
00174      // place modules in connections
00175      int rocLnkId = 0; 
00176      for (int rocDetId=rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
00177        rocLnkId++;
00178        DetectorRocId  detectorRocId;
00179        PixelBarrelName * name = new PixelBarrelName(part, layer, zmodule, ladder);
00180        detectorRocId.module = name;
00181        detectorRocId.rocDetId = rocDetId;
00182        CablingRocId   cablingRocId;
00183        cablingRocId.fedId = fedId;
00184        cablingRocId.linkId = linkId;
00185        cablingRocId.rocLinkId = rocLnkId;
00186        // fix for type-B modules in barrel
00187        if (name->isHalfModule() && (rocDetIds.min()>7)  
00188            && (part==PixelBarrelName::mO || PixelBarrelName::mI) ) {
00189             cablingRocId.rocLinkId = 9-rocLnkId;
00190             detectorRocId.rocDetId = rocDetId-8;
00191        }
00192        theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00193      } 
00194   }
00195 
00196   // check for endcap modules
00197   // check for Barrel modules
00198   pos = module.find("FPix");
00199   if (pos != string::npos) {
00200      string strH = module.substr(pos+6,2);
00201      PixelEndcapName::HalfCylinder part;
00202          if (strH=="mO") part = PixelEndcapName::mO;
00203      else if(strH=="mI") part = PixelEndcapName::mI;
00204      else if(strH=="pO") part = PixelEndcapName::pO;
00205      else                part = PixelEndcapName::pI;
00206      module = module.substr(pos+9);
00207 
00208      // disk
00209      pos = module.find("_");
00210      if (pos ==  string::npos) throw cms::Exception("problem with disk formatting");
00211      int disk = atoi( module.substr(1,pos-1).c_str());
00212      module = module.substr(pos+1);
00213 
00214      // blade
00215      pos = module.find("_");
00216      if (pos ==  string::npos) throw cms::Exception("problem with blade formatting");
00217      int blade = atoi( module.substr(3,pos-3).c_str());
00218      module = module.substr(pos+1);
00219 
00220      //pannel
00221      pos = module.find("_");
00222      if (pos ==  string::npos) throw cms::Exception("problem with pannel formatting");
00223      int pannel = atoi( module.substr(3,pos-3).c_str());
00224      module = module.substr(pos+1);
00225 
00226      // plaquete
00227 //     pos = module.find("_");
00228 //     if (pos ==  string::npos) throw cms::Exception("problem with plaquette formatting");
00229 //     int plaq = atoi( module.substr(3,pos-3).c_str());
00230 
00231      // pannel type
00232      pos = module.find("TYP:");
00233      if (pos ==  string::npos) throw cms::Exception("problem with pannel type formatting");
00234      string strT = module.substr(pos+5,3);
00235 
00236      PixelPannelType::PannelType pannelType; 
00237           if (strT=="P3R") pannelType=PixelPannelType::p3R;
00238      else if (strT=="P3L") pannelType=PixelPannelType::p3L;
00239      else if (strT=="P4R") pannelType=PixelPannelType::p4R;
00240      else if (strT=="P4L") pannelType=PixelPannelType::p4L;
00241      else throw cms::Exception("problem with pannel type formatting (unrecoginzed word)");
00242 
00243      if ( pannelType==PixelPannelType::p4L) {
00244 //     cout <<"----------- p4L"<<endl;
00245        int rocLnkId =0;
00246        for (int plaq = 1; plaq <= 4; plaq++) {
00247          Range rocs; int firstRoc=0; int step=0;
00248          if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
00249          if (plaq==2) { rocs = Range(0,5); firstRoc=0; step=+1; }
00250          if (plaq==3) { rocs = Range(0,7); firstRoc=0; step=+1; }
00251          if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
00252          for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00253            rocLnkId++;
00254            int rocDetId = firstRoc + step*iroc; 
00255 
00256            DetectorRocId  detectorRocId;
00257            detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00258            detectorRocId.rocDetId = rocDetId;
00259 
00260            CablingRocId   cablingRocId;
00261            cablingRocId.fedId = fedId;
00262            cablingRocId.linkId = linkId;
00263            cablingRocId.rocLinkId = rocLnkId;
00264 
00265            theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00266 //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
00267          }
00268        } 
00269      } 
00270      else if ( pannelType==PixelPannelType::p4R) {
00271 //     cout <<"----------- p4R"<<endl;
00272        int rocLnkId =0;
00273        for (int plaq = 4; plaq >= 1; plaq--) {
00274          Range rocs; int firstRoc=0; int step=0;
00275          if (plaq==1) { rocs = Range(0,1); firstRoc=1; step=-1; }
00276          if (plaq==2) { rocs = Range(0,5); firstRoc=3; step=+1; }
00277          if (plaq==3) { rocs = Range(0,7); firstRoc=4; step=+1; }
00278          if (plaq==4) { rocs = Range(0,4); firstRoc=0; step=+1; }
00279          for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00280            rocLnkId++;
00281            int rocDetId = firstRoc + step*iroc;
00282            if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
00283 
00284            DetectorRocId  detectorRocId;
00285            detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00286            detectorRocId.rocDetId = rocDetId;
00287 
00288            CablingRocId   cablingRocId;
00289            cablingRocId.fedId = fedId;
00290            cablingRocId.linkId = linkId;
00291            cablingRocId.rocLinkId = rocLnkId;
00292 
00293            theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00294 //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
00295          }
00296        }
00297      }
00298      else if ( pannelType==PixelPannelType::p3L) {
00299 //     cout <<"----------- p3L"<<endl;
00300        int rocLnkId =0;
00301        for (int plaq = 1; plaq <= 3; plaq++) {
00302          Range rocs; int firstRoc=0; int step=0;
00303          if (plaq==1) { rocs = Range(0,5); firstRoc=0; step=1; }
00304          if (plaq==2) { rocs = Range(0,7); firstRoc=0; step=1; }
00305          if (plaq==3) { rocs = Range(0,9); firstRoc=0; step=1; }
00306          for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00307            rocLnkId++;
00308            int rocDetId = firstRoc + step*iroc; 
00309 
00310            DetectorRocId  detectorRocId;
00311            detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00312            detectorRocId.rocDetId = rocDetId;
00313 
00314            CablingRocId   cablingRocId;
00315            cablingRocId.fedId = fedId;
00316            cablingRocId.linkId = linkId;
00317            cablingRocId.rocLinkId = rocLnkId;
00318 
00319            theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00320 //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
00321          }
00322        } 
00323      } 
00324      else if ( pannelType==PixelPannelType::p3R) {
00325 //     cout <<"----------- p3R"<<endl;
00326        int rocLnkId =0;
00327        for (int plaq = 3; plaq >= 1; plaq--) {
00328          Range rocs; int firstRoc=0; int step=0;
00329          if (plaq==1) { rocs = Range(0,5); firstRoc=3; step=1; }
00330          if (plaq==2) { rocs = Range(0,7); firstRoc=4; step=1; }
00331          if (plaq==3) { rocs = Range(0,9); firstRoc=5; step=1; }
00332          for (int iroc =rocs.min(); iroc<=rocs.max(); iroc++) {
00333            rocLnkId++;
00334            int rocDetId = firstRoc + step*iroc;
00335            if (rocDetId > rocs.max()) rocDetId = (rocDetId-1)%rocs.max();
00336 
00337            DetectorRocId  detectorRocId;
00338            detectorRocId.module = new PixelEndcapName(part,disk,blade,pannel,plaq);
00339            detectorRocId.rocDetId = rocDetId;
00340 
00341            CablingRocId   cablingRocId;
00342            cablingRocId.fedId = fedId;
00343            cablingRocId.linkId = linkId;
00344            cablingRocId.rocLinkId = rocLnkId;
00345 
00346            theConnection.push_back( make_pair(detectorRocId,cablingRocId));
00347 //         cout <<"PLAQ:"<<plaq<<" rocDetId: "<<rocDetId<<" rocLnkId:"<<rocLnkId<<endl;
00348          }
00349        }
00350      }
00351 
00352   }
00353 }
00354 
00355 PixelToLNKAssociateFromAscii::Range 
00356     PixelToLNKAssociateFromAscii::readRange( const string & l) const
00357 {
00358   bool first = true;
00359   int num1 = -1;
00360   int num2 = -1;
00361   const char * line = l.c_str();
00362   while (line) {
00363     char * evp = 0;
00364     int num = strtol(line, &evp, 10);
00365     { stringstream s; s<<"raad from line: "; s<<num; LogTrace("") << s.str(); }
00366     if (evp != line) {
00367       line = evp +1;
00368       if (first) { num1 = num; first = false; }
00369       num2 = num;
00370     } else line = 0;
00371   }
00372   if (first) {
00373     string s = "** PixelToLNKAssociateFromAscii, read data, cant intrpret: " ;
00374     edm::LogInfo(s) << endl 
00375               << l << endl 
00376               <<"=====> send exception " << endl;
00377     s += l;
00378     throw cms::Exception(s);
00379   }
00380   return Range(num1,num2);
00381 }
00382 

Generated on Tue Jun 9 17:25:45 2009 for CMSSW by  doxygen 1.5.4