calibration/ATRI/firstCalibTry.cxx
00001 #include <iostream> 00002 #include <fstream> 00003 00004 //Event Reader Includes 00005 #include "UsefulAtriStationEvent.h" 00006 #include "RawAtriStationEvent.h" 00007 #include "araSoft.h" 00008 00009 //ROOT Includes 00010 #include "TROOT.h" 00011 #include "TCanvas.h" 00012 #include "TTree.h" 00013 #include "TFile.h" 00014 #include "TH1.h" 00015 #include "TTree.h" 00016 #include "TTreeIndex.h" 00017 #include "TButton.h" 00018 #include "TGroupButton.h" 00019 #include "TThread.h" 00020 #include "TEventList.h" 00021 #include "TMath.h" 00022 #include "TCanvas.h" 00023 #include <TGClient.h> 00024 00025 00026 00027 Double_t estimateLag(TGraph *grIn, Double_t freq); 00028 Double_t estimateFirstZC(TGraph *grIn, Double_t freq, Int_t *nZC); 00029 Double_t estimateLastZC(TGraph *grIn, Double_t freq, Int_t *nZC); 00030 int firstCalibTry(int run,Double_t freq, Int_t dda, Int_t chan, bool debug=false); 00031 void plotEvent(Int_t event, Int_t block); 00032 void nextEvent(); 00033 void previousEvent(); 00034 00035 Double_t newTimeValsUnsorted[2][2][SAMPLES_PER_BLOCK/2]; 00036 Double_t newTimeVals[2][2][SAMPLES_PER_BLOCK/2]; 00037 Double_t newTimeValsEpsilon[2][2][SAMPLES_PER_BLOCK/2]; 00038 Double_t newEpsilon[2]; 00039 Int_t lastRun=0, lastEvent=0, lastDda=0, lastChan=0, lastBlock=0; 00040 00041 using namespace std; 00042 00043 int main(int argc, char **argv) 00044 { 00045 if(argc<5) { 00046 std::cerr << "Usage: " << argv[0] << " <run> <freq in GHz> <dda> <chan>\n"; 00047 return 1; 00048 } 00049 return firstCalibTry(atoi(argv[1]),atof(argv[2]),atoi(argv[3]),atoi(argv[4])); 00050 } 00051 00052 int firstCalibTry(int run,Double_t frequency,Int_t dda, Int_t chan, bool debug) 00053 { 00054 printf("Run %i Frequency %f dda %i channel %i\n", run, frequency, dda, chan); 00055 lastRun=run; 00056 lastDda=dda; 00057 lastChan=chan; 00058 //1. calculate the bin to bin widths for the odd and even samples in the two halves 00059 //this is only going to be done for one channel on one dda - wrapped into chanIndex 00060 Int_t chanIndex=chan+RFCHAN_PER_DDA*dda; 00061 00062 char inName[180]; 00063 sprintf(inName,"/unix/ara/data/hawaii2012/StationOne/root/run%d/event%d.root",run,run); 00064 00065 00066 00067 TFile *fp = new TFile(inName); 00068 if(!fp) { 00069 std::cerr << "Can't open file\n"; 00070 return -1; 00071 } 00072 TTree *eventTree = (TTree*) fp->Get("eventTree"); 00073 if(!eventTree) { 00074 std::cerr << "Can't find eventTree\n"; 00075 return -1; 00076 } 00077 RawAtriStationEvent *evPtr=0; 00078 eventTree->SetBranchAddress("event",&evPtr); 00079 char histName[180]; 00080 sprintf(histName,"output_root/sineOut_%3.0fMHz_run%d_dda%d_chan%d.root",1000*frequency,run,dda,chan); 00081 TFile *histFile = new TFile(histName,"RECREATE"); 00082 00083 Long64_t numEntries=eventTree->GetEntries(); 00084 00085 cout << "Number of entries in file is " << numEntries << endl; 00086 00087 Long64_t starEvery=numEntries/80; 00088 if(starEvery==0) starEvery++; 00089 00090 Double_t tVals[2][32]; 00091 Double_t vVals[2][32]; 00092 00093 Int_t numEvents[2]={0}; //used to scale the bin widths 00094 TH1F *histZC[2][2]; 00095 TH1F *histBinWidth[2][2]; 00096 for(int capArray=0;capArray<2;capArray++) { 00097 for(int half=0;half<2;half++) { 00098 sprintf(histName,"histZC%d_%d",capArray,half); 00099 histZC[capArray][half] = new TH1F(histName,histName,SAMPLES_PER_BLOCK/2,-0.5,(SAMPLES_PER_BLOCK/2)-0.5); 00100 sprintf(histName,"histBinWidth%d_%d",capArray,half); 00101 histBinWidth[capArray][half] = new TH1F(histName,histName,SAMPLES_PER_BLOCK/2,-0.5,(SAMPLES_PER_BLOCK/2)-0.5); 00102 } 00103 } 00104 00105 TH1F *histMean = new TH1F("histMean","histMean",1000,-0.1,0.1); 00106 00107 std::vector <Long64_t> entryVec; 00108 00109 for(Long64_t i=0;i<numEntries;i++) { 00110 if(i%starEvery==0) std::cerr << "*"; 00111 eventTree->GetEntry(i); 00112 UsefulAtriStationEvent realEvent(evPtr,AraCalType::kVoltageTime); 00113 00114 //all the ddas block zero will be the same block 00115 //the capArray will toggle as we move through the event 00116 Int_t capArray=evPtr->blockVec[dda].getCapArray(); 00117 00118 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00119 Double_t *rawT=gr->GetX(); 00120 Double_t *rawV=gr->GetY(); 00121 00122 Int_t numSamples=gr->GetN(); 00123 Int_t numBlocks=numSamples/64; 00124 00125 for(int block=0;block<numBlocks;block++) { 00126 Int_t thisCapArray=capArray; 00127 Double_t mean[2]={0};//gr->GetMean(2); 00128 if(block%2) thisCapArray=1-capArray; 00129 00130 //jpd picking out the correct block and splitting into two halves 00131 //128 samples = 2 caparrays 00132 //64 samples (block) split into two halfs -- equivalent to splitting into the odds and evens 00133 00134 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00135 tVals[samp%2][samp/2]=rawT[samp+SAMPLES_PER_BLOCK*block]; 00136 mean[samp%2]+=rawV[samp+SAMPLES_PER_BLOCK*block]*2/(SAMPLES_PER_BLOCK); 00137 } 00138 00139 //zero-mean the waveforms 00140 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00141 vVals[samp%2][samp/2]=rawV[samp+SAMPLES_PER_BLOCK*block]-mean[samp%2]; 00142 } 00143 00144 TGraph *grHalf[2]={0}; 00145 for(int half=0;half<2;half++) { 00146 grHalf[half] = new TGraph(SAMPLES_PER_BLOCK/2,tVals[half],vVals[half]); 00147 histMean->Fill(grHalf[half]->GetMean(2)); 00148 } 00149 00150 Double_t countZc[2]={0}; 00151 for(int half=0;half<2;half++) { 00152 for(int samp=0;samp<(SAMPLES_PER_BLOCK/2)-1;samp++) { 00153 Double_t val1=vVals[half][samp]; 00154 Double_t val2=vVals[half][samp+1]; 00155 if(val1<0 && val2>0) { 00156 countZc[half]++; 00157 } 00158 else if(val1>0 && val2<0) { 00159 countZc[half]++; 00160 } 00161 else if(val1==0 || val2==0) { 00162 countZc[half]+=0.5; 00163 } 00164 } 00165 } 00166 00167 //jpd previous section just counts total ZCs in the two halves 00168 //jpd require that the number in the two halves is ~equal 00169 if(TMath::Abs(countZc[0]-countZc[1])<2) { 00170 entryVec.push_back(i); 00171 // std::cerr << i << "\t" <<countZc[0] << "\t" << countZc[1] << "\t" << mean << "\n"; 00172 00173 numEvents[thisCapArray]++; 00174 for(int half=0;half<2;half++) { 00175 for(int samp=0;samp<(SAMPLES_PER_BLOCK/2)-1;samp++) { 00176 Double_t val1=vVals[half][samp]; 00177 Double_t val2=vVals[half][samp+1]; 00178 if(val1<0 && val2>0) { 00179 histZC[thisCapArray][half]->Fill(samp); 00180 histBinWidth[thisCapArray][half]->Fill(samp); 00181 } 00182 else if(val1>0 && val2<0) { 00183 histZC[thisCapArray][half]->Fill(samp); 00184 histBinWidth[thisCapArray][half]->Fill(samp); 00185 } 00186 else if(val1==0 || val2==0) { 00187 histZC[thisCapArray][half]->Fill(samp,0.5); 00188 histBinWidth[thisCapArray][half]->Fill(samp,0.5); 00189 } 00190 } 00191 } 00192 } 00193 delete grHalf[0]; 00194 delete grHalf[1]; 00195 } 00196 00197 delete gr; 00198 } 00199 00200 for(int capArray=0;capArray<2;capArray++) { 00201 for(int half=0;half<2;half++) { 00202 histBinWidth[capArray][half]->Scale(1./numEvents[capArray]); 00203 histBinWidth[capArray][half]->Scale(0.5/frequency); 00204 } 00205 } 00206 std::cerr << "\n"; 00207 00208 for(int capArray=0;capArray<2;capArray++) { 00209 for(int half=0;half<2;half++) { 00210 Double_t time=0; 00211 for(int samp=0;samp<SAMPLES_PER_BLOCK/2;samp++) { 00212 newTimeVals[capArray][half][samp]=time; 00213 time+=histBinWidth[capArray][half]->GetBinContent(samp+1); 00214 } 00215 if(debug) std::cout << "Mean : " << capArray << " " << half << " " << time/32. << "\n"; 00216 } 00217 } 00218 00219 00220 00221 00222 00223 if(debug){ 00224 cout << "-----------------Done bin widths-----------------" << endl; 00225 for(int capArray=0;capArray<2;capArray++) { 00226 for(int samp=0;samp<SAMPLES_PER_BLOCK/2;samp++) { 00227 printf("%i\t%i\t%0.3f\t%0.3f\n", capArray, samp, newTimeVals[capArray][0][samp], newTimeVals[capArray][1][samp]); 00228 } 00229 } 00230 } 00231 cout << "Doing Interleave timing" << endl; 00232 00233 //2. now calculate the interleave times 00234 00235 TTree *lagTree = new TTree("lagTree","lagTree"); 00236 Int_t cap[4], blockNo[4], ddaNo[4]; 00237 Double_t lag1,lag0,deltaLagg; 00238 Int_t block, thisCapArray; 00239 lagTree->Branch("block",&block,"block/I"); 00240 lagTree->Branch("capArray",&thisCapArray,"capArray/I"); 00241 lagTree->Branch("lag1",&lag1,"lag1/D"); 00242 lagTree->Branch("lag0",&lag0,"lag0/D"); 00243 lagTree->Branch("deltaLag",&deltaLagg,"deltaLag/D"); 00244 lagTree->Branch("cap", cap, "cap[4]/I"); 00245 lagTree->Branch("blockNo", blockNo, "blockNo[4]/I"); 00246 lagTree->Branch("ddaNo", ddaNo, "ddaNo[4]/I"); 00247 00248 00249 TH1F *histLag[2]; 00250 for(int capArray=0;capArray<2;capArray++) { 00251 sprintf(histName,"histLag%d",capArray); 00252 histLag[capArray] = new TH1F(histName,histName,10000,-5,5); 00253 } 00254 00255 std::vector<Long64_t>::iterator entryIt; 00256 00257 for(Long64_t i=0;i<numEntries;i++) { 00258 // for(entryIt=entryVec.begin();entryIt!=entryVec.end();entryIt++) { 00259 // for(int i =0; i<numEntries; i++){ 00260 //for(int i=100;i<101;i++){ 00261 // Long64_t i=*entryIt; 00262 if(i%starEvery==0) std::cerr << "*"; 00263 eventTree->GetEntry(i); 00264 if(evPtr->blockVec[0].getBlock()==0) continue; 00265 UsefulAtriStationEvent realEvent(evPtr,AraCalType::kVoltageTime); 00266 00267 //For now will assume all the ddas have the same block 00268 Int_t capArray=evPtr->blockVec[dda].getCapArray(); 00269 cap[0] =evPtr->blockVec[0].getCapArray(); 00270 cap[1] =evPtr->blockVec[1].getCapArray(); 00271 cap[2] =evPtr->blockVec[2].getCapArray(); 00272 cap[3] =evPtr->blockVec[3].getCapArray(); 00273 blockNo[0]=evPtr->blockVec[0].getBlock(); 00274 blockNo[1]=evPtr->blockVec[1].getBlock(); 00275 blockNo[2]=evPtr->blockVec[2].getBlock(); 00276 blockNo[3]=evPtr->blockVec[3].getBlock(); 00277 ddaNo[0]=evPtr->blockVec[0].getDda(); 00278 ddaNo[1]=evPtr->blockVec[1].getDda(); 00279 ddaNo[2]=evPtr->blockVec[2].getDda(); 00280 ddaNo[3]=evPtr->blockVec[3].getDda(); 00281 00282 00283 00284 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00285 Double_t *rawV=gr->GetY(); 00286 Int_t numSamples=gr->GetN(); 00287 Int_t numBlocks=numSamples/64; 00288 00289 for(block=0;block<numBlocks;block++) { 00290 Double_t mean[2]={0}; 00291 00292 thisCapArray=capArray; 00293 if(block%2) thisCapArray=1-capArray; 00294 00295 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00296 mean[samp%2]+=rawV[samp+SAMPLES_PER_BLOCK*block]; 00297 } 00298 mean[0]/=SAMPLES_PER_BLOCK/2; 00299 mean[1]/=SAMPLES_PER_BLOCK/2; 00300 00301 //zero-mean the waveforms 00302 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00303 tVals[samp%2][samp/2]=newTimeVals[thisCapArray][samp%2][samp/2]; 00304 vVals[samp%2][samp/2]=rawV[samp+SAMPLES_PER_BLOCK*block]-mean[samp%2]; 00305 } 00306 00307 TGraph *grHalf[2]={0}; 00308 for(int half=0;half<2;half++) { 00309 grHalf[half] = new TGraph(SAMPLES_PER_BLOCK/2,tVals[half],vVals[half]); 00310 } 00311 00312 lag0=estimateLag(grHalf[0],frequency); 00313 lag1=estimateLag(grHalf[1],frequency); 00314 00315 deltaLagg=lag0-lag1; 00316 while(TMath::Abs(deltaLagg-1.0/frequency)<TMath::Abs(deltaLagg)) 00317 deltaLagg-=1./frequency; 00318 while(TMath::Abs(deltaLagg+1.0/frequency)<TMath::Abs(deltaLagg)) 00319 deltaLagg+=1./frequency; 00320 00321 00322 //Arbitrary to make sample 1 after sample zero 00323 // if(deltaLagg<0) 00324 // deltaLagg+=1./frequency; 00325 00326 lagTree->Fill(); 00327 00328 histLag[thisCapArray]->Fill(deltaLagg); 00329 00330 // delete grHalf[0]; 00331 // delete grHalf[1]; 00332 00333 // printf("lag0 %0.3f lag1 %0.3f deltaLag %0.3f deltaLag\n", lag0, lag1, deltaLagg); 00334 00335 // TCanvas *can = new TCanvas();//"can","can",600,600); 00336 // can->Divide(1,2); 00337 // can->cd(1); 00338 // grHalf[0]->GetXaxis()->SetLabelSize(0.06); 00339 // grHalf[0]->Draw("alp"); 00340 // can->cd(2); 00341 // grHalf[1]->GetXaxis()->SetLabelSize(0.06); 00342 // grHalf[1]->Draw("alp"); 00343 00344 00345 delete grHalf[0]; 00346 delete grHalf[1]; 00347 00348 00349 } 00350 delete gr; 00351 00352 } 00353 std::cerr << "\n"; 00354 lagTree->AutoSave(); 00355 00356 // Double_t deltaLag[2]={histLag[0]->GetMean(),histLag[1]->GetMean()}; 00357 00358 Double_t timeVals[2][SAMPLES_PER_BLOCK]; 00359 Int_t indexVals[2][SAMPLES_PER_BLOCK]; 00360 00361 00362 for(int capArray=0;capArray<2;capArray++) { 00363 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00364 if(samp%2==0) 00365 timeVals[capArray][samp]=newTimeVals[capArray][samp%2][samp/2]; 00366 else 00367 { 00368 timeVals[capArray][samp]=newTimeVals[capArray][samp%2][samp/2]+histLag[capArray]->GetMean(); 00369 newTimeVals[capArray][samp%2][samp/2]+=histLag[capArray]->GetMean(); 00370 } 00371 } 00372 00373 //sorts timeVals[capArray] in ascending order, recording the indices in indexVals 00374 TMath::Sort(SAMPLES_PER_BLOCK,timeVals[capArray],indexVals[capArray],kFALSE); 00375 } 00376 00377 00378 00379 if(debug){ 00380 //jpd print em all to screen 00381 cout << endl << "-------------Done Interleaving-------------" << endl << "cap samp Time even Time odd" << endl; 00382 for(int capArray=0;capArray<2;capArray++) { 00383 for(int samp=0;samp<SAMPLES_PER_BLOCK/2;samp++) { 00384 printf("%i\t%i\t%0.3f\t%0.3f\n", capArray, samp, newTimeVals[capArray][0][samp], newTimeVals[capArray][1][samp]); 00385 } 00386 } 00387 cout << endl << "estimated interleave capArray 0 " << histLag[0]->GetMean() << " \t capArray 1 " << histLag[1]->GetMean()<< endl; 00388 } 00389 // cout << "--------------------------Time Ordered-----------------------" << endl; 00390 00391 // for(int capArray=0;capArray<2;capArray++){ 00392 // for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++){ 00393 // printf("%i\t%i\t%i\t%0.3f\n", capArray, samp, indexVals[capArray][samp],timeVals[capArray][samp]); 00394 // } 00395 // } 00396 00397 00398 cout << "Doing epsilon Calibration" << endl; 00399 00400 //3. Do epsilon calibration 00401 00402 // Double_t epsilon[2] = {0}; 00403 TH1F *histEpsilon[2]; 00404 00405 sprintf(histName,"histEpsilonCapArray%i",0); 00406 histEpsilon[0] = new TH1F(histName,histName, 1000, -5, 5); 00407 00408 00409 sprintf(histName,"histEpsilonCapArray%i",1); 00410 histEpsilon[1] = new TH1F(histName,histName, 1000, -5, 5); 00411 00412 00413 TTree *epsilonTree = new TTree("epsilonTree", "epsilonTree"); 00414 Double_t epsilonForTree=0; 00415 Double_t firstZC=0; 00416 Double_t lastZC=0; 00417 Int_t firstZCCount=0, lastZCCount=0; 00418 epsilonTree->Branch("block",&block,"block/I"); 00419 epsilonTree->Branch("capArray",&thisCapArray,"capArray/I"); 00420 epsilonTree->Branch("epsilon",&epsilonForTree,"epsilon/D"); 00421 epsilonTree->Branch("firstZC",&firstZC,"firstZC/D"); 00422 epsilonTree->Branch("lastZC",&lastZC,"lastZC/D"); 00423 epsilonTree->Branch("lastZCCount",&lastZCCount,"lastZCCount/I"); 00424 epsilonTree->Branch("firstZCCount",&firstZCCount,"firstZCCount/D"); 00425 00426 00427 for(int i=0; i<numEntries;i++){ 00428 //for(int i=100; i<101; i++){ 00429 if(i%starEvery==0) std::cerr << "*"; 00430 eventTree->GetEntry(i); 00431 if(evPtr->blockVec[0].getBlock()==0) continue; 00432 UsefulAtriStationEvent realEvent(evPtr,AraCalType::kVoltageTime); 00433 00434 //For now will assume all the ddas have the same block 00435 Int_t capArray=evPtr->blockVec[dda].getCapArray(); 00436 00437 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00438 Double_t *rawV=gr->GetY(); 00439 Int_t numSamples=gr->GetN(); 00440 Int_t numBlocks=numSamples/64; 00441 if(numBlocks<9) continue; 00442 for(block=4; block<numBlocks-5;block++){ 00443 thisCapArray=capArray; 00444 if(block%2) thisCapArray=1-capArray; 00445 TGraph *grHalf[2][2]={{0}}; 00446 Double_t vValsHalf[2][2][SAMPLES_PER_BLOCK/2]={{{0}}}; 00447 Double_t tValsHalf[2][2][SAMPLES_PER_BLOCK/2]={{{0}}}; 00448 Double_t mean[2][2]={{0}}; 00449 Double_t epsilon[2]={0}; 00450 00451 //find the mean of the two halves of each block 00452 00453 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++){ 00454 mean[thisCapArray][samp%2]+=rawV[samp+(block)*SAMPLES_PER_BLOCK]/(SAMPLES_PER_BLOCK/2); 00455 mean[1-thisCapArray][samp%2]+=rawV[samp+(block+1)*SAMPLES_PER_BLOCK]/(SAMPLES_PER_BLOCK/2); 00456 } 00457 00458 //use the previously time ordered values (v and t) using indexVals 00459 //done for the first block in the pair 00460 00461 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++){ 00462 tValsHalf[thisCapArray][samp%2][samp/2]=timeVals[thisCapArray][indexVals[thisCapArray][samp]]; 00463 vValsHalf[thisCapArray][samp%2][samp/2]=rawV[indexVals[thisCapArray][samp]+SAMPLES_PER_BLOCK*block]-mean[thisCapArray][samp%2]; 00464 } 00465 // 00466 00467 //find the last sample in the first block in the pair 00468 00469 Double_t lastSample = tValsHalf[thisCapArray][1][SAMPLES_PER_BLOCK/2-1]; //this is the last sample in the odd part 00470 if(lastSample<tValsHalf[thisCapArray][0][SAMPLES_PER_BLOCK/2-1]) 00471 lastSample = tValsHalf[thisCapArray][0][SAMPLES_PER_BLOCK/2-1]; //if the last sample in the even samples is after the last odd sample, change lastSample 00472 00473 //now fill the second block values, defining start time as the end of the first block 00474 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++){ 00475 tValsHalf[1-thisCapArray][samp%2][samp/2]=timeVals[1-thisCapArray][indexVals[1-thisCapArray][samp]]+lastSample-timeVals[1-thisCapArray][0]; 00476 vValsHalf[1-thisCapArray][samp%2][samp/2]=rawV[indexVals[1-thisCapArray][samp]+SAMPLES_PER_BLOCK*(block+1)]-mean[1-thisCapArray][samp%2]; 00477 } 00478 00479 for(int half=0;half<2;half++){ 00480 grHalf[thisCapArray][half] = new TGraph(SAMPLES_PER_BLOCK/2,tValsHalf[thisCapArray][half], vValsHalf[thisCapArray][half]); 00481 grHalf[1-thisCapArray][half] = new TGraph(SAMPLES_PER_BLOCK/2,tValsHalf[1-thisCapArray][half], vValsHalf[1-thisCapArray][half]); 00482 } 00483 00484 //epsilon is lastZC first block - firstZC in second block +1./frequency 00485 00486 //Done seperately for the even samples and then for the odd samples 00487 00488 firstZC = estimateFirstZC(grHalf[1-thisCapArray][0], frequency, &firstZCCount); 00489 lastZC = estimateLastZC(grHalf[thisCapArray][0],frequency, &lastZCCount); 00490 epsilon[thisCapArray]=lastZC-firstZC+1./frequency; 00491 00492 while(epsilon[thisCapArray]<0) epsilon[thisCapArray]+=1./frequency; 00493 while(epsilon[thisCapArray]>1./frequency) epsilon[thisCapArray]-=1./frequency; 00494 // if(vValsHalf[thisCapArray][0][SAMPLES_PER_BLOCK/2-1]<0&&vValsHalf[1-thisCapArray][0][0]>0) epsilon[thisCapArray]-=.5/frequency;//jpd check if we missed the first ZC 00495 histEpsilon[thisCapArray]->Fill(epsilon[thisCapArray]); 00496 00497 //fill the tree 00498 epsilonForTree=epsilon[thisCapArray]; 00499 epsilonTree->Fill(); 00500 00501 //Now for odd samples 00502 00503 firstZC = estimateFirstZC(grHalf[1-thisCapArray][1], frequency, &firstZCCount); 00504 lastZC = estimateLastZC(grHalf[thisCapArray][1],frequency, &lastZCCount); 00505 epsilon[thisCapArray]=lastZC-firstZC+1./frequency; 00506 00507 while(epsilon[thisCapArray]<0) epsilon[thisCapArray]+=1./frequency; 00508 while(epsilon[thisCapArray]>1./frequency) epsilon[thisCapArray]-=1./frequency; 00509 // if(vValsHalf[thisCapArray][1][SAMPLES_PER_BLOCK/2-1]<0&&vValsHalf[1-thisCapArray][1][0]>0) epsilon[thisCapArray]-=.5/frequency;//jpd check if we missed first ZC 00510 histEpsilon[thisCapArray]->Fill(epsilon[thisCapArray]); 00511 00512 //fill the tree 00513 epsilonForTree=epsilon[thisCapArray]; 00514 epsilonTree->Fill(); 00515 00516 00517 for(int half=0;half<2;half++){ 00518 delete grHalf[0][half]; 00519 delete grHalf[1][half]; 00520 } 00521 00522 00523 } 00524 delete gr; 00525 } 00526 00527 epsilonTree->AutoSave(); 00528 00529 00530 //3.A printing epsilon values to file 00531 00532 char outName[180]; 00533 sprintf(outName,"calibration_files/sampleTiming_run%d_dda%d_chan%d.txt",run,dda,chan); 00534 00535 std::ofstream OutFile(outName); 00536 for(int capArray=0;capArray<2;capArray++) { 00537 OutFile << dda << "\t" << chan << "\t" << capArray << "\t"; 00538 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00539 if(samp%2==0) 00540 OutFile << indexVals[capArray][samp] << " "; 00541 else 00542 OutFile << indexVals[capArray][samp] << " "; 00543 } 00544 OutFile << "\n"; 00545 OutFile << dda << "\t" << chan << "\t" << capArray << "\t"; 00546 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00547 if(samp%2==0) 00548 OutFile << timeVals[capArray][indexVals[capArray][samp]] << " "; 00549 else 00550 OutFile << timeVals[capArray][indexVals[capArray][samp]] << " "; 00551 } 00552 OutFile << "\n"; 00553 } 00554 OutFile.close(); 00555 00556 sprintf(outName,"calibration_files/epsilon_run%d_dda%d_chan%d.txt",run,dda,chan); 00557 00558 std::ofstream OutFile2(outName); 00559 for(int capArray=0; capArray<2; capArray++){ 00560 OutFile2 << dda << "\t" << chan << "\t" << capArray << "\t"; 00561 OutFile2 << histEpsilon[capArray]->GetMean(1) << "\n"; 00562 } 00563 00564 OutFile2 << "\n"; 00565 00566 OutFile2.close(); 00567 00568 //3.B Filling the newEpsilon[capArray] array 00569 00570 for(int capArray=0;capArray<2;capArray++){ 00571 newEpsilon[capArray]=histEpsilon[capArray]->GetMean(1); 00572 } 00573 00574 00575 00576 00577 for(int half=0;half<2;half++){ 00578 for(int samp=0; samp<SAMPLES_PER_BLOCK/2;samp++){ 00579 newTimeValsEpsilon[1][half][samp]+=newTimeVals[1][half][samp]+(newTimeVals[0][1][31]+histEpsilon[0]->GetMean(1)); 00580 newTimeValsEpsilon[0][half][samp]=newTimeVals[0][half][samp]; 00581 } 00582 } 00583 00584 if(debug){ 00585 cout << endl << "--------------Done epsilon values-------------" << endl << "cap samp Time even Time odd" << endl; 00586 //jpd print em all to screen 00587 for(int capArray=0;capArray<2;capArray++) { 00588 for(int samp=0;samp<SAMPLES_PER_BLOCK/2;samp++) { 00589 printf("%i\t%i\t%0.3f\t%0.3f\n", capArray, samp, newTimeValsEpsilon[capArray][0][samp], newTimeValsEpsilon[capArray][1][samp]); 00590 } 00591 } 00592 00593 cout << "Estimate epsilon 0 to 1 is " << histEpsilon[0]->GetMean(1) << " 1 to 0 is " << histEpsilon[1]->GetMean(1) << endl; 00594 } 00595 //jpd end of epsilon calculation 00596 00597 00598 00599 histFile->Write(); 00600 for(int capArray=0; capArray<2;capArray++){ 00601 if(histLag[capArray]) 00602 delete histLag[capArray]; 00603 if(histEpsilon[capArray]) 00604 delete histEpsilon[capArray]; 00605 for(int half=0; half<2; half++){ 00606 if(histZC[capArray][half]) 00607 delete histZC[capArray][half]; 00608 if(histBinWidth[capArray][half]) 00609 delete histBinWidth[capArray][half]; 00610 } 00611 } 00612 if(histMean) 00613 delete histMean; 00614 if(lagTree) 00615 delete lagTree; 00616 if(epsilonTree) 00617 delete epsilonTree; 00618 if(fp) 00619 delete fp; 00620 if(histFile) 00621 delete histFile; 00622 00623 cout << "\n"; 00624 return 0; 00625 } 00626 00627 00628 Double_t estimateLag(TGraph *grIn, Double_t freq) 00629 { 00630 // This funciton estimates the lag by just using all the negative-positive zero crossing 00631 00632 Int_t numPoints=grIn->GetN(); 00633 if(numPoints<3) return 0; 00634 Double_t period=1./freq; 00635 Double_t *tVals=grIn->GetX(); 00636 Double_t *vVals=grIn->GetY(); 00637 00638 Double_t zc[1000]={0}; 00639 Double_t rawZc[1000]={0}; 00640 int countZC=0; 00641 for(int i=2;i<numPoints;i++) { 00642 if(vVals[i-1]<0 && vVals[i]>0) { 00643 Double_t x1=tVals[i-1]; 00644 Double_t x2=tVals[i]; 00645 Double_t y1=vVals[i-1]; 00646 Double_t y2=vVals[i]; 00647 // std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n"; 00648 zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1; 00649 rawZc[countZC]=zc[countZC]; 00650 countZC++; 00651 // if(countZC==1) 00652 // break; 00653 } 00654 } 00655 00656 Double_t firstZC=zc[0]; 00657 while(firstZC>period) firstZC-=period; 00658 Double_t meanZC=0; 00659 for(int i=0;i<countZC;i++) { 00660 while((zc[i]-firstZC)>period) zc[i]-=period; 00661 if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC)) 00662 zc[i]-=period; 00663 meanZC+=zc[i]; 00664 // std::cout << i << "\t" << zc[i] << "\n"; 00665 } 00666 // TCanvas *can = new TCanvas(); 00667 // TGraph *gr = new TGraph(countZC,rawZc,zc); 00668 // gr->Draw("ap"); 00669 00670 // std::cout << "\n"; 00671 meanZC/=countZC; 00672 00673 // std::cout << zc << "\n"; 00674 return meanZC; 00675 00676 } 00677 00678 Double_t estimateFirstZC(TGraph *grIn, Double_t freq, Int_t *nZC) 00679 { 00680 // This funciton estimates the lag by just using all the negative-positive zero crossing 00681 00682 Int_t numPoints=grIn->GetN(); 00683 if(numPoints<3) return 0; 00684 Double_t period=1./freq; 00685 Double_t *tVals=grIn->GetX(); 00686 Double_t *vVals=grIn->GetY(); 00687 00688 Double_t zc[1000]={0}; 00689 Double_t rawZc[1000]={0}; 00690 int countZC=0; 00691 for(int i=2;i<numPoints;i++) { 00692 if(vVals[i-1]<0 && vVals[i]>0) { 00693 Double_t x1=tVals[i-1]; 00694 Double_t x2=tVals[i]; 00695 Double_t y1=vVals[i-1]; 00696 Double_t y2=vVals[i]; 00697 // std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n"; 00698 zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1; 00699 rawZc[countZC]=zc[countZC]; 00700 countZC++; 00701 // if(countZC==1) 00702 // break; 00703 } 00704 } 00705 00706 Double_t firstZC=zc[0]; 00707 // while(firstZC>period) firstZC-=period; 00708 Double_t meanZC=0; 00709 for(int i=0;i<countZC;i++) { 00710 while((zc[i]-firstZC)>period) zc[i]-=period; 00711 if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC)) 00712 zc[i]-=period; 00713 meanZC+=zc[i]; 00714 // std::cout << i << "\t" << zc[i] << "\n"; 00715 } 00716 // TCanvas *can = new TCanvas(); 00717 // TGraph *gr = new TGraph(countZC,rawZc,zc); 00718 // gr->Draw("ap"); 00719 00720 // std::cout << "\n"; 00721 meanZC/=countZC; 00722 00723 // std::cout << zc << "\n"; 00724 00725 *nZC=countZC; 00726 00727 return meanZC; 00728 00729 } 00730 00731 Double_t estimateLastZC(TGraph *grIn, Double_t freq, Int_t *nZC) 00732 { 00733 // This funciton estimates the lag by just using all the negative-positive zero crossing 00734 00735 Int_t numPoints=grIn->GetN(); 00736 if(numPoints<3) return 0; 00737 Double_t period=1./freq; 00738 Double_t *tVals=grIn->GetX(); 00739 Double_t *vVals=grIn->GetY(); 00740 00741 Double_t zc[1000]={0}; 00742 Double_t rawZc[1000]={0}; 00743 int countZC=0; 00744 for(int i=numPoints-1;i>1;i--) { 00745 if(vVals[i-1]<0 && vVals[i]>0) { 00746 Double_t x1=tVals[i-1]; 00747 Double_t x2=tVals[i]; 00748 Double_t y1=vVals[i-1]; 00749 Double_t y2=vVals[i]; 00750 // std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n"; 00751 zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1; 00752 rawZc[countZC]=zc[countZC]; 00753 countZC++; 00754 // if(countZC==1) 00755 // break; 00756 } 00757 } 00758 00759 Double_t lastZC=zc[0]; 00760 while(lastZC<(tVals[numPoints-1]-period)) lastZC+=period; 00761 Double_t meanZC=0; 00762 for(int i=0;i<countZC;i++) { 00763 while((lastZC-zc[i])>period) zc[i]+=period; 00764 // if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC)) 00765 // zc[i]-=period;] 00766 if((zc[i]+period-lastZC)<(lastZC-zc[i])) 00767 zc[i]+=period; 00768 00769 meanZC+=zc[i]; 00770 // std::cout << i << "\t" << zc[i] << "\n"; 00771 } 00772 // TCanvas *can = new TCanvas(); 00773 // TGraph *gr = new TGraph(countZC,rawZc,zc); 00774 // gr->Draw("ap"); 00775 00776 // std::cout << "\n"; 00777 meanZC/=countZC; 00778 00779 // std::cout << zc << "\n"; 00780 *nZC=countZC; 00781 00782 return meanZC; 00783 00784 } 00785 00786 00787 void plotEvent(Int_t event, Int_t block){ 00788 Int_t chanIndex=lastChan+RFCHAN_PER_DDA*lastDda; 00789 lastEvent=event; 00790 lastBlock=block; 00791 00792 char inName[180]; 00793 sprintf(inName,"/Users/jdavies/ara/data/hawaii2011/root/run%d/event%d.root",lastRun,lastRun); 00794 00795 TFile *fp = new TFile(inName); 00796 00797 TTree *eventTree = (TTree*) fp->Get("eventTree"); 00798 00799 RawAtriStationEvent *evPtr=0; 00800 eventTree->SetBranchAddress("event",&evPtr); 00801 00802 eventTree->GetEntry(event); 00803 UsefulAtriStationEvent realEvent(evPtr,AraCalType::kVoltageTime); 00804 00805 //For now will assume all the ddas have the same block 00806 Int_t capArray=evPtr->blockVec[0].getCapArray(); 00807 Int_t thisCapArray=0; 00808 00809 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00810 Double_t *rawT=gr->GetX(); 00811 Double_t *rawV=gr->GetY(); 00812 Double_t rawSubArrayT[128]; 00813 Double_t rawSubArrayV[128]; 00814 Double_t calibratedSubArrayT[128]; 00815 Double_t calibratedSubArrayV[128]; 00816 00817 Double_t mean[2]={0}; 00818 Int_t numSamples=gr->GetN(); 00819 00820 // if(numSamples>6*SAMPLES_PER_BLOCK){ 00821 // cout << "Too many samples to plot!" << endl; 00822 // return; 00823 // } 00824 Int_t numBlocks=numSamples/64; 00825 Double_t tVals[2][32]; 00826 Double_t tValsRaw[2][32]; 00827 Double_t vVals[2][32]; 00828 00829 printf("Analysing run %i event %i dda %i channel %i\n", lastRun, event, lastDda, lastChan); 00830 printf("%i samples %i blocks\n", numSamples, numBlocks); 00831 00832 TCanvas *can = new TCanvas();//"can","can",600,600); 00833 can->Divide(1,3); 00834 can->cd(1); 00835 00836 //jpd cut out the relevant part of the graph 00837 00838 for(int samp=0; samp<SAMPLES_PER_BLOCK*2;samp++){ 00839 rawSubArrayT[samp]=rawT[samp+SAMPLES_PER_BLOCK*block]; 00840 rawSubArrayV[samp]=rawV[samp+SAMPLES_PER_BLOCK*block]; 00841 } 00842 00843 //should probably zero mean it 00844 00845 //now want to calibrate the graph properly 00846 //firstly populate timeVals with the newTimeVals 00847 //then re-order timeVals 00848 00849 Int_t indexVals[2][SAMPLES_PER_BLOCK]; 00850 Double_t timeVals[2][SAMPLES_PER_BLOCK]; 00851 Int_t lastSampleFirstBlock; 00852 00853 for(int cap=0; cap<2;cap++){ 00854 for(int samp=0; samp<SAMPLES_PER_BLOCK;samp++){ 00855 timeVals[cap][samp]=newTimeVals[cap][samp%2][samp/2]; 00856 } 00857 TMath::Sort(SAMPLES_PER_BLOCK, timeVals[cap], indexVals[cap],kFALSE); 00858 } 00859 00860 //check that the sorting has worked properly 00861 //seems to be working fine! 00862 cout << "-------------sorted the time values--------------" <<endl; 00863 00864 for(int cap=0;cap<2;cap++){ 00865 for(int samp=0; samp<SAMPLES_PER_BLOCK;samp++){ 00866 printf("%i\t%i\t%i\t%0.3f\n", cap, samp, indexVals[cap][samp], timeVals[cap][samp]); 00867 } 00868 } 00869 00870 cout << "-----------------end of-------------------" << endl; 00871 00872 00873 00874 //find last sample in the first block and the capArray of this block 00875 00876 if(block%2) thisCapArray=1-capArray; 00877 lastSampleFirstBlock=indexVals[thisCapArray][SAMPLES_PER_BLOCK-1]; 00878 00879 cout << "first capArray is " << thisCapArray << " last sample is " << lastSampleFirstBlock << " " << timeVals[thisCapArray][lastSampleFirstBlock] << endl; 00880 00881 //fill the calibratedSubArray and re-order the voltages 00882 for(int samp=0; samp<SAMPLES_PER_BLOCK;samp++){ 00883 00884 calibratedSubArrayT[samp]=timeVals[thisCapArray][samp]; 00885 calibratedSubArrayV[samp]=rawSubArrayV[indexVals[thisCapArray][samp]]; 00886 00887 calibratedSubArrayT[samp+SAMPLES_PER_BLOCK]=timeVals[1-thisCapArray][samp]+timeVals[thisCapArray][lastSampleFirstBlock]+newEpsilon[thisCapArray]; 00888 calibratedSubArrayV[samp+SAMPLES_PER_BLOCK]=rawSubArrayV[indexVals[1-thisCapArray][samp]]; 00889 00890 } 00891 00892 00893 //check that the sorting has worked properly 00894 //seems to be working fine! 00895 cout << "-------------sorted the time values--------------" <<endl; 00896 00897 for(int cap=0;cap<2;cap++){ 00898 for(int samp=0; samp<SAMPLES_PER_BLOCK;samp++){ 00899 printf("%i\t%i\t%i\t%0.3f\n", cap, samp, indexVals[cap][samp], calibratedSubArrayT[samp+cap*SAMPLES_PER_BLOCK]); 00900 } 00901 } 00902 00903 cout << "-----------------end of-------------------" << endl; 00904 00905 00906 00907 00908 //jpd and form grSubArray from the wanted part of the graph 00909 00910 TGraph *grSubArray = new TGraph(SAMPLES_PER_BLOCK*2, rawSubArrayT, rawSubArrayV); 00911 TGraph *grCalSubArray = new TGraph(SAMPLES_PER_BLOCK*2, calibratedSubArrayT, calibratedSubArrayV); 00912 00913 // grSubArray->GetXaxis()->SetLabelSize(0.06); 00914 // grSubArray->SetMarkerStyle(22); 00915 // grSubArray->Draw("alp"); 00916 00917 grCalSubArray->GetXaxis()->SetLabelSize(0.06); 00918 grCalSubArray->SetMarkerStyle(22); 00919 grCalSubArray->Draw("alp"); 00920 00921 //gr->Draw("alp"); 00922 00923 for(int thisBlock=block;thisBlock<block+2;thisBlock++) { 00924 mean[0]=0; 00925 mean[1]=0; 00926 thisCapArray=capArray; 00927 if(thisBlock%2) thisCapArray=1-capArray; 00928 else thisCapArray = capArray; // possible issue here with the wrong capArray! 00929 00930 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00931 tVals[samp%2][samp/2]=rawT[samp+SAMPLES_PER_BLOCK*thisBlock]; 00932 tValsRaw[samp%2][samp/2]=rawT[samp+SAMPLES_PER_BLOCK*thisBlock]; 00933 mean[samp%2]+=rawV[samp+SAMPLES_PER_BLOCK*thisBlock]; 00934 } 00935 mean[0]/=SAMPLES_PER_BLOCK/2; 00936 mean[1]/=SAMPLES_PER_BLOCK/2; 00937 00938 00939 for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) { 00940 tVals[samp%2][samp/2]=newTimeVals[thisCapArray][samp%2][samp/2]; 00941 vVals[samp%2][samp/2]=rawV[samp+SAMPLES_PER_BLOCK*thisBlock];//-mean[samp%2]; 00942 } 00943 00944 TGraph *grHalf[2]={0}; 00945 TGraph *grHalfRaw[2]={0}; 00946 char graphName[180]; 00947 for(int half=0;half<2;half++) { 00948 grHalf[half] = new TGraph(SAMPLES_PER_BLOCK/2,tVals[half],vVals[half]); 00949 grHalfRaw[half] = new TGraph(SAMPLES_PER_BLOCK/2,tValsRaw[half],vVals[half]); 00950 00951 sprintf(graphName, "block %i half %i", thisBlock+1, half+1); 00952 grHalf[half]->SetNameTitle(graphName, graphName); 00953 sprintf(graphName, "block %i half %i - Raw",thisBlock+1, half+1); 00954 grHalfRaw[half]->SetNameTitle(graphName, graphName); 00955 00956 } 00957 00958 can->cd(thisBlock-block+2); 00959 grHalfRaw[0]->SetLineColor(1); 00960 grHalfRaw[0]->SetMarkerColor(1); 00961 grHalfRaw[0]->GetXaxis()->SetLabelSize(0.06); 00962 grHalfRaw[0]->SetMarkerStyle(22); 00963 grHalfRaw[0]->Draw("alp"); 00964 //can->cd(3); 00965 grHalfRaw[1]->SetLineColor(kGreen+2); 00966 grHalfRaw[1]->SetMarkerColor(kGreen+2); 00967 grHalfRaw[1]->GetXaxis()->SetLabelSize(0.06); 00968 grHalfRaw[1]->SetMarkerStyle(22); 00969 grHalfRaw[1]->Draw("lp"); 00970 00971 00972 } 00973 00974 } 00975 00976 00977 void nextEvent(){ 00978 00979 00980 plotEvent(lastEvent+1, lastBlock); 00981 00982 } 00983 void previousEvent(){ 00984 00985 plotEvent(lastEvent-1, lastBlock); 00986 00987 }
Generated on Tue Jul 16 16:58:01 2013 for ARA ROOT v3.10 Software by
