CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/CalibTracker/SiPixelConnectivity/src/PixelToFEDAssociateFromAscii.cc

Go to the documentation of this file.
00001 #include "CalibTracker/SiPixelConnectivity/interface/PixelToFEDAssociateFromAscii.h"
00002 
00003 #include "DataFormats/SiPixelDetId/interface/PixelBarrelName.h"
00004 #include "DataFormats/SiPixelDetId/interface/PixelEndcapName.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 
00007 #include <ostream>
00008 #include <fstream>
00009 #include "FWCore/Utilities/interface/Exception.h"
00010 
00011 using namespace std;
00012 
00013 
00014 PixelToFEDAssociateFromAscii::BarrelConnections PixelToFEDAssociateFromAscii::theBarrel
00015    = PixelToFEDAssociateFromAscii::BarrelConnections();
00016 PixelToFEDAssociateFromAscii::EndcapConnections PixelToFEDAssociateFromAscii::theEndcap
00017    = PixelToFEDAssociateFromAscii::EndcapConnections();
00018 
00019 
00020 PixelToFEDAssociateFromAscii::PixelToFEDAssociateFromAscii(const string & fn) {
00021   init(fn);
00022 }
00023 std::string PixelToFEDAssociateFromAscii::version() const
00024 {
00025   return theVersion; 
00026 }
00027 
00028 int PixelToFEDAssociateFromAscii::operator()(const PixelModuleName & id) const 
00029 {
00030   return id.isBarrel() ?
00031     operator()(dynamic_cast<const PixelBarrelName & >(id)) :
00032     operator()(dynamic_cast<const PixelEndcapName & >(id)) ;
00033 }
00034 
00035 int PixelToFEDAssociateFromAscii::operator()(const PixelBarrelName & id) const
00036 {
00037   for (BarrelConnections::const_iterator
00038       ibc = theBarrel.begin(); ibc != theBarrel.end(); ibc++) {
00039     for (vector<Bdu>::const_iterator
00040         ibd = (*ibc).second.begin(); ibd != (*ibc).second.end(); ibd++) {
00041       if (    ibd->b == id.shell() 
00042            && ibd->l.inside( id.layerName() )
00043            && ibd->z.inside( id.moduleName() )
00044            && ibd->f.inside( id.ladderName() ) ) return (*ibc).first; 
00045     }
00046   }
00047   edm::LogError("** PixelToFEDAssociateFromAscii WARNING, name: ")
00048        << id.name()<<" not associated to FED";
00049   return -1;
00050 }
00051 
00052 int PixelToFEDAssociateFromAscii::operator()(const PixelEndcapName & id) const 
00053 {
00054   for (EndcapConnections::const_iterator
00055       iec = theEndcap.begin(); iec != theEndcap.end(); iec++) {
00056     for (vector<Edu>::const_iterator
00057         ied = (*iec).second.begin(); ied != (*iec).second.end(); ied++) {
00058       if (    ied->e == id.halfCylinder() 
00059            && ied->d.inside( id.diskName() )
00060            && ied->b.inside( id.bladeName() ) ) return iec->first; 
00061     }
00062   }
00063   edm::LogError("** PixelToFEDAssociateFromAscii WARNING, name: ")
00064        << id.name()<<" not associated to FED";
00065   return -1;
00066 }
00067 
00068 
00069 void PixelToFEDAssociateFromAscii::init(const string & cfg_name)
00070 {
00071   LogDebug("init, input file:") << cfg_name.c_str();
00072 
00073   std::ifstream file( cfg_name.c_str() );
00074   if ( !file ) {
00075     edm::LogError(" ** PixelToFEDAssociateFromAscii,init ** ")
00076          << " cant open data file: " << cfg_name;
00077     return;
00078   } else {
00079     edm::LogInfo("PixelToFEDAssociateFromAscii, read data from: ") <<cfg_name ;
00080   }
00081 
00082   string line;
00083   pair< int, vector<Bdu> > barCon;
00084   pair< int, vector<Edu> > endCon;
00085 
00086   try {
00087   while (getline(file,line)) {
00088     //
00089     // treat # lines
00090     //
00091     string::size_type pos = line.find("#");
00092     if (pos != string::npos) line = line.erase(pos);
00093 
00094     string::size_type posF = line.find("FED:");
00095     string::size_type posB = line.find("S:");
00096     string::size_type posE = line.find("E:");
00097 
00098     LogDebug ( "line read" ) << line;
00099 
00100     //
00101     // treat version lines, reset date
00102     //
00103     if (     line.compare(0,3,"VER") == 0 ) { 
00104       edm::LogInfo("version: ")<<line;
00105       theVersion = line;
00106       send(barCon,endCon);
00107       theBarrel.clear();
00108       theEndcap.clear();
00109     }
00110 
00111     //
00112     // fed id line
00113     //
00114     else if ( posF != string::npos) { 
00115       line = line.substr(posF+4);
00116       int id = atoi(line.c_str());
00117       send(barCon,endCon); 
00118       barCon.first = id;
00119       endCon.first = id;
00120     }
00121 
00122     //
00123     // barrel connections
00124     //
00125     else if ( posB != string::npos) {
00126       line = line.substr(posB+2);
00127       barCon.second.push_back( getBdu(line) );
00128     }
00129 
00130     //
00131     // endcap connections
00132     //
00133     else if ( posE != string::npos) {
00134       line = line.substr(posE+2);
00135       endCon.second.push_back( getEdu(line) );
00136     }
00137   }
00138   send(barCon,endCon);
00139   } 
00140   catch(exception& err) {
00141     edm::LogError("**PixelToFEDAssociateFromAscii**  exception")<<err.what();
00142     theBarrel.clear();
00143     theEndcap.clear();
00144   }
00145 
00146   //
00147   // for debug
00148   //
00149   std::ostringstream str;
00150   str <<" **PixelToFEDAssociateFromAscii ** BARREL FED CONNECTIONS: "<< endl;
00151   for (BarrelConnections::const_iterator
00152       ibc = theBarrel.begin(); ibc != theBarrel.end(); ibc++) {
00153     str << "FED: " << ibc->first << endl;
00154     for (vector<Bdu>::const_iterator
00155         ibd = (*ibc).second.begin(); ibd != (*ibc).second.end(); ibd++) {
00156       str << "b: "<<ibd->b<<" l: "<<ibd->l<<" z: "<<ibd->z<<" f: "<<ibd->f<<endl;
00157     }
00158   }
00159   str <<" **PixelToFEDAssociateFromAscii ** ENDCAP FED CONNECTIONS: " << endl;
00160   for (EndcapConnections::const_iterator
00161     iec = theEndcap.begin(); iec != theEndcap.end(); iec++) {
00162     str << "FED: " << iec->first << endl;
00163     for (vector<Edu>::const_iterator
00164         ied = (*iec).second.begin(); ied != (*iec).second.end(); ied++) {
00165       str << " e: "<<ied->e<<" d: "<<ied->d<<" b: "<<ied->b<<endl;
00166     }
00167   }
00168   edm::LogInfo("PixelToFEDAssociateFromAscii")<<str.str();
00169 }
00170 
00171 void PixelToFEDAssociateFromAscii::send(
00172     pair<int,vector<Bdu> > & b, pair<int,vector<Edu> > & e) const 
00173 {
00174   if (b.second.size() > 0) theBarrel.push_back(b);
00175   if (e.second.size() > 0) theEndcap.push_back(e);
00176   b.second.clear();
00177   e.second.clear();
00178 }
00179 
00180 PixelToFEDAssociateFromAscii::Bdu PixelToFEDAssociateFromAscii::getBdu( string line) const
00181 {
00182   Bdu result;
00183   string::size_type pos;
00184 
00185   result.b =  readRange(line).first;
00186 
00187   pos = line.find("L:");
00188   if (pos != string::npos) line = line.substr(pos+2);
00189   result.l = readRange(line);
00190 
00191   pos = line.find("Z:");
00192   if (pos != string::npos) line = line.substr(pos+2);
00193   result.z = readRange(line);
00194 
00195   pos = line.find("F:");
00196   if (pos != string::npos) line = line.substr(pos+2);
00197   result.f = readRange(line);
00198 
00199   return result;
00200 }
00201 
00202 PixelToFEDAssociateFromAscii::Edu PixelToFEDAssociateFromAscii::getEdu( string line) const
00203 {
00204   Edu result;
00205   string::size_type pos;
00206 
00207   result.e = readRange(line).first;
00208 
00209   pos = line.find("D:");
00210   if (pos != string::npos) line = line.substr(pos+2);
00211   result.d = readRange(line);
00212 
00213   pos = line.find("B:");
00214   if (pos != string::npos) line = line.substr(pos+2);
00215   result.b = readRange(line);
00216   
00217   return result;
00218 }
00219 
00220 PixelToFEDAssociateFromAscii::Range 
00221     PixelToFEDAssociateFromAscii::readRange( const string & l) const
00222 {
00223   bool first = true;
00224   int num1 = -1;
00225   int num2 = -1;
00226   const char * line = l.c_str();
00227   while (line) {
00228     char * evp = 0;
00229     int num = strtol(line, &evp, 10);
00230     { stringstream s; s<<"raad from line: "; s<<num; LogDebug(s.str()); }
00231     if (evp != line) {
00232       line = evp +1;
00233       if (first) { num1 = num; first = false; }
00234       num2 = num;
00235     } else line = 0;
00236   }
00237   if (first) {
00238     string s = "** PixelToFEDAssociateFromAscii, read data, cant intrpret: " ;
00239     edm::LogInfo(s) << endl 
00240               << l << endl 
00241               <<"=====> send exception " << endl;
00242     s += l;
00243     throw cms::Exception(s);
00244   }
00245   return Range(num1,num2);
00246 }
00247