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