00001
00002
00003
00004
00005
00006
00007 #include "CalibFormats/SiPixelObjects/interface/PixelFEDConfig.h"
00008 #include <fstream>
00009 #include <iostream>
00010 #include <map>
00011 #include <assert.h>
00012
00013 using namespace pos;
00014 using namespace std;
00015
00016 PixelFEDConfig::PixelFEDConfig(std::vector<std::vector<std::string> >& tableMat ) : PixelConfigBase(" "," "," "){
00017 std::vector< std::string > ins = tableMat[0];
00018 std::map<std::string , int > colM;
00019 std::vector<std::string > colNames;
00020 colNames.push_back("PIXEL_FED" );
00021 colNames.push_back("CRATE_NUMBER" );
00022 colNames.push_back("VME_ADDRS_HEX");
00023
00024 for(unsigned int c = 0 ; c < tableMat[0].size() ; c++)
00025 {
00026 for(unsigned int n=0; n<colNames.size(); n++)
00027 {
00028 if(tableMat[0][c] == colNames[n])
00029 {
00030 colM[colNames[n]] = c;
00031 break;
00032 }
00033 }
00034 }
00035 for(unsigned int n=0; n<colNames.size(); n++)
00036 {
00037 if(colM.find(colNames[n]) == colM.end())
00038 {
00039 std::cerr << "[PixelFECConfig::PixelFECConfig()]\tCouldn't find in the database the column with name " << colNames[n] << std::endl;
00040 assert(0);
00041 }
00042 }
00043
00044 std::string fedname = "";
00045 unsigned int fednum = 0;
00046 fedconfig_.clear();
00047 bool flag = false;
00048 for(unsigned int r = 1 ; r < tableMat.size() ; r++){
00049
00050 fedname = tableMat[r][colM["PIXEL_FED"]];
00051
00052
00053
00054 fedname.erase(0,7);
00055 fednum = (unsigned int)atoi(fedname.c_str()) ;
00056
00057 if(fedconfig_.empty())
00058 {
00059 PixelFEDParameters tmp;
00060 unsigned int vme_base_address = 0 ;
00061 string hexVMEAddr = tableMat[r][colM["VME_ADDRS_HEX"]] ;
00062 sscanf(hexVMEAddr.c_str(), "%x", &vme_base_address) ;
00063 tmp.setFEDParameters( fednum, (unsigned int)atoi(tableMat[r][colM["CRATE_NUMBER"]].c_str()) ,
00064 vme_base_address);
00065 fedconfig_.push_back(tmp);
00066 }
00067 else
00068 {
00069 for( unsigned int y = 0; y < fedconfig_.size() ; y++)
00070 {
00071 if (fedconfig_[y].getFEDNumber() == fednum)
00072 {
00073 flag = true;
00074 break;
00075 }
00076 else flag = false;
00077 }
00078
00079 if(flag == false)
00080 {
00081 PixelFEDParameters tmp;
00082 tmp.setFEDParameters( fednum, (unsigned int)atoi(tableMat[r][colM["CRATE_NUMBER"]].c_str()) ,
00083 (unsigned int)atoi(tableMat[r][colM["VME_ADDRS_HEX"]].c_str()));
00084 fedconfig_.push_back(tmp);
00085 }
00086 }
00087 }
00088
00089 std::cout<<std::endl;
00090
00091 for( unsigned int x = 0 ; x < fedconfig_.size() ; x++)
00092 {
00093 std::cout<<fedconfig_[x]<<std::endl;
00094 }
00095
00096 std::cout<<fedconfig_.size()<<std::endl;
00097
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107 PixelFEDConfig::PixelFEDConfig(std::string filename):
00108 PixelConfigBase(" "," "," "){
00109
00110 std::ifstream in(filename.c_str());
00111
00112 if (!in.good()){
00113 std::cout << "Could not open:"<<filename.c_str()<<std::endl;
00114 assert(0);
00115 }
00116 else {
00117 std::cout << "Opened:"<<filename.c_str()<<std::endl;
00118 }
00119
00120 std::string dummy;
00121
00122 in >> dummy;
00123 in >> dummy;
00124 in >> dummy;
00125 in >> dummy;
00126 in >> dummy;
00127 in >> dummy;
00128
00129 do {
00130
00131 unsigned int fednumber;
00132 unsigned int crate;
00133 unsigned int vme_base_address;
00134
00135 in >> fednumber >> crate >> std::hex >> vme_base_address >> std::dec;
00136
00137 if (!in.eof() ){
00138
00139
00140 PixelFEDParameters tmp;
00141
00142 tmp.setFEDParameters(fednumber , crate , vme_base_address);
00143
00144 fedconfig_.push_back(tmp);
00145 }
00146
00147 }
00148 while (!in.eof());
00149 in.close();
00150
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 PixelFEDConfig::~PixelFEDConfig() {}
00163
00164 void PixelFEDConfig::writeASCII(std::string dir) const {
00165
00166 if (dir!="") dir+="/";
00167 string filename=dir+"fedconfig.dat";
00168
00169 ofstream out(filename.c_str());
00170 if(!out.good()){
00171 cout << "Could not open file:"<<filename<<endl;
00172 assert(0);
00173 }
00174
00175 out <<" #FED number crate vme base address" <<endl;
00176 for(unsigned int i=0;i<fedconfig_.size();i++){
00177 out << fedconfig_[i].getFEDNumber()<<" "
00178 << fedconfig_[i].getCrate()<<" "
00179 << "0x"<<hex<<fedconfig_[i].getVMEBaseAddress()<<dec<<endl;
00180 }
00181 out.close();
00182 }
00183
00184
00185 unsigned int PixelFEDConfig::getNFEDBoards() const{
00186
00187 return fedconfig_.size();
00188
00189 }
00190
00191 unsigned int PixelFEDConfig::getFEDNumber(unsigned int i) const{
00192
00193 assert(i<fedconfig_.size());
00194 return fedconfig_[i].getFEDNumber();
00195
00196 }
00197
00198
00199 unsigned int PixelFEDConfig::getCrate(unsigned int i) const{
00200
00201 assert(i<fedconfig_.size());
00202 return fedconfig_[i].getCrate();
00203
00204 }
00205
00206
00207 unsigned int PixelFEDConfig::getVMEBaseAddress(unsigned int i) const{
00208
00209 assert(i<fedconfig_.size());
00210 return fedconfig_[i].getVMEBaseAddress();
00211
00212 }
00213
00214
00215 unsigned int PixelFEDConfig::crateFromFEDNumber(unsigned int fednumber) const{
00216
00217 for(unsigned int i=0;i<fedconfig_.size();i++){
00218 if (fedconfig_[i].getFEDNumber()==fednumber) return fedconfig_[i].getCrate();
00219 }
00220
00221 std::cout << "Could not find FED number:"<<fednumber<<std::endl;
00222
00223 assert(0);
00224
00225 return 0;
00226
00227 }
00228
00229
00230 unsigned int PixelFEDConfig::VMEBaseAddressFromFEDNumber(unsigned int fednumber) const{
00231
00232 for(unsigned int i=0;i<fedconfig_.size();i++){
00233 if (fedconfig_[i].getFEDNumber()==fednumber) return fedconfig_[i].getVMEBaseAddress();
00234 }
00235
00236 std::cout << "Could not find FED number:"<<fednumber<<std::endl;
00237
00238 assert(0);
00239
00240 return 0;
00241
00242 }
00243
00244 unsigned int PixelFEDConfig::FEDNumberFromCrateAndVMEBaseAddress(unsigned int crate, unsigned int vmebaseaddress) const {
00245
00246 for(unsigned int i=0;i<fedconfig_.size();i++){
00247 if (fedconfig_[i].getCrate()==crate&&
00248 fedconfig_[i].getVMEBaseAddress()==vmebaseaddress) return fedconfig_[i].getFEDNumber();
00249 }
00250
00251 std::cout << "Could not find FED crate and address:"<<crate<<", "<<vmebaseaddress<<std::endl;
00252
00253 assert(0);
00254
00255 return 0;
00256
00257 }