00001 
00002 
00003 #include "parser/CalibHR2Parser.hh"
00004 #include "parser/AcquisitionParser.hh"
00005 
00006 #include "event/Event.hh"
00007 
00008 #include "geometry/Detector.hh"
00009 #include "geometry/Dif.hh"
00010 #include "geometry/Board.hh"
00011 #include "geometry/Chip.hh"
00012 #include "geometry/Hardroc2Chip.hh"
00013 #include "geometry/Chamber.hh"
00014 
00015 #include "tools/MicroException.hh"
00016 #include "tools/Toolbox.hh"
00017 #include "tools/SteerDesc.hh"
00018 #include "tools/Log.hh"
00019 
00020 #include <cstdio>
00021 #include <iostream>
00022 #include <iomanip>
00023 #include <fstream>
00024 #include <string>
00025 #include "math.h"
00026 
00027 using namespace std;
00028 
00029 
00030 
00031 #define bigTolittle Toolbox::bigTolittle
00032 
00033 using namespace std;
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 static unsigned int grayToBinary2(unsigned int gray) {
00043   
00044   unsigned int bin = ((gray >> 31) & 1) << 31;
00045   for (int bit = 30; bit >= 0; --bit)
00046     bin += ( ((gray >> bit) & 1) ^ ((bin >> (bit + 1)) & 1) ) << bit;
00047 
00048   return(bin);
00049 } 
00050 
00051 
00052 
00053 CalibHR2Parser::CalibHR2Parser(Run& aRun, FILE *aFile, ui32 _firstEventId)
00054 : AcquisitionParser(aRun, aFile,  _firstEventId)
00055 {
00056   FILE_LOG(logDEBUG1) << "--CalibHR2Parser constructor()"<< endl;
00057 
00058   run.setCalibrationRun(true);
00059 
00060   reset();
00061 }
00062 
00063 
00064 CalibHR2Parser::~CalibHR2Parser()
00065 {
00066   FILE_LOG(logDEBUG1)<< "----CalibHR2Parser destructeur"<< endl;
00067 }
00068 
00069 
00070 void CalibHR2Parser::reset()
00071 {
00072 init = false;
00073 newCalibration = true;
00074 newDif = true;
00075 newHR = true;
00076 difId = -1;
00077 dac[0] = -1;
00078 dac[1] = -1;
00079 dac[2] = -1;
00080 difId = -1;
00081 
00082 
00083 
00084 }
00085 
00086 void  CalibHR2Parser::setMotifValue(string line)
00087 {
00088     ui64 result = 0;
00089     string mot;
00090     int equalpos = 0;
00091 
00092     equalpos = line.find('=') ;
00093     if ( equalpos != string::npos )
00094     {
00095         mot = line.substr(equalpos + 2, line.size());  
00096 
00097         
00098 
00099         for(int i=MOTIFNB-1; i>=0 ;  --i)
00100         {
00101 
00102             motif[i] = false;
00103             if ( mot[i] == '1')
00104             {
00105                 motif[i] = true;
00106             }
00107         }
00108     }
00109 
00110 }
00111 
00112 
00113 
00114 
00115 int  CalibHR2Parser::getDacValue(string line)
00116 {
00117     int result = -1;
00118     string value;
00119     int equalpos = 0;
00120 
00121     equalpos = line.find('=') ;
00122     if ( equalpos != string::npos )
00123     {
00124         value = line.substr(equalpos + 2, line.size());  
00125         result = atoi(value.c_str());
00126     }
00127 
00128     return result;
00129 
00130 }
00131 
00132 
00133 void  CalibHR2Parser::initParsing(FILE* inputFile)
00134 {
00135 
00136     string line;
00137     size_t found;
00138 
00139 
00140     do
00141     {
00142        if ( getLine ( inputFile, line) == EOF ) { return ;}
00143        found = line.find("Chips en calibration" );
00144 
00145        line.clear();
00146     }
00147     while  (found==string::npos);
00148 
00149     
00150     if ( getLine ( inputFile, line) == EOF ) { return ;}
00151     found = line.find("motif Ctest" );
00152     if  (found!=string::npos)
00153     {
00154       setMotifValue(line);
00155     }
00156 }
00157 
00158 
00159 bool  CalibHR2Parser::checkNextLine(FILE* inputFile, string target )
00160 {
00161     size_t found;
00162     string line;
00163 
00164     int curPos = ftell(inputFile);
00165 
00166     if ( getLine ( inputFile, line) == EOF ) { return false ;}
00167 
00168     fseek(inputFile,curPos,SEEK_SET);
00169     found = line.find(target );
00170     if  (found!=string::npos)
00171     {
00172         return true;
00173     }
00174     return false;
00175 
00176 }
00177 
00178 
00179 int  CalibHR2Parser::getNextEvent(Event& event)
00180 {
00181     unsigned int nbHits = 0;
00182     
00183     
00184     if ( ! init)
00185     {
00186         initParsing(inputFile);
00187         init = true;
00188     }
00189 
00190     string line;
00191 
00192     if ( newCalibration)
00193     { 
00194         FILE_LOG(logDEBUG1) <<  "New calibration !" << endl;
00195         size_t found;
00196 
00197         do
00198         {
00199             int result = getLine ( inputFile, line);
00200              if (result == EOF)
00201              {
00202                  FILE_LOG(logDEBUG1) <<  "End of file !" << endl;
00203                  reset();
00204                  FILE_LOG(logDEBUG1) <<  "return LAST_FILE_EVENT:["  << LAST_FILE_EVENT <<  endl;
00205                  return LAST_FILE_EVENT;
00206              }
00207 
00208              
00209              found=line.find("DAC_2");
00210         }
00211         while (found==string::npos);
00212 
00213         dac[0] = -1;
00214         dac[1] = -1;
00215         dac[2] = getDacValue(line);
00216 
00217         
00218         if ( getLine ( inputFile, line) != EOF )
00219         {
00220             if ( line.find("DAC_1")!=string::npos) { dac[1] = getDacValue(line); }
00221             else { return EVENT_ERROR;  }  
00222         }
00223 
00224         
00225         if ( getLine ( inputFile, line) != EOF )
00226         {
00227             if ( line.find("DAC_0")!=string::npos) { dac[0] = getDacValue(line); }
00228             else { return EVENT_ERROR;  }  
00229         }
00230         FILE_LOG(logINFO) <<  "dac_2[" << dac[2] << "]" << endl;
00231         FILE_LOG(logINFO) <<  "dac_1[" << dac[1] << "]" << endl;
00232         FILE_LOG(logINFO) <<  "dac_0[" << dac[0] << "]" << endl;
00233 
00234         
00235         run.setName("turlututu");
00236         const Detector &detector = run.getDetector();
00237         try {
00238           ChamberMap_t chambers = detector.getChambers();
00239           for (ChamberMap_t::iterator chamberIt = chambers.begin(); chamberIt != chambers.end(); chamberIt++) {
00240             Chamber& chamber = *chamberIt->second;
00241             DifMap_t difs = chamber.getDifs();
00242             for (DifMap_t::iterator difIt = difs.begin(); difIt != difs.end(); difIt++) {
00243               Dif& dif = *difIt->second;
00244               BoardMap_t boards = dif.getBoards();
00245               for (BoardMap_t::iterator boardIt = boards.begin(); boardIt != boards.end(); boardIt++) {
00246                 Board& b = *boardIt->second;
00247                 ChipMap_t chips = b.getChips();
00248                 for (ChipMap_t::iterator chipIt = chips.begin(); chipIt != chips.end(); chipIt++) {
00249                   Hardroc2Chip &chip = dynamic_cast<Hardroc2Chip &> (*chipIt->second);
00250                   chip.setThresholdDac_0(dac[0]);
00251                   chip.setThresholdDac_1(dac[1]);
00252                   chip.setThresholdDac_2(dac[2]);
00253                   for (int chNum = 0; chNum < 64; chNum++) {
00254                     Channel& ch = chip.getChannelById(chNum);
00255                     ch.setEnable(1);
00256                   }
00257                 }
00258               }
00259             }
00260           }
00261         }
00262         catch (MicroException &e) { cout << e.getMessage() << endl; }
00263 
00264         newCalibration = false; 
00265         return NEW_CONFIG;
00266      } 
00267    if ( newDif)
00268    {
00269           FILE_LOG(logDEBUG1) <<  "New Dif !" << endl;
00270         
00271         int space = 0;  
00272         int b0Pos = 6;  
00273         bool find = false;
00274         do
00275         {
00276            b0Pos = ftell(inputFile);
00277            if (getLine ( inputFile, line) == EOF) {
00278              return(LAST_FILE_EVENT);
00279            }
00280 
00281            
00282            if (checkNextLine(inputFile,"DAC_") )
00283            {
00284                newCalibration = true;
00285                FILE_LOG(logWARNING) <<  "No data for DAC values:[" << dac[0] << "," << dac[1] << "," << dac[2] << "]" << endl;
00286                return getNextEvent( event);
00287            }
00288 
00289            int b0find = line.find("B0",18);
00290            int b4pos = 0;
00291            do
00292            {
00293                b4pos = line.find("B4",b4pos+1);
00294 
00295                if ( b4pos - b0find == 46 )
00296                {
00297                    find = true;
00298                }
00299            }
00300            while  (b4pos != string::npos && find == false );
00301 
00302         }
00303         while ( !find );
00304 
00305         b0Pos = b0Pos + 18 + 2 ;  
00306         fseek(inputFile, b0Pos, SEEK_SET);
00307 
00308         
00309         
00310 
00311        
00312        difId = getData(inputFile, 1);
00313        FILE_LOG(logDEBUG1) << "  difId[" << difId << "]" <<  endl;
00314 
00315        fseek(inputFile, 42, SEEK_CUR);
00316        
00317        
00318 
00319        int dataHeader = getData(inputFile, 1);
00320        FILE_LOG(logDEBUG1) << (hex) << "  dataHeader[" << dataHeader << "]" <<  endl;
00321        if (dataHeader == 0xB4)
00322        {
00323            FILE_LOG(logDEBUG1) << "  data header B4 found" << endl;
00324        }
00325        else
00326        {
00327            FILE_LOG(logERROR) << "  parsing file error. data header error : " << hex << dataHeader << dec << endl;
00328            return EVENT_ERROR;
00329        }
00330 
00331        newDif = false;
00332 
00333 
00334     }
00335     if ( newHR)
00336     {
00337           FILE_LOG(logDEBUG1) <<  "New HR !" << endl;
00338        
00339           if ( checkData(inputFile, 1) == 0xA3)
00340           {
00341              FILE_LOG(logERROR) <<  "ERROR TAG A3 just after newHR for DAC values:[" << dac[0] << "," << dac[1] << "," << dac[2] << "]" << endl;
00342              return EVENT_ERROR;
00343           }
00344        
00345        
00346        newHR = false;
00347     }
00348 
00349 
00350     
00351     
00352     
00353 
00354     int chipId = -1;
00355     
00356     int foo = getData(inputFile , 1);
00357     if ( foo == 0xA3)
00358     { 
00359         FILE_LOG(logDEBUG1) << " -------- TAG A3 " <<  endl;
00360         newHR = true;
00361         foo = getData(inputFile , 1);
00362         if ( foo == 0xA0 )
00363         {
00364             newDif = true;
00365             FILE_LOG(logDEBUG1) << " -------- Tag A0 derriere A3 " <<  endl;
00366             foo = getData(inputFile , 2 ); 
00367                         try {
00368                foo = getData(inputFile, 1 );
00369                         }
00370             catch ( MicroException &e) 
00371                         {
00372                 foo = 0;
00373             }
00374             if ( foo != 0xB0)
00375             {
00376 
00377                 if (  getLine ( inputFile, line) == EOF ) { return LAST_FILE_EVENT ;}  
00378                 if (checkNextLine(inputFile,"acquisition") )
00379                 {
00380                    newDif = true;
00381                 }
00382                 else 
00383                 {
00384 
00385 
00386                     newCalibration = true;
00387                     FILE_LOG(logDEBUG1) << " -------- Pas de B0 derrire le CRC donc newCalibration = true " <<  endl;
00388                                 }
00389             }
00390             else {
00391                 FILE_LOG(logDEBUG1) << " -------- B0 derrire le CRC donc Dif  " <<  endl;
00392             }
00393 
00394         }
00395         return getNextEvent( event);
00396     }
00397     else
00398     { 
00399       fseek ( inputFile, -2, SEEK_CUR);
00400       chipId = getData(inputFile, 1);
00401 
00402 
00403     
00404     int bcId = getData(inputFile, 3);
00405     FILE_LOG(logDEBUG1) << (hex) << "  bcid[" << bcId << "]" <<  endl;
00406 
00407 
00408     
00409 
00410     bool difFound = false;
00411     Detector& detector = run.getDetector();
00412     { const ChamberMap_t &chambers = detector.getChambers();
00413       for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00414         try {
00415           Chamber& chamber = *(it->second);
00416           const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00417         }
00418         catch (...) {
00419           
00420           continue;
00421         }
00422 
00423         
00424         difFound = true;
00425         break;
00426       } 
00427       if (!difFound) {
00428         FILE_LOG(logERROR) << "    No dif found with Id 0x" << hex << difId << dec << endl;
00429         return EVENT_ERROR;
00430       }
00431     }
00432 
00433    
00434         
00435 
00436         int canal[16]={0};
00437         for (int chNum = 48; chNum >= 0; chNum -= 16) {
00438                 unsigned int data4;
00439           try {
00440               data4 = getData(inputFile, 4); 
00441           }
00442           catch ( MicroException &e) 
00443           {
00444               continue;
00445           }
00446 
00447 
00448           int index = 0;
00449           for ( index = 0; index <= 15 ; index++)
00450           {
00451              
00452              canal[index] = (data4 >> ((15 - index )* 2 )) & 0x03; 
00453 
00454              if ( motif[index+chNum])
00455              {
00456                 if ( newHit(event, detector, canal[index], index + chNum    , chipId, difId) != 1 )
00457                                 {
00458 
00459                   try 
00460                   {
00461                         foo =  getData(inputFile, 1) ;
00462                         while (  foo != 0xA3 )
00463                                             {
00464     
00465                           foo =  getData(inputFile, 1) ;
00466                                             }
00467                     }
00468                                         catch ( MicroException &e) 
00469                     {
00470                         FILE_LOG(logINFO) << "End of line 0xA3 not Found !!!" << endl; 
00471                                                 newDif = true;
00472 
00473                     
00474                     
00475 
00476 
00477                     }
00478                                         
00479                                 }
00480                                 else 
00481                                 {
00482                         nbHits++;
00483                                 }
00484              }
00485           }
00486       }
00487 
00488       event.setId(++lastEventId);
00489       FILE_LOG(logDEBUG1) << "  STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00490       return EVENT_CORRECT;
00491     }
00492 
00493 }
00494 
00495 
00496 
00497 int CalibHR2Parser::getLine ( FILE*file, string &line )
00498 {
00499     line.clear();
00500     int result = fgetc(file ) ;
00501     while ( result != EOF )
00502     {
00503         line += result;
00504         if ( result == '\n' )
00505         {
00506             line.resize (line.size()-1);
00507             return 0;
00508         }
00509         result = fgetc(file ) ;
00510     }
00511     return EOF;
00512 }
00513 
00514 
00515 
00516 unsigned int CalibHR2Parser::checkData(FILE* inputFile, const int nBytes) {
00517 
00518     int curPos = ftell(inputFile);
00519     unsigned int result = getData(inputFile, nBytes);
00520     fseek(inputFile,curPos,SEEK_SET);
00521     return result;
00522 }
00523 
00524 
00525 
00526 unsigned int CalibHR2Parser::getData(FILE* inputFile, const int nBytes) {
00527   unsigned int result = 0;
00528   for (int byte = 0; byte < nBytes; ++byte) {
00529     int data = 0;
00530     if (fscanf(inputFile, "%2x", &data) != 1) {
00531             throw MicroException("get data: fcanf error");
00532 
00533 
00534     }
00535     result = (result << 8) + data;
00536   }
00537   return(result);
00538 } 
00539 
00540 
00541 
00542 
00543 unsigned int CalibHR2Parser::getDecimalData(FILE* inputFile, const int nBytes) {
00544   unsigned int result = 0;
00545   for (int byte = 0; byte < nBytes; ++byte) {
00546     int data = 0;
00547     if (fscanf(inputFile, "%2d", &data) != 1) {
00548       return(0);
00549     }
00550     result = (result << 8) + data;
00551   }
00552   return(result);
00553 } 
00554 
00555 
00556 
00557 
00558 int CalibHR2Parser::newHit(Event& event, const Detector &detector, int data, const int chNum, const int chipId, const int difId)
00559 {
00560   
00561 
00562     
00563 
00564  
00565 
00566   const ChamberMap_t &chambers = detector.getChambers();
00567   for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00568     try {
00569       Chamber& chamber = *(it->second);
00570       const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00571       
00572       
00573       
00574       
00575       try {
00576         const Channel& channel = chamber.getChannelById(chNum, chipId, difId);
00577         i16 analogValue = 0;
00578         
00579         ChannelHit *hit = new ChannelHit(channel, data, analogValue);
00580 
00581         event.insertHit(chamber, hit);
00582         FILE_LOG(logDEBUG1) << "      new Hit : ch " << chNum << ", chip " << chipId << ", dif " << difId
00583                            << " Data=" << data << " , x:" << channel.getX() << ", y:" << channel.getY() << endl;
00584         return(1);
00585       }
00586       catch (MicroException &e) {
00587         FILE_LOG(logERROR) << "ch " << chNum << ", chip " << chipId << ", dif " << difId << " : " << e.getMessage() << endl;
00588         return(0);
00589       }
00590     }
00591     catch (...) {
00592       continue;
00593     }
00594 
00595   } 
00596   return(0);
00597 } 
00598 
00599 
00600 
00601 
00602 
00603 string CalibHR2Parser::binaire(unsigned int nombre)
00604 {
00605 string res = "";
00606 unsigned int val = nombre;
00607 
00608 while (val != 0) {
00609 
00610 
00611    if (val & 1 == 1) {
00612 res = string("1") + res;
00613 }
00614     else {
00615 
00616 res = string("0") + res;
00617 }
00618 
00619 val = val >> 1;
00620 }
00621 return res;
00622 }