basic event building from evt -> _raw.root -> root is done

This commit is contained in:
Ryan Tang 2022-01-06 16:52:59 -05:00
parent d37251f2d7
commit 2c38a50cd0
9 changed files with 416 additions and 773 deletions

3
.gitignore vendored
View File

@ -9,7 +9,7 @@ xia2ev2*
to2root to2root
scan scan
pxi-time-order pxi-time-order
evt2root MergeEVT
evt2hist evt2hist
ev22txt ev22txt
EventBuilder EventBuilder
@ -21,5 +21,6 @@ EventBuilder
ti74pt7a ti74pt7a
fsu_run_2021 fsu_run_2021
data data
Data
test.cpp test.cpp

View File

@ -1,176 +0,0 @@
#define Monitor_raw_cxx
#include <TH2.h>
#include <TH1.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TMath.h>
#include <vector>
//############################################ User setting
int rawEnergyRange[2] = {500, 6000}; // in ch, {min, max}
int energyRange[3] = {1, 40, 2000}; // in keV, {resol, min, max}
TString e_corr = "correction_e.dat";
Long64_t maxEvent = -1;
bool isBuildEventRoot = true; // is true, no histogram
//############################################ end of user setting
#include "Monitor_raw.h"
//############################################ histogram declaration
TH1F * he[NCRYSTAL];
TH1F * heCal[NCRYSTAL];
TH2F * heCalvID;
//############################################ BEGIN
void Monitor_raw::Begin(TTree * tree){
TString option = GetOption();
totnumEntry = tree->GetEntries();
printf( "=========================================================================== \n");
printf( "========================== Monitor_raw.C/h ================================ \n");
printf( "====== total Entry : %lld \n", totnumEntry);
printf( "=========================================================================== \n");
if( isBuildEvent == false && isBuildEventRoot == false) {
printf("======================== Creating histograms\n");
for( int i = 0 ; i < NCRYSTAL ; i++){
he[i] = new TH1F (Form("he%02d", i), Form("e%02d", i), rawEnergyRange[1]-rawEnergyRange[0], rawEnergyRange[0], rawEnergyRange[1]);
heCal[i] = new TH1F (Form("heCal%02d", i), Form("e%02d (Cali.)", i), (energyRange[2]-energyRange[1])/energyRange[0], energyRange[1], energyRange[2]);
switch (i%4){
case 0: he[i]->SetLineColor(2);break;
case 1: he[i]->SetLineColor(kYellow+3);break;
case 2: he[i]->SetLineColor(kGreen+2);break;
case 3: he[i]->SetLineColor(4);break;
}
switch (i%4){
case 0: heCal[i]->SetLineColor(2);break;
case 1: heCal[i]->SetLineColor(kYellow+3);break;
case 2: heCal[i]->SetLineColor(kGreen+2);break;
case 3: heCal[i]->SetLineColor(4);break;
}
}
heCalvID = new TH2F("heCalvID", "ID vs Energy (Cali.); ID; Energy", NCRYSTAL, 0, NCRYSTAL, (energyRange[2]-energyRange[1])/energyRange[0], energyRange[1], energyRange[2]);
printf("------------------------ end of histograms creation.\n");
}
printf("======================== Load parameters.\n");
eCorr = LoadCorrectionParameters(e_corr);
time0 = 0;
timeDiff = 0;
ClearTreeData();
}
//############################################ PROCESS
Bool_t Monitor_raw::Process(Long64_t entry){
ProcessedEntries++;
/*********** Progress Bar ******************************************/
if (ProcessedEntries>totnumEntry*Frac-1) {
TString msg; msg.Form("%llu", totnumEntry/1000);
int len = msg.Sizeof();
printf(" %3.0f%% (%*llu/%llu k) processed in %6.1f sec | expect %6.1f sec\n",
Frac*100, len, ProcessedEntries/1000,totnumEntry/1000,StpWatch.RealTime(), StpWatch.RealTime()/Frac);
StpWatch.Start(kFALSE);
Frac+=0.1;
}
if ( maxEvent > 0 && entry > maxEvent ) {
Abort(Form("user abort, > %lld \n", maxEvent));
Terminate();
}
if( isBuildEventRoot || isBuildEvent ) entry = index[entry];
b_ID->GetEntry(entry);
b_energy->GetEntry(entry);
b_energy_timestamp->GetEntry(entry);
if ( isBuildEventRoot || isBuildEvent ) {
BuildEvent();
}else{
if( detID < 100 ){
he[detID]->Fill(energy);
///double eCal = ApplyCorrection(eCorr, detID, e); //slow, why?
double eCal = eCorr[detID][0] + eCorr[detID][1]*energy;
heCal[detID]->Fill(eCal);
heCalvID->Fill(detID, eCal);
}
}
return kTRUE;
}
//############################################ TERMINATE
void Monitor_raw::Terminate(){
printf("============================== finished.\n");
gROOT->cd();
if ( isBuildEventRoot || isBuildEvent ) {
saveFile->cd();
newtree->Write();
saveFile->Close();
}else{
int nCrystalPerClover = 4;
int nClover = NCRYSTAL / nCrystalPerClover;
TCanvas * cc = new TCanvas("cc", "cc", 2000, 2000);
if( cc->GetShowEventStatus() == 0 ) cc->ToggleEventStatus();
cc->Divide(1, 9, 0);
for (Int_t i = 0; i < nClover; i++) {
int canvasID = i + 1;
cc->cd(canvasID);
cc->cd(canvasID)->SetGrid();
cc->cd(canvasID)->SetTickx(2);
cc->cd(canvasID)->SetTicky(2);
cc->cd(canvasID)->SetBottomMargin(0.06);
cc->cd(canvasID)->SetLogy();
for( Int_t j = 0; j < nCrystalPerClover; j++){
int hID = nCrystalPerClover*i+ j ;
heCal[hID]->Draw("same");
//he[hID]->Draw("same");
}
}
cc->SetCrosshair(1);
TCanvas * c1 = new TCanvas("c1", "c1", 1000, 1000);
if( c1->GetShowEventStatus() == 0 ) c1->ToggleEventStatus();
c1->SetLogz();
c1->SetGridx();
heCalvID->SetNdivisions(-409, "X");
heCalvID->Draw("colz");
}
}

