calibration/ICRR/Station1/doInterleave.cxx
00001 //doInterleave by Jonathan Davies UCL 00002 00003 //Includes 00004 #include <iostream> 00005 #include <fstream> 00006 #include <cmath> 00007 00008 //AraRoot Includes 00009 //#include "UsefulAraEvent.h" 00010 //#include "RawAraEvent.h" 00011 //#include "AraEventCalibrator.h" 00012 00013 //AraRoot Includes -- using version 3.0 00014 #include "RawIcrrStationEvent.h" 00015 #include "UsefulIcrrStationEvent.h" 00016 #include "AraEventCalibrator.h" 00017 00018 //ROOT Includes 00019 #include "TTree.h" 00020 #include "TFile.h" 00021 #include "TGraph.h" 00022 #include "TF1.h" 00023 #include "TCanvas.h" 00024 #include "TChain.h" 00025 00026 #include <zlib.h> 00027 00028 int doInterleave(char[FILENAME_MAX], char[FILENAME_MAX], char[FILENAME_MAX], char[FILENAME_MAX], int, int, int); 00029 double findFirstZC(double*, double*, int, int, int*); 00030 00031 using namespace std; 00032 00033 int main(int argc, char *argv[]){ 00034 int freq, minRun, maxRun; 00035 char runDir[200], baseDir[200], temp[200], pedFile[200]; 00036 sprintf(runDir, "/Users/jdavies/ara/data/ara_station1_ICRR_calibration/root/Station1Test"); 00037 sprintf(baseDir, "/Users/jdavies/ara/calibration/ara_station1_ICRR/testing/testing"); 00038 sprintf(pedFile, "/Users/jdavies/ara/data/ara_station1_ICRR_calibration/data/peds/run_003747/peds_1326108401/peds_1326108401.602169.run003747.dat"); 00039 00040 // if(argc<5){ 00041 // printf("Correct usage : ./doBinWidths <frequency> <minRun> <maxRun> <Minus40 / Minus20 / Minus5 / RoomTemp>\n"); 00042 // return -1; 00043 // } 00044 // freq=atoi(argv[1]); 00045 // minRun=atoi(argv[2]); 00046 // maxRun=atoi(argv[3]); 00047 // strcpy(temp, argv[4]); 00048 00049 //jd to comment out 00050 freq=175; 00051 minRun=3763; 00052 maxRun=3769; 00053 sprintf(temp, "Minus20"); 00054 00055 00056 00057 printf("freq %i minRun %i maxRun %i temp %s\n", freq, minRun, maxRun, temp); 00058 00059 doInterleave(runDir, baseDir, temp, pedFile, freq, minRun,maxRun); 00060 00061 return 0; 00062 } 00063 00064 00065 int doInterleave(char runDir[FILENAME_MAX], char baseDir[FILENAME_MAX], char temp[FILENAME_MAX], char ped[FILENAME_MAX], int frequency, int minRun, int maxRun) 00066 { 00067 00068 //1. Open run files & create instance of event calibrator 00069 00070 // printf("Processing run files\n"); 00071 // printf("-----------------------------------------------\n"); 00072 // printf("Frequency %i MHz run %i to run %i\n", frequency, minRun, maxRun); 00073 00074 // RawAraEvent *evPtr=0; 00075 RawIcrrStationEvent *evPtr=0; 00076 00077 TChain *eventTree = new TChain("eventTree"); 00078 char runName[FILENAME_MAX]; 00079 for(int run=minRun; run<=maxRun;run++){ 00080 sprintf(runName, "%s/run%i/event%i.root", runDir, run, run); 00081 eventTree->Add(runName); 00082 } 00083 00084 eventTree->SetBranchAddress("event", &evPtr); 00085 00086 AraEventCalibrator *calibEvent = AraEventCalibrator::Instance(); 00087 00088 // calibEvent->setPedFile("/Users/jdavies/ara/data/ara_station1_ICRR_calibration/data/peds/run_002263/peds_1324969832/peds_1324969832.905582.run002263.dat"); 00089 calibEvent->setPedFile(ped, 1); 00090 00091 //1.1 Create a tree of interleave times - deltaT 00092 00093 TTree *interleaveTree = new TTree("interleaveTree", "interleaveTree"); 00094 Int_t chip=0,pair=0, event=0, noZC1=0, noZC2=0, firstSample1=0, firstSample2=0; 00095 Double_t deltaT=0, rawDeltaT=0; 00096 interleaveTree->Branch("chip", &chip, "chip/I"); 00097 interleaveTree->Branch("pair", &pair, "pair/I"); 00098 interleaveTree->Branch("event", &event, "event/I"); 00099 interleaveTree->Branch("noZC1", &noZC1, "noZC1/I"); 00100 interleaveTree->Branch("noZC2", &noZC2, "noZC2/I"); 00101 interleaveTree->Branch("firstSample1", &firstSample1, "firstSample1/I"); 00102 interleaveTree->Branch("firstSample2", &firstSample2, "firstSample2/I"); 00103 interleaveTree->Branch("deltaT", &deltaT, "deltaT/D"); 00104 interleaveTree->Branch("rawDeltaT", &rawDeltaT, "rawDeltaT/D"); 00105 00106 //1.2 Create some histograms to populate with interleave times 00107 00108 TH1D *histInterleave[2][4]; 00109 char histName[FILENAME_MAX]; 00110 for(chip=0;chip<2;chip++){ 00111 for(pair=0;pair<4;pair++){ 00112 sprintf(histName, "histInterleaveChip%iPair%i", chip, pair); 00113 histInterleave[chip][pair] = new TH1D(histName, histName, 1000, -1, 0); 00114 } 00115 } 00116 00117 00118 //2. Run through the event tree and get events 00119 00120 Long64_t numEntries=eventTree->GetEntries(); 00121 // if(numEntries>45000) numEntries=45000; 00122 Long64_t starEvery=numEntries/80; 00123 if(starEvery==0) starEvery++; 00124 00125 printf("No of entries is %i\n", numEntries); 00126 00127 for(event=0;event<numEntries;event++) { 00128 if(event%starEvery==0) fprintf(stderr, "*"); 00129 00130 //printf("*%i", event);//std::cerr << "*"; 00131 eventTree->GetEntry(event); 00132 //2.1 skip any events where the rap around happens in the first / last 20 samples 00133 //2.2 Interleaved channels are consecutive channels on a chip = 4 pairs 00134 // UsefulAraEvent *realEvent = new UsefulAraEvent(evPtr, AraCalType::kFirstCalib); 00135 UsefulIcrrStationEvent *realEvent = new UsefulIcrrStationEvent(evPtr, AraCalType::kFirstCalib); 00136 for(chip=0; chip<2; chip++){ 00137 for(pair=0;pair<4; pair++){ 00138 firstSample1=evPtr->getEarliestSample(2*pair+chip*9); 00139 firstSample2=evPtr->getEarliestSample(2*pair+chip*9+1); 00140 if(firstSample1<20||firstSample2<20) continue; 00141 TGraph *chan1 = realEvent->getGraphFromElecChan(2*pair+chip*9); 00142 TGraph *chan2 = realEvent->getGraphFromElecChan(2*pair+chip*9+1); 00143 00144 //zero-mean the waveform 00145 double *tempx1=chan1->GetX(); 00146 double *tempy1=chan1->GetY(); 00147 double *tempx2=chan2->GetX(); 00148 double *tempy2=chan2->GetY(); 00149 00150 //2.3 Zero-mean the waveforms 00151 int n1 =chan1->GetN(); 00152 int n2 =chan2->GetN(); 00153 double offset=0; 00154 for(int i=0; i<n1; i++){ 00155 offset+=tempy1[i]/n1; 00156 } 00157 for(int i=0; i<n1; i++){ 00158 tempy1[i]=tempy1[i]-offset; 00159 } 00160 offset=0; 00161 for(int i=0; i<n2; i++){ 00162 offset+=tempy2[i]/n2; 00163 } 00164 for(int i=0; i<n2; i++){ 00165 tempy2[i]=tempy2[i]-offset; 00166 } 00167 00168 //chan1Calib and chan2Calib are the calibrated waveforms 00169 //2.4 find the phase of each 00170 00171 deltaT=findFirstZC(tempx1,tempy1, n1, frequency, &noZC1); 00172 deltaT-=findFirstZC(tempx2,tempy2, n2, frequency, &noZC2); 00173 rawDeltaT=deltaT; 00174 00175 if(noZC1-noZC2!=0) continue; 00176 while(deltaT<-1./(frequency*0.001)) deltaT+=1./(frequency*0.001); 00177 while(deltaT>1./(frequency*0.001)) deltaT-=1./(frequency*0.001); 00178 while(deltaT>0) deltaT-=1./(frequency*0.001); 00179 00180 interleaveTree->Fill(); 00181 histInterleave[chip][pair]->Fill(deltaT); 00182 00183 }//pair 00184 }//chip 00185 // if(realEvent) delete realEvent; 00186 delete realEvent; 00187 00188 }//event 00189 00190 // delete calibEvent; 00191 00192 // printf("Completed the event loop/n"); 00193 00194 00195 //3.1 Write the output to calib file 00196 00197 ofstream textOut; 00198 char calibName[FILENAME_MAX]; 00199 sprintf(calibName, "%s/calib/%s/interleaveFile.txt", baseDir, temp); 00200 textOut.open(calibName); 00201 00202 // printf("Opened the calib file\n"); 00203 00204 for(chip=0;chip<2;chip++){ 00205 for(pair=0;pair<4;pair++){ 00206 textOut << chip << "\t" << pair << "\t" << histInterleave[chip][pair]->GetMean() << endl; 00207 } 00208 } 00209 00210 textOut.close(); 00211 00212 //3.2 Create a root file for the output 00213 00214 char outName[FILENAME_MAX]; 00215 sprintf(outName, "%s/root/%s/Interleave%iMHzTun%iTo%i.root", baseDir, temp, frequency, minRun, maxRun); 00216 TFile *outFile= new TFile(outName, "RECREATE"); 00217 00218 // printf("Created the output root file\n"); 00219 00220 00221 interleaveTree->Write(); 00222 for(chip=0;chip<2;chip++){ 00223 for(pair=0;pair<4;pair++){ 00224 histInterleave[chip][pair]->Write(); 00225 if(histInterleave[chip][pair]) delete histInterleave[chip][pair]; 00226 } 00227 } 00228 00229 outFile->Write(); 00230 outFile->Close(); 00231 00232 return 0; 00233 } 00234 00235 Double_t findFirstZC(double *fX, double *fY, int noSamples, int frequency, int *returnZC){ 00236 Int_t noZC=0; 00237 Double_t firstZC=0, phase=0; 00238 00239 for(int sample=0;sample<noSamples-1;sample++){ 00240 if(fY[sample]<0&&fY[sample+1]>0){ 00241 phase=(fY[sample])*(fX[sample]-fX[sample+1])/(fY[sample+1]-fY[sample])+fX[sample]-frequency*noZC; 00242 firstZC+=phase; 00243 noZC++; 00244 } 00245 } 00246 *returnZC=noZC; 00247 return firstZC/noZC; 00248 } 00249
Generated on Tue Jul 16 16:58:02 2013 for ARA ROOT v3.10 Software by
