From bd453a99e0fb916eb8d5b7ae69928a2dfd7021d5 Mon Sep 17 00:00:00 2001 From: "Ryan@WorkStation" Date: Fri, 7 Jan 2022 14:46:32 -0500 Subject: [PATCH] added skip event in to2root --- Analyzer.C | 52 +++++---- armory/to2root.cpp | 50 ++++++--- process_run | 269 ++++++++++++++++++++++++++++++++------------- 3 files changed, 257 insertions(+), 114 deletions(-) diff --git a/Analyzer.C b/Analyzer.C index d451509..5fb7522 100644 --- a/Analyzer.C +++ b/Analyzer.C @@ -34,6 +34,8 @@ TH2F * hcoin; TH2F * hcrystalBGO; +TH1F * hTDiff; + ///----- after calibration and BGO veto TH2F * heCalVID; TH1F * heCal[NCRYSTAL]; @@ -58,6 +60,8 @@ void Analyzer::Begin(TTree * tree){ heVID = new TH2F("heVID", "e vs ID; det ID; e [ch]", NCRYSTAL, 0, NCRYSTAL, rawEnergyRange[1] - rawEnergyRange[0], rawEnergyRange[0], rawEnergyRange[1]); heCalVID = new TH2F("heCalVID", Form("eCal vs ID (BGO veto > %.1f); det ID; Energy [keV]", BGO_threshold), NCRYSTAL, 0, NCRYSTAL, (energyRange[2] - energyRange[1])/energyRange[0], energyRange[1], energyRange[2]); + hTDiff = new TH1F("hTDiff", "data time different within an event; tick [10 ns]", 110, 0, 110); + heVID->SetNdivisions(-409, "X"); heCalVID->SetNdivisions(-409, "X"); @@ -113,32 +117,39 @@ Bool_t Analyzer::Process(Long64_t entry){ for( int i = 0; i < NCRYSTAL; i++) eCal[i] = TMath::QuietNaN(); + ///printf("---------------------------- %d \n", multi); ///=========== Looping data for the event for( int i = 0; i < multi ; i ++){ + + int id = detID[i]; + + ///printf("%d %f %llu\n", id, e[i], e_t[i]); //======== Fill raw data - if( detID[i] < 100 ){ /// gamma data - heVID->Fill( detID[i], e[i]); - he[detID[i]]->Fill(e[i]); + if( 0 <= id && id < NCRYSTAL ){ /// gamma data + heVID->Fill( id, e[i]); + he[id]->Fill(e[i]); for ( int j = i + 1; j < multi; j++){ - if( 100 <= detID[j] && detID[j] < 200 ) hcrystalBGO->Fill(detID[i], detID[j]-100); /// crystal - BGO coincident + if( 100 <= detID[j] && detID[j] < 200 ) hcrystalBGO->Fill(id, detID[j]-100); /// crystal - BGO coincident - if( detID[j] < 100 ) hcoin->Fill(detID[i], detID[j]); /// crystal-crystal coincident + if( detID[j] < 100 ) hcoin->Fill(id, detID[j]); /// crystal-crystal coincident } } - if ( 100 < detID[i] && detID[i] < 200 ){ /// BGO data + if ( 100 < id && id < 200 ){ /// BGO data } + if ( i > 0 ) hTDiff->Fill( e_t[i] - e_t[0]); + //======== BGO veto bool dropflag = false; - if( detID[i] < 100 && multi > 1) { + if( id < NCRYSTAL && multi > 1) { for( int j = i + 1; j < multi; j++){ - if( detID[j] > 100 && (detID[j]-100)*4 < detID[i] && detID[i] < (detID[j]-100 +1)*4) { + if( detID[j] >= 100 && (detID[j]-100)*4 <= id && id < (detID[j]-100 +1)*4) { dropflag = true; break; } @@ -146,27 +157,28 @@ Bool_t Analyzer::Process(Long64_t entry){ } if( dropflag ) return kTRUE; - //========= apply correction - //int order = (int) eCorr[detID].size(); - //for( int i = 0; i < order ; i++){ - // eCal[detID] += eCorr[detID][i] * TMath::Power(e[detID], i); - //} - - if( detID[i] < 100 ) { + + if( 0<= id && id < NCRYSTAL ) { if( e_corr == "" ){ - eCal[detID[i]] = e[i]; + eCal[id] = e[i]; }else{ - eCal[detID[i]] = eCorr[detID[i]][0] + eCorr[detID[i]][1] * e[i]; + ///========= apply correction + int order = (int) eCorr[id].size(); + eCal[id] = 0; + for( int k = 0; k < order ; k++){ + eCal[id] += eCorr[id][k] * TMath::Power(e[i], k); + } } - heCalVID->Fill( detID[i], eCal[detID[i]]); - heCal[detID[i]]->Fill(eCal[detID[i]]); + heCalVID->Fill( id, eCal[id]); + heCal[id]->Fill(eCal[id]); for ( int j = i + 1; j < multi; j++){ - if( 100 <= detID[j] && detID[j] < 200 ) hcrystalBGO_G->Fill(detID[i], detID[j]-100); /// crystal - BGO coincident + if( 100 <= detID[j] && detID[j] < 200 ) hcrystalBGO_G->Fill(id, detID[j]-100); /// crystal - BGO coincident } } + } diff --git a/armory/to2root.cpp b/armory/to2root.cpp index 5248cb5..9e8fa7f 100644 --- a/armory/to2root.cpp +++ b/armory/to2root.cpp @@ -31,8 +31,6 @@ #define RAWE_REBIN_FACTOR 2.0 // Rebin 32k pixie16 spectra to something smaller to fit better into 8k. -#define MAXMULTI 100 - #include "../mapping.h" ///////////////////// @@ -80,38 +78,49 @@ int main(int argc, char **argv) { printf("=====================================\n"); // Check that the corrent number of arguments were provided. - if (argc != 2 && argc != 3 ) { + if (argc != 2 && argc != 3 && argc != 4 && argc != 5) { printf("Incorrect number of arguments:\n"); - printf("%s [*.to File] [timeWindow] \n", argv[0]); + printf("%s [*.to File] \n", argv[0]); printf(" timeWindow : number of tick, 1 tick = 10 ns. default = 100 \n"); + printf(" fraction : 0 to 100, default 100, last fraction of evt.to to root \n"); + printf(" e.g. 10, last 10%% of the evt.to to root \n"); + printf(" saveFile : default is replace evt.to with root \n"); return 1; } //CERN ROOT things TString inFileName = argv[1]; - TString outFileName = inFileName; - int timeWindow = 100; if( argc >= 3 ) timeWindow = atoi(argv[2]); + int frac = 100; + if( argc >= 4 ) frac = abs(atoi(argv[3])); + + TString outFileName = inFileName; outFileName.Remove(inFileName.First('.')); outFileName.Append(".root"); + int pos = inFileName.Last('/'); + outFileName.Remove(0, pos+1); + if( argc >= 5) outFileName = argv[4]; printf(" in file : %s \n", inFileName.Data()); - printf(" our file : %s \n", outFileName.Data()); + printf(" our file : \033[1;31m%s\033[m\n", outFileName.Data()); printf(" max number of detector channal: %d \n", MAX_ID); + + printf(" Skipping the frist \033[0;34m %d %% \033[m of data \n", 100 - frac); + printf("------------------------ Event building time window : %d tics = %d nsec \n", timeWindow, timeWindow*10); - + TFile * outRootFile = new TFile(outFileName, "recreate"); outRootFile->cd(); TTree * tree = new TTree("tree", "tree"); unsigned long long evID = 0; int multi = 0; - int id[MAXMULTI] = {0}; - double e[MAXMULTI] = {TMath::QuietNaN()}; - unsigned long long e_t[MAXMULTI] = {0}; + int id[MAX_ID] = {0}; + double e[MAX_ID] = {TMath::QuietNaN()}; + unsigned long long e_t[MAX_ID] = {0}; Int_t multiCry = 0 ; /// this is total multiplicity for all crystal //unsigned short pileup[MAXMULTI]; @@ -144,7 +153,7 @@ int main(int argc, char **argv) { ///////////////////// // MAIN WHILE LOOP // ///////////////////// - while (1) { //main while loop + while (!feof(fpr)) { //main while loop ///////////////////////////////// // UNPACK DATA AND EVENT BUILD // @@ -152,18 +161,24 @@ int main(int argc, char **argv) { long long int etime = -1; long long int tdif = -1; - int sevtmult=0; while (1) { //get subevents and event build for one "event" if (fread(sub, sizeof(int)*HEADER_LENGTH, 1, fpr) != 1) break; + fprpos = ftell(fpr); + if ( fprpos < fprsize * ( 1. - frac/100.) ) { + //printf("%ld / %ld \n", fprpos, fprsize); + data.elen = (sub[0] & 0x7FFE0000) >> 17; + fseek(fpr, sizeof(int)*(data.elen - HEADER_LENGTH), SEEK_CUR); + continue; + } data.chn = sub[0] & 0xF; /// channel in digitizer data.sln = (sub[0] & 0xF0) >> 4; /// digitizer ID data.crn = (sub[0] & 0xF00) >> 8; /// crate data.id = data.crn*MAX_BOARDS_PER_CRATE*MAX_CHANNELS_PER_BOARD + (data.sln-BOARD_START)*MAX_CHANNELS_PER_BOARD + data.chn; data.hlen = (sub[0] & 0x1F000) >> 12; - data.elen = (sub[0] & 0x7FFE0000) >> 17; + data.elen = (sub[0] & 0x7FFE0000) >> 17; data.fcode = (sub[0] & 0x80000000) >> 31; data.time = ( (long long int)(sub[2] & 0xFFFF) << 32) + sub[1]; data.ctime = (sub[2] & 0x7FFF0000) >> 16; @@ -175,11 +190,11 @@ int main(int argc, char **argv) { tempf = (float)data.e/RAWE_REBIN_FACTOR;// + RAND; data.e = (int)tempf; - + //Set reference time for event building if (etime == -1) { etime = data.time; - tdif = 0; + tdif = 0; multi = 0; }else { tdif = data.time - etime; @@ -204,11 +219,11 @@ int main(int argc, char **argv) { pileUpCount++; } - //more data than just the header; read entire sub event, first rewind, then read data.elen fseek(fpr, -sizeof(int)*HEADER_LENGTH, SEEK_CUR); //if (fread(sub, sizeof(int)*dataBlock[sevtmult].elen, 1, fpr) != 1) break; if (fread(sub, sizeof(int)*data.elen, 1, fpr) != 1) break; + /** //trace @@ -268,7 +283,6 @@ int main(int argc, char **argv) { printf("Total dataBlock: \x1B[32m%llu \x1B[31m(%d%% pileup)\x1B[0m\nTotal Events: \x1B[32m%llu (%.1f )\x1B[0m\nPercent Complete: \x1B[32m%ld%% of %.3f GB\x1B[0m\nTime used:%3.0f min %5.2f sec\033[3A\r", dataCount, (int)((100*pileUpCount)/dataCount), evID+1, (float)dataCount/((float)evID+1), (100*fprpos/fprsize), tempf, TMath::Floor(time/60.), time - TMath::Floor(time/60.)*60.); } - outRootFile->cd(); tree->Fill(); diff --git a/process_run b/process_run index b2e395b..00667ec 100755 --- a/process_run +++ b/process_run @@ -1,22 +1,32 @@ #!/bin/bash DIR=$(pwd) -DATA_DIR=data - TIMEWINDOW=100 +Method=1 #1 : evt -> evt.to -> root , 2 : evt -> _raw.root -> root + +if [ $Method -eq 1 ]; then + DATA_DIR=. +else + DATA_DIR=data +fi + if [ $# -eq 0 ] || [ $1 == "-help" ]; then - echo "$./process_run [Run Folder] [Merge] [BuildEvents] [Analysis]" + echo "$./process_run [RunFile] [Merge] [BuildEvents] [Analysis]" echo " Run Folder = the name of run folder" echo " Merge = 1/0/-1 || 1 = merge, 0 = not merge, -1 = force merge " - echo " BuildEvents = 1/0/-1 || " + echo " BuildEvents = 1/0/-1 || if |x| > 1, take last x %, e.g. 10, last 10 %" echo " Analysis = 1/0/-1 || " echo "" - ls -l --color $DATA_DIR/ + if [ $Method -eq 1 ]; then + ls -l --color $DATA_DIR/ + fi + exit 1 fi; +RunFile=$1 RunFolder=$1 isMerge=1 @@ -34,85 +44,192 @@ BLUE='\033[0;34m' Cyan='\033[0;36m' NC='\033[0m' -if [ -f $DATA_DIR/$RunFolder/*.evt ]; then - echo -e "found evt files." -else - echo -e "cannot found any evt files in $DATA_DIR/$RunFolder/. Abort." - exit -fi - -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 +################################### method 1 evt -> evt.to -> root +if [ $Method -eq 1 ]; then + if [ -f $RunFile ]; then + echo -e "found evt files. "${RunFile} else - - ./armory/MergeEVT ${RunFolder}"_raw.root" `ls ${DATA_DIR}/${RunFolder}/*.evt` - + echo -e "!!!!! cannot found evt files ${RunFile}. Abort." + exit 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" $TIMEWINDOW + + pos=`expr index "${RunFile}" /` + temp=${RunFile:pos} + pos=`expr index "${temp}" /` + toFile=${temp:pos}".to" + pos=`expr index "${toFile}" .` + rootFile=${toFile:0:pos}"root" + + #echo ${RunFile} + #echo ${toFile} + #echo ${rootFile} + + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Time sort evt to evt.to $NC" + if [ $isMerge -eq 1 ]; then + if [ -f ${toFile} ]; then #_raw.root exist + #check evt DateTime + evtDateTime=`stat -c "%Z" ${RunFile} | sort -rn | head -1` + toDateTime=`stat -c "%Z" ${toFile} | sort -rn | head -1` + if [ $evtDateTime -gt $toDateTime ]; then #if evt file is newer + ./armory/pxi-time-order ${RunFile} + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Time sort finished$NC" + else + echo "there is no newer evt files" + fi else - echo -e "${RunFolder}.root is up-to-date." + ./armory/pxi-time-order ${RunFile} + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Time sort finished$NC" 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/pxi-time-order ${RunFile} + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Time sort finished$NC" + fi + + + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events $NC" + if [ ${isBuildEvents} -ge 1 ]; then + if [ -f ${rootFolder} ]; then # root exist + toDateTime=`stat -c "%Z" ${toFolder} | sort -rn | head -1` + rootDateTime=`stat -c "%Z" ${rootFolder} | sort -rn | head -1` + if [ ${toDateTime} -gt ${rootDateTime} ]; then + + if [ ${isBuildEvents} -gt 1 ]; then + ./armory/to2root ${toFile} $TIMEWINDOW ${isBuildEvents} + else + ./armory/to2root ${toFile} $TIMEWINDOW + fi + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events finsihed.$NC" + else + echo -e "${rootFile} is up-to-date." + fi + else + ./armory/to2root ${toFile} $TIMEWINDOW + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events finsihed.$NC" + fi + fi + + if [ ${isBuildEvents} -eq 0 ]; then + echo -e "$YELLOW skipped by user $NC" + fi + + if [ ${isBuildEvents} -le -1 ]; then + echo -e "$YELLOW forced by user $NC" + if [ ${isBuildEvents} -lt 1 ]; then + ./armory/to2root ${toFile} $TIMEWINDOW ${isBuildEvents} + else + ./armory/to2root ${toFile} $TIMEWINDOW + fi + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events finsihed.$NC" + fi + + + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Analysis $NC" + if [ ${isAnalysis} -eq 1 ]; then + root -l "process_run.c(\"${rootFile}\")" + fi + + if [ ${isAnalysis} -eq 0 ]; then + echo -e "$YELLOW skipped by user $NC" + fi + + if [ ${isAnalysis} -eq -1 ]; then + echo -e "$YELLOW forced by user $NC" + root -l "process_run.c(\"${rootFile}\")" + fi + + +fi + + + +################################### method 2 evt -> _raw.root -> root +if [ $Method -eq 2 ]; then + + if [ -f $DATA_DIR/$RunFolder/*.evt ]; then + echo -e "found evt files." else - ./armory/EventBuilder ${RunFolder}"_raw.root" $TIMEWINDOW - fi + echo -e "cannot found any evt files in $DATA_DIR/$RunFolder/. Abort." + exit + fi + + 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" $TIMEWINDOW + else + echo -e "${RunFolder}.root is up-to-date." + fi + else + ./armory/EventBuilder ${RunFolder}"_raw.root" $TIMEWINDOW + 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" $TIMEWINDOW + fi + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events finsihed.$NC" + + + echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Analysis $NC" + if [ ${isAnalysis} -eq 1 ]; then + root -l "process_run.c(\"${RunFolder}.root\")" + fi + + if [ ${isAnalysis} -eq 0 ]; then + echo -e "$YELLOW skipped by user $NC" + fi + + if [ ${isAnalysis} -eq -1 ]; then + echo -e "$YELLOW forced by user $NC" + root -l "process_run.c(\"${RunFolder}.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" $TIMEWINDOW -fi -echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Build Events finsihed.$NC" -echo -e "$RED>>> `date` >>>>>>>>>>>>>>>>>>>>>>> Analysis $NC" -if [ ${isAnalysis} -eq 1 ]; then - root -l "process_run.c(\"${RunFolder}.root\")" -fi - -if [ ${isAnalysis} -eq 0 ]; then - echo -e "$YELLOW skipped by user $NC" -fi - -if [ ${isAnalysis} -eq -1 ]; then - echo -e "$YELLOW forced by user $NC" - root -l "process_run.c(\"${RunFolder}.root\")" -fi -