2021-12-17 18:36:12 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "TFile.h"
|
|
|
|
#include "TTree.h"
|
|
|
|
#include "TString.h"
|
|
|
|
#include "TMath.h"
|
|
|
|
#include "TBenchmark.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define MAX_CRATES 2
|
|
|
|
#define MAX_BOARDS_PER_CRATE 13
|
|
|
|
#define MAX_CHANNELS_PER_BOARD 16
|
|
|
|
#define BOARD_START 2
|
|
|
|
|
|
|
|
|
|
|
|
class measurment{
|
|
|
|
|
|
|
|
public:
|
|
|
|
UShort_t ch;
|
|
|
|
UShort_t slot;
|
|
|
|
UShort_t crate;
|
|
|
|
UShort_t headerLength; /// headerLength > 4, more data except tarce.
|
|
|
|
UShort_t eventLength; /// eventLength = headerLength + trace
|
|
|
|
Bool_t pileup;
|
|
|
|
ULong64_t time;
|
|
|
|
UShort_t cfd;
|
|
|
|
UShort_t energy;
|
|
|
|
UShort_t trace_length;
|
|
|
|
Bool_t trace_out_of_range;
|
|
|
|
|
2021-12-21 19:43:24 -05:00
|
|
|
Long64_t timeDiff;
|
|
|
|
|
2021-12-17 18:36:12 -05:00
|
|
|
UShort_t id;
|
|
|
|
|
|
|
|
measurment(){};
|
|
|
|
|
|
|
|
void Clear(){
|
|
|
|
ch = 0;
|
|
|
|
slot = 0;
|
|
|
|
crate = 0;
|
|
|
|
eventLength = 0;
|
|
|
|
pileup = false;
|
|
|
|
time = 0;
|
|
|
|
cfd = 0;
|
|
|
|
energy = 0;
|
|
|
|
trace_length = 0;
|
|
|
|
trace_out_of_range = 0;
|
2021-12-21 19:43:24 -05:00
|
|
|
timeDiff = 0;
|
2021-12-17 18:36:12 -05:00
|
|
|
id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Print(){
|
|
|
|
printf("Crate: %d, Slot: %d, Ch: %d | id: %d \n", crate, slot, ch, id);
|
|
|
|
printf("HeaderLength: %d, Event Length: %d, energy: %d, timeStamp: %llu\n", headerLength, eventLength, energy, time);
|
|
|
|
printf("trace_length: %d, pile-up:%d\n", trace_length, pileup);
|
|
|
|
}
|
|
|
|
};
|
2021-12-20 17:26:48 -05:00
|
|
|
//#############################################
|
|
|
|
// main
|
|
|
|
//#############################################
|
2021-12-17 18:36:12 -05:00
|
|
|
int main(int argn, char **argv) {
|
|
|
|
|
|
|
|
if (argn != 2 && argn != 3 ) {
|
|
|
|
printf("Usage :\n");
|
|
|
|
printf("%s [evt File] [timeWindow] \n", argv[0]);
|
|
|
|
printf(" timeWindow : number of tick, 1 tick = 10 ns. default = 100 \n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TString inFileName = argv[1];
|
|
|
|
TString outFileName = inFileName;
|
|
|
|
outFileName.Remove(inFileName.First('.'));
|
|
|
|
outFileName.Append("_raw.root");
|
|
|
|
|
2021-12-21 19:43:24 -05:00
|
|
|
long int inFilePos = 0;
|
2021-12-17 18:36:12 -05:00
|
|
|
TBenchmark gClock;
|
|
|
|
gClock.Reset();
|
|
|
|
gClock.Start("timer");
|
|
|
|
|
2021-12-21 19:43:24 -05:00
|
|
|
Long64_t measureID = -1;
|
2021-12-17 18:36:12 -05:00
|
|
|
|
|
|
|
measurment data;
|
|
|
|
|
|
|
|
printf("====================================\n");
|
|
|
|
|
|
|
|
FILE * inFile = fopen(inFileName, "r");
|
|
|
|
if( inFile == NULL ){
|
|
|
|
printf("Cannot read file : %s \n", inFileName.Data());
|
|
|
|
return -404;
|
|
|
|
}
|
|
|
|
|
|
|
|
//get file size
|
|
|
|
fseek(inFile, 0L, SEEK_END);
|
2021-12-20 17:10:44 -05:00
|
|
|
long int inFileSize = ftell(inFile);
|
2021-12-17 18:36:12 -05:00
|
|
|
rewind(inFile); ///back to the File begining
|
|
|
|
|
2021-12-20 17:10:44 -05:00
|
|
|
printf(" in file: %s\n", inFileName.Data());
|
|
|
|
printf("out file: %s\n", outFileName.Data());
|
|
|
|
printf("--------------------------------\n");
|
2021-12-20 17:26:48 -05:00
|
|
|
|
|
|
|
//====== ROOT file
|
2021-12-20 17:10:44 -05:00
|
|
|
TFile * outFile = new TFile(outFileName, "recreate");
|
|
|
|
TTree * tree = new TTree("tree", "tree");
|
|
|
|
|
2021-12-21 19:43:24 -05:00
|
|
|
tree->Branch("evID", &measureID, "data_ID/L");
|
2021-12-20 17:10:44 -05:00
|
|
|
tree->Branch("detID", &data.id, "det_ID/s");
|
|
|
|
tree->Branch("e", &data.energy, "energy/s");
|
|
|
|
tree->Branch("t", &data.time, "time_stamp/l");
|
2021-12-21 19:43:24 -05:00
|
|
|
//tree->Branch("tdiff", &data.timeDiff, "time_Diff/L");
|
2021-12-20 17:10:44 -05:00
|
|
|
|
|
|
|
//=======TODO online event building
|
|
|
|
|
2021-12-17 18:36:12 -05:00
|
|
|
unsigned int header[4]; //read 4 header, unsigned int = 4 byte = 32 bits.
|
|
|
|
unsigned long long nWords = 0;
|
2021-12-21 19:43:24 -05:00
|
|
|
|
|
|
|
ULong64_t timeLast = 0;
|
2021-12-17 18:36:12 -05:00
|
|
|
|
|
|
|
//=============== Read File
|
2021-12-20 17:26:48 -05:00
|
|
|
/// while ( ! feof(inFile) ){
|
2021-12-21 19:43:24 -05:00
|
|
|
while ( inFilePos <= inFileSize ){
|
2021-12-17 18:36:12 -05:00
|
|
|
|
|
|
|
fread(header, sizeof(header), 1, inFile);
|
2021-12-20 17:26:48 -05:00
|
|
|
inFilePos = ftell(inFile);
|
2021-12-21 19:43:24 -05:00
|
|
|
measureID ++;
|
2021-12-17 18:36:12 -05:00
|
|
|
|
|
|
|
/// see the Pixie-16 user manual, Table4-2
|
|
|
|
data.ch = header[0] & 0xF ;
|
|
|
|
data.slot = (header[0] >> 4) & 0xF;
|
|
|
|
data.crate = (header[0] >> 8) & 0xF;
|
|
|
|
data.headerLength = (header[0] >> 12) & 0x1F;
|
|
|
|
data.eventLength = (header[0] >> 17) & 0x3FFF;
|
|
|
|
data.pileup = header[0] >> 31 ;
|
|
|
|
data.time = ((ULong64_t)(header[2] & 0xFFFF) << 32) + header[1];
|
|
|
|
data.cfd = header[2] >> 16 ;
|
2021-12-20 17:10:44 -05:00
|
|
|
data.energy = header[3] & 0xFFFF;
|
2021-12-17 18:36:12 -05:00
|
|
|
data.trace_length = (header[3] >> 16) & 0x7FFF;
|
|
|
|
data.trace_out_of_range = header[3] >> 31;
|
|
|
|
|
|
|
|
data.id = data.crate*MAX_BOARDS_PER_CRATE*MAX_CHANNELS_PER_BOARD + (data.slot-BOARD_START)*MAX_CHANNELS_PER_BOARD + data.ch;
|
|
|
|
|
|
|
|
nWords += data.eventLength;
|
|
|
|
|
2021-12-21 19:43:24 -05:00
|
|
|
//if( measureID == 0 ) {
|
|
|
|
// data.timeDiff = 0;
|
|
|
|
//}else{
|
|
|
|
// data.timeDiff = (Long64_t) data.time - timeLast;
|
|
|
|
//}
|
|
|
|
//timeLast = data.time;
|
|
|
|
|
|
|
|
//if( data.timeDiff == false ){
|
|
|
|
// printf("----------------------nWords: %llu, inFilePos: %lu\n", nWords, inFilePos);
|
|
|
|
// for(int i = 0; i < 4; i++){
|
|
|
|
// printf(" %x\n", header[i]);
|
|
|
|
// }
|
|
|
|
// data.Print();
|
|
|
|
//}
|
2021-12-17 18:36:12 -05:00
|
|
|
|
|
|
|
//=== jump to next measurement
|
|
|
|
if( data.eventLength > 4 ){
|
|
|
|
fseek(inFile, sizeof(int) * (data.eventLength-4), SEEK_CUR);
|
2021-12-20 17:26:48 -05:00
|
|
|
inFilePos = ftell(inFile);
|
2021-12-17 18:36:12 -05:00
|
|
|
}
|
2021-12-20 17:10:44 -05:00
|
|
|
|
2021-12-17 18:36:12 -05:00
|
|
|
//event stats, print status every 10000 events
|
|
|
|
if ( measureID % 10000 == 0 ) {
|
2021-12-20 17:26:48 -05:00
|
|
|
inFilePos = ftell(inFile);
|
2021-12-20 17:10:44 -05:00
|
|
|
float tempf = (float)inFileSize/(1024.*1024.*1024.);
|
2021-12-17 18:36:12 -05:00
|
|
|
gClock.Stop("timer");
|
|
|
|
double time = gClock.GetRealTime("timer");
|
|
|
|
gClock.Start("timer");
|
2021-12-21 19:43:24 -05:00
|
|
|
printf("Total measurements: \x1B[32m%lld \x1B[0m\nPercent Complete: \x1B[32m%ld%% of %.3f GB\x1B[0m\nTime used:%3.0f min %5.2f sec\033[A\033[A\r",
|
2021-12-20 17:26:48 -05:00
|
|
|
measureID, (100*inFilePos/inFileSize), tempf, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
|
2021-12-17 18:36:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//cern fill tree
|
|
|
|
outFile->cd();
|
|
|
|
tree->Fill();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:26:48 -05:00
|
|
|
inFilePos = ftell(inFile);
|
2021-12-17 18:36:12 -05:00
|
|
|
gClock.Stop("timer");
|
|
|
|
double time = gClock.GetRealTime("timer");
|
|
|
|
gClock.Start("timer");
|
2021-12-20 17:10:44 -05:00
|
|
|
float tempf = (float)inFileSize/(1024.*1024.*1024.);
|
2021-12-21 19:43:24 -05:00
|
|
|
printf("Total measurements: \x1B[32m%lld \x1B[0m\nPercent Complete: \x1B[32m%ld%% of %.3f GB\x1B[0m\nTime used:%3.0f min %5.2f sec\033[A\r",
|
2021-12-20 17:26:48 -05:00
|
|
|
measureID, (100*inFilePos/inFileSize), tempf, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
|
2021-12-17 18:36:12 -05:00
|
|
|
|
|
|
|
fclose(inFile);
|
|
|
|
|
|
|
|
//cern save root
|
|
|
|
outFile->cd();
|
|
|
|
tree->Write();
|
|
|
|
outFile->Close();
|
|
|
|
|
|
|
|
gClock.Stop("timer");
|
|
|
|
time = gClock.GetRealTime("timer");
|
|
|
|
printf("\n==================== finished.\r\n");
|
|
|
|
printf("Total time spend : %3.0f min %5.2f sec\n", TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
|
|
|
|
|
|
|
|
}
|