View File

@ -1,296 +0,0 @@
#ifndef Monitor_raw_h
#define Monitor_raw_h
#include <TROOT.h>
#include <TChain.h>
#include <TTree.h>
#include <TFile.h>
#include <TMacro.h>
#include <TSelector.h>
#include <TStopwatch.h>
#include <TTreeIndex.h>
#include "mapping.h"
#include "armory/AnalysisLibrary.h"
// Header file for the classes stored in the TTree if any.
class Monitor_raw : public TSelector {
public :
TTree *fChain; //!pointer to the analyzed TTree or TChain
// Fixed size dimensions of array or collections stored in the TTree if any.
// Declaration of leaf types
Long64_t evID;
UShort_t detID;
UShort_t energy;
ULong64_t energy_t;
// List of branches
TBranch *b_data_ID; //!
TBranch *b_ID; //!
TBranch *b_energy; //!
TBranch *b_energy_timestamp; //!
Monitor_raw(TTree * /*tree*/ =0) : fChain(0) { isBuildEvent = false;
timeWindow = 100;
saveFileName = "test.root";
}
virtual ~Monitor_raw() { }
virtual Int_t Version() const { return 2; }
virtual void Begin(TTree *tree);
virtual void SlaveBegin(TTree *tree);
virtual void Init(TTree *tree);
virtual Bool_t Notify();
virtual Bool_t Process(Long64_t entry);
virtual Int_t GetEntry(Long64_t entry, Int_t getall = 0) { return fChain ? fChain->GetTree()->GetEntry(entry, getall) : 0; }
virtual void SetOption(const char *option) { fOption = option; }
virtual void SetObject(TObject *obj) { fObject = obj; }
virtual void SetInputList(TList *input) { fInput = input; }
virtual TList *GetOutputList() const { return fOutput; }
virtual void SlaveTerminate();
virtual void Terminate();
void SetBuildEvent(bool buildOption = false){ isBuildEvent = buildOption;}
ClassDef(Monitor_raw,0);
//=================== progress
ULong64_t ProcessedEntries = 0;
Float_t Frac = 0.1; ///Progress bar
TStopwatch StpWatch;
//=================== correction parameters
vector<vector<double>> eCorr;
//=================== output tree;
bool isBuildEvent;
int timeWindow;
ULong64_t time0; //time-0 for each event
int timeDiff;
Long64_t * index; //!
TFile * saveFile; //!
TTree * newtree; //!
TString saveFileName;
Long64_t totnumEntry; /// of original root
//tree
void ClearTreeData();
void BuildEvent();
Int_t eventID;
double e[NCRYSTAL];
ULong64_t e_t[NCRYSTAL];
double bgo[NBGO];
ULong64_t bgo_t[NBGO];
Short_t other[NOTHER];
Short_t multi;
};
#endif
#ifdef Monitor_raw_cxx
void Monitor_raw::Init(TTree *tree)
{
// Set branch addresses and branch pointers
if (!tree) return;
fChain = (TChain *) tree;
fChain->SetMakeClass(1);
fChain->SetBranchAddress("evID", &evID, &b_data_ID);
fChain->SetBranchAddress("id", &detID, &b_ID);
fChain->SetBranchAddress("e", &energy, &b_energy);
fChain->SetBranchAddress("e_t", &energy_t, &b_energy_timestamp);
if( isBuildEventRoot || isBuildEvent){
printf("======================== Buidling Index using the timestamp\n");
tree->BuildIndex("e_t");
TTreeIndex *in = (TTreeIndex*) tree->GetTreeIndex();
index = in->GetIndex();
//for(int i = 0; i < 100; i++){
// printf(" %3d | %lld \n", i, index[i]);
//}
TString option = GetOption();
//printf("======================== Formation of output file name\n");
//int numFile = fChain->GetListOfFiles()->GetLast() + 1; //need input of TChain
//
//printf(".......... number of files : %d \n", numFile);
//
//if( numFile > 0 ) {
// int oldRunNum = -100;
// bool contFlag = false; // is runNumber continue;
// for( int i = 0; i < numFile ; i++){
// TString name = ((TChain *) fChain)->GetListOfFiles()->At(i)->GetTitle();
// int found = name.Last('/');
// name.Remove(0, found + 1 ); // this should give "XXX_run0XX.root"
// TString prefix = name;
// found = name.Last('.');
// name.Remove(found); // this should give "XXX_run0XX"
// found = name.Last('_');
// int runNum = name.Remove(0, found+4).Atoi(); // this should give the 3 digit run number
//
// if( i == 0 ) {
// found = prefix.First("_");
// prefix.Remove(found);
// saveFileName = prefix + "_run";
// }
//
// if( runNum == oldRunNum + 1 ){
// int kk = saveFileName.Sizeof();
// if( contFlag == false ){
// saveFileName.Remove(kk-2); //remove the "-"
// saveFileName += "-";
// }else{
// saveFileName.Remove(kk-5); //remove the runNum and "-"
// }
// contFlag = true;
// }
// if( runNum > oldRunNum + 1) contFlag = false;
//
// saveFileName += Form("%03d_", runNum);
// oldRunNum = runNum;
// }
// int kk = saveFileName.Sizeof();
// saveFileName.Remove(kk-2); // remove the last "-"
// saveFileName += ".root";
//}else{
// gROOT->ProcessLine(".q");
//}
if( option != "" ) saveFileName = option;
printf("save file name : %s \n", saveFileName.Data());
printf("---------------------------------------------\n");
//TODO;
saveFile = new TFile(saveFileName, "recreate");
saveFile->cd();
newtree = new TTree("tree", "tree");
printf("======================== Create output tree\n");
newtree->Branch("evID", &eventID, "event_ID/l");
newtree->Branch("e", e, Form("e[%d]/D", NCRYSTAL));
newtree->Branch("e_t", e_t, Form("e_timestamp[%d]/l", NCRYSTAL));
//newtree->Branch("p", pileup, Form("pile_up_flag[%d]/s", NCRYSTAL));
//newtree->Branch("hit", hit, Form("hit[%d]/s", NCRYSTAL));
newtree->Branch("bgo", bgo, Form("BGO_e[%d]/D", NBGO));
newtree->Branch("bgo_t", bgo_t, Form("BGO_timestamp[%d]/l", NBGO));
newtree->Branch("other", other, Form("other_e[%d]/D", NOTHER));
newtree->Branch("multi", &multi, "multiplicity_crystal/I");
if( eCorr.size() > 0 ) {
TMacro energyCorr(e_corr);
energyCorr.Write("energyCorr");
}
}
printf("======================== Start processing....\n");
StpWatch.Start();
eventID = 0;
}
void Monitor_raw::ClearTreeData(){
for( int i = 0; i < NCRYSTAL; i++){
e[i] = TMath::QuietNaN();
e_t[i] = 0;
//pileup[i] = 0;
//hit[i] = 0;
}
for( int i = 0; i < NBGO; i++) {
bgo[i] = TMath::QuietNaN();
bgo_t[i] = 0 ;
}
for( int i = 0; i < NOTHER; i++) {
other[i] = TMath::QuietNaN();
}
multi = 0;
}
void Monitor_raw::BuildEvent(){
if( time0 == 0) time0 = energy_t;
timeDiff = (int) (energy_t - time0);
if( timeDiff < timeWindow ) {
if ( detID < NCRYSTAL ){
e[detID] = energy;
e_t[detID] = energy_t;
multi++;
}
if ( 100 <= detID && detID < 100 + NBGO ){
bgo[detID-100] = energy;
bgo_t[detID-100] = energy_t;
}
if ( 200 <= detID && detID < 200 + NOTHER){
other[detID-200] = energy;
}
//printf("%d | %3d %6d %10llu, %3d\n", multi, detID, energy, energy_t, timeDiff);
}else{
//---- end of event
eventID ++;
saveFile->cd();
newtree->Fill();
ClearTreeData();
/// fill 1st data of an event
time0 = energy_t;
timeDiff = 0;
if ( detID < NCRYSTAL ){
e[detID] = energy;
e_t[detID] = energy_t;
multi = 1;
}
if ( 100 <= detID && detID < 100 + NBGO ){
bgo[detID-100] = energy;
bgo_t[detID-100] = energy_t;
}
if ( 200 <= detID && detID < 200 + NOTHER){
other[detID-200] = energy;
}
}
}
Bool_t Monitor_raw::Notify()
{
return kTRUE;
}
void Monitor_raw::SlaveBegin(TTree * /*tree*/){
TString option = GetOption();
}
void Monitor_raw::SlaveTerminate(){}
#endif // #ifdef Monitor_raw_cxx

