calibration/ATRI/thirdCalibTry.cxx
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 //AraRoot Includes 00012 #include "UsefulAtriStationEvent.h" 00013 #include "RawAtriStationEvent.h" 00014 #include "araSoft.h" 00015 00016 00017 //Root Includes 00018 #include "TTree.h" 00019 #include "TFile.h" 00020 #include "TH1.h" 00021 #include "TTree.h" 00022 #include "TMath.h" 00023 #include "TCanvas.h" 00024 00025 00026 //Standard Includes 00027 #include <iostream> 00028 #include <fstream> 00029 00030 00031 //Global Variables 00032 Double_t inter_sample_times[DDA_PER_ATRI][RFCHAN_PER_DDA][2][SAMPLES_PER_BLOCK*2]={{{{0}}}}; //[dda][chan][capArray][sample] 00033 Int_t inter_sample_index[DDA_PER_ATRI][RFCHAN_PER_DDA][2][SAMPLES_PER_BLOCK*2]={{{{0}}}}; //[dda][chan][capArray][sample] 00034 Double_t epsilon_times[DDA_PER_ATRI][RFCHAN_PER_DDA][2]={{{0}}}; //[dda][chan][capArray] 00035 00036 00037 //Prototype Functions 00038 TGraph* zeroMean(TGraph*); 00039 Int_t estimate_phase(TGraph*, Double_t, Double_t*, Int_t*); 00040 Int_t estimate_phase_two_blocks(TGraph*, Double_t, Double_t*, Int_t*); 00041 TGraph *getBlockGraph(TGraph*, Int_t); 00042 TGraph *getTwoBlockGraph(TGraph*, Int_t); 00043 00044 TGraph* apply_bin_calibration(TGraph*, Int_t, Int_t, Int_t); 00045 TGraph* apply_bin_calibration_two_blocks(TGraph*, Int_t, Int_t, Int_t); 00046 Int_t save_inter_sample_times(char*); 00047 Int_t save_epsilon_times(char*); 00048 Int_t save_inter_sample_times_even(char*); 00049 Int_t save_epsilon_times_even(char*); 00050 00051 Int_t findLastZC(TGraph*, Double_t, Double_t*); 00052 Int_t findFirstZC(TGraph*, Double_t, Double_t*); 00053 00054 TGraph* getHalfGraph(TGraph*, Int_t); 00055 TGraph* getHalfGraphTwoBlocks(TGraph*, Int_t); 00056 Int_t calibrateDdaChan(char*, Int_t, Int_t, Double_t, bool); 00057 00058 00059 int main(int argc, char **argv) 00060 { 00061 Int_t runNum=0, pedNum=0; 00062 Double_t freq=0; 00063 char baseName[FILENAME_MAX]; 00064 bool debug=false; 00065 00066 if(argc<5) { 00067 std::cerr << "Usage: " << argv[0] << " <baseDir> <runNum> <pedNum> <freq in GHz>\n"; 00068 return 1; 00069 } 00070 00071 runNum=atoi(argv[2]); 00072 pedNum=atoi(argv[3]); 00073 freq=atof(argv[4]); 00074 if(argc>5){ 00075 if(atoi(argv[5])) debug=true; 00076 } 00077 00078 return calibrateDdaChan(argv[1], runNum, pedNum, freq, debug); 00079 00080 } 00081 00082 00083 Int_t calibrateDdaChan(char* baseDirName, Int_t runNum, Int_t pedNum, Double_t freq, bool debug=false){ 00084 Double_t period=1./freq; 00085 Int_t chanIndex=0; 00086 Int_t dda=0, chan=0; 00087 char runFileName[FILENAME_MAX]; 00088 char pedFileName[FILENAME_MAX]; 00089 sprintf(runFileName, "%s/root/run%i/event%i.root", baseDirName, runNum, runNum); 00090 sprintf(pedFileName, "%s/raw_data/run_%06i/pedestalValues.run%06d.dat", baseDirName, pedNum, pedNum); 00091 00092 // /unix/ara/data/ntu2012/StationTwo/raw_data/run_000464/pedestalWidths.run000464.dat 00093 printf("runFileName %s\npedFileName %s\nfreq %f GHz\n", runFileName, pedFileName, freq); 00094 00095 TFile *fp = new TFile(runFileName); 00096 if(!fp) { 00097 std::cerr << "Can't open file\n"; 00098 return -1; 00099 } 00100 TTree *eventTree = (TTree*) fp->Get("eventTree"); 00101 if(!eventTree) { 00102 std::cerr << "Can't find eventTree\n"; 00103 return -1; 00104 } 00105 RawAtriStationEvent *evPtr=0; 00106 eventTree->SetBranchAddress("event",&evPtr); 00107 Long64_t numEntries=eventTree->GetEntries(); 00108 00109 if(debug) std::cout << "Number of entries in file is " << numEntries << std::endl; 00110 00111 Long64_t starEvery=numEntries/80; 00112 if(starEvery==0) starEvery++; 00113 00114 Int_t stationId=0; 00115 eventTree->GetEntry(0); 00116 stationId= evPtr->stationId; 00117 if(debug) std::cerr << "stationId " << stationId << "\n"; 00118 AraEventCalibrator *calib = AraEventCalibrator::Instance(); 00119 calib->setAtriPedFile(pedFileName, stationId); 00120 00121 //General output stuff 00122 char outFileName[FILENAME_MAX]; 00123 sprintf(outFileName, "%s/root/run%i/calibThirdTry.root", baseDirName, runNum); 00124 TFile *outFile = new TFile(outFileName, "RECREATE"); 00125 Int_t capArray=0,thisCapArray=0, block=0,half=0,sample=0; 00126 Int_t numEvents[DDA_PER_ATRI][RFCHAN_PER_DDA]={{0}}; 00127 00128 //BinWidth Histos 00129 TH1F *histBinWidth[DDA_PER_ATRI][RFCHAN_PER_DDA][2]; 00130 char histName[FILENAME_MAX]; 00131 for(half=0;half<2;half++) { 00132 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00133 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00134 sprintf(histName,"histBinWidth_dda%d_chan%d_%d",dda, chan,half); 00135 histBinWidth[dda][chan][half] = new TH1F(histName,histName,SAMPLES_PER_BLOCK,-0.5,SAMPLES_PER_BLOCK-0.5); 00136 00137 } 00138 } 00139 } 00140 00141 //Interleave Histos 00142 TTree *lagTree = new TTree("lagTree","lagTree"); 00143 Int_t noZCs[2]={0}; 00144 Double_t lag1,lag0,deltaLag; 00145 lagTree->Branch("dda",&dda,"dda/I"); 00146 lagTree->Branch("chan",&chan,"chan/I"); 00147 lagTree->Branch("block",&block,"block/I"); 00148 lagTree->Branch("noZCs", &noZCs, "noZCs[2]/I"); 00149 lagTree->Branch("lag1",&lag1,"lag1/D"); 00150 lagTree->Branch("lag0",&lag0,"lag0/D"); 00151 lagTree->Branch("deltaLag",&deltaLag,"deltaLag/D"); 00152 Double_t lag[DDA_PER_ATRI][RFCHAN_PER_DDA] = {{0}}; 00153 TH1F *lagHist[DDA_PER_ATRI][RFCHAN_PER_DDA]={{0}}; 00154 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00155 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00156 sprintf(histName,"lag_hist_dda%i_chan%i",dda, chan); 00157 lagHist[dda][chan] = new TH1F(histName,histName,200,-1.0,1.0); 00158 } 00159 } 00160 00161 00162 00163 00164 //Epsilon Histos 00165 TTree *epsilonTree = new TTree("epsilonTree", "epsilonTree"); 00166 Double_t epsilon=0; 00167 Double_t firstZC=0, lastZC=0; 00168 Int_t firstZCCount=0, lastZCCount=0; 00169 Int_t atriBlock=0; 00170 TH1* histEpsilon[DDA_PER_ATRI][RFCHAN_PER_DDA][2]={{{0}}}; //[dda][chan][half]; 00171 epsilonTree->Branch("dda",&dda,"dda/I"); 00172 epsilonTree->Branch("chan",&chan,"chan/I"); 00173 epsilonTree->Branch("block",&block,"block/I"); 00174 epsilonTree->Branch("epsilon",&epsilon,"epsilon/D"); 00175 epsilonTree->Branch("firstZC",&firstZC,"firstZC/D"); 00176 epsilonTree->Branch("lastZC",&lastZC,"lastZC/D"); 00177 epsilonTree->Branch("lastZCCount",&lastZCCount,"lastZCCount/I"); 00178 epsilonTree->Branch("firstZCCount",&firstZCCount,"firstZCCount/I"); 00179 epsilonTree->Branch("half", &half, "half/I"); 00180 epsilonTree->Branch("atriBlock", &atriBlock, "atriBlock/I"); 00181 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00182 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00183 for(half=0;half<2;half++){ 00184 sprintf(histName,"epsilon_hist_dda%i_chan%i_half%i",dda, chan,half); 00185 histEpsilon[dda][chan][half] = new TH1F(histName,histName,600,-3.0,3.0); 00186 } 00187 } 00188 } 00189 00190 00191 Double_t time=0, deltaTime=0; 00192 Int_t index=0; 00193 TTree *binWidthsTree = new TTree("binWidthsTree", "binWidthsTree"); 00194 binWidthsTree->Branch("dda", &dda, "dda/I"); 00195 binWidthsTree->Branch("chan", &chan, "chan/I"); 00196 binWidthsTree->Branch("capArray", &capArray, "capArray/I"); 00197 binWidthsTree->Branch("sample", &sample, "sample/I"); 00198 binWidthsTree->Branch("time", &time, "time/D"); 00199 binWidthsTree->Branch("index", &index, "index/I"); 00200 binWidthsTree->Branch("epsilon", &epsilon, "epsilon/D"); 00201 binWidthsTree->Branch("deltaTime", &deltaTime, "deltaTime/D"); 00202 00203 //BinWidth Calibration 00204 for(int entry=0;entry<numEntries;entry++){ 00205 if(entry%starEvery==0) std::cerr <<"*"; 00206 eventTree->GetEntry(entry); 00207 UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed); 00208 capArray = evPtr->blockVec[0].getCapArray(); //capArray of first block 00209 00210 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00211 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00212 chanIndex=chan+RFCHAN_PER_DDA*dda; 00213 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00214 TGraph *grZeroMean = zeroMean(gr); 00215 Int_t numSamples = grZeroMean->GetN(); 00216 Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK; 00217 00218 00219 for(block=0; block<numBlocks-1; block++){ //FIXME -- Only use blocks > 0 00220 if(block%2) thisCapArray=1-capArray; 00221 else thisCapArray=capArray; 00222 if(thisCapArray==1) continue; 00223 TGraph *grTwoBlock = getTwoBlockGraph(grZeroMean, block); 00224 numEvents[dda][chan]++; 00225 for(half=0;half<2;half++){ 00226 TGraph *grHalf = getHalfGraphTwoBlocks(grTwoBlock, half); 00227 Double_t *yVals = grHalf->GetY(); 00228 for(sample=0;sample<SAMPLES_PER_BLOCK-1;sample++){ 00229 Double_t val1 = yVals[sample]; 00230 Double_t val2 = yVals[sample+1]; 00231 if(val1<0 && val2>0){ 00232 histBinWidth[dda][chan][half]->Fill(sample); 00233 } 00234 else if(val1>0 && val2<0){ 00235 histBinWidth[dda][chan][half]->Fill(sample); 00236 } 00237 else if(val1==0 || val2==0){ 00238 histBinWidth[dda][chan][half]->Fill(sample, 0.5); 00239 } 00240 }//sample 00241 if(grHalf) delete grHalf; 00242 }//half 00243 if(grTwoBlock) delete grTwoBlock; 00244 }//block 00245 00246 delete gr; 00247 delete grZeroMean; 00248 }//chan 00249 }//dda 00250 00251 }//entry 00252 std::cerr << "\n"; 00253 00254 //Scale the ZC and calculate the bin widths 00255 00256 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00257 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00258 for(half=0;half<2;half++){ 00259 histBinWidth[dda][chan][half]->Scale(1./numEvents[dda][chan]); 00260 histBinWidth[dda][chan][half]->Scale(0.5*period); 00261 histBinWidth[dda][chan][half]->Write(); 00262 }//half 00263 }//chan 00264 }//dda 00265 00266 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00267 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00268 for(half=0;half<2;half++){ 00269 time=0; 00270 for(capArray=0;capArray<2;capArray++){ 00271 for(sample=0;sample<SAMPLES_PER_BLOCK/2;sample++){ 00272 00273 inter_sample_times[dda][chan][capArray][2*sample+half]=time; 00274 inter_sample_index[dda][chan][capArray][2*sample+half]=2*sample+half; 00275 time+=histBinWidth[dda][chan][half]->GetBinContent(sample+SAMPLES_PER_BLOCK/2*capArray+1); 00276 if(debug&&dda==0&&chan==3) printf("capArray %i half %i sample %i index %d time %f\n", capArray, half, sample, inter_sample_index[dda][chan][capArray][2*sample+half], inter_sample_times[dda][chan][capArray][2*sample+half]); 00277 }//sample 00278 }//capArray 00279 }//half 00280 }//chan 00281 }//dda 00282 00283 //Interleave Calibration 00284 // if(debug) numEntries=10; 00285 for(int entry=0;entry<numEntries;entry++){ 00286 if(entry%starEvery==0) std::cerr <<"*"; 00287 eventTree->GetEntry(entry); 00288 UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed); 00289 capArray = evPtr->blockVec[0].getCapArray(); //capArray of first block 00290 00291 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00292 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00293 chanIndex=chan+RFCHAN_PER_DDA*dda; 00294 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00295 TGraph *grZeroMean = zeroMean(gr); 00296 Int_t numSamples = grZeroMean->GetN(); 00297 Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK; 00298 00299 for(block=0; block<numBlocks-1; block++){ //FIXME -- only use blocks>0 00300 if(block%2) thisCapArray=1-capArray; 00301 else thisCapArray=capArray; 00302 if(thisCapArray==1) continue; 00303 TGraph *grBlock = getTwoBlockGraph(grZeroMean, block); 00304 TGraph *grBlockCalibrated = apply_bin_calibration_two_blocks(grBlock, thisCapArray, dda, chan); 00305 TGraph *grHalf0 = getHalfGraphTwoBlocks(grBlockCalibrated, 0); 00306 TGraph *grHalf1 = getHalfGraphTwoBlocks(grBlockCalibrated, 1); 00307 Int_t retVal = estimate_phase_two_blocks(grHalf0, period, &lag0, &noZCs[0]); 00308 if(retVal==0){ 00309 retVal = estimate_phase_two_blocks(grHalf1, period, &lag1, &noZCs[1]); 00310 if(retVal==0){ 00311 deltaLag = lag0-lag1;//FIXME 00312 while(TMath::Abs(deltaLag-period)<TMath::Abs(deltaLag)) 00313 deltaLag-=period; 00314 while(TMath::Abs(deltaLag+period)<TMath::Abs(deltaLag)) 00315 deltaLag+=period; 00316 lagTree->Fill(); 00317 if(TMath::Abs(noZCs[0]-noZCs[1])==0) lagHist[dda][chan]->Fill(deltaLag); 00318 } 00319 } 00320 if(grHalf0) delete grHalf0; 00321 if(grHalf1) delete grHalf1; 00322 if(grBlockCalibrated) delete grBlockCalibrated; 00323 if(grBlock) delete grBlock; 00324 }//block 00325 00326 00327 delete gr; 00328 delete grZeroMean; 00329 }//chan 00330 }//dda 00331 00332 }//entry 00333 std::cerr << "\n"; 00334 00335 00336 //Now calculate the lag 00337 char varexp[100]; 00338 char selection[100]; 00339 char name[100]; 00340 00341 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00342 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00343 lag[dda][chan] = lagHist[dda][chan]->GetMean(1); 00344 // if((lagHist[dda][chan]->GetRMS())>0.1) printf("dda %i chan %i rms %f\n", dda, chan, lagHist[dda][chan]->GetRMS()); 00345 printf("dda %i chan %i capArray %i lag %f rms %f\n", dda, chan, capArray ,lag[dda][chan], lagHist[dda][chan]->GetRMS()); 00346 }//chan 00347 }//dda 00348 00349 00350 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00351 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00352 for(capArray=0;capArray<2;capArray++){ 00353 for(half=0;half<2;half++){ 00354 Double_t time=0; 00355 for(sample=0;sample<SAMPLES_PER_BLOCK/2;sample++){ 00356 if(lag[dda][chan]>0){ 00357 if(dda==0&&chan==3&&debug) printf("dda %d chan %d capArray %d half %d sample %d lag %f interSampleTime %f new %f\n", dda, chan, capArray, half, sample, lag[dda][chan], inter_sample_times[dda][chan][capArray][2*sample+half], inter_sample_times[dda][chan][capArray][2*sample+half]+(lag[dda][chan])*half); 00358 inter_sample_times[dda][chan][capArray][2*sample+half]=inter_sample_times[dda][chan][capArray][2*sample+half]+(lag[dda][chan])*half; 00359 00360 } 00361 else { 00362 if(dda==0&&chan==3&&debug) printf("dda %d chan %d capArray %d half %d sample %d lag %f interSampleTime %f new %f\n", dda, chan, capArray, half, sample, lag[dda][chan], inter_sample_times[dda][chan][capArray][2*sample+half], inter_sample_times[dda][chan][capArray][2*sample+half]+(lag[dda][chan])*(half-1)); 00363 inter_sample_times[dda][chan][capArray][2*sample+half]=inter_sample_times[dda][chan][capArray][2*sample+half]+(lag[dda][chan])*(half-1); 00364 00365 } 00366 }//sample 00367 }//half 00368 }//capArray 00369 }//chan 00370 }//dda 00371 00372 //now sort the times 00373 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00374 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00375 for(capArray=0;capArray<2;capArray++){ 00376 TMath::Sort(SAMPLES_PER_BLOCK,inter_sample_times[dda][chan][capArray],inter_sample_index[dda][chan][capArray],kFALSE); 00377 } 00378 } 00379 } 00380 00381 //Now revert the inter_sample times to start at zero for each capArray; 00382 Double_t firstTime=0; 00383 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00384 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00385 firstTime=inter_sample_times[dda][chan][1][inter_sample_index[dda][chan][1][0]]; 00386 epsilon_times[dda][chan][1]=firstTime-inter_sample_times[dda][chan][0][inter_sample_index[dda][chan][0][SAMPLES_PER_BLOCK-1]]; 00387 if(dda==0&&chan==3&&debug) printf("dda %d chan %d firstTime %f epsilon_times %f\n", dda, chan, firstTime, epsilon_times[dda][chan][1]); 00388 for(sample=0;sample<SAMPLES_PER_BLOCK;sample++){ 00389 if(dda==0&&chan==3&&debug) printf("dda %d chan %d sample %d inter_sample_times %f new %f\n", dda, chan,sample, inter_sample_times[dda][chan][1][inter_sample_index[dda][chan][1][sample]], inter_sample_times[dda][chan][1][inter_sample_index[dda][chan][1][sample]]-firstTime ); 00390 inter_sample_times[dda][chan][1][inter_sample_index[dda][chan][1][sample]]=inter_sample_times[dda][chan][1][inter_sample_index[dda][chan][1][sample]]-firstTime; 00391 00392 } 00393 } 00394 } 00395 00396 00397 00398 //Now calculate epsilon 00399 for(int entry=0;entry<numEntries;entry++){ 00400 if(entry%starEvery==0) std::cerr <<"*"; 00401 eventTree->GetEntry(entry); 00402 UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed); 00403 capArray = evPtr->blockVec[0].getCapArray(); //capArray of first block 00404 atriBlock = evPtr->blockVec[0].getBlock(); 00405 00406 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00407 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00408 chanIndex=chan+RFCHAN_PER_DDA*dda; 00409 00410 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00411 TGraph *grZeroMean = zeroMean(gr); 00412 Int_t numSamples = grZeroMean->GetN(); 00413 Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK; 00414 00415 for(block=0; block<numBlocks-1; block++){ 00416 if(block%2) thisCapArray=1-capArray; 00417 else thisCapArray=capArray; 00418 if(thisCapArray==0) continue; 00419 TGraph *grBlock0 = getBlockGraph(grZeroMean, block); 00420 TGraph *grBlockCalibrated0 = apply_bin_calibration(grBlock0, thisCapArray, dda, chan); 00421 TGraph *grBlock1 = getBlockGraph(grZeroMean, block+1); 00422 TGraph *grBlockCalibrated1 = apply_bin_calibration(grBlock1, 1-thisCapArray, dda, chan); 00423 00424 TGraph *grBlock0Half0 = getHalfGraph(grBlockCalibrated0, 0); 00425 TGraph *grBlock0Half1 = getHalfGraph(grBlockCalibrated0, 1); 00426 TGraph *grBlock1Half0 = getHalfGraph(grBlockCalibrated1, 0); 00427 TGraph *grBlock1Half1 = getHalfGraph(grBlockCalibrated1, 1); 00428 00429 half = 0; 00430 lastZCCount = findLastZC(grBlock0Half0, period, &lastZC); 00431 firstZCCount = findFirstZC(grBlock1Half0, period, &firstZC); 00432 Double_t *tVals = grBlockCalibrated0->GetX(); //FIXME -- is this really the last sample? 00433 Double_t lastSample = tVals[SAMPLES_PER_BLOCK-1]; 00434 epsilon = -firstZC+lastZC-lastSample+period; 00435 if(epsilon < -0.5*period) epsilon+=period; 00436 if(epsilon > +0.5*period) epsilon-=period; 00437 if(TMath::Abs(lastZCCount-firstZCCount)==0) histEpsilon[dda][chan][half]->Fill(epsilon); 00438 epsilonTree->Fill(); 00439 00440 half = 1; 00441 lastZCCount = findLastZC(grBlock0Half1, period, &lastZC); 00442 firstZCCount = findFirstZC(grBlock1Half1, period, &firstZC); 00443 lastSample = tVals[SAMPLES_PER_BLOCK-1]; 00444 epsilon = -firstZC+lastZC-lastSample+period; 00445 if(epsilon < -0.5*period) epsilon+=period; 00446 if(epsilon > +0.5*period) epsilon-=period; 00447 if(TMath::Abs(lastZCCount-firstZCCount)==0) histEpsilon[dda][chan][half]->Fill(epsilon); 00448 epsilonTree->Fill(); 00449 00450 if(grBlock0Half0) delete grBlock0Half0; 00451 if(grBlock0Half1) delete grBlock0Half1; 00452 if(grBlock1Half0) delete grBlock1Half0; 00453 if(grBlock1Half1) delete grBlock1Half1; 00454 00455 if(grBlockCalibrated0) delete grBlockCalibrated0; 00456 if(grBlock0) delete grBlock0; 00457 if(grBlockCalibrated1) delete grBlockCalibrated1; 00458 if(grBlock1) delete grBlock1; 00459 }//block 00460 00461 delete gr; 00462 delete grZeroMean; 00463 }//chan 00464 }//dda 00465 }//entry 00466 std::cerr << "\n"; 00467 00468 //zCalculate actual epsilon from Tree 00469 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00470 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00471 // for(capArray=0;capArray<2;capArray++){ 00472 // Double_t deltaT=(inter_sample_times[dda][chan][1-capArray][inter_sample_index[dda][chan][1-capArray][63]]-inter_sample_times[dda][chan][1-capArray][inter_sample_index[dda][chan][1-capArray][62]]); 00473 Double_t deltaT=(inter_sample_times[dda][chan][0][inter_sample_index[dda][chan][0][63]]-inter_sample_times[dda][chan][0][inter_sample_index[dda][chan][0][62]]); 00474 deltaT=0; //FIXME 00475 epsilon_times[dda][chan][0] = histEpsilon[dda][chan][0]->GetMean(1)+deltaT; //FIXME -- only using one half here 00476 if((histEpsilon[dda][chan][0]->GetRMS())>0.1) printf("dda %i chan %i half 0 rms %f\n", dda, chan, histEpsilon[dda][chan][0]->GetRMS()); 00477 if((histEpsilon[dda][chan][1]->GetRMS())>0.1) printf("dda %i chan %i half 0 rms %f\n", dda, chan, histEpsilon[dda][chan][1]->GetRMS()); 00478 00479 // }//capArray 00480 }//chan 00481 }//dda 00482 00483 save_inter_sample_times_even(outFileName); 00484 save_epsilon_times_even(outFileName); 00485 00486 save_inter_sample_times(outFileName); 00487 save_epsilon_times(outFileName); 00488 00489 for(dda=0;dda<DDA_PER_ATRI;dda++){ 00490 for(chan=0;chan<RFCHAN_PER_DDA;chan++){ 00491 for(capArray=0;capArray<2;capArray++){ 00492 for(sample=0;sample<SAMPLES_PER_BLOCK;sample++){ 00493 time=inter_sample_times[dda][chan][capArray][inter_sample_index[dda][chan][capArray][sample]]; 00494 if(sample>0) deltaTime=time-inter_sample_times[dda][chan][capArray][inter_sample_index[dda][chan][capArray][sample-1]]; 00495 else if(sample==63) deltaTime=epsilon_times[dda][chan][1-capArray]; 00496 else deltaTime=99; 00497 00498 index=inter_sample_index[dda][chan][capArray][sample]; 00499 epsilon=epsilon_times[dda][chan][capArray]; 00500 binWidthsTree->Fill(); 00501 }//sample 00502 }//capArray 00503 }//chan 00504 }//dda 00505 00506 00507 00508 outFile->Write(); 00509 00510 return 0; 00511 } 00512 00513 00514 00515 TGraph* zeroMean(TGraph* gr){ 00516 Double_t *xVals=gr->GetX(); 00517 Double_t *yVals=gr->GetY(); 00518 Int_t maxN = gr->GetN(); 00519 00520 if(maxN<1) return NULL; 00521 00522 Double_t mean=0; 00523 for(int i=0;i<maxN; i++){ 00524 mean+=yVals[i]/maxN; 00525 } 00526 Double_t *yValsNew = new Double_t[maxN]; 00527 for(int i=0;i<maxN; i++){ 00528 yValsNew[i]=yVals[i]-mean; 00529 } 00530 TGraph *grZeroMeaned = new TGraph(maxN, xVals, yValsNew); 00531 00532 delete yValsNew; 00533 return grZeroMeaned; 00534 00535 } 00536 00537 TGraph *getBlockGraph(TGraph *fullEventGraph, Int_t block){ 00538 Int_t numSamples = fullEventGraph->GetN(); 00539 Int_t numBlocks = numSamples / SAMPLES_PER_BLOCK; 00540 if(block > numBlocks) return NULL; 00541 Double_t *fullX = fullEventGraph->GetX(); 00542 Double_t *fullY = fullEventGraph->GetY(); 00543 Double_t *blockX = new Double_t[SAMPLES_PER_BLOCK]; 00544 Double_t *blockY = new Double_t[SAMPLES_PER_BLOCK]; 00545 for(int sample=0;sample<SAMPLES_PER_BLOCK; sample++){ 00546 blockY[sample] = fullY[sample + block*SAMPLES_PER_BLOCK]; 00547 blockX[sample] = fullX[sample]; 00548 } 00549 TGraph *blockGraph = new TGraph(SAMPLES_PER_BLOCK, blockX, blockY); 00550 delete blockX; 00551 delete blockY; 00552 return blockGraph; 00553 } 00554 00555 TGraph *getTwoBlockGraph(TGraph *fullEventGraph, Int_t block){ 00556 Int_t numSamples = fullEventGraph->GetN(); 00557 Int_t numBlocks = numSamples / SAMPLES_PER_BLOCK; 00558 if(block >= numBlocks-1) return NULL; 00559 Double_t *fullX = fullEventGraph->GetX(); 00560 Double_t *fullY = fullEventGraph->GetY(); 00561 Double_t *blockX = new Double_t[SAMPLES_PER_BLOCK*2]; 00562 Double_t *blockY = new Double_t[SAMPLES_PER_BLOCK*2]; 00563 for(int sample=0;sample<SAMPLES_PER_BLOCK*2; sample++){ 00564 blockY[sample] = fullY[sample + block*SAMPLES_PER_BLOCK]; 00565 blockX[sample] = fullX[sample]; 00566 } 00567 TGraph *blockGraph = new TGraph(SAMPLES_PER_BLOCK*2, blockX, blockY); 00568 delete blockX; 00569 delete blockY; 00570 return blockGraph; 00571 } 00572 00573 00574 TGraph *getHalfGraph(TGraph *fullGraph, Int_t half){ 00575 Int_t numSamples = fullGraph->GetN(); 00576 Double_t *xFull = fullGraph->GetX(); 00577 Double_t *yFull = fullGraph->GetY(); 00578 Double_t *newX = new Double_t[numSamples/2]; 00579 Double_t *newY = new Double_t[numSamples/2]; 00580 00581 for(Int_t sample=0;sample<numSamples;sample++){ 00582 if(sample%2!=half) continue; 00583 newX[sample/2]=xFull[sample]; 00584 newY[sample/2]=yFull[sample]; 00585 } 00586 TGraph *halfGraph = new TGraph(numSamples/2, newX, newY); 00587 00588 delete newX; 00589 delete newY; 00590 return halfGraph; 00591 00592 } 00593 00594 TGraph *getHalfGraphTwoBlocks(TGraph *fullGraph, Int_t half){ 00595 Int_t numSamples = fullGraph->GetN(); 00596 Double_t *xFull = fullGraph->GetX(); 00597 Double_t *yFull = fullGraph->GetY(); 00598 00599 if(numSamples != 2*SAMPLES_PER_BLOCK){ 00600 fprintf(stderr, "Wrong number of samples got %d expected %d\n", numSamples, SAMPLES_PER_BLOCK); 00601 return NULL; 00602 } 00603 00604 Double_t *newX = new Double_t[SAMPLES_PER_BLOCK]; 00605 Double_t *newY = new Double_t[SAMPLES_PER_BLOCK]; 00606 00607 for(Int_t sample=0;sample<2*SAMPLES_PER_BLOCK;sample++){ 00608 if(sample%2!=half) continue; 00609 newX[sample/2]=xFull[sample]; 00610 newY[sample/2]=yFull[sample]; 00611 } 00612 TGraph *halfGraph = new TGraph(SAMPLES_PER_BLOCK, newX, newY); 00613 00614 delete newX; 00615 delete newY; 00616 return halfGraph; 00617 00618 } 00619 00620 TGraph* apply_bin_calibration(TGraph* grBlock, Int_t capArray, Int_t dda, Int_t chan){ 00621 Int_t numSamples = grBlock->GetN(); 00622 if(numSamples!=SAMPLES_PER_BLOCK){ 00623 00624 fprintf(stderr, "%s : wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK); 00625 return NULL; 00626 00627 } 00628 00629 Double_t *yVals = grBlock->GetY(); 00630 Double_t *xVals = new Double_t[SAMPLES_PER_BLOCK]; 00631 00632 for(Int_t sample=0;sample<SAMPLES_PER_BLOCK;sample++){ 00633 xVals[sample] = inter_sample_times[dda][chan][capArray][inter_sample_index[dda][chan][capArray][sample]]; 00634 }//sample 00635 //FIXME -- need to take into account the ordering of samples 00636 //Maybe make a note in the calibration file name 00637 00638 TGraph *grBlockCalibrated = new TGraph(SAMPLES_PER_BLOCK, xVals, yVals); 00639 delete xVals; 00640 00641 return grBlockCalibrated; 00642 } 00643 00644 TGraph* apply_bin_calibration_two_blocks(TGraph* grBlock, Int_t capArray, Int_t dda, Int_t chan){ 00645 Int_t numSamples = grBlock->GetN(); 00646 if(numSamples!=SAMPLES_PER_BLOCK*2){ 00647 00648 fprintf(stderr, "%s : wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK*2); 00649 return NULL; 00650 00651 } 00652 00653 Double_t *yVals = grBlock->GetY(); 00654 Double_t *xVals = new Double_t[SAMPLES_PER_BLOCK*2]; 00655 00656 for(Int_t sample=0;sample<SAMPLES_PER_BLOCK;sample++){ 00657 xVals[sample] = inter_sample_times[dda][chan][capArray][inter_sample_index[dda][chan][capArray][sample]]; 00658 }//sample 00659 00660 for(Int_t sample=0;sample<SAMPLES_PER_BLOCK;sample++){ 00661 xVals[sample+SAMPLES_PER_BLOCK] = inter_sample_times[dda][chan][1-capArray][inter_sample_index[dda][chan][1-capArray][sample]]; 00662 }//sample 00663 00664 TGraph *grBlockCalibrated = new TGraph(2*SAMPLES_PER_BLOCK, xVals, yVals); 00665 delete xVals; 00666 00667 return grBlockCalibrated; 00668 } 00669 00670 Int_t save_inter_sample_times(char* name){ 00671 00672 char outName[180]; 00673 sprintf(outName, "%s_sample_timing.txt", name); 00674 std::ofstream OutFile(outName); 00675 Int_t capArray, sample; 00676 00677 for(int dda=0;dda<DDA_PER_ATRI;dda++){ 00678 for(int chan=0;chan<RFCHAN_PER_DDA;chan++){ 00679 for(int capArray=0;capArray<2;capArray++) { 00680 OutFile << dda << "\t" << chan << "\t" << capArray << "\t"; 00681 for(sample=0;sample<SAMPLES_PER_BLOCK;sample++) { 00682 //Index values 00683 OutFile << inter_sample_index[dda][chan][capArray][sample] << " "; 00684 } 00685 OutFile << "\n"; 00686 OutFile << dda << "\t" << chan << "\t" << capArray << "\t"; 00687 for(int sample=0;sample<SAMPLES_PER_BLOCK;sample++) { 00688 //time values 00689 OutFile << inter_sample_times[dda][chan][capArray][inter_sample_index[dda][chan][capArray][sample]] << " "; 00690 } 00691 OutFile << "\n"; 00692 } 00693 } 00694 } 00695 OutFile.close(); 00696 00697 return 0; 00698 } 00699 00700 Int_t estimate_phase(TGraph *gr, Double_t period, Double_t *meanPhase, Int_t *totalZCs){ 00701 Double_t *yVals = gr->GetY(); 00702 Double_t *xVals = gr->GetX(); 00703 Int_t numSamples = gr->GetN(); 00704 if(numSamples != SAMPLES_PER_BLOCK/2){ 00705 fprintf(stderr, "%s : Wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK/2); 00706 return -1; 00707 } 00708 Double_t phase=0; 00709 Int_t numZCs=0; 00710 00711 for(int sample=0;sample<numSamples-1;sample++){ 00712 Double_t y1=yVals[sample]; 00713 Double_t y2=yVals[sample+1]; 00714 if(y1<0 && y2>0){ 00715 Double_t x1=xVals[sample]; 00716 Double_t x2=xVals[sample+1]; 00717 Double_t zc=((0-y1)/(y2-y1))*(x2-x1)+x1; 00718 // if(zc<0.6) //printf("sample %i y1 %f y2 %f x1 %f x2 %f\n", sample, y1, y2, x1, x2); 00719 phase+=zc-numZCs*period; 00720 //printf("zc num %i val %f adjusted val %f\n", numZCs, zc, zc-numZCs*period); 00721 numZCs++; 00722 //return zc; 00723 } 00724 }//sample 00725 00726 if(!numZCs) 00727 phase=0; 00728 else phase=phase/numZCs; 00729 00730 *totalZCs = numZCs; 00731 *meanPhase = phase; 00732 return 0; 00733 00734 00735 } 00736 00737 Int_t estimate_phase_two_blocks(TGraph *gr, Double_t period, Double_t *meanPhase, Int_t *totalZCs){ 00738 Double_t *yVals = gr->GetY(); 00739 Double_t *xVals = gr->GetX(); 00740 Int_t numSamples = gr->GetN(); 00741 Double_t phase=0; 00742 Int_t numZCs=0; 00743 00744 for(int sample=0;sample<numSamples-1;sample++){ 00745 Double_t y1=yVals[sample]; 00746 Double_t y2=yVals[sample+1]; 00747 if(y1<0 && y2>0){ 00748 Double_t x1=xVals[sample]; 00749 Double_t x2=xVals[sample+1]; 00750 Double_t zc=((0-y1)/(y2-y1))*(x2-x1)+x1; 00751 // if(zc<0.6) //printf("sample %i y1 %f y2 %f x1 %f x2 %f\n", sample, y1, y2, x1, x2); 00752 phase+=zc-numZCs*period; 00753 //printf("zc num %i val %f adjusted val %f\n", numZCs, zc, zc-numZCs*period); 00754 numZCs++; 00755 //return zc; 00756 } 00757 }//sample 00758 00759 if(!numZCs) 00760 phase=0; 00761 else phase=phase/numZCs; 00762 00763 *totalZCs = numZCs; 00764 *meanPhase = phase; 00765 return 0; 00766 } 00767 00768 00769 Int_t findFirstZC(TGraph *graph, Double_t period, Double_t *lastAvZC){ 00770 Double_t *postWrap[2], thisZC=0, lastZC=0, meanZC=0; 00771 Int_t noZCs=0; 00772 Int_t noSamples=graph->GetN(); 00773 postWrap[0]=graph->GetX(); 00774 postWrap[1]=graph->GetY(); 00775 00776 // if(debug>1) printf("Finding last ZC\n"); 00777 00778 for(Int_t Sample=0; Sample<noSamples-1; Sample++){ 00779 if(postWrap[1][Sample]<0&&postWrap[1][Sample+1]>0){ 00780 Double_t x1=postWrap[0][Sample]; 00781 Double_t x2=postWrap[0][Sample+1]; 00782 Double_t y1=postWrap[1][Sample]; 00783 Double_t y2=postWrap[1][Sample+1]; 00784 thisZC=y1*(x1-x2)/(y2-y1)+(x1); 00785 if(!noZCs){ 00786 lastZC=thisZC; 00787 } 00788 // printf("zc %i position %f adj position %f\n", noZCs+1, thisZC, thisZC-noZCs*period); 00789 thisZC-=noZCs*period; 00790 meanZC+=thisZC; 00791 noZCs++; 00792 } 00793 } 00794 00795 // if(debug>1) printf("Average value is %f\n", meanZC/noZCs); 00796 00797 if(noZCs){ 00798 *lastAvZC=meanZC/noZCs; 00799 return noZCs; 00800 } 00801 return -1; 00802 } 00803 Int_t findLastZC(TGraph *graph, Double_t period, Double_t *lastAvZC){ 00804 Double_t *preWrap[2], thisZC=0, lastZC=0, meanZC=0; 00805 Int_t noZCs=0; 00806 Int_t noSamples=graph->GetN(); 00807 preWrap[0]=graph->GetX(); 00808 preWrap[1]=graph->GetY(); 00809 00810 // if(debug>1) printf("Finding last ZC\n"); 00811 00812 for(Int_t Sample=noSamples-1; Sample>0; --Sample){ 00813 if(preWrap[1][Sample-1]<0&&preWrap[1][Sample]>0){ 00814 Double_t x1=preWrap[0][Sample-1]; 00815 Double_t x2=preWrap[0][Sample]; 00816 Double_t y1=preWrap[1][Sample-1]; 00817 Double_t y2=preWrap[1][Sample]; 00818 00819 thisZC=y1*(x1-x2)/(y2-y1)+(x1); 00820 if(!noZCs){ 00821 lastZC=thisZC; 00822 } 00823 // printf("zc %i position %f adj position %f\n", noZCs+1, thisZC, thisZC+noZCs*period); 00824 thisZC+=noZCs*period; 00825 meanZC+=thisZC; 00826 noZCs++; 00827 } 00828 } 00829 00830 // if(debug>1) printf("Average value is %f\n", meanZC/noZCs); 00831 00832 if(noZCs){ 00833 *lastAvZC=meanZC/noZCs; 00834 return noZCs; 00835 } 00836 return -1; 00837 } 00838 00839 00840 00841 Int_t save_epsilon_times(char* name){ 00842 00843 char outName[180]; 00844 sprintf(outName, "%s_epsilon_timing.txt", name); 00845 std::ofstream OutFile(outName); 00846 Int_t capArray, sample; 00847 00848 for(Int_t dda=0;dda<DDA_PER_ATRI;dda++){ 00849 for(Int_t chan=0;chan<RFCHAN_PER_DDA;chan++){ 00850 for(int capArray=0;capArray<2;capArray++){ 00851 OutFile << dda << "\t" 00852 << chan << "\t" 00853 << capArray << "\t"; 00854 OutFile << epsilon_times[dda][chan][capArray] << "\n"; 00855 } 00856 } 00857 } 00858 OutFile.close(); 00859 00860 return 0; 00861 00862 } 00863 Int_t save_epsilon_times_even(char* name){ 00864 00865 char outName[180]; 00866 sprintf(outName, "%s_epsilon_timing_even.txt", name); 00867 std::ofstream OutFile(outName); 00868 Int_t capArray, sample; 00869 00870 for(Int_t dda=0;dda<DDA_PER_ATRI;dda++){ 00871 for(Int_t chan=0;chan<RFCHAN_PER_DDA;chan++){ 00872 for(int capArray=0;capArray<2;capArray++){ 00873 OutFile << dda << "\t" 00874 << chan << "\t" 00875 << capArray << "\t"; 00876 00877 if(chan<6){ 00878 if(inter_sample_index[dda][chan][1-capArray][63]==63) 00879 OutFile << epsilon_times[dda][chan][capArray] + inter_sample_times[dda][chan][1-capArray][63] - inter_sample_times[dda][chan][1-capArray][62]<< "\n"; 00880 else 00881 OutFile << epsilon_times[dda][chan][capArray] << "\n"; 00882 } 00883 else{ 00884 if(inter_sample_index[dda][5][1-capArray][63]==63) 00885 OutFile << epsilon_times[dda][5][capArray] + inter_sample_times[dda][5][1-capArray][63] - inter_sample_times[dda][5][1-capArray][62]<< "\n"; 00886 else 00887 OutFile << epsilon_times[dda][5][capArray] << "\n"; 00888 } 00889 } 00890 } 00891 } 00892 OutFile.close(); 00893 00894 return 0; 00895 00896 } 00897 Int_t save_inter_sample_times_even(char* name){ 00898 00899 char outName[180]; 00900 sprintf(outName, "%s_sample_timing_even.txt", name); 00901 std::ofstream OutFile(outName); 00902 Int_t capArray, sample; 00903 00904 for(int dda=0;dda<DDA_PER_ATRI;dda++){ 00905 for(int chan=0;chan<RFCHAN_PER_DDA;chan++){ 00906 for(int capArray=0;capArray<2;capArray++) { 00907 OutFile << dda << "\t" << chan << "\t" << capArray << "\t" << 32 << "\t"; 00908 for(sample=0;sample<SAMPLES_PER_BLOCK;sample++) { 00909 //Index values 00910 if(sample%2==0) OutFile << sample << " "; 00911 } 00912 OutFile << "\n"; 00913 OutFile << dda << "\t" << chan << "\t" << capArray << "\t" << 32 << "\t"; 00914 for(int sample=0;sample<SAMPLES_PER_BLOCK;sample++) { 00915 //time values 00916 if(sample%2==0&&chan<6) OutFile << inter_sample_times[dda][chan][capArray][sample] << " "; 00917 if(sample%2==0&&chan>=6) OutFile << inter_sample_times[dda][5][capArray][sample] << " "; 00918 } 00919 OutFile << "\n"; 00920 } 00921 } 00922 } 00923 OutFile.close(); 00924 00925 return 0; 00926 }
Generated on Tue Jul 16 16:58:02 2013 for ARA ROOT v3.10 Software by
