00001
00002 #include "MicrorocSCReader.hh"
00003 #include "Run.hh"
00004 #include "Detector.hh"
00005 #include "Chamber.hh"
00006 #include "Dif.hh"
00007 #include "Board.hh"
00008 #include "Chip.hh"
00009 #include "MicrorocChip.hh"
00010 #include "MicrorocChannel.hh"
00011 #include "MicroException.hh"
00012 #include "tools/Log.hh"
00013 #include "ilcConfDb/IlcConfDb.hh"
00014
00015 #include "configObjects/LdaConfiguration.h"
00016 #include "configObjects/DccConfiguration.h"
00017 #include "configObjects/AsicConfiguration.h"
00018
00019
00020 #include <string>
00021
00022
00023
00024 using namespace std;
00025
00026
00027
00028 MicrorocSCReader::MicrorocSCReader(Run& aRun, string& aData, ui16 difId)
00029 : run(aRun), initialised(false), bitsSize(0), bitsIndex(0)
00030 {
00031 init(aData);
00032
00033 }
00034
00035 MicrorocSCReader::MicrorocSCReader(Run& aRun, std::vector<int> aData, ui16 difId)
00036 : run(aRun), initialised(false) , bitsSize(0), bitsIndex(0)
00037 {
00038 init(aData);
00039
00040
00041 }
00042
00043 MicrorocSCReader::MicrorocSCReader(Run& aRun)
00044 : run(aRun), initialised(false) , bitsSize(0), bitsIndex(0)
00045 {
00046 }
00047
00048
00049 MicrorocSCReader::~MicrorocSCReader()
00050
00051 {
00052 }
00053
00054
00055
00056
00057 void MicrorocSCReader::parseSCFromDb(IlcConfDb& ilcconf)
00058 {
00059 FILE_LOG(logINFO) << "Get Slowcontrol for run "<< run.getId() << " from IPNL database, it may take a while ..." << endl;
00060 try
00061 {
00062 AsicConfiguration& asicConf = ilcconf.getAsicConfiguration(run.getId() ) ;
00063 vector<ConfigObject*> asics = asicConf.getVector();
00064 FILE_LOG(logDEBUG) << "nb asic sc in db[" << asics.size() << "]" << endl;
00065 for ( unsigned int i = 0 ; i < asics.size() ; i++ )
00066 {
00067 FILE_LOG(logDEBUG) << "asic index[" << i << "]" << endl;
00068 int chipId, difId = 0;
00069 ui32 dac0, dac1 ,dac2 ,shaperLg ,shaperHg = 0;
00070 vector<int> pedestal_offset;
00071 string sstimulate, mask0, mask1, mask2;
00072 vector<bool> stimulate;
00073 try
00074 {
00075 chipId = asics[i]->getInt("HEADER");
00076 difId = asics[i]->getInt("DIF_ID");
00077 FILE_LOG(logDEBUG) << "asic DIF_ID[" << difId << "], Chip ID[" << chipId << "]" << endl;
00078 dac0 = asics[i]->getInt("BB0");
00079 dac1 = asics[i]->getInt("BB1");
00080 dac2 = asics[i]->getInt("BB2");
00081 shaperLg = asics[i]->getInt("SWLG");
00082 shaperHg = asics[i]->getInt("SWHG");
00083 pedestal_offset = asics[i]->getIntVector("B0B3");
00084 sstimulate = asics[i]->getString("CTEST");
00085 stimulate = convertCTEST(sstimulate);
00086 mask0 = asics[i]->getString("MASK0");
00087 mask1 = asics[i]->getString("MASK1");
00088 mask2 = asics[i]->getString("MASK2");
00089
00090 try {
00091 Dif& dif = run.getDetector().getDifById(difId);
00092
00093 MicrorocChip &chip = dynamic_cast<MicrorocChip &> (dif.getChipById(chipId));
00094
00095 chip.setShaper_lg(shaperLg);
00096 chip.setShaper_hg(shaperHg);
00097 chip.setThresholdDac_0(dac0);
00098 chip.setThresholdDac_1(dac1);
00099 chip.setThresholdDac_2(dac2);
00100
00101 FILE_LOG(logDEBUG1) << "wanted[" << chipId << "] found chip[" << chip.getChipSoftId().toString() <<endl;
00102 MicrorocChip µChip = dynamic_cast<MicrorocChip &> (chip);
00103 microChip.setBypassed(false);
00104 microChip.setConfigured(true);
00105 for ( int channelNum = 0 ; channelNum <=63; channelNum++)
00106 {
00107 MicrorocChannel µChannel = dynamic_cast<MicrorocChannel &> (microChip.getChannelById(channelNum));
00108 microChannel.setStimulate(stimulate[channelNum]);
00109 microChannel.setEnable(true);
00110 microChannel.setPedestalOffset(pedestal_offset[channelNum]);
00111 }
00112 }
00113 catch (MicroException& e)
00114 {
00115 FILE_LOG(logERROR) << "no chip with id[" << dec << chipId << "] for Dif[" << difId << "], found " <<endl;
00116 }
00117 initialised = true;
00118 }
00119 catch (Exception e){ FILE_LOG(logDEBUG) << "DEBUG Slow control parameter name error fost chip difId[" << difId << "], chipId["<< chipId << "]" << endl;}
00120 }
00121 }
00122 catch ( MicroException e ) { FILE_LOG(logERROR) << e.getMessage(); }
00123 }
00124
00125 void MicrorocSCReader::init(std::vector<int> vec )
00126 {
00127 ostringstream oss;
00128 unsigned long data;
00129 initialised = true;
00130
00131 for ( long i = 0 ; i < vec.size() ;i++ )
00132 {
00133 {
00134 data = vec[i];
00135 oss << bitset<8>(data);
00136 data = 0;
00137 }
00138 }
00139
00140 bitset<592> b(oss.str());
00141 bits.push_back( b);
00142
00143
00144 }
00145
00146 void MicrorocSCReader::init(string s )
00147 {
00148 initialised = true;
00149
00150 FILE_LOG(logDEBUG) << "Slow control init: s[" << s << "]" <<endl;
00151
00152 ui32 nbBits = s.size() * 4;
00153 ui32 nbCaraPerChip = 594 / 4 ;
00154 ui16 nbChips = nbBits / 592;
00155
00156 for ( long j = 0 ; j < nbChips ;j++ )
00157 {
00158 ostringstream oss;
00159 unsigned long data;
00160 for ( long i = 0 ; i < nbCaraPerChip ;i++ )
00161 {
00162 {
00163 stringstream ss;
00164 FILE_LOG(logDEBUG) << "j*nbCaraPerChip)+i[" << j*nbCaraPerChip+i << "] value[" << s.substr((j*nbCaraPerChip)+i,1) << "] " << endl;
00165 ss << s.substr((j*nbCaraPerChip)+i,1);
00166
00167 ss >> hex >> data;
00168 oss << bitset<4>(data);
00169 data = 0;
00170 }
00171 }
00172 bitset<592> b(oss.str());
00173 FILE_LOG(logDEBUG) << "bitset for index[ " << bits.size() << "]: " << endl;
00174 FILE_LOG(logDEBUG) << "[" << oss.str() << "]: " << endl;
00175 bits.push_back(b);
00176 }
00177
00178
00179 FILE_LOG(logDEBUG) << "bits Size[" << bits.size() << "]" <<endl;
00180 }
00181
00182
00183
00184 void MicrorocSCReader::parseSC(ui16 difId)
00185 {
00186
00187 FILE_LOG(logDEBUG) << "MicrorocSCReader::parseSC sizeof bits[" <<bits.size() << "]" << endl;
00188
00189 Detector& detector = run.getDetector();
00190 bitsIndex= 0;
00191
00192 for ( unsigned int index = 0; index < bits.size() ; index++ )
00193 {
00194 ui16 chipId = getChipId();
00195 try {
00196
00197
00198
00199
00200 Dif& dif = detector.getDifById(difId);
00201 MicrorocChip &chip = dynamic_cast<MicrorocChip &> (dif.getChipById(chipId));
00202
00203 chip.setShaper_lg(getLowShaper());
00204 chip.setShaper_hg(getHighShaper());
00205 ui32 dac0 = getChipDac(0);
00206 ui32 dac1 = getChipDac(1);
00207 ui32 dac2 = getChipDac(2);
00208
00209 if ( ! run.isCalibrationRun() )
00210 {
00211 chip.setThresholdDac_0(dac0);
00212 chip.setThresholdDac_1(dac1);
00213 chip.setThresholdDac_2(dac2);
00214 }
00215
00216 FILE_LOG(logDEBUG1) << "wanted[" << chipId << "] found chip[" << chip.getChipSoftId().toString() <<endl;
00217 MicrorocChip µChip = dynamic_cast<MicrorocChip &> (chip);
00218 microChip.setBypassed(false);
00219 FILE_LOG(logDEBUG) << "chip[" << chipId << "] set bypass to False"<< endl;
00220 microChip.setConfigured(true);
00221 FILE_LOG(logDEBUG) << "chip Id[" << microChip.getId() << "], DAC 0[" << dac0 << "]" << endl;
00222 FILE_LOG(logDEBUG) << "chip Id[" << microChip.getId() << "], DAC 1[" << dac1 << "]" << endl;
00223 FILE_LOG(logDEBUG) << "chip Id[" << microChip.getId() << "], DAC 2[" << dac2 << "]" << endl;
00224 for ( int channelNum = 0 ; channelNum <=63; channelNum++)
00225 {
00226 MicrorocChannel µChannel = dynamic_cast<MicrorocChannel &> (microChip.getChannelById(channelNum));
00227 ui16 dac = getChannelDac(channelNum);
00228 microChannel.setStimulate(true);
00229 microChannel.setEnable(true);
00230 microChannel.setPedestalOffset(dac);
00231 }
00232 }
00233 catch (MicroException& e){
00234 FILE_LOG(logERROR) << "no chip with id[" << dec << chipId << "] found " <<endl;
00235 }
00236
00237 bitsIndex++;
00238 }
00239 }
00240
00241
00242 ui16 MicrorocSCReader::getBit(ui16 bitNum)const
00243 {
00244 return bits[bitsIndex][bitNum-1];
00245 }
00246
00247
00248
00249 ui16 MicrorocSCReader::getChipId() const
00250 {
00251 ui16 result = getBit(chip_id) << 7;
00252 FILE_LOG(logDEBUG1) << "MicrorocSCReader::getChipId bit0[" << getBit(chip_id) << "]" << endl;
00253
00254 for ( ui16 bit = 1 ; bit <= 7 ; bit++)
00255 {
00256 FILE_LOG(logDEBUG1) << "MicrorocSCReader::getChipId bit" << bit <<"[" << getBit(chip_id+bit) << "]" << endl;
00257 result = result + ( getBit(chip_id+bit) << 7-bit );
00258 }
00259
00260 return result;
00261 }
00262
00263 ui16 MicrorocSCReader::getChannelDac(ui16 channelId ) const
00264 {
00265 ui16 bit0Pos = dac_channel_pos + (channelId * dac_channel_nbits );
00266 ui16 bit0 = getBit(bit0Pos);
00267 ui16 bit1 = getBit(bit0Pos+1);
00268 ui16 bit2 = getBit(bit0Pos+2);
00269 ui16 bit3 = getBit(bit0Pos+3);
00270
00271
00272 return ( bit0 + bit1*2 + bit2 *4 + bit3*8 );
00273 }
00274
00275
00276 ui16 MicrorocSCReader::getLowShaper( ) const
00277 {
00278 ui16 bit0 = getBit(low_shaper_bit_0);
00279 ui16 bit1 = getBit(low_shaper_bit_1);
00280 return bit0 + bit1*2;
00281 }
00282
00283
00284 ui16 MicrorocSCReader::getHighShaper( ) const
00285 {
00286 ui16 bit0 = getBit(high_shaper_bit_0);
00287 ui16 bit1 = getBit(high_shaper_bit_1);
00288 return bit0 + bit1*2;
00289 }
00290
00291
00292 ui32 MicrorocSCReader::getChipDac( ui16 dac ) const
00293 {
00294
00295
00296
00297
00298 ui16 index = 0;
00299 if ( dac == 0 ) { index = chip_dac0; }
00300 else if ( dac == 1 ) { index = chip_dac1; }
00301 else if ( dac == 2 ) { index = chip_dac2; }
00302 else { FILE_LOG(logINFO) << "dac"<< dac << " does not exist" << endl; exit(-1) ; }
00303
00304 ui16 bit0 = !getBit(index);
00305 ui16 bit1 = !getBit(index+1);
00306 ui16 bit2 = !getBit(index+2);
00307 ui16 bit3 = !getBit(index+3);
00308 ui16 bit4 = !getBit(index+4);
00309 ui16 bit5 = !getBit(index+5);
00310 ui16 bit6 = !getBit(index+6);
00311 ui16 bit7 = !getBit(index+7);
00312 ui16 bit8 = !getBit(index+8);
00313 ui16 bit9 = !getBit(index+9);
00314
00315
00316 ui32 result = bit9 + bit8*2 + bit7*4 + bit6*8 + bit5*16 + bit4*32 + bit3*64 + bit2*128 + bit1*256 + bit0*512 ;
00317
00318
00319 return result;
00320 }
00321
00322 vector<bool> MicrorocSCReader::convertCTEST(string chaine)
00323 {
00324 vector<bool> result ;
00325
00326 chaine.substr(chaine.find("0X")+2 , chaine.length()-2);
00327
00328
00329 string svalue = chaine.substr(chaine.find("0X")+2 , chaine.length()-2);
00330
00331
00332 unsigned long int x;
00333 stringstream ss;
00334 ss << std::hex << svalue;
00335 ss >> x;
00336
00337 for ( unsigned int i = 0 ; i< 16 ; i++ )
00338 {
00339 unsigned int foo = (x & 0xf) ;
00340 x = x >> 4;
00341 for ( unsigned int j = 0 ; j < 4 ; j++ )
00342 {
00343 bool bit = foo & 0x1 ;
00344 foo = foo >> 1;
00345 result.push_back(bit);
00346 }
00347
00348 }
00349
00350
00351
00352 return result;
00353 }