View File

@ -56,7 +56,7 @@ int main(int argn, char **argv){
int timeWindow = 100; int timeWindow = 100;
if( argn >= 3 ) timeWindow = atoi(argv[2]); if( argn >= 3 ) timeWindow = atoi(argv[2]);
printf("======================== Opening input _raw.root \n"); printf(">>> Opening input %s \n", inFileName.Data());
TFile * inFile = new TFile(inFileName, "READ"); TFile * inFile = new TFile(inFileName, "READ");
if( inFile->IsOpen() == false ) { if( inFile->IsOpen() == false ) {
printf("!!!! cannot open file %s \n", inFileName.Data()); printf("!!!! cannot open file %s \n", inFileName.Data());
@ -84,7 +84,7 @@ int main(int argn, char **argv){
printf( "total Entry : %lld \n", totnumEntry); printf( "total Entry : %lld \n", totnumEntry);
printf("======================== Buidling Index using the timestamp\n"); printf(">>> Buidling Index using the timestamp\n");
tree->BuildIndex("e_t"); tree->BuildIndex("e_t");
TTreeIndex *in = (TTreeIndex*) tree->GetTreeIndex(); TTreeIndex *in = (TTreeIndex*) tree->GetTreeIndex();
Long64_t * index = in->GetIndex(); Long64_t * index = in->GetIndex();
@ -92,12 +92,15 @@ int main(int argn, char **argv){
ULong64_t time0; //time-0 for each event ULong64_t time0; //time-0 for each event
int timeDiff; int timeDiff;
printf("======================== Create output tree\n");
TString outFileName = inFileName; TString outFileName = inFileName;
outFileName.Remove(inFileName.First('.')); outFileName.Remove(inFileName.First("_raw"));
outFileName.Append(".root"); outFileName.Append(".root");
if( argn >=4 ) outFileName = argv[3]; if( argn >=4 ) outFileName = argv[3];
printf(">>> out File name : %s\n", outFileName.Data());
printf(">>> Create output tree\n");
TFile * saveFile = new TFile(outFileName, "recreate"); TFile * saveFile = new TFile(outFileName, "recreate");
saveFile->cd(); saveFile->cd();
TTree * newtree = new TTree("tree", "tree"); TTree * newtree = new TTree("tree", "tree");
@ -117,7 +120,7 @@ int main(int argn, char **argv){
ClearTreeData(); ClearTreeData();
printf("======================== Start processing....\n"); printf("================== Start processing....\n");
Float_t Frac = 0.1; ///Progress bar Float_t Frac = 0.1; ///Progress bar
TStopwatch StpWatch; TStopwatch StpWatch;
StpWatch.Start(); StpWatch.Start();

222
armory/MergeEVT.cpp Normal file
View File

@ -0,0 +1,222 @@
#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
#include "../mapping.h"
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;
Long64_t timeDiff;
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;
timeDiff = 0;
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);
}
};
//#############################################
// main
//#############################################
int main(int argn, char **argv) {
printf("=====================================\n");
printf("=== evt --> _raw.root ===\n");
printf("=====================================\n");
if (argn < 3 ) {
printf("Usage :\n");
printf("%s [outFile] [evt1] [evt2] [evt3] ..... \n", argv[0]);
printf("e.g.: \n");
printf("%s hahaha_raw.root haha-000.evt haha-001.evt haha-002.evt\n", argv[0]);
printf("%s hahaha_raw.root `ls haha-*.evt`\n", argv[0]);
return 1;
}
TString outFileName = argv[1];
int nFiles = argn-2;
TString inFileName[nFiles];
for( int i = 0; i < nFiles ; i++){
inFileName[i] = argv[i+2];
printf(" in file - %2d: %s\n", i, inFileName[i].Data());
}
printf(" out file: %s\n", outFileName.Data());
Long64_t measureID = -1;
measurment data;
printf("====================================\n");
//====== ROOT file
TFile * outFile = new TFile(outFileName, "recreate");
TTree * tree = new TTree("tree", "tree");
tree->Branch("evID", &measureID, "data_ID/L");
tree->Branch("id", &data.id, "ID/s");
tree->Branch("e", &data.energy, "crystal_energy/s");
tree->Branch("e_t", &data.time, "crystal_timestamp/l");
long int inFileSize = 0;
long int inFilePos = 0;
TBenchmark gClock;
gClock.Reset();
gClock.Start("timer");
//=========================================
//=========================================
//=========================================
//=========================================
for( int i = 0; i < nFiles; i++){
FILE * inFile = fopen(inFileName[i], "r");
if( inFile == NULL ){
printf("Cannot read file : %s \n", inFileName[i].Data());
continue;
}
Long64_t measureCount = 0;
printf("\033[1;31mProcessing file: %s\033[0m\n", inFileName[i].Data());
TBenchmark clock2;
clock2.Reset();
clock2.Start("timer");
//get file size
fseek(inFile, 0L, SEEK_END);
inFileSize = ftell(inFile);
rewind(inFile); ///back to the File begining
inFilePos = 0;
unsigned int header[4]; //read 4 header, unsigned int = 4 byte = 32 bits.
unsigned long long nWords = 0;
//ULong64_t timeLast = 0;
//=============== Read File
/// while ( ! feof(inFile) ){
while( inFilePos < inFileSize || feof(inFile) ){
fread(header, sizeof(header), 1, inFile);
inFilePos = ftell(inFile);
measureID ++;
measureCount++;
/// 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 ;
data.energy = header[3] & 0xFFFF;
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;
data.id = mapping[data.id];
data.energy = data.energy / 2. ; // factor 2 is the rawe_rebin_factor;
nWords += data.eventLength;
//=== jump to next measurement
if( data.eventLength > 4 ){
fseek(inFile, sizeof(int) * (data.eventLength-4), SEEK_CUR);
inFilePos = ftell(inFile);
}
//event stats, print status every 10000 events
if ( measureID % 10000 == 0 ) {
float tempf = (float)inFileSize/(1024.*1024.*1024.);
gClock.Stop("timer");
double time = gClock.GetRealTime("timer");
gClock.Start("timer");
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",
measureID +1 , (100*inFilePos/inFileSize), tempf, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
}
//cern fill tree
outFile->cd();
tree->Fill();
}
inFilePos = ftell(inFile);
fclose(inFile);
clock2.Stop("timer");
double time = clock2.GetRealTime("timer");
float tempf = (float)inFileSize/(1024.*1024.*1024.);
printf(" measurements: \x1B[32m%lld \x1B[0m | %.3f GB\n", measureCount, tempf);
printf(" Time used:%3.0f min %5.2f sec\n", TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
printf(" Root file size so far: %.4f GB\n", outFile->GetSize()/1024./1024./1024.);
}
gClock.Stop("timer");
double time = gClock.GetRealTime("timer");
gClock.Start("timer");
float tempf = (float)inFileSize/(1024.*1024.*1024.);
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",
measureID+1, (100*inFilePos/inFileSize), tempf, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
//cern save root
outFile->cd();
double totRootSize = outFile->GetSize()/1024./1024./1024.;
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.);
printf(" File size of %s : %.3f GB \n", outFileName.Data(), totRootSize);
}

View File

@ -1,190 +0,0 @@
#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
#include "../mapping.h"
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;
Long64_t timeDiff;
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;
timeDiff = 0;
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);
}
};
//#############################################
// main
//#############################################
int main(int argn, char **argv) {
if (argn != 2 ) {
printf("Usage :\n");
printf("%s [evt File] \n", argv[0]);
return 1;
}
TString inFileName = argv[1];
TString outFileName = inFileName;
outFileName.Remove(inFileName.First('.'));
outFileName.Append("_raw.root");
TBenchmark gClock;
gClock.Reset();
gClock.Start("timer");
Long64_t measureID = -1;
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);
long int inFileSize = ftell(inFile);
rewind(inFile); ///back to the File begining
long int inFilePos = 0;
printf(" in file: %s\n", inFileName.Data());
printf("out file: %s\n", outFileName.Data());
printf("--------------------------------\n");
//====== ROOT file
TFile * outFile = new TFile(outFileName, "recreate");
TTree * tree = new TTree("tree", "tree");
tree->Branch("evID", &measureID, "data_ID/L");
tree->Branch("id", &data.id, "ID/s");
tree->Branch("e", &data.energy, "crystal_energy/s");
tree->Branch("e_t", &data.time, "crystal_timestamp/l");
//tree->Branch("tdiff", &data.timeDiff, "time_Diff/L");
unsigned int header[4]; //read 4 header, unsigned int = 4 byte = 32 bits.
unsigned long long nWords = 0;
//ULong64_t timeLast = 0;
//=============== Read File
/// while ( ! feof(inFile) ){
while( inFilePos < inFileSize || feof(inFile) ){
fread(header, sizeof(header), 1, inFile);
inFilePos = ftell(inFile);
measureID ++;
/// 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 ;
data.energy = header[3] & 0xFFFF;
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;
data.id = mapping[data.id];
data.energy = data.energy / 2. ; // factor 2 is the rawe_rebin_factor;
nWords += data.eventLength;
//=== jump to next measurement
if( data.eventLength > 4 ){
fseek(inFile, sizeof(int) * (data.eventLength-4), SEEK_CUR);
inFilePos = ftell(inFile);
}
//event stats, print status every 10000 events
if ( measureID % 10000 == 0 ) {
float tempf = (float)inFileSize/(1024.*1024.*1024.);
gClock.Stop("timer");
double time = gClock.GetRealTime("timer");
gClock.Start("timer");
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",
measureID +1 , (100*inFilePos/inFileSize), tempf, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
}
//cern fill tree
outFile->cd();
tree->Fill();
}
inFilePos = ftell(inFile);
gClock.Stop("timer");
double time = gClock.GetRealTime("timer");
gClock.Start("timer");
float tempf = (float)inFileSize/(1024.*1024.*1024.);
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",
measureID+1, (100*inFilePos/inFileSize), tempf, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.);
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.);
}

