CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CalibFormats/SiPixelObjects/src/PixelPortCardConfig.cc

Go to the documentation of this file.
00001 //
00002 // This class specifies the settings on the TKPCIFEC
00003 // and the settings on the portcard
00004 //
00005 //
00006 //
00007 //
00008 
00009 #include "CalibFormats/SiPixelObjects/interface/PixelPortCardConfig.h"
00010 #include "CalibFormats/SiPixelObjects/interface/PixelPortCardSettingNames.h"
00011 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00012 #include <fstream>
00013 #include <sstream>
00014 #include <iostream>
00015 #include <string>
00016 #include <vector>
00017 #include <assert.h>
00018 #include <stdexcept>
00019 #include <set>
00020 
00021 using namespace std;
00022 using namespace pos::PortCardSettingNames;
00023 using namespace pos;
00024 
00025 //added by Umesh
00026 PixelPortCardConfig::PixelPortCardConfig(vector < vector< string> >  &tableMat):PixelConfigBase(" "," "," ")
00027 {
00028   string mthn = "]\t[PixelPortCardConfig::PixelPortCardConfig()]\t\t    " ;
00029   map<string , int > colM;
00030   vector<string> colNames;
00031 
00123   colNames.push_back("CONFIG_KEY"  );
00124   colNames.push_back("KEY_TYPE"    );
00125   colNames.push_back("KEY_ALIAS"   );
00126   colNames.push_back("VERSION"     );
00127   colNames.push_back("KIND_OF_COND");
00128   colNames.push_back("PORT_CARD"   );
00129   colNames.push_back("TRKFEC"      );
00130   colNames.push_back("RING"        );
00131   colNames.push_back("CHANNEL"     );
00132   colNames.push_back("CCU_ADDR"    );
00133   colNames.push_back("I2C_CNTRL"   );
00134   colNames.push_back("I2C_SPEED"   );
00135   colNames.push_back("AOH_BIAS1"   );
00136   colNames.push_back("AOH_BIAS2"   );
00137   colNames.push_back("AOH_BIAS3"   );
00138   colNames.push_back("AOH_BIAS4"   );
00139   colNames.push_back("AOH_BIAS5"   );
00140   colNames.push_back("AOH_BIAS6"   );
00141   colNames.push_back("AOH_GAIN1"   );
00142   colNames.push_back("AOH_GAIN2"   );
00143   colNames.push_back("AOH_GAIN3"   );
00144   colNames.push_back("AOH_GAIN4"   );
00145   colNames.push_back("AOH_GAIN5"   );
00146   colNames.push_back("AOH_GAIN6"   );
00147   colNames.push_back("AOH1_BIAS1"  );
00148   colNames.push_back("AOH1_BIAS2"  );
00149   colNames.push_back("AOH1_BIAS3"  );
00150   colNames.push_back("AOH1_BIAS4"  );
00151   colNames.push_back("AOH1_BIAS5"  );
00152   colNames.push_back("AOH1_BIAS6"  );
00153   colNames.push_back("AOH1_GAIN1"  );
00154   colNames.push_back("AOH1_GAIN2"  );
00155   colNames.push_back("AOH1_GAIN3"  );
00156   colNames.push_back("AOH1_GAIN4"  );
00157   colNames.push_back("AOH1_GAIN5"  );
00158   colNames.push_back("AOH1_GAIN6"  );
00159   colNames.push_back("AOH2_BIAS1"  );
00160   colNames.push_back("AOH2_BIAS2"  );
00161   colNames.push_back("AOH2_BIAS3"  );
00162   colNames.push_back("AOH2_BIAS4"  );
00163   colNames.push_back("AOH2_BIAS5"  );
00164   colNames.push_back("AOH2_BIAS6"  );
00165   colNames.push_back("AOH2_GAIN1"  );
00166   colNames.push_back("AOH2_GAIN2"  );
00167   colNames.push_back("AOH2_GAIN3"  );
00168   colNames.push_back("AOH2_GAIN4"  );
00169   colNames.push_back("AOH2_GAIN5"  );
00170   colNames.push_back("AOH2_GAIN6"  );
00171   colNames.push_back("AOH3_BIAS1"  );
00172   colNames.push_back("AOH3_BIAS2"  );
00173   colNames.push_back("AOH3_BIAS3"  );
00174   colNames.push_back("AOH3_BIAS4"  );
00175   colNames.push_back("AOH3_BIAS5"  );
00176   colNames.push_back("AOH3_BIAS6"  );
00177   colNames.push_back("AOH3_GAIN1"  );
00178   colNames.push_back("AOH3_GAIN2"  );
00179   colNames.push_back("AOH3_GAIN3"  );
00180   colNames.push_back("AOH3_GAIN4"  );
00181   colNames.push_back("AOH3_GAIN5"  );
00182   colNames.push_back("AOH3_GAIN6"  );
00183   colNames.push_back("AOH4_BIAS1"  );
00184   colNames.push_back("AOH4_BIAS2"  );
00185   colNames.push_back("AOH4_BIAS3"  );
00186   colNames.push_back("AOH4_BIAS4"  );
00187   colNames.push_back("AOH4_BIAS5"  );
00188   colNames.push_back("AOH4_BIAS6"  );
00189   colNames.push_back("AOH4_GAIN1"  );
00190   colNames.push_back("AOH4_GAIN2"  );
00191   colNames.push_back("AOH4_GAIN3"  );
00192   colNames.push_back("AOH4_GAIN4"  );
00193   colNames.push_back("AOH4_GAIN5"  );
00194   colNames.push_back("AOH4_GAIN6"  );
00195   colNames.push_back("DELAY25_GCR" );
00196   colNames.push_back("DELAY25_SCL" );
00197   colNames.push_back("DELAY25_TRG" );
00198   colNames.push_back("DELAY25_SDA" );
00199   colNames.push_back("DELAY25_RCL" );
00200   colNames.push_back("DELAY25_RDA" );
00201   colNames.push_back("DOH_BIAS0"   );
00202   colNames.push_back("DOH_BIAS1"   );
00203   colNames.push_back("DOH_SEU_GAIN");
00204   colNames.push_back("PLL_CTR1"    );
00205   colNames.push_back("PLL_CTR2"    );
00206   colNames.push_back("PLL_CTR3"    );
00207   colNames.push_back("PLL_CTR4"    );
00208   colNames.push_back("PLL_CTR5"    );
00209 
00210   //these are arbitrary integers that control the sort order
00211   unsigned int othercount=100;
00212   unsigned int delay25count=50;
00213   aohcount_=1000;
00214   unsigned int pllcount=1;
00215 
00216   for(unsigned int c = 0 ; c < tableMat[0].size() ; c++)
00217     {
00218       for(unsigned int n=0; n<colNames.size(); n++)
00219         {
00220           if(tableMat[0][c] == colNames[n]){
00221             colM[colNames[n]] = c;
00222             break;
00223           }
00224         }
00225     }//end for
00226   for(unsigned int n=0; n<colNames.size(); n++)
00227     {
00228       if(colM.find(colNames[n]) == colM.end())
00229         {
00230           std::cerr << __LINE__ << mthn << "\tCouldn't find in the database the column with name " << colNames[n] << std::endl;
00231           assert(0);
00232         }
00233     }
00234 
00235   portcardname_ = tableMat[1][colM["PORT_CARD"]] ;
00236 //  cout << __LINE__ << mthn << "Loading PortCard " << portcardname_ << endl ;
00237   if(portcardname_.find("FPix") != std::string::npos)
00238     {
00239       type_ = "fpix" ;
00240     }
00241   else if(portcardname_.find("BPix") != std::string::npos)
00242     {
00243       type_ = "bpix" ;
00244     }
00245   fillNameToAddress();
00246   fillDBToFileAddress() ;
00247   
00248   TKFECID_        =      tableMat[1][colM["TRKFEC"]]              ;
00249   ringAddress_    = atoi(tableMat[1][colM["RING"]].c_str()    ) ;
00250   ccuAddress_     = atoi(tableMat[1][colM["CCU_ADDR"]].c_str()     ) ;
00251   channelAddress_ = atoi(tableMat[1][colM["CHANNEL"]].c_str() ) ;
00252   i2cSpeed_       = atoi(tableMat[1][colM["I2C_SPEED"]].c_str()       ) ;
00253 /*
00254   cout << __LINE__ << "]\t" << mthn << 
00255     "ringAddress_\t"    << ringAddress_     << endl <<
00256     "ccuAddress_\t"     << ccuAddress_      << endl <<
00257     "channelAddress_\t" << channelAddress_  << endl <<
00258     "i2cSpeed_\t"       << i2cSpeed_        << endl ;
00259  */ 
00260 
00261 
00262   for(unsigned int col = 0 ; col < tableMat[1].size() ; col++)    //Goes to every column of the Matrix
00263     {
00264       std::string settingName;
00265       unsigned int i2c_address;
00266       unsigned int i2c_values;
00267       
00268       settingName = tableMat[0][col] ;
00269       i2c_values  = atoi(tableMat[1][col].c_str()) ;
00270       
00271       // Special handling for AOHX_GainY
00272       if( type_ == "fpix" && settingName.find("AOH_") != string::npos && settingName.find("GAIN") != string::npos // contains both "AOH_" and "Gain"
00273                && settingName.find("123") == string::npos && settingName.find("456") == string::npos ) // does not contain "123" or "456"
00274         {
00275           setDataBaseAOHGain(settingName, i2c_values);
00276           //      cout << __LINE__ << "]\t" << mthn << "Setting " << settingName << "\tto value " << std::hex << i2c_values << std::dec << std::endl ;
00277         }
00278       else if(type_ == "bpix" && settingName.find("AOH") != string::npos && settingName.find("GAIN") != string::npos // contains both "AOH" and "Gain"
00279               && settingName.find("AOH_") == string::npos                                                            // must not contain AOH_ 'cause this is for forward
00280               && settingName.find("123")  == string::npos && settingName.find("456") == string::npos )               // does not contain "123" or "456"
00281         {
00282           if(portcardname_.find("PRT2")!=std::string::npos  && 
00283              (settingName.find("AOH3_")!=std::string::npos   ||              
00284              settingName.find("AOH4_")!=std::string::npos ) ) continue ;
00285           setDataBaseAOHGain(settingName, i2c_values);
00286           //      cout << __LINE__ << "]\t" << mthn << "Setting " << settingName << "\tto value " << std::hex << i2c_values << std::dec << std::endl ;
00287         }
00288       // FIXMR
00289        else if ( settingName == k_PLL_CTR5 ) // special handling
00290        {
00291           unsigned int last_CTR2 = 0x0;
00292           if ( containsSetting(k_PLL_CTR2) ) last_CTR2 = getdeviceValuesForSetting( k_PLL_CTR2 );
00293         
00294           device_.push_back( make_pair(getdeviceAddressForSetting(k_PLL_CTR2), new_PLL_CTR2_value(settingName, last_CTR2)) );
00295           device_.push_back( make_pair(getdeviceAddressForSetting(k_PLL_CTR4or5), i2c_values) );
00296 
00297           key_.push_back( pllcount++); //these are arbitrary integers that control the sort order
00298           key_.push_back(pllcount++);
00299        }
00300       // FIXMR
00301       else // no special handling for this name
00302         {
00303           if((settingName.find("DELAY25_") != std::string::npos) || 
00304              (settingName.find("_BIAS") != std::string::npos) || 
00305              (settingName.find("PLL_CTR2") != std::string::npos) ||
00306              (settingName.find("PLL_CTR5") != std::string::npos)  ||
00307              ((settingName.find("DOH_SEU_GAIN") != std::string::npos) && type_=="bpix")) 
00308             //Note that DOH_SEU_GAIN will be *ignored* for fpix
00309             {
00310               map<string,string>::iterator iter = nameDBtoFileConversion_.find(settingName);
00311               if(iter == nameDBtoFileConversion_.end()) continue ;
00312               map<string, unsigned int>::iterator foundName_itr = nameToAddress_.find(nameDBtoFileConversion_[settingName]);
00313               
00314               if ( foundName_itr != nameToAddress_.end() )
00315                 {
00316                   if(portcardname_.find("PRT2")!=std::string::npos  && 
00317                      (settingName.find("AOH3_")!=std::string::npos   ||              
00318                       settingName.find("AOH4_")!=std::string::npos )) continue ;                     
00319                   i2c_address = foundName_itr->second;
00320                 }
00321               else
00322                 {
00323                   i2c_address = strtoul(settingName.c_str(), 0, 16); // convert string to integer using base 16
00324                 }
00325               if(type_ == "fpix"  && 
00326                  (
00327                   settingName.find("AOH1_")!=std::string::npos   ||                  
00328                   settingName.find("AOH2_")!=std::string::npos   ||
00329                   settingName.find("AOH3_")!=std::string::npos   ||  
00330                   settingName.find("AOH4_")!=std::string::npos   
00331                   )
00332                  ) continue ;
00333               
00334               pair<unsigned int, unsigned int> p(i2c_address, i2c_values);
00335               /*
00336               cout << __LINE__ 
00337                    << mthn << "Setting\t" 
00338                    << "|"
00339                    << settingName
00340                    << "|->"
00341                    << nameDBtoFileConversion_[settingName] 
00342                    << "\twith pair:\t(" 
00343                    << i2c_address
00344                    << ","
00345                    << i2c_values
00346                    << ")"
00347                    << endl ;
00348               */
00349               device_.push_back(p);
00350               if (settingName.find("AOH")!=string::npos)      key_.push_back(aohcount_++);
00351               else if (settingName.find("Delay25")!=string::npos) key_.push_back(delay25count++);
00352               else if (settingName.find("PLL")!=string::npos) key_.push_back(pllcount++);
00353               else key_.push_back(othercount++);
00354 
00355             }
00356         }
00357     } // End of table columns
00358 
00359 
00360   sortDeviceList();
00361 
00362 }
00363 
00364 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00365 PixelPortCardConfig::PixelPortCardConfig(std::string filename):
00366   PixelConfigBase(" "," "," "){
00367 
00368   string mthn = "[PixelPortCardConfig::PixelPortCardConfig()]\t\t    " ;
00369   //std::cout << __LINE__ << "]\t" << mthn << "filename:"<<filename<<std::endl;
00370 
00371   size_t portcardpos=filename.find(std::string("portcard_"));
00372   //std::cout << __LINE__ << "]\t" << mthn << "portcardpos:"<<portcardpos<<std::endl;
00373   assert(portcardpos!=std::string::npos);
00374   size_t datpos=filename.find(std::string(".dat"));
00375   //std::cout << __LINE__ << "]\t" << mthn << "datpos:"<<datpos<<std::endl;
00376   assert(datpos!=std::string::npos);
00377   assert(datpos>portcardpos);
00378   
00379   portcardname_=filename.substr(portcardpos+9,datpos-portcardpos-9);
00380 
00381   //std::cout << "Portcard name extracted from file name:"<<portcardname_<<std::endl;
00382 
00383   std::ifstream in(filename.c_str());
00384   
00385   if(!in.good()){
00386     std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
00387     throw std::runtime_error("Failed to open file "+filename);
00388   }
00389   else {
00390     std::cout << __LINE__ << "]\t" << mthn << "Opened: "         << filename << std::endl;
00391   }
00392   
00393   string dummy;
00394 
00395   in >> dummy;
00396   if ( dummy == "Name:" ) // check that port card name matches the file name
00397   {
00398     in >> dummy; assert( dummy==portcardname_ );
00399     in >> dummy;
00400   }
00401   if ( dummy == "Type:" ) // read in the type, defaulting to "fpix" if not specified
00402   {
00403     in >> type_;
00404     assert( type_ == "fpix" || type_ == "bpix" );
00405     in >> dummy;
00406   }
00407   else
00408   {
00409     type_ = "fpix";
00410   }
00411   fillNameToAddress();
00412   fillDBToFileAddress() ;
00413   assert(dummy=="TKFECID:");        in >> TKFECID_;
00414   in >> dummy; assert(dummy=="ringAddress:");    in >> std::hex >> ringAddress_;
00415   in >> dummy; assert(dummy=="ccuAddress:");     in >> std::hex >> ccuAddress_;
00416   in >> dummy; assert(dummy=="channelAddress:"); in >> std::hex >> channelAddress_;
00417   in >> dummy; assert(dummy=="i2cSpeed:");       in >> std::hex >> i2cSpeed_;
00418     
00419   //std::cout << __LINE__ << "]\t" << mthn 
00420   //          <<TKFECAddress_<<", "<<ringAddress_<<", "<<ccuAddress_<<", "<<channelAddress_<<", "<<i2cSpeed_<<std::endl;
00421   
00422   assert( nameToAddress_.size() != 0 );
00423   do {
00424       
00425     std::string settingName;
00426     unsigned int i2c_address;
00427     unsigned int i2c_values;
00428     
00429     in >> settingName >> std::hex >> i2c_values >> std::dec;
00430     if (in.eof()) break;
00431     
00432     if ( settingName[settingName.size()-1] == ':' ) settingName.resize( settingName.size()-1 ); // remove ':' from end of string, if it's there
00433     
00434     // Special handling for AOHX_GainY
00435     if ( settingName.find("AOH") != string::npos && settingName.find("Gain") != string::npos // contains both "AOH" and "Gain"
00436       && settingName.find("123") == string::npos && settingName.find("456") == string::npos ) // does not contain "123" or "456"
00437     {
00438         setAOHGain(settingName, i2c_values);
00439     }
00440     else if ( settingName == k_PLL_CTR4 || settingName == k_PLL_CTR5 ) // special handling
00441     {
00442         unsigned int last_CTR2 = 0x0;
00443         if ( containsSetting(k_PLL_CTR2) ) last_CTR2 = getdeviceValuesForSetting( k_PLL_CTR2 );
00444         
00445         device_.push_back( make_pair(getdeviceAddressForSetting(k_PLL_CTR2), new_PLL_CTR2_value(settingName, last_CTR2)) );
00446         device_.push_back( make_pair(getdeviceAddressForSetting(k_PLL_CTR4or5), i2c_values) );
00447     }
00448     else // no special handling for this name
00449     {
00450         std::map<std::string, unsigned int>::iterator foundName_itr = nameToAddress_.find(settingName);
00451     
00452         if ( foundName_itr != nameToAddress_.end() )
00453         {
00454                 i2c_address = foundName_itr->second;
00455         }
00456         else
00457         {
00458                 i2c_address = strtoul(settingName.c_str(), 0, 16); // convert string to integer using base 16
00459         }
00460         pair<unsigned int, unsigned int> p(i2c_address, i2c_values);
00461         device_.push_back(p);
00462     }
00463   }
00464   while (!in.eof());
00465   
00466   in.close();
00467 
00468 }
00469 
00470 void PixelPortCardConfig::sortDeviceList() {
00471 
00472   std::set < pair < unsigned int,  pair <unsigned int, unsigned int> > > sorted;
00473 
00474   for (unsigned int i=0; i<device_.size(); i++ ) {
00475     //cout<<key_.at(i)<<"\t"<<device_.at(i).first<<"  "<<device_.at(i).second<<endl;    
00476     sorted.insert( make_pair(key_.at(i) , device_.at(i) ));
00477   }
00478 
00479 //  cout<<" -=-=-=-= done with sorting -=-=-="<<endl;
00480   device_.clear();
00481   for ( set < pair < unsigned int, pair <unsigned int, unsigned int> > >::iterator i=sorted.begin() ; i!=sorted.end() ; ++i) {
00482     device_.push_back(i->second);
00483   }
00484 
00485   //  for (unsigned int i=0; i<device_.size(); i++ ) {
00486   //    cout<<"  \t"<<device_.at(i).first<<"  "<<device_.at(i).second<<endl;    
00487   //  }
00488   
00489 }
00490 
00491 
00492 unsigned int PixelPortCardConfig::new_PLL_CTR2_value(std::string CTR4or5, unsigned int last_CTR2) const
00493 {
00494         if      ( CTR4or5 == k_PLL_CTR4 ) return 0xdf & last_CTR2;
00495         else if ( CTR4or5 == k_PLL_CTR5 ) return 0x20 | last_CTR2;
00496         else assert(0);
00497 }
00498 
00499 void PixelPortCardConfig::setAOHGain(std::string settingName, unsigned int value)
00500 {
00501         assert( settingName.find("AOH") != string::npos && settingName.find("Gain") != string::npos // contains both "AOH" and "Gain"
00502         && settingName.find("123") == string::npos && settingName.find("456") == string::npos ); // does not contain "123" or "456"
00503         
00504         unsigned int i2c_address;
00505         
00506         // Get the i2c address of this AOH, and the channel on the AOH.
00507         string::size_type GainPosition = settingName.find("Gain");
00508         unsigned int whichAOH;
00509         if ( settingName[GainPosition-2] == 'H' ) whichAOH = 0; // fpix
00510         else  // bpix
00511         {
00512                 char whichAOHDigit[2]={0,0};
00513                 whichAOHDigit[0]=settingName[GainPosition-2];
00514                 whichAOH = atoi( whichAOHDigit );
00515         }
00516         char digit[2]={0,0};
00517         digit[0]=settingName[GainPosition+4];
00518         unsigned int channelOnAOH = atoi( digit );
00519         assert( (type_=="fpix" && whichAOH==0)||(type_=="bpix" && 1 <= whichAOH&&whichAOH <= 4) );
00520         assert( 1 <= channelOnAOH && channelOnAOH <= 6 );
00521         
00522         if      ( whichAOH == 0 && channelOnAOH <= 3 ) i2c_address = k_fpix_AOH_Gain123_address;
00523         else if ( whichAOH == 0 && channelOnAOH >= 4 ) i2c_address = k_fpix_AOH_Gain456_address;
00524         else if ( whichAOH == 1 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH1_Gain123_address;
00525         else if ( whichAOH == 1 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH1_Gain456_address;
00526         else if ( whichAOH == 2 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH2_Gain123_address;
00527         else if ( whichAOH == 2 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH2_Gain456_address;
00528         else if ( whichAOH == 3 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH3_Gain123_address;
00529         else if ( whichAOH == 3 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH3_Gain456_address;
00530         else if ( whichAOH == 4 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH4_Gain123_address;
00531         else if ( whichAOH == 4 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH4_Gain456_address;
00532         else assert(0);
00533         
00534         // Search for this address in the previously-defined settings.
00535         bool foundOne = false;
00536         for (unsigned int i=0;i<device_.size();i++)
00537         {
00538                 if ( device_[i].first == i2c_address ) // Change this setting in all previous instances
00539                 {
00540                         foundOne = true;
00541                         unsigned int oldValue = device_[i].second;
00542                         if      ( channelOnAOH%3 == 1 )
00543                                 device_[i].second = (0x3c & oldValue) + ((value & 0x3)<<0); // replace bits 0 and 1 with value
00544                         else if ( channelOnAOH%3 == 2 )
00545                                 device_[i].second = (0x33 & oldValue) + ((value & 0x3)<<2); // replace bits 2 and 3 with value
00546                         else if ( channelOnAOH%3 == 0 )
00547                                 device_[i].second = (0x0f & oldValue) + ((value & 0x3)<<4); // replace bits 4 and 5 with value
00548                         else assert(0);
00549                         //std::cout << "Changed setting "<< k_fpix_AOH_Gain123 <<"(address 0x"<<std::hex<<k_fpix_AOH_Gain123_address<<") from 0x"<<oldValue<<" to 0x"<< device_[i].second << std::dec <<"\n";
00550                 }
00551         }
00552         if ( foundOne ) return;
00553         else // If this was not set previously, add this setting with the other two gains set to zero.
00554         {
00555                 unsigned int i2c_value;
00556                 if      ( channelOnAOH%3 == 1 ) i2c_value  = ((value & 0x3)<<0);
00557                 else if ( channelOnAOH%3 == 2 ) i2c_value  = ((value & 0x3)<<2);
00558                 else if ( channelOnAOH%3 == 0 ) i2c_value  = ((value & 0x3)<<4);
00559                 else assert(0);
00560                 
00561                 pair<unsigned int, unsigned int> p(i2c_address, i2c_value);
00562                 device_.push_back(p);
00563                 return;
00564         }
00565 }
00566 
00567 void PixelPortCardConfig::setDataBaseAOHGain(std::string settingName, unsigned int value)
00568 {
00569         unsigned int i2c_address;
00570         
00571         // Get the i2c address of this AOH, and the channel on the AOH.
00572         string::size_type GainPosition = settingName.find("GAIN");
00573         unsigned int whichAOH;
00574         if(type_ == "fpix")
00575           {
00576             whichAOH = 0 ; // fpix
00577           }
00578         else  // bpix
00579         {
00580                 char whichAOHDigit[2]={0,0};
00581                 whichAOHDigit[0]=settingName[GainPosition-2];
00582                 whichAOH = atoi( whichAOHDigit );
00583         }
00584         char digit[2]={0,0};
00585         digit[0]=settingName[GainPosition+4];
00586         unsigned int channelOnAOH = atoi( digit );
00587         assert( (type_=="fpix" && whichAOH==0)||(type_=="bpix" && 1 <= whichAOH&&whichAOH <= 4) );
00588         assert( 1 <= channelOnAOH && channelOnAOH <= 6 );
00589         
00590         if      ( whichAOH == 0 && channelOnAOH <= 3 ) i2c_address = k_fpix_AOH_Gain123_address;
00591         else if ( whichAOH == 0 && channelOnAOH >= 4 ) i2c_address = k_fpix_AOH_Gain456_address;
00592         else if ( whichAOH == 1 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH1_Gain123_address;
00593         else if ( whichAOH == 1 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH1_Gain456_address;
00594         else if ( whichAOH == 2 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH2_Gain123_address;
00595         else if ( whichAOH == 2 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH2_Gain456_address;
00596         else if ( whichAOH == 3 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH3_Gain123_address;
00597         else if ( whichAOH == 3 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH3_Gain456_address;
00598         else if ( whichAOH == 4 && channelOnAOH <= 3 ) i2c_address = k_bpix_AOH4_Gain123_address;
00599         else if ( whichAOH == 4 && channelOnAOH >= 4 ) i2c_address = k_bpix_AOH4_Gain456_address;
00600         else assert(0);
00601         
00602         // Search for this address in the previously-defined settings.
00603         bool foundOne = false;
00604         for (unsigned int i=0;i<device_.size();i++)
00605         {
00606                 if ( device_[i].first == i2c_address ) // Change this setting in all previous instances
00607                 {
00608                         foundOne = true;
00609                         unsigned int oldValue = device_[i].second;
00610                         if      ( channelOnAOH%3 == 1 )
00611                                 device_[i].second = (0x3c & oldValue) + ((value & 0x3)<<0); // replace bits 0 and 1 with value
00612                         else if ( channelOnAOH%3 == 2 )
00613                                 device_[i].second = (0x33 & oldValue) + ((value & 0x3)<<2); // replace bits 2 and 3 with value
00614                         else if ( channelOnAOH%3 == 0 )
00615                                 device_[i].second = (0x0f & oldValue) + ((value & 0x3)<<4); // replace bits 4 and 5 with value
00616                         else assert(0);
00617                         //std::cout << "Changed setting "<< k_fpix_AOH_Gain123 <<"(address 0x"<<std::hex<<k_fpix_AOH_Gain123_address<<") from 0x"<<oldValue<<" to 0x"<< device_[i].second << std::dec <<"\n";
00618                 }
00619         }
00620         if ( foundOne ) return;
00621         else // If this was not set previously, add this setting with the other two gains set to zero.
00622         {
00623                 unsigned int i2c_value;
00624                 if      ( channelOnAOH%3 == 1 ) i2c_value  = ((value & 0x3)<<0);
00625                 else if ( channelOnAOH%3 == 2 ) i2c_value  = ((value & 0x3)<<2);
00626                 else if ( channelOnAOH%3 == 0 ) i2c_value  = ((value & 0x3)<<4);
00627                 else assert(0);
00628                 
00629                 pair<unsigned int, unsigned int> p(i2c_address, i2c_value);
00630                 device_.push_back(p);
00631                 key_.push_back(aohcount_++);
00632                 return;
00633         }
00634 }
00635 
00636 void PixelPortCardConfig::fillNameToAddress()
00637 {
00638         if ( nameToAddress_.size() != 0 ) return;
00639         
00640         if ( type_ == "fpix" )
00641         {
00642                 nameToAddress_[PortCardSettingNames::k_AOH_Bias1] = PortCardSettingNames::k_fpix_AOH_Bias1_address;
00643                 nameToAddress_[PortCardSettingNames::k_AOH_Bias2] = PortCardSettingNames::k_fpix_AOH_Bias2_address;
00644                 nameToAddress_[PortCardSettingNames::k_AOH_Bias3] = PortCardSettingNames::k_fpix_AOH_Bias3_address;
00645                 nameToAddress_[PortCardSettingNames::k_AOH_Bias4] = PortCardSettingNames::k_fpix_AOH_Bias4_address;
00646                 nameToAddress_[PortCardSettingNames::k_AOH_Bias5] = PortCardSettingNames::k_fpix_AOH_Bias5_address;
00647                 nameToAddress_[PortCardSettingNames::k_AOH_Bias6] = PortCardSettingNames::k_fpix_AOH_Bias6_address;
00648                 nameToAddress_[PortCardSettingNames::k_AOH_Gain123] = PortCardSettingNames::k_fpix_AOH_Gain123_address;
00649                 nameToAddress_[PortCardSettingNames::k_AOH_Gain456] = PortCardSettingNames::k_fpix_AOH_Gain456_address;
00650                 
00651                 nameToAddress_[PortCardSettingNames::k_PLL_CTR1] = PortCardSettingNames::k_fpix_PLL_CTR1_address;
00652                 nameToAddress_[PortCardSettingNames::k_PLL_CTR2] = PortCardSettingNames::k_fpix_PLL_CTR2_address;
00653                 nameToAddress_[PortCardSettingNames::k_PLL_CTR3] = PortCardSettingNames::k_fpix_PLL_CTR3_address;
00654                 nameToAddress_[PortCardSettingNames::k_PLL_CTR4or5] = PortCardSettingNames::k_fpix_PLL_CTR4or5_address;
00655                 
00656                 nameToAddress_[PortCardSettingNames::k_Delay25_RDA] = PortCardSettingNames::k_fpix_Delay25_RDA_address;
00657                 nameToAddress_[PortCardSettingNames::k_Delay25_RCL] = PortCardSettingNames::k_fpix_Delay25_RCL_address;
00658                 nameToAddress_[PortCardSettingNames::k_Delay25_SDA] = PortCardSettingNames::k_fpix_Delay25_SDA_address;
00659                 nameToAddress_[PortCardSettingNames::k_Delay25_TRG] = PortCardSettingNames::k_fpix_Delay25_TRG_address;
00660                 nameToAddress_[PortCardSettingNames::k_Delay25_SCL] = PortCardSettingNames::k_fpix_Delay25_SCL_address;
00661                 nameToAddress_[PortCardSettingNames::k_Delay25_GCR] = PortCardSettingNames::k_fpix_Delay25_GCR_address;
00662                 
00663                 nameToAddress_[PortCardSettingNames::k_DOH_Ch0Bias_CLK]  = PortCardSettingNames::k_fpix_DOH_Ch0Bias_CLK_address;
00664                 nameToAddress_[PortCardSettingNames::k_DOH_Dummy]        = PortCardSettingNames::k_fpix_DOH_Dummy_address;
00665                 nameToAddress_[PortCardSettingNames::k_DOH_Ch1Bias_Data] = PortCardSettingNames::k_fpix_DOH_Ch1Bias_Data_address;
00666                 nameToAddress_[PortCardSettingNames::k_DOH_Gain_SEU]     = PortCardSettingNames::k_fpix_DOH_Gain_SEU_address;
00667         }
00668         else if ( type_ == "bpix" )
00669         {
00670                 nameToAddress_[PortCardSettingNames::k_AOH1_Bias1] = PortCardSettingNames::k_bpix_AOH1_Bias1_address;
00671                 nameToAddress_[PortCardSettingNames::k_AOH1_Bias2] = PortCardSettingNames::k_bpix_AOH1_Bias2_address;
00672                 nameToAddress_[PortCardSettingNames::k_AOH1_Bias3] = PortCardSettingNames::k_bpix_AOH1_Bias3_address;
00673                 nameToAddress_[PortCardSettingNames::k_AOH1_Bias4] = PortCardSettingNames::k_bpix_AOH1_Bias4_address;
00674                 nameToAddress_[PortCardSettingNames::k_AOH1_Bias5] = PortCardSettingNames::k_bpix_AOH1_Bias5_address;
00675                 nameToAddress_[PortCardSettingNames::k_AOH1_Bias6] = PortCardSettingNames::k_bpix_AOH1_Bias6_address;
00676                 nameToAddress_[PortCardSettingNames::k_AOH1_Gain123] = PortCardSettingNames::k_bpix_AOH1_Gain123_address;
00677                 nameToAddress_[PortCardSettingNames::k_AOH1_Gain456] = PortCardSettingNames::k_bpix_AOH1_Gain456_address;
00678                 
00679                 nameToAddress_[PortCardSettingNames::k_AOH2_Bias1] = PortCardSettingNames::k_bpix_AOH2_Bias1_address;
00680                 nameToAddress_[PortCardSettingNames::k_AOH2_Bias2] = PortCardSettingNames::k_bpix_AOH2_Bias2_address;
00681                 nameToAddress_[PortCardSettingNames::k_AOH2_Bias3] = PortCardSettingNames::k_bpix_AOH2_Bias3_address;
00682                 nameToAddress_[PortCardSettingNames::k_AOH2_Bias4] = PortCardSettingNames::k_bpix_AOH2_Bias4_address;
00683                 nameToAddress_[PortCardSettingNames::k_AOH2_Bias5] = PortCardSettingNames::k_bpix_AOH2_Bias5_address;
00684                 nameToAddress_[PortCardSettingNames::k_AOH2_Bias6] = PortCardSettingNames::k_bpix_AOH2_Bias6_address;
00685                 nameToAddress_[PortCardSettingNames::k_AOH2_Gain123] = PortCardSettingNames::k_bpix_AOH2_Gain123_address;
00686                 nameToAddress_[PortCardSettingNames::k_AOH2_Gain456] = PortCardSettingNames::k_bpix_AOH2_Gain456_address;
00687                 
00688                 nameToAddress_[PortCardSettingNames::k_AOH3_Bias1] = PortCardSettingNames::k_bpix_AOH3_Bias1_address;
00689                 nameToAddress_[PortCardSettingNames::k_AOH3_Bias2] = PortCardSettingNames::k_bpix_AOH3_Bias2_address;
00690                 nameToAddress_[PortCardSettingNames::k_AOH3_Bias3] = PortCardSettingNames::k_bpix_AOH3_Bias3_address;
00691                 nameToAddress_[PortCardSettingNames::k_AOH3_Bias4] = PortCardSettingNames::k_bpix_AOH3_Bias4_address;
00692                 nameToAddress_[PortCardSettingNames::k_AOH3_Bias5] = PortCardSettingNames::k_bpix_AOH3_Bias5_address;
00693                 nameToAddress_[PortCardSettingNames::k_AOH3_Bias6] = PortCardSettingNames::k_bpix_AOH3_Bias6_address;
00694                 nameToAddress_[PortCardSettingNames::k_AOH3_Gain123] = PortCardSettingNames::k_bpix_AOH3_Gain123_address;
00695                 nameToAddress_[PortCardSettingNames::k_AOH3_Gain456] = PortCardSettingNames::k_bpix_AOH3_Gain456_address;
00696                 
00697                 nameToAddress_[PortCardSettingNames::k_AOH4_Bias1] = PortCardSettingNames::k_bpix_AOH4_Bias1_address;
00698                 nameToAddress_[PortCardSettingNames::k_AOH4_Bias2] = PortCardSettingNames::k_bpix_AOH4_Bias2_address;
00699                 nameToAddress_[PortCardSettingNames::k_AOH4_Bias3] = PortCardSettingNames::k_bpix_AOH4_Bias3_address;
00700                 nameToAddress_[PortCardSettingNames::k_AOH4_Bias4] = PortCardSettingNames::k_bpix_AOH4_Bias4_address;
00701                 nameToAddress_[PortCardSettingNames::k_AOH4_Bias5] = PortCardSettingNames::k_bpix_AOH4_Bias5_address;
00702                 nameToAddress_[PortCardSettingNames::k_AOH4_Bias6] = PortCardSettingNames::k_bpix_AOH4_Bias6_address;
00703                 nameToAddress_[PortCardSettingNames::k_AOH4_Gain123] = PortCardSettingNames::k_bpix_AOH4_Gain123_address;
00704                 nameToAddress_[PortCardSettingNames::k_AOH4_Gain456] = PortCardSettingNames::k_bpix_AOH4_Gain456_address;
00705                 
00706                 nameToAddress_[PortCardSettingNames::k_PLL_CTR1] = PortCardSettingNames::k_bpix_PLL_CTR1_address;
00707                 nameToAddress_[PortCardSettingNames::k_PLL_CTR2] = PortCardSettingNames::k_bpix_PLL_CTR2_address;
00708                 nameToAddress_[PortCardSettingNames::k_PLL_CTR3] = PortCardSettingNames::k_bpix_PLL_CTR3_address;
00709                 nameToAddress_[PortCardSettingNames::k_PLL_CTR4or5] = PortCardSettingNames::k_bpix_PLL_CTR4or5_address;
00710                 
00711                 nameToAddress_[PortCardSettingNames::k_Delay25_RDA] = PortCardSettingNames::k_bpix_Delay25_RDA_address;
00712                 nameToAddress_[PortCardSettingNames::k_Delay25_RCL] = PortCardSettingNames::k_bpix_Delay25_RCL_address;
00713                 nameToAddress_[PortCardSettingNames::k_Delay25_SDA] = PortCardSettingNames::k_bpix_Delay25_SDA_address;
00714                 nameToAddress_[PortCardSettingNames::k_Delay25_TRG] = PortCardSettingNames::k_bpix_Delay25_TRG_address;
00715                 nameToAddress_[PortCardSettingNames::k_Delay25_SCL] = PortCardSettingNames::k_bpix_Delay25_SCL_address;
00716                 nameToAddress_[PortCardSettingNames::k_Delay25_GCR] = PortCardSettingNames::k_bpix_Delay25_GCR_address;
00717                 
00718                 nameToAddress_[PortCardSettingNames::k_DOH_Ch0Bias_CLK]  = PortCardSettingNames::k_bpix_DOH_Ch0Bias_CLK_address;
00719                 nameToAddress_[PortCardSettingNames::k_DOH_Dummy]        = PortCardSettingNames::k_bpix_DOH_Dummy_address;
00720                 nameToAddress_[PortCardSettingNames::k_DOH_Ch1Bias_Data] = PortCardSettingNames::k_bpix_DOH_Ch1Bias_Data_address;
00721                 nameToAddress_[PortCardSettingNames::k_DOH_Gain_SEU]     = PortCardSettingNames::k_bpix_DOH_Gain_SEU_address;
00722         }
00723         else assert(0);
00724         
00725         return;
00726 }
00727 
00728 void PixelPortCardConfig::fillDBToFileAddress()
00729 {
00730   if(type_ == "fpix")
00731     {
00732       //   nameDBtoFileConversion_["CONFIG_KEY_ID"         ] = ;
00733       //   nameDBtoFileConversion_["CONFIG_KEY"            ] = ;
00734       //   nameDBtoFileConversion_["VERSION"               ] = ;
00735       //   nameDBtoFileConversion_["CONDITION_DATA_SET_ID" ] = ;
00736       //   nameDBtoFileConversion_["KIND_OF_CONDITION_ID"  ] = ;
00737       //   nameDBtoFileConversion_["KIND_OF_COND"          ] = ;
00738       //   nameDBtoFileConversion_["SERIAL_NUMBER"         ] = ;
00739       //   nameDBtoFileConversion_["PORT_CARD_ID"          ] = ;
00740       //   nameDBtoFileConversion_["PORT_CARD"             ] = ;
00741       //   nameDBtoFileConversion_["TRKFEC_NAME"           ] = ;
00742       //   nameDBtoFileConversion_["RINGADDRESS"           ] = ;
00743       //   nameDBtoFileConversion_["CHANNELADDRESS"        ] = ;
00744       //   nameDBtoFileConversion_["CCUADDRESS"            ] = ;
00745       //   nameDBtoFileConversion_["I2C_CNTRL"             ] = ;
00746       //   nameDBtoFileConversion_["I2CSPEED"              ] = ;
00747       nameDBtoFileConversion_["AOH_BIAS1"                 ] = k_AOH_Bias1  ;
00748       nameDBtoFileConversion_["AOH_BIAS2"                 ] = k_AOH_Bias2  ;
00749       nameDBtoFileConversion_["AOH_BIAS3"                 ] = k_AOH_Bias3  ;
00750       nameDBtoFileConversion_["AOH_BIAS4"                 ] = k_AOH_Bias4  ;
00751       nameDBtoFileConversion_["AOH_BIAS5"                 ] = k_AOH_Bias5  ;
00752       nameDBtoFileConversion_["AOH_BIAS6"                 ] = k_AOH_Bias6  ;
00753       nameDBtoFileConversion_["AOH1_BIAS1"                ] = k_AOH1_Bias1 ;
00754       nameDBtoFileConversion_["AOH1_BIAS2"                ] = k_AOH1_Bias2 ;
00755       nameDBtoFileConversion_["AOH1_BIAS3"                ] = k_AOH1_Bias3 ;
00756       nameDBtoFileConversion_["AOH1_BIAS4"                ] = k_AOH1_Bias4 ;
00757       nameDBtoFileConversion_["AOH1_BIAS5"                ] = k_AOH1_Bias5 ;
00758       nameDBtoFileConversion_["AOH1_BIAS6"                ] = k_AOH1_Bias6 ;
00759 //       nameDBtoFileConversion_["AOH1_GAIN1"             ] = ;
00760 //       nameDBtoFileConversion_["AOH1_GAIN2"             ] = ;
00761 //       nameDBtoFileConversion_["AOH1_GAIN3"             ] = ;
00762 //       nameDBtoFileConversion_["AOH1_GAIN4"             ] = ;
00763 //       nameDBtoFileConversion_["AOH1_GAIN5"             ] = ;
00764 //       nameDBtoFileConversion_["AOH1_GAIN6"             ] = ;
00765       nameDBtoFileConversion_["AOH2_BIAS1"                ] = k_AOH2_Bias1 ;
00766       nameDBtoFileConversion_["AOH2_BIAS2"                ] = k_AOH2_Bias2 ;
00767       nameDBtoFileConversion_["AOH2_BIAS3"                ] = k_AOH2_Bias3 ;
00768       nameDBtoFileConversion_["AOH2_BIAS4"                ] = k_AOH2_Bias4 ;
00769       nameDBtoFileConversion_["AOH2_BIAS5"                ] = k_AOH2_Bias5 ;
00770       nameDBtoFileConversion_["AOH2_BIAS6"                ] = k_AOH2_Bias6 ;
00771 //       nameDBtoFileConversion_["AOH2_GAIN1"             ] = ;
00772 //       nameDBtoFileConversion_["AOH2_GAIN2"             ] = ;
00773 //       nameDBtoFileConversion_["AOH2_GAIN3"             ] = ;
00774 //       nameDBtoFileConversion_["AOH2_GAIN4"             ] = ;
00775 //       nameDBtoFileConversion_["AOH2_GAIN5"             ] = ;
00776 //       nameDBtoFileConversion_["AOH2_GAIN6"             ] = ;
00777       nameDBtoFileConversion_["AOH3_BIAS1"                ] = k_AOH3_Bias1 ;
00778       nameDBtoFileConversion_["AOH3_BIAS2"                ] = k_AOH3_Bias2 ;
00779       nameDBtoFileConversion_["AOH3_BIAS3"                ] = k_AOH3_Bias3 ;
00780       nameDBtoFileConversion_["AOH3_BIAS4"                ] = k_AOH3_Bias4 ;
00781       nameDBtoFileConversion_["AOH3_BIAS5"                ] = k_AOH3_Bias5 ;
00782       nameDBtoFileConversion_["AOH3_BIAS6"                ] = k_AOH3_Bias6 ;
00783 //       nameDBtoFileConversion_["AOH3_GAIN1"             ] = ;
00784 //       nameDBtoFileConversion_["AOH3_GAIN2"             ] = ;
00785 //       nameDBtoFileConversion_["AOH3_GAIN3"             ] = ;
00786 //       nameDBtoFileConversion_["AOH3_GAIN4"             ] = ;
00787 //       nameDBtoFileConversion_["AOH3_GAIN5"             ] = ;
00788 //       nameDBtoFileConversion_["AOH3_GAIN6"             ] = ;
00789       nameDBtoFileConversion_["AOH4_BIAS1"                ] = k_AOH4_Bias1 ;
00790       nameDBtoFileConversion_["AOH4_BIAS2"                ] = k_AOH4_Bias2 ;
00791       nameDBtoFileConversion_["AOH4_BIAS3"                ] = k_AOH4_Bias3 ;
00792       nameDBtoFileConversion_["AOH4_BIAS4"                ] = k_AOH4_Bias4 ;
00793       nameDBtoFileConversion_["AOH4_BIAS5"                ] = k_AOH4_Bias5 ;
00794       nameDBtoFileConversion_["AOH4_BIAS6"                ] = k_AOH4_Bias6 ;
00795 //       nameDBtoFileConversion_["AOH4_GAIN1"             ] = ;
00796 //       nameDBtoFileConversion_["AOH4_GAIN2"             ] = ;
00797 //       nameDBtoFileConversion_["AOH4_GAIN3"             ] = ;
00798 //       nameDBtoFileConversion_["AOH4_GAIN4"             ] = ;
00799 //       nameDBtoFileConversion_["AOH4_GAIN5"             ] = ;
00800 //       nameDBtoFileConversion_["AOH4_GAIN6"             ] = ;
00801 //       nameDBtoFileConversion_["AOH5_BIAS1"             ] = ;
00802 //       nameDBtoFileConversion_["AOH5_BIAS2"             ] = ;
00803 //       nameDBtoFileConversion_["AOH5_BIAS3"             ] = ;
00804 //       nameDBtoFileConversion_["AOH5_BIAS4"             ] = ;
00805 //       nameDBtoFileConversion_["AOH5_BIAS5"             ] = ;
00806 //       nameDBtoFileConversion_["AOH5_BIAS6"             ] = ;
00807 //       nameDBtoFileConversion_["AOH5_GAIN1"             ] = ;
00808 //       nameDBtoFileConversion_["AOH5_GAIN2"             ] = ;
00809 //       nameDBtoFileConversion_["AOH5_GAIN3"             ] = ;
00810 //       nameDBtoFileConversion_["AOH5_GAIN4"             ] = ;
00811 //       nameDBtoFileConversion_["AOH5_GAIN5"             ] = ;
00812 //       nameDBtoFileConversion_["AOH5_GAIN6"             ] = ;
00813 //       nameDBtoFileConversion_["AOH6_BIAS1"             ] = ;
00814 //       nameDBtoFileConversion_["AOH6_BIAS2"             ] = ;
00815 //       nameDBtoFileConversion_["AOH6_BIAS3"             ] = ;
00816 //       nameDBtoFileConversion_["AOH6_BIAS4"             ] = ;
00817 //       nameDBtoFileConversion_["AOH6_BIAS5"             ] = ;
00818 //       nameDBtoFileConversion_["AOH6_BIAS6"             ] = ;
00819 //       nameDBtoFileConversion_["AOH6_GAIN1"             ] = ;
00820 //       nameDBtoFileConversion_["AOH6_GAIN2"             ] = ;
00821 //       nameDBtoFileConversion_["AOH6_GAIN3"             ] = ;
00822 //       nameDBtoFileConversion_["AOH6_GAIN4"             ] = ;
00823 //       nameDBtoFileConversion_["AOH6_GAIN5"             ] = ;
00824 //       nameDBtoFileConversion_["AOH6_GAIN6"             ] = ;
00825       nameDBtoFileConversion_["DELAY25_GCR"              ] = k_Delay25_GCR ;
00826       nameDBtoFileConversion_["DELAY25_SCL"              ] = k_Delay25_SCL ;
00827       nameDBtoFileConversion_["DELAY25_TRG"              ] = k_Delay25_TRG ;
00828       nameDBtoFileConversion_["DELAY25_SDA"              ] = k_Delay25_SDA ;
00829       nameDBtoFileConversion_["DELAY25_RCL"              ] = k_Delay25_RCL ;
00830       nameDBtoFileConversion_["DELAY25_RDA"              ] = k_Delay25_RDA ;
00831       //   nameDBtoFileConversion_["DEL3_GCR"              ] = ;
00832       //   nameDBtoFileConversion_["DEL3_SCL"              ] = ;
00833       //   nameDBtoFileConversion_["DEL3_TRG"              ] = ;
00834       //   nameDBtoFileConversion_["DEL3_SDA"              ] = ;
00835       //   nameDBtoFileConversion_["DEL3_RCL"              ] = ;
00836       //   nameDBtoFileConversion_["DEL3_RDA"              ] = ;
00837       nameDBtoFileConversion_["DOH_BIAS0"                ] = k_DOH_Ch0Bias_CLK  ;
00838       nameDBtoFileConversion_["DOH_BIAS1"                ] = k_DOH_Ch1Bias_Data ;
00839       nameDBtoFileConversion_["DOH_SEU_GAIN"             ] = k_DOH_Gain_SEU     ;
00840       //   nameDBtoFileConversion_["DOH3_BIAS0"            ] =  ;
00841       //   nameDBtoFileConversion_["DOH3_BIAS1"            ] =  ;
00842       //   nameDBtoFileConversion_["DOH3_SEU_GAIN"         ] =  ;
00843       nameDBtoFileConversion_["PLL_CTR1"                 ] = k_PLL_CTR1 ;
00844       nameDBtoFileConversion_["PLL_CTR2"                 ] = k_PLL_CTR2 ;
00845       nameDBtoFileConversion_["PLL_CTR3"                 ] = k_PLL_CTR3 ;
00846       nameDBtoFileConversion_["PLL_CTR4"                 ] = k_PLL_CTR4 ;
00847       nameDBtoFileConversion_["PLL_CTR5"                 ] = k_PLL_CTR5 ;
00848       //   nameDBtoFileConversion_["PLL3_CTR1"             ] = ;
00849       //   nameDBtoFileConversion_["PLL3_CTR2"             ] = ;
00850       //   nameDBtoFileConversion_["PLL3_CTR3"             ] = ;
00851       //   nameDBtoFileConversion_["PLL3_CTR4_5"           ] = ;
00852     }
00853   else if(type_ == "bpix")
00854     {
00855       //   nameDBtoFileConversion_["CONFIG_KEY_ID"         ] = ;
00856       //   nameDBtoFileConversion_["CONFIG_KEY"            ] = ;
00857       //   nameDBtoFileConversion_["VERSION"               ] = ;
00858       //   nameDBtoFileConversion_["CONDITION_DATA_SET_ID" ] = ;
00859       //   nameDBtoFileConversion_["KIND_OF_CONDITION_ID"  ] = ;
00860       //   nameDBtoFileConversion_["KIND_OF_COND"          ] = ;
00861       //   nameDBtoFileConversion_["SERIAL_NUMBER"         ] = ;
00862       //   nameDBtoFileConversion_["PORT_CARD_ID"          ] = ;
00863       //   nameDBtoFileConversion_["PORT_CARD"             ] = ;
00864       //   nameDBtoFileConversion_["TRKFEC_NAME"           ] = ;
00865       //   nameDBtoFileConversion_["RINGADDRESS"           ] = ;
00866       //   nameDBtoFileConversion_["CHANNELADDRESS"        ] = ;
00867       //   nameDBtoFileConversion_["CCUADDRESS"            ] = ;
00868       //   nameDBtoFileConversion_["I2C_CNTRL"             ] = ;
00869       //   nameDBtoFileConversion_["I2CSPEED"              ] = ;
00870       nameDBtoFileConversion_["AOH1_BIAS1"                ] = k_AOH1_Bias1 ;
00871       nameDBtoFileConversion_["AOH1_BIAS2"                ] = k_AOH1_Bias2 ;
00872       nameDBtoFileConversion_["AOH1_BIAS3"                ] = k_AOH1_Bias3 ;
00873       nameDBtoFileConversion_["AOH1_BIAS4"                ] = k_AOH1_Bias4 ;
00874       nameDBtoFileConversion_["AOH1_BIAS5"                ] = k_AOH1_Bias5 ;
00875       nameDBtoFileConversion_["AOH1_BIAS6"                ] = k_AOH1_Bias6 ;
00876       //   nameDBtoFileConversion_["AOH1_GAIN1"            ] = ;
00877       //   nameDBtoFileConversion_["AOH1_GAIN2"            ] = ;
00878       //   nameDBtoFileConversion_["AOH1_GAIN3"            ] = ;
00879       //   nameDBtoFileConversion_["AOH1_GAIN4"            ] = ;
00880       //   nameDBtoFileConversion_["AOH1_GAIN5"            ] = ;
00881       //   nameDBtoFileConversion_["AOH1_GAIN6"            ] = ;
00882       nameDBtoFileConversion_["AOH2_BIAS1"                ] = k_AOH2_Bias1 ;
00883       nameDBtoFileConversion_["AOH2_BIAS2"                ] = k_AOH2_Bias2 ;
00884       nameDBtoFileConversion_["AOH2_BIAS3"                ] = k_AOH2_Bias3 ;
00885       nameDBtoFileConversion_["AOH2_BIAS4"                ] = k_AOH2_Bias4 ;
00886       nameDBtoFileConversion_["AOH2_BIAS5"                ] = k_AOH2_Bias5 ;
00887       nameDBtoFileConversion_["AOH2_BIAS6"                ] = k_AOH2_Bias6 ;
00888       //   nameDBtoFileConversion_["AOH2_GAIN1"            ] = ;
00889       //   nameDBtoFileConversion_["AOH2_GAIN2"            ] = ;
00890       //   nameDBtoFileConversion_["AOH2_GAIN3"            ] = ;
00891       //   nameDBtoFileConversion_["AOH2_GAIN4"            ] = ;
00892       //   nameDBtoFileConversion_["AOH2_GAIN5"            ] = ;
00893       //   nameDBtoFileConversion_["AOH2_GAIN6"            ] = ;
00894       nameDBtoFileConversion_["AOH3_BIAS1"                ] = k_AOH3_Bias1 ;
00895       nameDBtoFileConversion_["AOH3_BIAS2"                ] = k_AOH3_Bias2 ;
00896       nameDBtoFileConversion_["AOH3_BIAS3"                ] = k_AOH3_Bias3 ;
00897       nameDBtoFileConversion_["AOH3_BIAS4"                ] = k_AOH3_Bias4 ;
00898       nameDBtoFileConversion_["AOH3_BIAS5"                ] = k_AOH3_Bias5 ;
00899       nameDBtoFileConversion_["AOH3_BIAS6"                ] = k_AOH3_Bias6 ;
00900       //   nameDBtoFileConversion_["AOH3_GAIN1"            ] = ;
00901       //   nameDBtoFileConversion_["AOH3_GAIN2"            ] = ;
00902       //   nameDBtoFileConversion_["AOH3_GAIN3"            ] = ;
00903       //   nameDBtoFileConversion_["AOH3_GAIN4"            ] = ;
00904       //   nameDBtoFileConversion_["AOH3_GAIN5"            ] = ;
00905       //   nameDBtoFileConversion_["AOH3_GAIN6"            ] = ;
00906       nameDBtoFileConversion_["AOH4_BIAS1"                ] = k_AOH4_Bias1 ;
00907       nameDBtoFileConversion_["AOH4_BIAS2"                ] = k_AOH4_Bias2 ;
00908       nameDBtoFileConversion_["AOH4_BIAS3"                ] = k_AOH4_Bias3 ;
00909       nameDBtoFileConversion_["AOH4_BIAS4"                ] = k_AOH4_Bias4 ;
00910       nameDBtoFileConversion_["AOH4_BIAS5"                ] = k_AOH4_Bias5 ;
00911       nameDBtoFileConversion_["AOH4_BIAS6"                ] = k_AOH4_Bias6 ;
00912       //   nameDBtoFileConversion_["AOH4_GAIN1"            ] = ;
00913       //   nameDBtoFileConversion_["AOH4_GAIN2"            ] = ;
00914       //   nameDBtoFileConversion_["AOH4_GAIN3"            ] = ;
00915       //   nameDBtoFileConversion_["AOH4_GAIN4"            ] = ;
00916       //   nameDBtoFileConversion_["AOH4_GAIN5"            ] = ;
00917       //   nameDBtoFileConversion_["AOH4_GAIN6"            ] = ;
00918       //   nameDBtoFileConversion_["AOH5_BIAS1"            ] = ;
00919       //   nameDBtoFileConversion_["AOH5_BIAS2"            ] = ;
00920       //   nameDBtoFileConversion_["AOH5_BIAS3"            ] = ;
00921       //   nameDBtoFileConversion_["AOH5_BIAS4"            ] = ;
00922       //   nameDBtoFileConversion_["AOH5_BIAS5"            ] = ;
00923       //   nameDBtoFileConversion_["AOH5_BIAS6"            ] = ;
00924       //   nameDBtoFileConversion_["AOH5_GAIN1"            ] = ;
00925       //   nameDBtoFileConversion_["AOH5_GAIN2"            ] = ;
00926       //   nameDBtoFileConversion_["AOH5_GAIN3"            ] = ;
00927       //   nameDBtoFileConversion_["AOH5_GAIN4"            ] = ;
00928       //   nameDBtoFileConversion_["AOH5_GAIN5"            ] = ;
00929       //   nameDBtoFileConversion_["AOH5_GAIN6"            ] = ;
00930       //   nameDBtoFileConversion_["AOH6_BIAS1"            ] = ;
00931       //   nameDBtoFileConversion_["AOH6_BIAS2"            ] = ;
00932       //   nameDBtoFileConversion_["AOH6_BIAS3"            ] = ;
00933       //   nameDBtoFileConversion_["AOH6_BIAS4"            ] = ;
00934       //   nameDBtoFileConversion_["AOH6_BIAS5"            ] = ;
00935       //   nameDBtoFileConversion_["AOH6_BIAS6"            ] = ;
00936       //   nameDBtoFileConversion_["AOH6_GAIN1"            ] = ;
00937       //   nameDBtoFileConversion_["AOH6_GAIN2"            ] = ;
00938       //   nameDBtoFileConversion_["AOH6_GAIN3"            ] = ;
00939       //   nameDBtoFileConversion_["AOH6_GAIN4"            ] = ;
00940       //   nameDBtoFileConversion_["AOH6_GAIN5"            ] = ;
00941       //   nameDBtoFileConversion_["AOH6_GAIN6"            ] = ;
00942       nameDBtoFileConversion_["DELAY25_GCR"              ] = k_Delay25_GCR ;
00943       nameDBtoFileConversion_["DELAY25_SCL"              ] = k_Delay25_SCL ;
00944       nameDBtoFileConversion_["DELAY25_TRG"              ] = k_Delay25_TRG ;
00945       nameDBtoFileConversion_["DELAY25_SDA"              ] = k_Delay25_SDA ;
00946       nameDBtoFileConversion_["DELAY25_RCL"              ] = k_Delay25_RCL ;
00947       nameDBtoFileConversion_["DELAY25_RDA"              ] = k_Delay25_RDA ;
00948       //   nameDBtoFileConversion_["DEL3_GCR"              ] = ;
00949       //   nameDBtoFileConversion_["DEL3_SCL"              ] = ;
00950       //   nameDBtoFileConversion_["DEL3_TRG"              ] = ;
00951       //   nameDBtoFileConversion_["DEL3_SDA"              ] = ;
00952       //   nameDBtoFileConversion_["DEL3_RCL"              ] = ;
00953       //   nameDBtoFileConversion_["DEL3_RDA"              ] = ;
00954       nameDBtoFileConversion_["DOH_BIAS0"            ] = k_DOH_Ch0Bias_CLK  ;
00955       nameDBtoFileConversion_["DOH_BIAS1"            ] = k_DOH_Ch1Bias_Data ;
00956       nameDBtoFileConversion_["DOH_SEU_GAIN"         ] = k_DOH_Gain_SEU     ;
00957       //   nameDBtoFileConversion_["DOH3_BIAS0"            ] =  ;
00958       //   nameDBtoFileConversion_["DOH3_BIAS1"            ] =  ;
00959       //   nameDBtoFileConversion_["DOH3_SEU_GAIN"         ] =  ;
00960       nameDBtoFileConversion_["PLL_CTR1"             ] = k_PLL_CTR1 ;
00961       nameDBtoFileConversion_["PLL_CTR2"             ] = k_PLL_CTR2 ;
00962       nameDBtoFileConversion_["PLL_CTR3"             ] = k_PLL_CTR3 ;
00963       nameDBtoFileConversion_["PLL_CTR4"             ] = k_PLL_CTR4 ;
00964       nameDBtoFileConversion_["PLL_CTR5"             ] = k_PLL_CTR5 ;
00965       //   nameDBtoFileConversion_["PLL3_CTR1"             ] = ;
00966       //   nameDBtoFileConversion_["PLL3_CTR2"             ] = ;
00967       //   nameDBtoFileConversion_["PLL3_CTR3"             ] = ;
00968       //   nameDBtoFileConversion_["PLL3_CTR4_5"           ] = ;
00969     }
00970     
00971     
00972 }
00973 
00974 
00975 void PixelPortCardConfig::writeASCII(std::string dir) const {
00976 
00977   std::string mthn = "[PixelPortCardConfig::writeASCII()]\t\t\t\t    " ;
00978   if (dir!="") dir+="/";
00979   std::string filename=dir+"portcard_"+portcardname_+".dat";
00980 
00981   std::ofstream out(filename.c_str());
00982   if (!out.good()){
00983     std::cout << __LINE__ << "]\t" << mthn << "Could not open file: " << filename.c_str() << std::endl;
00984     assert(0);
00985   }
00986 
00987   out << "Name: " << portcardname_ << std::endl;
00988   out << "Type: " << type_ << std::endl;
00989   out << "TKFECID: " << TKFECID_ << std::endl;
00990   out << "ringAddress: 0x" <<std::hex<< ringAddress_ <<std::dec<< std::endl;
00991   out << "ccuAddress: 0x" <<std::hex<< ccuAddress_ <<std::dec<< std::endl;
00992   
00993   out << "channelAddress: 0x" <<std::hex<< channelAddress_ <<std::dec<< std::endl;
00994 
00995   out << "i2cSpeed: 0x" <<std::hex<< i2cSpeed_ <<std::dec<< std::endl;
00996 
00997   bool found_PLL_CTR2 = false;
00998   unsigned int last_PLL_CTR2_value = 0x0;
00999   for (unsigned int i=0;i<device_.size();i++)
01000   {
01001     unsigned int deviceAddress = device_.at(i).first;
01002     
01003     // Special handling for AOH gains
01004     if (    ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain123_address )
01005          || ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain456_address )
01006          || ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain123_address )
01007          || ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain456_address )
01008          || ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain123_address )
01009          || ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain456_address )
01010          || ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain123_address )
01011          || ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain456_address )
01012          || ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain123_address )
01013          || ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain456_address )
01014        )
01015     {
01016                 std::string whichAOHString;
01017                 unsigned int zeroOrThree;
01018                 if      ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain123_address )  { whichAOHString = "";  zeroOrThree = 0; }
01019                 else if ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain456_address )  { whichAOHString = "";  zeroOrThree = 3; }
01020                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain123_address ) { whichAOHString = "1"; zeroOrThree = 0; }
01021                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain456_address ) { whichAOHString = "1"; zeroOrThree = 3; }
01022                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain123_address ) { whichAOHString = "2"; zeroOrThree = 0; }
01023                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain456_address ) { whichAOHString = "2"; zeroOrThree = 3; }
01024                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain123_address ) { whichAOHString = "3"; zeroOrThree = 0; }
01025                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain456_address ) { whichAOHString = "3"; zeroOrThree = 3; }
01026                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain123_address ) { whichAOHString = "4"; zeroOrThree = 0; }
01027                 else if ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain456_address ) { whichAOHString = "4"; zeroOrThree = 3; }
01028                 else assert(0);
01029                 
01030                 out << "AOH"<<whichAOHString<<"_Gain"<<zeroOrThree+1<<": 0x"<< (((device_[i].second) & 0x03)>>0) << std::endl; // output bits 0 & 1
01031                 out << "AOH"<<whichAOHString<<"_Gain"<<zeroOrThree+2<<": 0x"<< (((device_[i].second) & 0x0c)>>2) << std::endl; // output bits 2 & 3
01032                 out << "AOH"<<whichAOHString<<"_Gain"<<zeroOrThree+3<<": 0x"<< (((device_[i].second) & 0x30)>>4) << std::endl; // output bits 4 & 5
01033                 continue;
01034     }
01035     // End of special handling
01036     
01037     // Check to see if there's a name corresponding to this address.
01038     std::string settingName = "";
01039     for ( std::map<std::string, unsigned int>::const_iterator nameToAddress_itr = nameToAddress_.begin(); nameToAddress_itr != nameToAddress_.end(); ++nameToAddress_itr )
01040     {
01041 //       cout << "[PixelPortCardConfig::WriteASCII()]\tnameToAddress.first:  " << nameToAddress_itr->first  << endl ;
01042 //       cout << "[PixelPortCardConfig::WriteASCII()]\tnameToAddress.second: " << nameToAddress_itr->second << endl ;
01043 //       cout << "[PixelPortCardConfig::WriteASCII()]\tdeviceAddress:        " << deviceAddress             << endl ;
01044       if ( nameToAddress_itr->second == deviceAddress ) {settingName = nameToAddress_itr->first; break;}
01045       if(nameToAddress_itr == (--nameToAddress_.end()))
01046         {
01047           cout << "[PixelPortCardConfig::WriteASCII()]\tdeviceAddress:        " << deviceAddress   << " NOT FOUND"  << endl ;
01048         }
01049     }
01050 
01051     
01052     // Special handling for PLL addresses.
01053     if ( settingName == k_PLL_CTR2 )
01054     {
01055         if ( found_PLL_CTR2 && last_PLL_CTR2_value == device_.at(i).second ) continue; // don't save duplicate CTR2 settings
01056         found_PLL_CTR2 = true;
01057         last_PLL_CTR2_value = device_.at(i).second;
01058     }
01059     if ( found_PLL_CTR2 && settingName == k_PLL_CTR4or5 ) // change name to PLL_CTR4 or PLL_CTR5
01060     {
01061         if ( (last_PLL_CTR2_value & 0x20) == 0x0 ) settingName = k_PLL_CTR4;
01062         else                                       settingName = k_PLL_CTR5;
01063     }
01064     // end of special handling
01065     
01066     if ( settingName=="" ) out << "0x" <<std::hex<< device_.at(i).first <<std::dec;
01067     else                   out << settingName << ":";
01068     
01069     out << " 0x" <<std::hex<< device_.at(i).second <<std::dec<< std::endl;
01070   }
01071 
01072   out.close();
01073 }
01074 
01075 unsigned int PixelPortCardConfig::getdevicesize() const{
01076   return device_.size();
01077 }
01078 
01079 std::string  PixelPortCardConfig::getTKFECID() const{
01080   return TKFECID_;
01081 }
01082 
01083 unsigned int PixelPortCardConfig::getringAddress() const{
01084   return ringAddress_;
01085 }
01086 
01087 unsigned int PixelPortCardConfig::getccuAddress() const{
01088   return ccuAddress_;
01089 }
01090 
01091 unsigned int PixelPortCardConfig::getchannelAddress() const{
01092   return channelAddress_;
01093 }
01094 
01095 unsigned int PixelPortCardConfig::geti2cSpeed() const{
01096   return i2cSpeed_;
01097 }
01098 
01099 std::string  PixelPortCardConfig::gettype() const{
01100   return type_;
01101 }
01102 
01103 unsigned int PixelPortCardConfig::getdeviceAddress(unsigned int i) const{
01104   assert(i<device_.size());
01105   return device_[i].first;
01106 }
01107 
01108 unsigned int PixelPortCardConfig::getdeviceValues(unsigned int i) const{
01109   assert(i<device_.size());
01110   return device_[i].second;
01111 }
01112 
01113 void PixelPortCardConfig::setdeviceValues(unsigned int address, unsigned int value) {
01114   for (int i=device_.size()-1; i>=0; i--) // Change only the last occurance of address, if there are more than one.
01115     {
01116       if( device_.at(i).first==address )
01117         {
01118           device_.at(i).second=value;
01119           return;
01120         }
01121     }
01122   
01123   // This address wasn't found in the list, so add it and its value.
01124   pair<unsigned int, unsigned int> p(address, value);
01125   device_.push_back(p);
01126   
01127   return;
01128 }
01129 
01130 void PixelPortCardConfig::setdeviceValues(std::string settingName, unsigned int value)
01131 {
01132         setdeviceValues( getdeviceAddressForSetting(settingName), value );
01133         return;
01134 }
01135 
01136 unsigned int PixelPortCardConfig::getdeviceAddressForSetting(std::string settingName) const
01137 {
01138   //std::cout << "[PixelPortCardConfig::getdeviceAddressForSetting()]\t    settingName: " << settingName<< std::endl ;
01139   std::map<std::string, unsigned int>::const_iterator foundName_itr = nameToAddress_.find(settingName);
01140   assert( foundName_itr != nameToAddress_.end() );
01141   return foundName_itr->second;
01142 }
01143 
01144 unsigned int PixelPortCardConfig::getdeviceValuesForSetting(std::string settingName) const
01145 {
01146         return getdeviceValuesForAddress( getdeviceAddressForSetting(settingName) );
01147 }
01148 
01149 unsigned int PixelPortCardConfig::getdeviceValuesForAddress(unsigned int address) const
01150 {
01151         for (int i=device_.size()-1; i>=0; i--) // Get the last occurance of address, if there are more than one.
01152     {
01153       if( device_.at(i).first==address )
01154         {
01155           return device_.at(i).second;
01156         }
01157     }
01158   assert(0); // didn't find this device
01159   return 0;
01160 }
01161 
01162 bool PixelPortCardConfig::containsDeviceAddress(unsigned int deviceAddress) const
01163 {
01164         for ( std::vector<std::pair<unsigned int, unsigned int> >::const_iterator device_itr = device_.begin(); device_itr != device_.end(); device_itr++ )
01165         {
01166                 if ( device_itr->first == deviceAddress ) return true;
01167         }
01168         return false;
01169 }
01170 
01171 unsigned int PixelPortCardConfig::AOHBiasAddressFromAOHNumber(unsigned int AOHNumber) const
01172 {
01173         std::string mthn = "[PixelPortCardConfig::AOHBiasAddressFromAOHNumber()]    " ;
01174         if ( type_ == "fpix" )
01175         {
01176                 if      (AOHNumber == 1) return PortCardSettingNames::k_fpix_AOH_Bias1_address;
01177                 else if (AOHNumber == 2) return PortCardSettingNames::k_fpix_AOH_Bias2_address;
01178                 else if (AOHNumber == 3) return PortCardSettingNames::k_fpix_AOH_Bias3_address;
01179                 else if (AOHNumber == 4) return PortCardSettingNames::k_fpix_AOH_Bias4_address;
01180                 else if (AOHNumber == 5) return PortCardSettingNames::k_fpix_AOH_Bias5_address;
01181                 else if (AOHNumber == 6) return PortCardSettingNames::k_fpix_AOH_Bias6_address;
01182                 else {std::cout << __LINE__ << "]\t" << mthn 
01183                                 << "ERROR: For fpix, AOH number must be in the range 1-6, but the given AOH number was "
01184                                 << AOHNumber
01185                                 << "."
01186                                 << std::endl; 
01187                                 assert(0);}
01188         }
01189         else if ( type_ == "bpix" )
01190         {
01191                 if      (AOHNumber == 1) return PortCardSettingNames::k_bpix_AOH1_Bias1_address;
01192                 else if (AOHNumber == 2) return PortCardSettingNames::k_bpix_AOH1_Bias2_address;
01193                 else if (AOHNumber == 3) return PortCardSettingNames::k_bpix_AOH1_Bias3_address;
01194                 else if (AOHNumber == 4) return PortCardSettingNames::k_bpix_AOH1_Bias4_address;
01195                 else if (AOHNumber == 5) return PortCardSettingNames::k_bpix_AOH1_Bias5_address;
01196                 else if (AOHNumber == 6) return PortCardSettingNames::k_bpix_AOH1_Bias6_address;
01197                 else if (AOHNumber == 7) return PortCardSettingNames::k_bpix_AOH2_Bias1_address;
01198                 else if (AOHNumber == 8) return PortCardSettingNames::k_bpix_AOH2_Bias2_address;
01199                 else if (AOHNumber == 9) return PortCardSettingNames::k_bpix_AOH2_Bias3_address;
01200                 else if (AOHNumber ==10) return PortCardSettingNames::k_bpix_AOH2_Bias4_address;
01201                 else if (AOHNumber ==11) return PortCardSettingNames::k_bpix_AOH2_Bias5_address;
01202                 else if (AOHNumber ==12) return PortCardSettingNames::k_bpix_AOH2_Bias6_address;
01203                 else if (AOHNumber ==13) return PortCardSettingNames::k_bpix_AOH3_Bias1_address;
01204                 else if (AOHNumber ==14) return PortCardSettingNames::k_bpix_AOH3_Bias2_address;
01205                 else if (AOHNumber ==15) return PortCardSettingNames::k_bpix_AOH3_Bias3_address;
01206                 else if (AOHNumber ==16) return PortCardSettingNames::k_bpix_AOH3_Bias4_address;
01207                 else if (AOHNumber ==17) return PortCardSettingNames::k_bpix_AOH3_Bias5_address;
01208                 else if (AOHNumber ==18) return PortCardSettingNames::k_bpix_AOH3_Bias6_address;
01209                 else if (AOHNumber ==19) return PortCardSettingNames::k_bpix_AOH4_Bias1_address;
01210                 else if (AOHNumber ==20) return PortCardSettingNames::k_bpix_AOH4_Bias2_address;
01211                 else if (AOHNumber ==21) return PortCardSettingNames::k_bpix_AOH4_Bias3_address;
01212                 else if (AOHNumber ==22) return PortCardSettingNames::k_bpix_AOH4_Bias4_address;
01213                 else if (AOHNumber ==23) return PortCardSettingNames::k_bpix_AOH4_Bias5_address;
01214                 else if (AOHNumber ==24) return PortCardSettingNames::k_bpix_AOH4_Bias6_address;
01215                 else {std::cout << __LINE__ << "]\t" << mthn 
01216                                 << "ERROR: For bpix, AOH number must be in the range 1-24, but the given AOH number was "
01217                                 << AOHNumber
01218                                 << "."
01219                                 << std::endl; 
01220                                 assert(0);}
01221         }
01222         else assert(0);
01223 }
01224 
01225 std::string PixelPortCardConfig::AOHGainStringFromAOHNumber(unsigned int AOHNumber) const
01226 {
01227         std::string mthn = "[PixelPortCardConfig::AOHGainStringFromAOHNumber()]    " ;
01228         if ( type_ == "fpix" )
01229         {
01230                 if      (AOHNumber == 1) return "AOH_Gain1";
01231                 else if (AOHNumber == 2) return "AOH_Gain2";
01232                 else if (AOHNumber == 3) return "AOH_Gain3";
01233                 else if (AOHNumber == 4) return "AOH_Gain4";
01234                 else if (AOHNumber == 5) return "AOH_Gain5";
01235                 else if (AOHNumber == 6) return "AOH_Gain6";
01236                 else {std::cout << __LINE__ << "]\t" << mthn 
01237                                 << "ERROR: For fpix, AOH number must be in the range 1-6, but the given AOH number was "
01238                                 << AOHNumber 
01239                                 << "."
01240                                 << std::endl; 
01241                                 assert(0);}
01242         }
01243         else if ( type_ == "bpix" )
01244         {
01245                 if      (AOHNumber == 1) return "AOH1_Gain1";
01246                 else if (AOHNumber == 2) return "AOH1_Gain2";
01247                 else if (AOHNumber == 3) return "AOH1_Gain3";
01248                 else if (AOHNumber == 4) return "AOH1_Gain4";
01249                 else if (AOHNumber == 5) return "AOH1_Gain5";
01250                 else if (AOHNumber == 6) return "AOH1_Gain6";
01251                 else if (AOHNumber == 7) return "AOH2_Gain1";
01252                 else if (AOHNumber == 8) return "AOH2_Gain2";
01253                 else if (AOHNumber == 9) return "AOH2_Gain3";
01254                 else if (AOHNumber ==10) return "AOH2_Gain4";
01255                 else if (AOHNumber ==11) return "AOH2_Gain5";
01256                 else if (AOHNumber ==12) return "AOH2_Gain6";
01257                 else if (AOHNumber ==13) return "AOH3_Gain1";
01258                 else if (AOHNumber ==14) return "AOH3_Gain2";
01259                 else if (AOHNumber ==15) return "AOH3_Gain3";
01260                 else if (AOHNumber ==16) return "AOH3_Gain4";
01261                 else if (AOHNumber ==17) return "AOH3_Gain5";
01262                 else if (AOHNumber ==18) return "AOH3_Gain6";
01263                 else if (AOHNumber ==19) return "AOH4_Gain1";
01264                 else if (AOHNumber ==20) return "AOH4_Gain2";
01265                 else if (AOHNumber ==21) return "AOH4_Gain3";
01266                 else if (AOHNumber ==22) return "AOH4_Gain4";
01267                 else if (AOHNumber ==23) return "AOH4_Gain5";
01268                 else if (AOHNumber ==24) return "AOH4_Gain6";
01269                 else {std::cout << __LINE__ << "]\t" << mthn 
01270                                 << "ERROR: For bpix, AOH number must be in the range 1-24, but the given AOH number was "
01271                                 << AOHNumber
01272                                 << "."
01273                                 << std::endl; 
01274                                 assert(0);}
01275         }
01276         else assert(0);
01277 }
01278 
01279 unsigned int PixelPortCardConfig::AOHGainAddressFromAOHNumber(unsigned int AOHNumber) const
01280 {
01281         std::string mthn = "[PixelPortCardConfig::AOHGainAddressFromAOHNumber()]    " ;
01282         unsigned int address;
01283         if ( type_ == "fpix" )
01284         {
01285                 if      (AOHNumber == 1) address =  PortCardSettingNames::k_fpix_AOH_Gain123_address;
01286                 else if (AOHNumber == 2) address =  PortCardSettingNames::k_fpix_AOH_Gain123_address;
01287                 else if (AOHNumber == 3) address =  PortCardSettingNames::k_fpix_AOH_Gain123_address;
01288                 else if (AOHNumber == 4) address =  PortCardSettingNames::k_fpix_AOH_Gain456_address;
01289                 else if (AOHNumber == 5) address =  PortCardSettingNames::k_fpix_AOH_Gain456_address;
01290                 else if (AOHNumber == 6) address =  PortCardSettingNames::k_fpix_AOH_Gain456_address;
01291                 else {std::cout << __LINE__ << "]\t" << mthn 
01292                                 << "ERROR: For fpix, AOH number must be in the range 1-6, but the given AOH number was "
01293                                 << AOHNumber 
01294                                 << "."
01295                                 << std::endl; 
01296                                 assert(0);}
01297         }
01298         else if ( type_ == "bpix" )
01299         {
01300                 if      (AOHNumber == 1) address =  PortCardSettingNames::k_bpix_AOH1_Gain123_address;
01301                 else if (AOHNumber == 2) address =  PortCardSettingNames::k_bpix_AOH1_Gain123_address;
01302                 else if (AOHNumber == 3) address =  PortCardSettingNames::k_bpix_AOH1_Gain123_address;
01303                 else if (AOHNumber == 4) address =  PortCardSettingNames::k_bpix_AOH1_Gain456_address;
01304                 else if (AOHNumber == 5) address =  PortCardSettingNames::k_bpix_AOH1_Gain456_address;
01305                 else if (AOHNumber == 6) address =  PortCardSettingNames::k_bpix_AOH1_Gain456_address;
01306                 else if (AOHNumber == 7) address =  PortCardSettingNames::k_bpix_AOH2_Gain123_address;
01307                 else if (AOHNumber == 8) address =  PortCardSettingNames::k_bpix_AOH2_Gain123_address;
01308                 else if (AOHNumber == 9) address =  PortCardSettingNames::k_bpix_AOH2_Gain123_address;
01309                 else if (AOHNumber ==10) address =  PortCardSettingNames::k_bpix_AOH2_Gain456_address;
01310                 else if (AOHNumber ==11) address =  PortCardSettingNames::k_bpix_AOH2_Gain456_address;
01311                 else if (AOHNumber ==12) address =  PortCardSettingNames::k_bpix_AOH2_Gain456_address;
01312                 else if (AOHNumber ==13) address =  PortCardSettingNames::k_bpix_AOH3_Gain123_address;
01313                 else if (AOHNumber ==14) address =  PortCardSettingNames::k_bpix_AOH3_Gain123_address;
01314                 else if (AOHNumber ==15) address =  PortCardSettingNames::k_bpix_AOH3_Gain123_address;
01315                 else if (AOHNumber ==16) address =  PortCardSettingNames::k_bpix_AOH3_Gain456_address;
01316                 else if (AOHNumber ==17) address =  PortCardSettingNames::k_bpix_AOH3_Gain456_address;
01317                 else if (AOHNumber ==18) address =  PortCardSettingNames::k_bpix_AOH3_Gain456_address;
01318                 else if (AOHNumber ==19) address =  PortCardSettingNames::k_bpix_AOH4_Gain123_address;
01319                 else if (AOHNumber ==20) address =  PortCardSettingNames::k_bpix_AOH4_Gain123_address;
01320                 else if (AOHNumber ==21) address =  PortCardSettingNames::k_bpix_AOH4_Gain123_address;
01321                 else if (AOHNumber ==22) address =  PortCardSettingNames::k_bpix_AOH4_Gain456_address;
01322                 else if (AOHNumber ==23) address =  PortCardSettingNames::k_bpix_AOH4_Gain456_address;
01323                 else if (AOHNumber ==24) address =  PortCardSettingNames::k_bpix_AOH4_Gain456_address;
01324                 else {std::cout << __LINE__ << "]\t" << mthn 
01325                                 << "ERROR: For bpix, AOH number must be in the range 1-24, but the given AOH number was "
01326                                 << AOHNumber
01327                                 << "."
01328                                 << std::endl; 
01329                                 assert(0);}
01330         }
01331         else assert(0);
01332         
01333         return address;
01334 }
01335 
01336 unsigned int PixelPortCardConfig::getAOHGain(unsigned int AOHNumber) const
01337 {
01338         const unsigned int address = AOHGainAddressFromAOHNumber(AOHNumber);
01339         const unsigned int threeGainsValue = getdeviceValuesForAddress(address);
01340         
01341         if ( AOHNumber%3 == 1 ) return (((threeGainsValue) & 0x03)>>0); // return bits 0 & 1
01342         if ( AOHNumber%3 == 2 ) return (((threeGainsValue) & 0x0c)>>2); // return bits 2 & 3
01343         if ( AOHNumber%3 == 0 ) return (((threeGainsValue) & 0x30)>>4); // return bits 4 & 5
01344         
01345         assert(0);
01346 }
01347 //=============================================================================================
01348 void PixelPortCardConfig::writeXMLHeader(pos::PixelConfigKey key, 
01349                                          int version, 
01350                                          std::string path, 
01351                                          std::ofstream *outstream,
01352                                          std::ofstream *out1stream,
01353                                          std::ofstream *out2stream) const
01354 {
01355   std::string mthn = "[PixelPortCardConfig::writeXMLHeader()]\t\t\t    " ;
01356   std::stringstream fullPath ;
01357   fullPath << path << "/Pixel_PortCardSettings_" << PixelTimeFormatter::getmSecTime() << ".xml" ;
01358   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << std::endl ;
01359   
01360   outstream->open(fullPath.str().c_str()) ;
01361   
01362   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                    << std::endl ;
01363   *outstream << "<ROOT xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'>"                               << std::endl ;
01364   *outstream << " <HEADER>"                                                                                  << std::endl ;
01365   *outstream << "  <TYPE>"                                                                                   << std::endl ;
01366   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_PORTCARD_SETTINGS</EXTENSION_TABLE_NAME>"                    << std::endl ;
01367   *outstream << "   <NAME>Pixel Port Card Settings</NAME>"                                                   << std::endl ;
01368   *outstream << "  </TYPE>"                                                                                  << std::endl ;
01369   *outstream << "  <RUN>"                                                                                    << std::endl ;
01370   *outstream << "   <RUN_TYPE>Pixel Port Card Settings</RUN_TYPE>"                                           << std::endl ;
01371   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                              << std::endl ;
01372   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
01373   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                            << std::endl ; 
01374   *outstream << "  </RUN>"                                                                                   << std::endl ;
01375   *outstream << " </HEADER>"                                                                                 << std::endl ;
01376   *outstream << ""                                                                                           << std::endl ;
01377   *outstream << " <DATA_SET>"                                                                                << std::endl ;
01378   *outstream << "  <PART>"                                                                                   << std::endl ;
01379   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                                 << std::endl ;
01380   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                              << std::endl ;
01381   *outstream << "  </PART>"                                                                                  << std::endl ;
01382   *outstream << "  <VERSION>"             << version      << "</VERSION>"                                    << std::endl ;
01383   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>"                        << std::endl ;
01384   *outstream << "  <CREATED_BY_USER>"     << getAuthor()  << "</CREATED_BY_USER>"                            << std::endl ;
01385   *outstream << ""                                                                                           << std::endl ;
01386 }
01387 
01388 //=============================================================================================
01389 void PixelPortCardConfig::writeXML(std::ofstream *outstream,
01390                                    std::ofstream *out1stream,
01391                                    std::ofstream *out2stream) const 
01392 {
01393   std::string mthn = "[PixelPortCardConfig::writeXML()]\t\t\t    " ;
01394 
01395 
01396   *outstream << "  <DATA>"                                                                                   << std::endl;
01397   *outstream << "   <PORT_CARD>"      << portcardname_    << "</PORT_CARD>"                                  << std::endl;
01398   *outstream << "   <TRKFEC>"         << TKFECID_         << "</TRKFEC>"                                     << std::endl;
01399   *outstream << "   <RING>"           << ringAddress_     << "</RING>"                                       << std::endl;
01400   *outstream << "   <CCU_ADDR>"       << ccuAddress_      << "</CCU_ADDR>"                                   << std::endl;
01401   *outstream << "   <CHANNEL>"        << channelAddress_  << "</CHANNEL>"                                    << std::endl;
01402   *outstream << "   <I2C_SPEED>"      << i2cSpeed_        << "</I2C_SPEED>"                                  << std::endl;
01403 
01404   bool found_PLL_CTR2 = false;
01405   unsigned int last_PLL_CTR2_value = 0x0;
01406   for (unsigned int i=0;i<device_.size();i++)
01407   {
01408     unsigned int deviceAddress = device_.at(i).first;
01409     
01410     // Special handling for AOH gains
01411     if (    ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain123_address )
01412          || ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain456_address )
01413          || ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain123_address )
01414          || ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain456_address )
01415          || ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain123_address )
01416          || ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain456_address )
01417          || ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain123_address )
01418          || ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain456_address )
01419          || ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain123_address )
01420          || ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain456_address )
01421        )
01422     {
01423      std::string whichAOHString;
01424      unsigned int zeroOrThree;
01425      if      ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain123_address )  { whichAOHString = "";  zeroOrThree = 0; }
01426      else if ( type_=="fpix" && deviceAddress == k_fpix_AOH_Gain456_address )  { whichAOHString = "";  zeroOrThree = 3; }
01427      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain123_address ) { whichAOHString = "1"; zeroOrThree = 0; }
01428      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH1_Gain456_address ) { whichAOHString = "1"; zeroOrThree = 3; }
01429      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain123_address ) { whichAOHString = "2"; zeroOrThree = 0; }
01430      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH2_Gain456_address ) { whichAOHString = "2"; zeroOrThree = 3; }
01431      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain123_address ) { whichAOHString = "3"; zeroOrThree = 0; }
01432      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH3_Gain456_address ) { whichAOHString = "3"; zeroOrThree = 3; }
01433      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain123_address ) { whichAOHString = "4"; zeroOrThree = 0; }
01434      else if ( type_=="bpix" && deviceAddress == k_bpix_AOH4_Gain456_address ) { whichAOHString = "4"; zeroOrThree = 3; }
01435      else assert(0);
01436 
01437      *outstream << "   <AOH" << whichAOHString << "_GAIN" << zeroOrThree+1 << ">" << (((device_[i].second) & 0x03)>>0) << "</AOH" << whichAOHString << "_GAIN" << zeroOrThree+1 << ">" << std::endl; // output bits 0 & 1
01438      *outstream << "   <AOH" << whichAOHString << "_GAIN" << zeroOrThree+2 << ">" << (((device_[i].second) & 0x0c)>>2) << "</AOH" << whichAOHString << "_GAIN" << zeroOrThree+2 << ">" << std::endl; // output bits 2 & 3
01439      *outstream << "   <AOH" << whichAOHString << "_GAIN" << zeroOrThree+3 << ">" << (((device_[i].second) & 0x30)>>4) << "</AOH" << whichAOHString << "_GAIN" << zeroOrThree+3 << ">" << std::endl; // output bits 4 & 5
01440      continue;
01441     }
01442     // End of special handling
01443     
01444     // Check to see if there's a name corresponding to this address.
01445     std::string settingName = "";
01446     for ( std::map<std::string, unsigned int>::const_iterator nameToAddress_itr = nameToAddress_.begin(); nameToAddress_itr != nameToAddress_.end(); ++nameToAddress_itr )
01447     {
01448       if ( nameToAddress_itr->second == deviceAddress ) {settingName = nameToAddress_itr->first; break;}
01449     }
01450     for ( std::map<std::string, std::string>::const_iterator nameDBtoFileConversion_itr = nameDBtoFileConversion_.begin(); nameDBtoFileConversion_itr != nameDBtoFileConversion_.end(); ++nameDBtoFileConversion_itr )
01451     {
01452       if ( nameDBtoFileConversion_itr->second.find(settingName) != std::string::npos ) {settingName = nameDBtoFileConversion_itr->first; break;}
01453     }
01454     
01455     // Special handling for PLL addresses.
01456     if ( settingName == k_PLL_CTR2 )
01457     {
01458         if ( found_PLL_CTR2 && last_PLL_CTR2_value == device_.at(i).second ) continue; // don't save duplicate CTR2 settings
01459         found_PLL_CTR2 = true;
01460         last_PLL_CTR2_value = device_.at(i).second;
01461     }
01462     if ( found_PLL_CTR2 && settingName == k_PLL_CTR4or5 ) // change name to PLL_CTR4 or PLL_CTR5
01463     {
01464         if ( (last_PLL_CTR2_value & 0x20) == 0x0 ) settingName = k_PLL_CTR4;
01465         else                                       settingName = k_PLL_CTR5;
01466     }
01467     // end of special handling
01468     
01469     if ( settingName=="" ) *outstream << device_.at(i).first;
01470     else                   *outstream << "   <" << settingName << ">" ;
01471     
01472     *outstream << device_.at(i).second << "</" << settingName << ">" << std::endl;
01473   }
01474   
01475   *outstream << "  </DATA>" << std::endl;
01476 
01477 }
01478 //=============================================================================================
01479 void PixelPortCardConfig::writeXMLTrailer(std::ofstream *outstream,
01480                                           std::ofstream *out1stream,
01481                                           std::ofstream *out2stream) const
01482 {
01483   std::string mthn = "[PixelPortCardConfig::writeXMLTrailer()]\t\t\t    " ;
01484   
01485   *outstream << " </DATA_SET>"                                                                               << std::endl ;
01486   *outstream << "</ROOT> "                                                                                   << std::endl ;
01487 
01488   outstream->close() ;
01489 }
01490