Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed


#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <TROOT.h>
#include <TSystem.h>
#include <TFile.h>
#include <TDirectory.h>
#include <TCanvas.h>
#include <TH2D.h>

#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>



#include "base.h"

std::string serialNumberOut;

const char * getSN(int runNumber){
  char rNum[0xF];
  sprintf(rNum,"%04d",runNumber);
  std::string rNumber = rNum;
 
  const char * serialNumberFromGraph;
  std::string serialNumberTemp;

  const char * serialNumberFromGraphHEX;
  std::string serialNumberTempHEX;

  std::string delimiter;
  size_t pos = 0;

  TH2D * sum_hxy = (TH2D *) gDirectory->Get("hxy0_sum_0");
  // TH2D * sum_hxy = (TH2D *) gDirectory->Get("hxy1_sum_0"); // Za rune od 113 - 117, kjer je bil modul 0 izklopljen
  TH2D * h2d = (TH2D *) gDirectory->Get("h2d_0");

  if(!sum_hxy) {
    printf("Ni naloženega dokumenta!\n");
    return (char*) "NoFILE";
  }

  serialNumberFromGraph = sum_hxy->GetTitle(); // you get something like: HAPD=ZJ5859/021,ZJ5862/023,ZJ5858/022,ZJ5865/025
  serialNumberTemp = serialNumberFromGraph;
  delimiter = "=";
  pos = serialNumberTemp.find(delimiter);
  serialNumberTemp.erase(0, pos + delimiter.length()); // you get something like: ZJ5859/021,ZJ5862/023,ZJ5858/022,ZJ5865/025
  //std::cout << "Vrstica 1 " << serialNumberTemp << std::endl;
  serialNumberOut = serialNumberTemp;
  if(h2d){
    serialNumberFromGraphHEX = h2d->GetTitle()+1; // you get something like: FEB0=0x11354161 FEB1=0x11354161 FEB2=0x11354161 FEB3=0x11354161
                                                  // there is a whitespace at the beginning of title you want to remove, so you set pointer to +1.
    serialNumberTempHEX = serialNumberFromGraphHEX; // you duplicate string
    //std::cout << "Vrstica 2 " << serialNumberTempHEX << std::endl;

    std::string SNtemp, FEBsnHEX, output;

    delimiter = ",";
      //int iteration=0;
    while (serialNumberTemp.length()!=0) {
        //std::cout << iteration << std::endl;
        //iteration++;
      if((pos = serialNumberTemp.find(delimiter))!=std::string::npos) SNtemp = serialNumberTemp.substr(0, pos);
      else SNtemp = serialNumberTemp.substr(0, serialNumberTemp.length());
        //std::cout << "Vrstica 3 " << SNtemp << std::endl;
        //std::cout << "Vrstica 3a " << SNtemp.substr(0,SNtemp.find("/")) << " " << SNtemp.substr(SNtemp.find("/")+1) << std::endl;
      if(pos!=std::string::npos) serialNumberTemp.erase(0, pos + delimiter.length());
      else serialNumberTemp.erase(0, serialNumberTemp.length());
        //std::cout << "Vrstica 4 " << serialNumberTemp << std::endl;
      if(!SNtemp.compare("noserial")) continue;
      FEBsnHEX = serialNumberTempHEX.substr(serialNumberTempHEX.find("=")+1,serialNumberTempHEX.find(" ")-serialNumberTempHEX.find("=")-1);
        //std::cout << "Vrstica 5 " << FEBsnHEX << std::endl;
      output = rNumber + " " + SNtemp.substr(0,SNtemp.find("/")) + " "  + SNtemp.substr(SNtemp.find("/")+1) + " "  + FEBsnHEX;
      WriteSN(output);
        //std::cout << output << std::endl;
      serialNumberTempHEX.erase(0,serialNumberTempHEX.find(" ")+1);
    }
  }
  printf("base.c output: %s\n", serialNumberOut.c_str());
  return serialNumberOut.c_str();
}

int WriteSN(std::string compareLine){
  const char * filename = "FEBserial.txt";
  std::string line;

  std::fstream file;
  file.open(filename, std::fstream::in);
  if (file.is_open()){
    while (std::getline(file,line)){
      //std::cout << line << std::endl;
      if(!line.compare(compareLine)) return 0;
    }
    file.close();
  }

  file.open(filename, std::fstream::app);
  if (file.is_open()){
    file << compareLine << endl;
   
    std::cout << "Add into " << filename << ": " << compareLine << std::endl;

    file.close();
  }

  return 1;
}

int file_exists(char * fileName){
  struct stat fbuf;
  int i = stat (fileName, &fbuf);
  if (i==0) return 1; //File found
  else return 0;
}

GRID mapping(){
  int f[6];
  int ndim = 400;
  char line[400];
  GRID m;
  FILE * fp = fopen("mapNEW.map","r");

  /*
  Stolpec 0: ASIC
  Stolpec 1: Kanal na ASICU
  Stolpec 2: x (iz sprednje strani)
  Stolpec 3: y (iz sprednje strani)
  ...
  */

  while (fgets(line,ndim,fp) != NULL) {
    sscanf(line,"%d%d%d%d%d%d",&f[0],&f[1],&f[2],&f[3],&f[4],&f[5]);
    //m.koordinatniSistem[f[2]][f[3]] = f[0]*36+f[1];
    m.koordinatniSistem[11-f[2]][11-f[3]] = f[0]*36+f[1];
    m.pozicijaPixla[f[0]*36+f[1]] = f[4];
    m.pozicijaPixlaNaCipu[f[0]*36+f[1]] = f[5];
  }
  fclose(fp);
  return m;
}

int CreateFEBsnFile(){
  /*
  you have to manualy save:
  http://www-f9.ijs.si/~f9daq/aerorich/febtest/lvhvbit/xsltproc.php?xml=febps.xml&xsl=febsnmap.xsl
  and then leave in this file only 1 line that contains info
  (21 0x0130ADFFC6E95A64<br/>22 0x013D3048229681BE<br/> ..........)
  */

  std::string line;

  size_t pos = 0;
  std::string token;

  ifstream inputFile("xsltproc.html");
  ofstream outpuFile;
  outpuFile.open ("FEBserial.txt");

  if (inputFile.is_open()){
    while (std::getline(inputFile,line)){
      std::cout << line << std::endl;
    }
    inputFile.close();
  }
  else std::cout << "Unable to open file" << std::endl;

  std::string delimiter = "<br/>";

  while ((pos = line.find(delimiter)) != std::string::npos) {
    token = line.substr(0, pos);
    if (token.length()>3) token = token.erase(token.find(" ")+3,1); // removes the first number after 0x... in
    std::cout << token << std::endl;
    outpuFile << token << std::endl;
    line.erase(0, pos + delimiter.length());
  }

  std::cout << line << std::endl;
  outpuFile << line;

  outpuFile.close();

  return 0;

}

int FEBsn(char * searchFEBserial){
  FILE * fp = fopen("FEBserial.txt","r");
  int FEBserialINT;
  char FEBserialHEX[0xFF];
  int ndim = 400;
  char line[400];
  while (fgets(line,ndim,fp) != NULL) {
    sprintf(FEBserialHEX,"");
    sscanf(line,"%d%s",&FEBserialINT,FEBserialHEX);
    //printf("%d \t %s\n",FEBserialINT, FEBserialHEX);
    if(!strcmp(FEBserialHEX,searchFEBserial)) return FEBserialINT;
  }
  return -1;
}