38
armory/makefile Normal file
View File

@ -0,0 +1,38 @@
CC=g++
#all: xia2root xia2ev2_nopart pixie2root scan pxi-time-order
#all: xia2root xia2ev2_nopart pixie2root scan evt2root evt2hist
#all: xia2root to2root MergeEVT evt2hist pxi-time-order ev22txt EventBuilder
#all: xia2root to2root MergeEVT pxi-time-order ev22txt EventBuilder
all: to2root MergeEVT ev22txt EventBuilder pxi-time-order
#this is FSU evt to root
xia2root: ../armory/xia2root.cpp
$(CC) ../armory/xia2root.cpp -o xia2root `root-config --cflags --glibs`
#xia2ev2_nopart: armory/xia2ev2_nopart.cpp
# $(CC) armory/xia2ev2_nopart.cpp -o xia2ev2_nopart
#this is for eventbuild
to2root: ../armory/to2root.cpp
$(CC) ../armory/to2root.cpp -o to2root `root-config --cflags --glibs`
#this is for online root
MergeEVT: ../armory/MergeEVT.cpp
$(CC) ../armory/MergeEVT.cpp -o MergeEVT `root-config --cflags --glibs`
#this is for online spectrums
evt2hist: ../armory/evt2hist.cpp
$(CC) ../armory/evt2hist.cpp -o evt2hist `root-config --cflags --glibs`
pxi-time-order: ../armory/pxi-time-order.c
$(CC) ../armory/pxi-time-order.c -o pxi-time-order
ev22txt: ../armory/ev22txt.cpp
$(CC) ../armory/ev22txt.cpp -o ev22txt
EventBuilder: ../armory/EventBuilder.cpp
$(CC) ../armory/EventBuilder.cpp -o EventBuilder `root-config --cflags --glibs`
clean:
-rm xia2root to2root MergeEVT evt2hist pxi-time-order ev22txt EventBuilder

