#include "AculConvert.h" ClassImp(AculConvert); AculConvert::AculConvert() { fRawEvent = new AculRaw(); } AculConvert::~AculConvert() { delete fRawEvent; } Int_t AculConvert::ConvertRun(const char* experimentalrun, const Int_t nofirstfolder, const Int_t nolastfolder/*, const Char_t* runaddress*/) { TString fullWayToRun = experimentalrun; // fullWayToRun.ReplaceAll("/", "\\"); //for win fOutFaultFile.open("convertFault.log"); Char_t nameOfFolder[260]; // Int_t i = 0; for (Int_t i = nofirstfolder; i <= nolastfolder; i++) { sprintf(nameOfFolder, "%s.%03d", fullWayToRun.Data(), i); ConvertFolder(nameOfFolder); } fOutFaultFile.close(); return 0; } Int_t AculConvert::ConvertFolder(const char* rawdatafolder) { TString fullWayToFolder = rawdatafolder; // fullWayToFolder.ReplaceAll("/", "\\"); //for win //necessary names preparing Char_t nameOfFile[260]; Char_t nameOfRun[40]; Char_t nameOfFileProtocol[260]; // strcpy(nameOfRun, fullWayToFolder(fullWayToFolder.Last('\\') + 1, fullWayToFolder.Length()).Data()); //for win strcpy(nameOfRun, fullWayToFolder(fullWayToFolder.Last('/') + 1, fullWayToFolder.Length()).Data()); //for linux strcpy(strstr(nameOfRun, "."), "\0"); // sprintf(nameOfFileProtocol, "%s\\%s.PRO", fullWayToFolder.Data(), nameOfRun); //for win sprintf(nameOfFileProtocol, "%s/%s.PRO", fullWayToFolder.Data(), nameOfRun); //for linux // printf("%s\\%s.PRO\n", fullWayToFolder.Data(), nameOfRun); //for windows printf("%s/%s.PRO\n", fullWayToFolder.Data(), nameOfRun); //for linux //file protocol reading if (ReadFileProtocol(nameOfFileProtocol) != 0) { return -1; } //root file creating Char_t rootFileName[260]; // sprintf(rootFileName, "%s\.root", fullWayToFolder(fullWayToFolder.Last('\\') + 1, fullWayToFolder.Length()).Data()); //for win // sprintf(rootFileName, "%s\.root", fullWayToFolder(fullWayToFolder.Last('/') + 1, fullWayToFolder.Length()).Data()); //for linux sprintf(rootFileName, "%s.root", fullWayToFolder(fullWayToFolder.Last('/') + 1, fullWayToFolder.Length()).Data()); //for linux cout << endl << endl << "===========================================================" << endl << "== Raw data converting to file " << rootFileName << endl << "===========================================================" << endl << endl; TFile *fw = new TFile(rootFileName, "RECREATE"); TTree *tw = new TTree("RAW", "Transcription"); tw->Bronch("channels", "AculRaw", &fRawEvent); //folder processing Int_t i = 0; do { // sprintf(nameOfFile, "%s\\%s.%03d", rawdatafolder, nameOfRun, i); //for win sprintf(nameOfFile, "%s/%s.%03d", rawdatafolder, nameOfRun, i); //for linux i++; cout << endl << "=================================================" << endl; } while ( ConvertFile(nameOfFile, /*fullWayToFolder,*/ fw) == 0 ); fw->Close(); return 0; } Int_t AculConvert::ConvertFile(const char* rawdatafile, TFile* rootRawFile) { //file with ACCULINNA raw data opening FILE *dataFile; TString fullWayToFile = rawdatafile; // fullWayToFile.ReplaceAll("/", "\\"); // cout << fullWayToFile.Data() << endl; //file for error output opening if ((dataFile = fopen(fullWayToFile.Data(), "rb")) == NULL) { cout << endl << "File " << fullWayToFile.Data() << " was not found\n"; if ( fOutFaultFile.is_open() ) { fOutFaultFile << "File " << fullWayToFile.Data() << " was not found\n"; } return -1; } cout << endl << endl << "Data file " << fullWayToFile.Data() << " was opened" << endl; Char_t filename[50]; // sprintf(filename, "%s", fullWayToFile(fullWayToFile.Last('\\') + 1, fullWayToFile.Length()).Data()); //for windows sprintf(filename, "%s", fullWayToFile(fullWayToFile.Last('/') + 1, fullWayToFile.Length()).Data()); //for linux //ROOT file for transcripted data creating TTree *treeRaw; if (rootRawFile == NULL) { Char_t rootFileName[260]; // sprintf(rootFileName, "%s\.root", filename); sprintf(rootFileName, "%s.root", filename); rootRawFile = new TFile(rootFileName, "RECREATE"); treeRaw = new TTree("RAW", "Transcription to .root file"); treeRaw->Bronch("channels", "AculRaw", &fRawEvent); //file protocol reading TString protocolName = filename; protocolName.Replace(protocolName.First('.'), 4, ".PRO"); if (ReadFileProtocol(protocolName.Data()) != 0) { return -1; } // ReadFileProtocol( protocolName.Data() ); } //TTree with class structure // AculRaw *rawEvent = new AculRaw();incorrEventsNumber else { treeRaw = (TTree*)(rootRawFile->Get("RAW")); } // treeRaw->Bronch("channels", "AculRaw", &fRawEvent); //file reading const int BUFFSIZE = 8500000; static unsigned char buffer[BUFFSIZE]; for (Int_t i = 0; i < BUFFSIZE; i++) { buffer[i] = 0; } //cout << "hovno4" << endl; fseek(dataFile, 0, SEEK_SET); if ( fread(buffer, sizeof(buffer), 1, dataFile) != 0 ) { cout << "!!!Data file is larger than expected!!!" << endl; if ( fOutFaultFile.is_open() ) { fOutFaultFile << "!!!Data file " << filename << " is larger than expected!!!" << endl; } return -2; } cout << endl << "------------------------" << endl << " Buffer analysis " << endl << "------------------------" << endl; ////////////////////////////////////////////////////////////////////////////////////////////////////// // event structure: // // n-times 2 bytes cells with parameters according to file protocol // // 2 bytes cell with number of FLOAT parameters // // pairs of 2 bytes cells with identifier (crate, address, subaddress) and value // // 2 bytes cell with end of the event sign (first byte, F0) and trigger identifier (second byte) // // // structure of 2 bytes cell: |lower|higher| ////////////////////////////////////////////////////////////////////////////////////////////////////// Int_t bufferPosition = 0; Int_t corrEventsNumber = 0; Int_t incorrEventsNumber = 0; Int_t parametresNumber = 0; Int_t identifier; //2 bytes (0FFF mask) //1111(empty positions)111(crateMask)11111(blockPositionMask)1111(addresMask) const Int_t addressMask = 15; const Int_t blockPositionMask = 31; const Int_t crateMask = 7; Int_t address = 0; Int_t blockPosition = 0; Int_t crate = 0; UInt_t value = 0; while (bufferPosition < BUFFSIZE) { parametresNumber = static_cast(buffer[bufferPosition + 2*PERMPARNUMBER]); //verification of the correct EndOfEvent identifier position while ( static_cast(buffer[bufferPosition + 2*PERMPARNUMBER + 2/*2 bytes with parametres Number*/ + 4*parametresNumber + 1/*cell with trigger ID*/]) != ENDOFEVENT && bufferPosition < BUFFSIZE) { bufferPosition++; //search for the beginning of the correct event if ( static_cast(buffer[bufferPosition + 2*PERMPARNUMBER + 2 + 4*parametresNumber + 1]) == ENDOFEVENT ) { incorrEventsNumber++; //increased after end of the event identifier cout << "!!!damaged event!!!" << endl; if ( fOutFaultFile.is_open() ) { fOutFaultFile << "!!!damaged event!!!" << endl; } } }//while ////////////////////////////////// //// correct event processing //// ////////////////////////////////// if ( static_cast(buffer[bufferPosition + 2*PERMPARNUMBER + 2/*2 bytes with parametres Number*/ + 4*parametresNumber/*vysvetlit*/ + 1/*cell with trigger ID*/]) == ENDOFEVENT ) { //rawEvent reseting fRawEvent->Reset(); //trigger fRawEvent->trigger = static_cast(buffer[bufferPosition + 2*PERMPARNUMBER + 2/*2 bytes with parametres Number*/ + 4*parametresNumber/*vysvetlit*/ /*+ 1*/ /*cell with trigger ID*/]); //dodelat MWPC`s //floating parametres for (Int_t i = 0; i < parametresNumber; i++) { identifier = static_cast( buffer[bufferPosition + 2*PERMPARNUMBER + 2 + 4*i] + (buffer[bufferPosition + 2*PERMPARNUMBER + 3 + 4*i] << 8) ); address = 0; address = identifier & addressMask; blockPosition = 0; blockPosition = (identifier >> 4) & blockPositionMask; crate = 0; crate = (identifier >> 9) & crateMask; value = 0; value = static_cast( buffer[bufferPosition + 2*PERMPARNUMBER + 4 + 4*i] + (buffer[bufferPosition + 2*PERMPARNUMBER + 5 + 4*i] << 8) ); if (crate == 3) { fRawEvent->C3[blockPosition][address] = static_cast(value&fMaskC3[blockPosition]); } } bufferPosition = bufferPosition + 2*PERMPARNUMBER + 1 + 4*parametresNumber + 2 + 1; corrEventsNumber++; treeRaw->Fill(); }//if }//while cout << endl << "In file " << filename << " was found " << corrEventsNumber << " correct events" << endl << "In file " << filename << " was found " << incorrEventsNumber << " incorrect events" << endl; treeRaw->AutoSave(); if (rootRawFile->GetName() == "ConvertFileOutput.root") { //promyslet, vhodny nazev rootRawFile->Close(); } //close data file if (fclose(dataFile) == EOF) { //parameter file closing cout << "File " << filename << " closing error\n"; if ( fOutFaultFile.is_open() ) { fOutFaultFile << "File " << filename << " closing error\n"; } return -1; } cout << endl << "Data file " << filename << " was closed" << endl << endl; return 0; } Int_t AculConvert::ReadFileProtocol(const char* protocolname) { for (Int_t i = 0; i < BLOCKSNUMBER; i++) { fMaskC3[i] = 0; } //file protocol opening std::ifstream protocolFile; protocolFile.open(protocolname); if (!protocolFile.is_open()) { cout << "File " << protocolname << " was not found\n"; if ( fOutFaultFile.is_open() ) { fOutFaultFile << "File " << protocolname << " was not found\n"; } return -1; } const Int_t lineLength = 200; Char_t line[lineLength]; UInt_t crate, address, subaddress; unsigned int mask; //reading parametres while (!protocolFile.eof()) { protocolFile.getline(line, lineLength); if ( line[0] != '#' ) { sscanf(line, "%*d %*s %*d %d %d %d %*c %x", &crate, &address, &subaddress, &mask); if (crate == 3) { fMaskC3[address] = static_cast(mask); //writing parameters } } } protocolFile.close(); return 0; }