CMS 3D CMS Logo

PixelDetectorConfig.cc

Go to the documentation of this file.
00001 //
00002 // Implementation of the detector configuration
00003 //
00004 //
00005 //
00006 //
00007 
00008 #include "CalibFormats/SiPixelObjects/interface/PixelDetectorConfig.h"
00009 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00010 #include <fstream>
00011 #include <iostream>
00012 #include <sstream>
00013 #include <ios>
00014 #include <assert.h>
00015 #include <stdio.h>
00016 
00017 using namespace std;
00018 using namespace pos;
00019 
00020 
00021 PixelDetectorConfig::PixelDetectorConfig(std::vector< std::vector < std::string> > &tableMat):PixelConfigBase("","",""){
00022 
00023   std::vector< std::string > ins = tableMat[0];
00024   std::map<std::string , int > colM;
00025   std::vector<std::string > colNames;
00026   /*
00027     CONFIG_KEY_ID                             NOT NULL NUMBER(38)
00028     CONFG_KEY                                 NOT NULL VARCHAR2(80)
00029     VERSION                                            VARCHAR2(40)
00030     KIND_OF_COND                              NOT NULL VARCHAR2(40)
00031     SERIAL_NUMBER                                      VARCHAR2(40)
00032     ROC_NAME                                  NOT NULL VARCHAR2(200)
00033     PANEL_NAME                                NOT NULL VARCHAR2(200)
00034     ROC_STATUS                                NOT NULL VARCHAR2(200)
00035   */
00036   colNames.push_back("CONFIG_KEY_ID");//0
00037   colNames.push_back("CONFIG_KEY");   //1
00038   colNames.push_back("VERSION");      //2
00039   colNames.push_back("KIND_OF_COND"); //3
00040   colNames.push_back("SERIAL_NUMBER");//4
00041   colNames.push_back("ROC_NAME");     //5
00042   colNames.push_back("PANEL_NAME");   //6
00043   colNames.push_back("ROC_STATUS");   //7
00044 
00045   for(unsigned int c = 0 ; c < ins.size() ; c++){
00046     for(unsigned int n=0; n<colNames.size(); n++){
00047       if(tableMat[0][c] == colNames[n]){
00048         colM[colNames[n]] = c;
00049         break;
00050       }
00051     }
00052   }//end for
00053   for(unsigned int n=0; n<colNames.size(); n++){
00054     if(colM.find(colNames[n]) == colM.end()){
00055       std::cerr << "[PixelDetectorConfig::PixelDetectorConfig()]\tCouldn't find in the database the column with name " << colNames[n] << std::endl;
00056       assert(0);
00057     }
00058   }
00059   
00060 
00061   modules_.clear();
00062   rocs_.clear() ;
00063   std::string module= "";
00064   for(unsigned int r = 1 ; r < tableMat.size() ; r++)
00065     {    //Goes to every row of the Matrix
00066       PixelROCName roc(tableMat[r][colM["ROC_NAME"]]) ; // see DACSettings!!!!
00067       PixelROCStatus rocstatus;
00068       std::string status = tableMat[r][colM["ROC_STATUS"]];
00069       // The following is due to the fact that enabled ROCs are 
00070       // labelled as ON in the DataBase, but have NO label in 
00071       // configuration files!!!
00072       if(status.find("ON") != string::npos)
00073         {
00074           status = "" ;
00075         }
00076       if (status!=""){
00077         rocstatus.set(status);
00078       }
00079       rocs_[roc]=rocstatus;
00080       if (!rocstatus.get(PixelROCStatus::noInit)){
00081 
00082         PixelModuleName module(tableMat[r][colM["PANEL_NAME"]]);
00083         if (!containsModule(module)) {
00084           modules_.push_back(module);
00085         }
00086       }
00087   }//end for r
00088 
00089   std::cout<<"Number of Modules in Detector Configuration Class:"<<getNModules()<<std::endl;
00090 
00091 }//end constructor
00092 
00093 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00094 
00095 PixelDetectorConfig::PixelDetectorConfig(std::string filename):
00096   PixelConfigBase("","",""){
00097 
00098   if (filename[filename.size()-1]=='t'){
00099 
00100     std::ifstream in(filename.c_str());
00101 
00102     if (!in.good()){
00103       std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t    Could not open: "<<filename<<std::endl;
00104       assert(0);
00105     }
00106     else {
00107       std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t    Opened: "<<filename<<std::endl;
00108     }
00109         
00110     if (in.eof()){
00111       std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t    EOF before reading anything!"<<std::endl;
00112       ::abort();
00113     }
00114 
00115         
00116     modules_.clear();
00117     rocs_.clear() ;
00118         
00119     std::string module;
00120         
00121     in >> module;
00122 
00123     if (module=="Rocs:") {
00124       std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t    New format of detconfig"<<std::endl;
00125       //new format with list of ROCs.
00126       std::string rocname;
00127       in >> rocname;
00128       while (!in.eof()){
00129         //cout << "Read rocname:"<<rocname<<endl;
00130         PixelROCName roc(rocname);
00131         std::string line;
00132         getline(in,line);
00133         //cout << "Read line:'"<<line<<"'"<<endl;
00134         istringstream instring(line);
00135         PixelROCStatus rocstatus;
00136         std::string status;
00137         while (!instring.eof()) {
00138           instring >> status;
00139 //        cout << "Read status:"<<status<<endl;
00140           if (status!=""){
00141             rocstatus.set(status);
00142           }
00143         }
00144         rocs_[roc]=rocstatus;
00145         if (!rocstatus.get(PixelROCStatus::noInit)){
00146           PixelModuleName module(rocname);
00147           if (!containsModule(module)) {
00148             modules_.push_back(module);
00149           }
00150         }
00151         in >> rocname;
00152       }
00153       return;
00154     }
00155         
00156 
00157     //std::cout << "Read module:"<<module<<std::endl;
00158 
00159     if (in.eof()) std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t    EOF after reading first module name"
00160                             << std::endl;
00161 
00162     std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t    Old format of detconfig"<<std::endl;
00163     while (!in.eof()){
00164 
00165       //std::cout << "Read module:"<<module<<std::endl;
00166 
00167       PixelModuleName moduleName(module);
00168 
00169       modules_.push_back(moduleName);
00170             
00171       in >> module;
00172             
00173       assert(modules_.size()<10000);
00174             
00175     }
00176         
00177     in.close();
00178 
00179   }
00180   else{
00181 
00182     assert(0);
00183 
00184     /*
00185       std::ifstream in(filename.c_str(),std::ios::binary);
00186 
00187       if (!in.good()){
00188       std::cout << "Could not open:"<<filename<<std::endl;
00189       assert(0);
00190       }
00191       else {
00192       std::cout << "Opened:"<<filename<<std::endl;
00193       }
00194 
00195       char nchar;
00196 
00197       in.read(&nchar,1);
00198         
00199       std::string s1;
00200 
00201       //wrote these lines of code without ref. needs to be fixed
00202       for(int i=0;i< nchar; i++){
00203       char c;
00204       in >>c;
00205       s1.push_back(c);
00206       }
00207 
00208       //std::cout << "READ ROC name:"<<s1<<std::endl;
00209 
00210       dacsettings_.clear();
00211 
00212 
00213       while (!in.eof()){
00214 
00215       //std::cout << "PixelDetectorConfig::PixelDetectorConfig read s1:"<<s1<<std::endl;
00216 
00217       PixelROCName rocid(s1);
00218 
00219       //td::cout << "PixelDetectorConfig::PixelDetectorConfig read rocid:"<<rocid<<std::endl;
00220             
00221       PixelROCDetectorConfig tmp;
00222       
00223       tmp.readBinary(in, rocid);
00224 
00225       dacsettings_.push_back(tmp);
00226 
00227 
00228       in.read(&nchar,1);
00229 
00230       s1.clear();
00231 
00232       if (in.eof()) continue;
00233 
00234       //wrote these lines of code without ref. needs to be fixed
00235       for(int i=0;i< nchar; i++){
00236       char c;
00237       in >>c;
00238       s1.push_back(c);
00239       }
00240 
00241 
00242       }
00243 
00244       in.close();
00245 
00246     */
00247 
00248   }
00249 
00250 
00251   //std::cout << "Read dac settings for "<<dacsettings_.size()<<" ROCs"<<std::endl;
00252 
00253 
00254 }
00255 
00256 unsigned int PixelDetectorConfig::getNModules() const {
00257 
00258   return modules_.size();
00259 
00260 }
00261  
00262 PixelModuleName PixelDetectorConfig::getModule(unsigned int i) const {
00263 
00264   return modules_[i];
00265 
00266 }
00267 
00268 std::set <unsigned int> PixelDetectorConfig::getFEDs(PixelNameTranslation* translation) const 
00269 {
00270 
00271   std::set <unsigned int> feds;
00272   assert(modules_.size()!=0);
00273   std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00274         
00275   for (;imodule!=modules_.end();++imodule) {
00276   
00277                 std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
00278                 for ( std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin(); channelsOnThisModule_itr != channelsOnThisModule.end(); channelsOnThisModule_itr++ )
00279                 {
00280                         const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
00281                         unsigned int fednumber=channel_hdwaddress.fednumber();
00282                         feds.insert(fednumber);
00283                 }
00284 
00285   }
00286         
00287   return feds;
00288 }
00289 
00290 
00291 // Returns the FED numbers and channels within each FED that are used
00292 std::map <unsigned int, std::set<unsigned int> > PixelDetectorConfig::getFEDsAndChannels(PixelNameTranslation* translation) const
00293 {
00294   //      FED Number                channels
00295 
00296   std::map <unsigned int, std::set<unsigned int> > fedsChannels;
00297   assert(modules_.size()!=0);
00298   std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00299 
00300   for (;imodule!=modules_.end();++imodule) {
00301   
00302                 std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
00303                 for ( std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin(); channelsOnThisModule_itr != channelsOnThisModule.end(); channelsOnThisModule_itr++ )
00304                 {
00305                         const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
00306                         unsigned int fednumber=channel_hdwaddress.fednumber();
00307                         unsigned int fedchannel=channel_hdwaddress.fedchannel();
00308                         fedsChannels[fednumber].insert(fedchannel);
00309                 }
00310   
00311   }
00312 
00313   return fedsChannels;
00314 }
00315  
00316 bool PixelDetectorConfig::containsModule(const PixelModuleName& moduleToFind) const
00317 {
00318   for ( std::vector<PixelModuleName>::const_iterator modules_itr = modules_.begin(); modules_itr != modules_.end(); modules_itr++ )
00319     {
00320       if ( *modules_itr == moduleToFind ) return true;
00321     }
00322   return false;
00323 }
00324 
00325 // modified by MR on 11-01-2008 15:06:51
00326 void PixelDetectorConfig::writeASCII(std::string dir) const {
00327 
00328   if (dir!="") dir+="/";
00329   std::string filename=dir+"detectconfig.dat";
00330 
00331   std::ofstream out(filename.c_str(), std::ios_base::out) ;
00332   if(!out) {
00333     std::cout << "[PixelDetectorConfig::writeASCII()]\t\t    Could not open file " << filename << " for write" << std::endl ;
00334     exit(1);
00335   }
00336 
00337 
00338   if(rocs_.size() == 0) 
00339     {
00340       std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00341       
00342       for (;imodule!=modules_.end();++imodule) 
00343         {
00344           out << *imodule << std::endl;
00345         }
00346     } 
00347   else 
00348     {
00349       out << "Rocs:" << endl ;
00350       std::map<PixelROCName, PixelROCStatus>::const_iterator irocs = rocs_.begin();
00351       for(; irocs != rocs_.end() ; irocs++)
00352         {
00353           out << (irocs->first).rocname() << " " << (irocs->second).statusName() << endl ;
00354         }
00355     }
00356   
00357   out.close();
00358 
00359 }
00360 
00361 //=============================================================================================
00362 void PixelDetectorConfig::writeXML(pos::PixelConfigKey key, int version, std::string path) const {
00363   std::string mthn = "[PixelDetectorConfig::writeXML()]\t\t\t    " ;
00364   std::stringstream fullPath ;
00365 
00366   fullPath << path << "/detectorconfig.xml" ;
00367   cout << mthn << "Writing to: " << fullPath.str() << endl ;
00368   
00369   std::ofstream out(fullPath.str().c_str()) ;
00370   
00371   out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                    << endl ;
00372   out << "<ROOT xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'>"                               << endl ;
00373   out << " <HEADER>"                                                                                  << endl ;
00374   out << "  <TYPE>"                                                                                   << endl ;
00375   out << "   <EXTENSION_TABLE_NAME>FPIX_DETECTOR_CONFIG</EXTENSION_TABLE_NAME>"                       << endl ;
00376   out << "   <NAME>FPix Detector Configuration</NAME>"                                                << endl ;
00377   out << "  </TYPE>"                                                                                  << endl ;
00378   out << "  <RUN>"                                                                                    << endl ;
00379   out << "   <RUN_TYPE>test</RUN_TYPE>"                                                               << endl ;
00380   out << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                              << endl ;
00381   out << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl ;
00382   out << "   <COMMENT_DESCRIPTION>Test of DetectorConfig xml</COMMENT_DESCRIPTION>"                   << endl ;
00383   out << "   <LOCATION>CERN TAC</LOCATION>"                                                           << endl ;
00384   out << "   <INITIATED_BY_USER>Dario Menasce</INITIATED_BY_USER>"                                    << endl ;
00385   out << "  </RUN>"                                                                                   << endl ;
00386   out << " </HEADER>"                                                                                 << endl ;
00387   out << ""                                                                                           << endl ;
00388   out << " <DATA_SET>"                                                                                << endl ;
00389   out << "  <VERSION>" << version << "</VERSION>"                                                     << endl ;
00390   out << "  <PART>"                                                                                   << endl ;
00391   out << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                                 << endl ;
00392   out << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                              << endl ;
00393   out << "  </PART>"                                                                                  << endl ;
00394   out << ""                                                                                           << endl ;
00395 
00396   if(rocs_.size() == 0) 
00397     {
00398       std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00399       
00400       // This needs to be fixed: given a module name, actually loop over ROCs to write the XML data
00401       for (;imodule!=modules_.end();++imodule) 
00402         {
00403           out << "  <DATA>"                                                                           << endl ;
00404 //---->          out << "   <ROC_NAME>" << (irocs->first).rocname() << "</ROC_NAME>"                  << endl ;
00405           out << "   <ROC_STATUS>on</ROC_STATUS>"                                                     << endl ;
00406           out << "  </DATA>"                                                                          << endl ;
00407           out << " "                                                                                  << endl ;
00408         }
00409     } 
00410   else 
00411     {
00412       std::map<PixelROCName, PixelROCStatus>::const_iterator irocs = rocs_.begin();
00413       for(; irocs != rocs_.end() ; irocs++)
00414         {
00415           std::string sts = (irocs->second).statusName() ;
00416           if( sts == "" ) {sts = "on" ;}
00417           out << "  <DATA>"                                                                           << endl ;
00418           out << "   <ROC_NAME>"   << (irocs->first).rocname() << "</ROC_NAME>"                       << endl ;
00419           out << "   <ROC_STATUS>" << sts << "</ROC_STATUS>"                                          << endl ;
00420           out << "  </DATA>"                                                                          << endl ;
00421           out << " "                                                                                  << endl ;
00422         }
00423     }
00424   out << " </DATA_SET>"                                                                               << endl ;
00425   out << "</ROOT> "                                                                                   << endl ;
00426   out.close() ;
00427   assert(0) ;
00428 }
00429 
00430 //=============================================================================================
00431 void PixelDetectorConfig::addROC(   PixelROCName &theROC)  // Added by Dario (March 3, 2008)
00432 {
00433  std::string mthn = "[PixelDetectorConfig::addROC()]\t\t\t\t" ;
00434  std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00435  if( theROCIt == rocs_.end() ) // if theROC was not there, add it and turn it on
00436  {
00437   PixelROCStatus  theStatus ;
00438   theStatus.reset() ;
00439   rocs_[theROC] = theStatus ; 
00440 //  cout << mthn << "Non existing ROC (" << theROC.rocname() << "): adding it"  << endl ;  
00441  } else {
00442   theROCIt->second.reset() ;  // otherwise just turn it on by resetting it to zero
00443 //  cout << mthn << "Already existing ROC (" << theROC.rocname() << "): switching it on"  << endl ;  
00444  }
00445 }
00446 
00447 //=============================================================================================
00448 void PixelDetectorConfig::addROC(   PixelROCName &theROC, string statusLabel)  // modified by MR on 14-05-2008 11:29:51
00449 {
00450  std::string mthn = "[PixelDetectorConfig::addROC()]\t\t\t\t" ;
00451  std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00452  if( theROCIt == rocs_.end() ) // if theROC was not there, add it and turn it on
00453  {
00454   PixelROCStatus  theStatus ;
00455   theStatus.set(statusLabel) ;
00456   theStatus.reset() ;
00457   rocs_[theROC] = theStatus ; 
00458 //  cout << mthn << "Non existing ROC (" << theROC.rocname() << "): adding it"  << endl ;  
00459  } else {
00460   theROCIt->second.set(statusLabel) ;  // otherwise just turn it on by resetting it to zero
00461 //  cout << mthn << "Already existing ROC (" << theROC.rocname() << "): switching it on"  << endl ;  
00462  }
00463 }
00464 
00465 //=============================================================================================
00466 void PixelDetectorConfig::removeROC(PixelROCName &theROC)  // Added by Dario (March 3, 2008)
00467 {
00468  std::string mthn = "[PixelDetectorConfig::removeROC()]\t\t\t\t" ;
00469  std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00470  if( theROCIt != rocs_.end() ) // if theROC was there remove it, otherwise ignore
00471  {
00472   theROCIt->second.set("noInit") ;  
00473 //  cout << mthn << "Already existing ROC (" << theROC.rocname() << "): switching it off"  << endl ;  
00474  } else {
00475   PixelROCStatus  theStatus ;
00476   theStatus.set("noInit") ;
00477   rocs_[theROC] = theStatus ; 
00478 //  cout << mthn << "ROC " << theROC.rocname() << " was not individually declared in the file: declare and switch off"  << endl ;  
00479  }
00480 }
00481 
00482 //std::ostream& operator<<(std::ostream& s, const PixelDetectorConfig& dacs){
00483 //
00484 //  s << dacs.getDetectorConfig(0) <<std::endl; 
00485 //
00486 //  return s;
00487 //
00488 //}
00489 

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