View File

@ -1,33 +0,0 @@
CC=g++
#all: xia2root xia2ev2_nopart pixie2root scan pxi-time-order
#all: xia2root xia2ev2_nopart pixie2root scan evt2root evt2hist
all: xia2root to2root evt2root evt2hist pxi-time-order ev22txt EventBuilder
#this is FSU evt to root
xia2root: armory/xia2root.cpp
$(CC) armory/xia2root.cpp -o xia2root `root-config --cflags --glibs`
#xia2ev2_nopart: armory/xia2ev2_nopart.cpp
# $(CC) armory/xia2ev2_nopart.cpp -o xia2ev2_nopart
#this is for eventbuild
to2root: armory/to2root.cpp
$(CC) armory/to2root.cpp -o to2root `root-config --cflags --glibs`
#this is for online root
evt2root: armory/evt2root.cpp
$(CC) armory/evt2root.cpp -o evt2root `root-config --cflags --glibs`
#this is for online spectrums
evt2hist: armory/evt2hist.cpp
$(CC) armory/evt2hist.cpp -o evt2hist `root-config --cflags --glibs`
pxi-time-order: armory/pxi-time-order.c
$(CC) armory/pxi-time-order.c -o pxi-time-order
ev22txt: armory/ev22txt.cpp
$(CC) armory/ev22txt.cpp -o ev22txt
EventBuilder: armory/EventBuilder.cpp
$(CC) armory/EventBuilder.cpp -o EventBuilder `root-config --cflags --glibs`

View File

@ -1,14 +1,13 @@
#1/bin/bash #1/bin/bash
DIR=$(pwd) DIR=$(pwd)
DATA_DIR=fsu_run_2021 DATA_DIR=data
sortTime=$DIR/$DATA_DIR/pxi-time-order
if [ $# -eq 0 ] || [ $1 == "-help" ]; then if [ $# -eq 0 ] || [ $1 == "-help" ]; then
echo "$./process_run [Run Folder] [Time Sort] [EventBuild] [Analysis]" echo "$./process_run [Run Folder] [Merge] [isBuildEvents] [Analysis]"
echo " Run Folder = the name of run folder" echo " Run Folder = the name of run folder"
echo " Time Sort = 1/0/-1 || 1 = sort, 0 = not sort, -1 = force sort " echo " Merge = 1/0/-1 || 1 = merge, 0 = not merge, -1 = force merge "
echo " EventBuild = 1/0/-1 || " echo " isBuildEvents = 1/0/-1 || "
echo " Analysis = 1/0/-1 || " echo " Analysis = 1/0/-1 || "
echo "" echo ""
exit 1 exit 1
@ -16,10 +15,10 @@ fi;
RunFolder=$1 RunFolder=$1
TimeSort=1 isMerge=1
if [ $# -gt 1 ]; then TimeSort=$2; fi if [ $# -gt 1 ]; then isMerge=$2; fi
EventBuild=1 isBuildEvents=1
if [ $# -gt 2 ]; then EventBuild=$3; fi if [ $# -gt 2 ]; then isBuildEvents=$3; fi
RED='\033[1;31m' RED='\033[1;31m'
YELLOW='\033[1;33m' YELLOW='\033[1;33m'
@ -29,76 +28,151 @@ BLUE='\033[0;34m'
Cyan='\033[0;36m' Cyan='\033[0;36m'
NC='\033[0m' NC='\033[0m'
echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Merge evt files to ${RunFolder}_raw.root $NC"
if [ $isMerge -eq 1 ]; then
# check is ${RunFolder}"_raw.root" exist
if [ -f ${RunFolder}_raw.root ]; then #_raw.root exist
#check evt DateTime
evtDateTime=`stat -c "%Z" $DATA_DIR/$RunFolder/*.evt | sort -rn | head -1`
rawRootDateTime=`stat -c "%Z" ${RunFolder}"_raw.root" | sort -rn | head -1`
if [ $evtDateTime -gt $rawRootDateTime ]; then #if evt file is newer
./armory/MergeEVT ${RunFolder}"_raw.root" `ls ${DATA_DIR}/${RunFolder}/*.evt`
else
echo "there is no newer evt files"
fi
else
./armory/MergeEVT ${RunFolder}"_raw.root" `ls ${DATA_DIR}/${RunFolder}/*.evt`
fi
fi
if [ $isMerge -eq 0 ]; then
echo -e "$YELLOW skipped by user $NC"
fi
if [ $isMerge -eq -1 ]; then
echo -e "$YELLOW forced by user $NC"
./armory/MergeEVT ${RunFolder}"_raw.root" `ls ${DATA_DIR}/${RunFolder}/*.evt`
fi
echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Merge finished$NC"
echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events$NC"
if [ ${isBuildEvents} -eq 1 ]; then
if [ -f ${RunFolder}.root ]; then # root exist
rawRootDateTime=`stat -c "%Z" ${RunFolder}"_raw.root" | sort -rn | head -1`
rootDateTime=`stat -c "%Z" ${RunFolder}".root" | sort -rn | head -1`
if [ ${rawRootDateTime} -gt ${rootDateTime} ]; then
./armory/EventBuilder ${RunFolder}"_raw.root"
else
echo -e "${RunFolder}.root is up-to-date."
fi
else
./armory/EventBuilder ${RunFolder}"_raw.root"
fi
fi
if [ ${isBuildEvents} -eq 0 ]; then
echo -e "$YELLOW skipped by user $NC"
fi
if [ ${isBuildEvents} -eq -1 ]; then
echo -e "$YELLOW forced by user $NC"
./armory/EventBuilder ${RunFolder}"_raw.root"
fi
echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events finsihed.$NC"
echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Analysis $NC"
#cd $RunFolder #cd $RunFolder
#ls -lhtr *.evt #ls -lhtr *.evt
cd $DATA_DIR/$RunFolder #cd $DATA_DIR/$RunFolder
fileList=$(ls *.evt) #fileList=$(ls *.evt)
numFile=$(ls -lhtr *.evt | wc -l) #numFile=$(ls -lhtr *.evt | wc -l)
count=0 #count=0
#
#cd $DIR
cd $DIR
for a in $fileList
do
count=$((${count}+1))
echo -e "$YELLOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ["$count"/"$numFile"]"
echo -e $a
echo -e "------------------------------------------$NC"
#............ check if *.to file exist, if yes, check timestamp, if timestamp is eariler, sort time
if [ $TimeSort -eq 1 ]; then
evtDateTime=`stat -c "%Z" $DATA_DIR/$RunFolder/$a | sort -rn | head -1`
istoExist=`ls -1 $a.to 2> /dev/null | wc -l`
if [ ${istoExist} -gt 0 ]; then
echo "--- found $a.to"
toDateTime=`stat -c "%Z" $a.to | sort -rn | head -1`
if [ ${evtDateTime} -ge ${toDateTime} ]; then
$sortTime $DATA_DIR/$RunFolder/$a
else
echo "$a.to is generated after $a. skip."
fi
else
echo "cannot find $a.to, sort"
$sortTime $DATA_DIR/$RunFolder/$a
fi
elif [ $TimeSort -eq 0 ]; then
echo "skipped time sort by user."
else
echo "force Time sort"
$sortTime $DATA_DIR/$RunFolder/$a
fi
#............ check if *.root file exist, if yes, check timestamp, if timestamp is eariler, built
if [ $EventBuild -eq 1 ]; then
len=`echo $a | wc -c`
len=$(($len-5))
rootFile=${a:0:$len}".root"
isRootExist=`ls -1 $rootFile 2> /dev/null | wc -l`
if [ ${isRootExist} -gt 0 ]; then
echo "--- found $rootFile"
rootDateTime=`stat -c "%Z" $rootFile | sort -rn | head -1`
if [ ${toDateTime} -ge ${rootDateTime} ]; then
$DIR/pixie2root $a.to
else
echo "$rootFile is generated after $a.to skip."
fi
else
echo "cannot find $rootFile, build events"
$DIR/pixie2root $a.to
fi
elif [ $TimeSort -eq 0 ]; then
echo "skipped event build by user."
else
echo "force event build"
$DIR/pixie2root $a.to
fi
done
fileList=$(ls *.root)
#for a in $fileList
#do
# count=$((${count}+1))
# echo -e "$YELLOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ["$count"/"$numFile"]"
# echo -e $a
# echo -e "------------------------------------------$NC"
#
# #............ check if *.to file exist, if yes, check timestamp, if timestamp is eariler, sort time
#
# if [ $isMerge -eq 1 ]; then
# evtDateTime=`stat -c "%Z" $DATA_DIR/$RunFolder/$a | sort -rn | head -1`
# istoExist=`ls -1 $a.to 2> /dev/null | wc -l`
# if [ ${istoExist} -gt 0 ]; then
# echo "--- found $a.to"
# toDateTime=`stat -c "%Z" $a.to | sort -rn | head -1`
# if [ ${evtDateTime} -ge ${toDateTime} ]; then
# $sortTime $DATA_DIR/$RunFolder/$a
# else
# echo "$a.to is generated after $a. skip."
# fi
# else
# echo "cannot find $a.to, sort"
# $sortTime $DATA_DIR/$RunFolder/$a
# fi
# elif [ $isMerge -eq 0 ]; then
# echo "skipped time sort by user."
# else
# echo "force Time sort"
# $sortTime $DATA_DIR/$RunFolder/$a
# fi
#
#
# #............ check if *.root file exist, if yes, check timestamp, if timestamp is eariler, built
#
# if [ $isBuildEvents -eq 1 ]; then
# len=`echo $a | wc -c`
# len=$(($len-5))
# rootFile=${a:0:$len}".root"
# isRootExist=`ls -1 $rootFile 2> /dev/null | wc -l`
# if [ ${isRootExist} -gt 0 ]; then
# echo "--- found $rootFile"
# rootDateTime=`stat -c "%Z" $rootFile | sort -rn | head -1`
# if [ ${toDateTime} -ge ${rootDateTime} ]; then
# $DIR/pixie2root $a.to
# else
# echo "$rootFile is generated after $a.to skip."
# fi
# else
# echo "cannot find $rootFile, build events"
# $DIR/pixie2root $a.to
# fi
# elif [ $isMerge -eq 0 ]; then
# echo "skipped event build by user."
# else
# echo "force event build"
# $DIR/pixie2root $a.to
# fi
#
#done
#
#fileList=$(ls *.root)