ARA ROOT v3.10 Software

calibration/ATRI/secondCalibTrySingleChannel.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[2][2][32]={{{0}}}; //[capArray][half][sample]
00033 Int_t inter_sample_index[2][2][32]={{{0}}};
00034 Double_t epsilon_times[2]={0}; //[capArray]
00035 
00036 
00037 //Prototype Functions
00038 TGraph* zeroMean(TGraph*);
00039 Int_t estimate_phase(TGraph*, Double_t, Double_t*, Int_t*);
00040 TGraph *getBlockGraph(TGraph*, Int_t);
00041 
00042 //Need to test these functions
00043 TGraph* apply_bin_calibration(TGraph*, Int_t);
00044 Int_t save_inter_sample_times(char*, Int_t, Int_t);
00045 Int_t save_epsilon_times(char*, Int_t, Int_t);
00046 Int_t findLastZC(TGraph*, Double_t, Double_t*);
00047 Int_t findFirstZC(TGraph*, Double_t, Double_t*);
00048 
00049 TGraph* getHalfGraph(TGraph*, Int_t);
00050 Int_t calibrateDdaChan(char*, Int_t, Int_t, Double_t, Int_t, Int_t, bool);
00051 
00052 
00053 int main(int argc, char **argv)
00054 {
00055   Int_t runNum=0, pedNum=0, dda=0, chan=0;
00056   Double_t freq=0;
00057   char baseName[FILENAME_MAX];
00058   bool debug=false;
00059 
00060   if(argc<7) {
00061     std::cerr << "Usage: " << argv[0] << " <baseDir> <runNum> <pedNum> <freq in GHz> <dda> <chan>\n";
00062     return 1;
00063   }
00064   sprintf(baseName, argv[1]);
00065   runNum=atoi(argv[2]);
00066   pedNum=atoi(argv[3]);
00067   freq=atof(argv[4]);
00068   dda=atoi(argv[5]);
00069   chan=atoi(argv[6]);
00070   if(argc>7){
00071     if(atoi(argv[7])) debug=true;
00072   }
00073 
00074   return calibrateDdaChan(baseName, runNum, pedNum, freq, dda, chan, debug);
00075 
00076 }
00077 
00078 
00079 Int_t calibrateDdaChan(char* baseDirName, Int_t runNum, Int_t pedNum, Double_t freq, Int_t dda, Int_t chan, bool debug=false){
00080   Double_t period=1./freq;
00081   Int_t chanIndex=chan+RFCHAN_PER_DDA*dda;
00082 
00083   char runFileName[FILENAME_MAX];
00084   char pedFileName[FILENAME_MAX];
00085   sprintf(runFileName, "%s/root/run%i/event%i.root", baseDirName, runNum, runNum);
00086   sprintf(pedFileName, "%s/raw_data/run_%06i/pedestalWidths.run%06d.dat", baseDirName, pedNum, pedNum);
00087 
00088   // /unix/ara/data/ntu2012/StationTwo/raw_data/run_000464/pedestalWidths.run000464.dat 
00089   printf("runFileName %s\npedFileName %s\nfreq %f dda %i chan %i\n", runFileName, pedFileName, freq, dda, chan);
00090   
00091   TFile *fp = new TFile(runFileName);
00092   if(!fp) {
00093     std::cerr << "Can't open file\n";
00094     return -1;
00095   }
00096   TTree *eventTree = (TTree*) fp->Get("eventTree");
00097   if(!eventTree) {
00098     std::cerr << "Can't find eventTree\n";
00099     return -1;
00100   }
00101   RawAtriStationEvent *evPtr=0;
00102   eventTree->SetBranchAddress("event",&evPtr);
00103   Long64_t numEntries=eventTree->GetEntries();
00104 
00105   if(debug)  std::cout << "Number of entries in file is " << numEntries << std::endl;
00106 
00107   Long64_t starEvery=numEntries/80;
00108   if(starEvery==0) starEvery++;
00109   
00110   Int_t stationId=0;
00111   eventTree->GetEntry(0);
00112   stationId= evPtr->stationId;
00113   if(debug)  std::cerr << "stationId " << stationId << "\n";
00114   AraEventCalibrator *calib = AraEventCalibrator::Instance();
00115   calib->setAtriPedFile(pedFileName, stationId);
00116   
00117   //General output stuff
00118   char outFileName[FILENAME_MAX];
00119   sprintf(outFileName, "%s/root/run%i/binWidthsSingleChannel.root", baseDirName, runNum);
00120   TFile *outFile = new TFile(outFileName, "RECREATE");
00121   Int_t capArray=0,thisCapArray=0, block=0,half=0,sample=0;
00122   Int_t numEvents[2]={0};
00123 
00124   //BinWidth Histos
00125   TH1F *histBinWidth[2][2]; 
00126   char histName[FILENAME_MAX];
00127   for(int capArray=0;capArray<2;capArray++) {
00128     for(int half=0;half<2;half++) {
00129       sprintf(histName,"histBinWidth%d_%d",capArray,half);
00130       histBinWidth[capArray][half] = new TH1F(histName,histName,SAMPLES_PER_BLOCK/2,-0.5,(SAMPLES_PER_BLOCK/2)-0.5);
00131     }
00132   }
00133 
00134   //Interleave Histos
00135   TTree *lagTree = new TTree("lagTree","lagTree");
00136   Int_t noZCs[2]={0};
00137   Double_t lag1,lag0,deltaLag;
00138 
00139   lagTree->Branch("block",&block,"block/I");
00140   lagTree->Branch("noZCs", &noZCs, "noZCs[2]/I");
00141   lagTree->Branch("thisCapArray",&thisCapArray,"thisCapArray/I");
00142   lagTree->Branch("capArray",&capArray,"capArray/I");
00143   lagTree->Branch("lag1",&lag1,"lag1/D");
00144   lagTree->Branch("lag0",&lag0,"lag0/D");
00145   lagTree->Branch("deltaLag",&deltaLag,"deltaLag/D");
00146   Double_t lag[2] = {0};
00147   TH1F *lagHist[2]={0};
00148   for(int capArray=0;capArray<2;capArray++){
00149     sprintf(histName, "hist_lag_capArray%i", capArray);
00150     lagHist[capArray] = new TH1F(histName, histName, 200, -1.0, +1.0);
00151   }
00152 
00153 
00154   //Epsilon Histos
00155   TTree *epsilonTree = new TTree("epsilonTree", "epsilonTree");
00156   Double_t epsilon=0;
00157   Double_t firstZC=0, lastZC=0;
00158   Int_t firstZCCount=0, lastZCCount=0;
00159   Int_t atriBlock=0;
00160   TH1* histEpsilon[2]={0};
00161   epsilonTree->Branch("block",&block,"block/I");
00162   epsilonTree->Branch("thisCapArray",&thisCapArray,"thisCapArray/I");
00163   epsilonTree->Branch("capArray",&capArray,"capArray/I");
00164   epsilonTree->Branch("epsilon",&epsilon,"epsilon/D");
00165   epsilonTree->Branch("firstZC",&firstZC,"firstZC/D");
00166   epsilonTree->Branch("lastZC",&lastZC,"lastZC/D");
00167   epsilonTree->Branch("lastZCCount",&lastZCCount,"lastZCCount/I");
00168   epsilonTree->Branch("firstZCCount",&firstZCCount,"firstZCCount/I");
00169   epsilonTree->Branch("half", &half, "half/I");
00170   epsilonTree->Branch("atriBlock", &atriBlock, "atriBlock/I");
00171 
00172 
00173   //BinWidth Calibration
00174   for(int entry=0;entry<numEntries;entry++){
00175     if(entry%starEvery==0) std::cerr <<"*";
00176     eventTree->GetEntry(entry);
00177     UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed);
00178     capArray = evPtr->blockVec[0].getCapArray(); //capArray of first block
00179     
00180     TGraph *gr = realEvent.getGraphFromElecChan(chanIndex);
00181     TGraph *grZeroMean = zeroMean(gr);
00182     Int_t numSamples = grZeroMean->GetN();
00183     Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK;
00184 
00185 
00186     for(block=1; block<numBlocks; block++){ //FIXME -- Only use blocks > 0
00187       if(block%2) thisCapArray=1-capArray;
00188       else thisCapArray=capArray;
00189       TGraph *grBlock = getBlockGraph(grZeroMean, block);
00190       numEvents[thisCapArray]++;
00191       for(half=0;half<2;half++){
00192         TGraph *grHalf = getHalfGraph(grBlock, half);
00193         Double_t *yVals = grHalf->GetY();
00194         for(sample=0;sample<(SAMPLES_PER_BLOCK)/2-1;sample++){
00195           Double_t val1 = yVals[sample];
00196           Double_t val2 = yVals[sample+1];
00197           if(val1<0 && val2>0){
00198             histBinWidth[thisCapArray][half]->Fill(sample);
00199           }
00200           else if(val1>0 && val2<0){
00201             histBinWidth[thisCapArray][half]->Fill(sample);
00202           }
00203           else if(val1==0 || val2==0){
00204             histBinWidth[thisCapArray][half]->Fill(sample, 0.5);
00205           }
00206         }//sample
00207         if(grHalf) delete grHalf;
00208       }//half      
00209       if(grBlock) delete grBlock;
00210     }//block
00211     
00212     delete gr;
00213     delete grZeroMean;
00214     
00215   }//entry
00216   std::cerr << "\n";
00217 
00218   //Scale the ZC and calculate the bin widths
00219   for(capArray=0;capArray<2;capArray++){
00220     for(half=0;half<2;half++){
00221       histBinWidth[capArray][half]->Scale(1./numEvents[capArray]);
00222       histBinWidth[capArray][half]->Scale(0.5*period);
00223       histBinWidth[capArray][half]->Write();
00224     }//half
00225   }//capArray
00226   
00227   for(capArray=0;capArray<2;capArray++){
00228     for(half=0;half<2;half++){
00229       Double_t time=0;
00230       for(sample=0;sample<SAMPLES_PER_BLOCK/2;sample++){
00231         time+=histBinWidth[capArray][half]->GetBinContent(sample);
00232         inter_sample_times[capArray][half][sample]=time;
00233         inter_sample_index[capArray][half][sample]=sample;
00234         if(debug) printf("capArray %i half %i sample %i time %f\n", capArray, half, sample, time);
00235       }//sample
00236     }//half
00237   }//capArray
00238   
00239 
00240 
00241 
00242 
00243   //Interleave Calibration
00244   if(debug) numEntries=10;
00245   for(int entry=0;entry<numEntries;entry++){
00246     if(entry%starEvery==0) std::cerr <<"*";
00247     eventTree->GetEntry(entry);
00248     UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed);
00249     capArray = evPtr->blockVec[dda].getCapArray(); //capArray of first block
00250     
00251     TGraph *gr = realEvent.getGraphFromElecChan(chanIndex);
00252     TGraph *grZeroMean = zeroMean(gr);
00253     Int_t numSamples = grZeroMean->GetN();
00254     Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK;
00255 
00256     for(block=1; block<numBlocks; block++){ //FIXME -- only use blocks>0
00257       if(block%2) thisCapArray=1-capArray;
00258       else thisCapArray=capArray;
00259       TGraph *grBlock = getBlockGraph(grZeroMean, block);
00260       TGraph *grBlockCalibrated = apply_bin_calibration(grBlock, thisCapArray);
00261       TGraph *grHalf0 = getHalfGraph(grBlockCalibrated, 0);
00262       TGraph *grHalf1 = getHalfGraph(grBlockCalibrated, 1);
00263       Int_t retVal = estimate_phase(grHalf0, period, &lag0, &noZCs[0]);
00264       if(retVal==0){
00265         retVal = estimate_phase(grHalf1, period, &lag1, &noZCs[1]);
00266         if(retVal==0){
00267           deltaLag = lag0-lag1;//FIXME
00268           while(TMath::Abs(deltaLag-period)<TMath::Abs(deltaLag))
00269             deltaLag-=period;
00270           while(TMath::Abs(deltaLag+period)<TMath::Abs(deltaLag))
00271             deltaLag+=period;
00272           lagTree->Fill();
00273           if(TMath::Abs(noZCs[0]-noZCs[1])==0) lagHist[thisCapArray]->Fill(deltaLag);
00274         }
00275 
00276       }
00277 
00278       if(grHalf0) delete grHalf0;
00279       if(grHalf1) delete grHalf1;
00280       if(grBlockCalibrated) delete grBlockCalibrated;
00281       if(grBlock) delete grBlock;
00282     }//block
00283 
00284     
00285     delete gr;
00286     delete grZeroMean;
00287     
00288   }//entry
00289   std::cerr << "\n";
00290 
00291   //Now calculate the lag
00292   for(capArray=0;capArray<2;capArray++){
00293   //   char varexp[100];
00294   //   char selection[100];
00295   //   char name[100];
00296   //   sprintf(varexp, "deltaLag>>tempHist");
00297   //   sprintf(selection, "thisCapArray==%i&&noZCs[0]==noZCs[1]", capArray);
00298   //   sprintf(name, "lag_hist_capArray_%i", capArray);
00299   //   lagTree->Draw(varexp, selection);
00300   //   lagHist[capArray] = (TH1D*) gPad->GetPrimitive("tempHist");
00301   //   lagHist[capArray]->SetName(name);
00302   //   lagHist[capArray]->SetTitle(name);
00303   //   lagHist[capArray]->Write();
00304     lag[capArray] = lagHist[capArray]->GetMean(1);
00305    }//capArray
00306   
00307   for(capArray=0;capArray<2;capArray++){
00308     for(half=0;half<2;half++){
00309       Double_t time=0;
00310       for(sample=0;sample<SAMPLES_PER_BLOCK/2;sample++){
00311         inter_sample_times[capArray][half][sample]=inter_sample_times[capArray][half][sample]+lag[capArray]*half;
00312         inter_sample_index[capArray][half][sample]=inter_sample_index[capArray][half][sample];
00313       }//sample
00314     }//half
00315   }//capArray
00316 
00317  
00318   //Now calculate epsilon
00319   for(int entry=0;entry<numEntries;entry++){
00320     if(entry%starEvery==0) std::cerr <<"*";
00321     eventTree->GetEntry(entry);
00322     UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed);
00323     capArray = evPtr->blockVec[dda].getCapArray(); //capArray of first block
00324     atriBlock = evPtr->blockVec[dda].getBlock();
00325     TGraph *gr = realEvent.getGraphFromElecChan(chanIndex);
00326     TGraph *grZeroMean = zeroMean(gr);
00327     Int_t numSamples = grZeroMean->GetN();
00328     Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK;
00329     
00330     for(block=1; block<numBlocks-1; block++){ //FIXME -- only use blocks > 0
00331       if(block%2) thisCapArray=1-capArray;
00332       else thisCapArray=capArray;
00333       TGraph *grBlock0 = getBlockGraph(grZeroMean, block);
00334       TGraph *grBlockCalibrated0 = apply_bin_calibration(grBlock0, thisCapArray);
00335       TGraph *grBlock1 = getBlockGraph(grZeroMean, block+1);
00336       TGraph *grBlockCalibrated1 = apply_bin_calibration(grBlock1, 1-thisCapArray);
00337 
00338       // lastZCCount = findLastZC(grBlockCalibrated0, period, &lastZC);
00339       // firstZCCount = findFirstZC(grBlockCalibrated1, period, &firstZC);
00340       // Double_t *tVals = grBlockCalibrated0->GetX();
00341       // Double_t lastSample = tVals[SAMPLES_PER_BLOCK-1];
00342       // epsilon = firstZC-lastZC+lastSample;
00343       // epsilonTree->Fill();
00344 
00345       TGraph *grBlock0Half0 = getHalfGraph(grBlockCalibrated0, 0);
00346       TGraph *grBlock0Half1 = getHalfGraph(grBlockCalibrated0, 1);
00347       TGraph *grBlock1Half0 = getHalfGraph(grBlockCalibrated1, 0);
00348       TGraph *grBlock1Half1 = getHalfGraph(grBlockCalibrated1, 1);
00349 
00350       lastZCCount = findLastZC(grBlock0Half0, period, &lastZC);
00351       firstZCCount = findFirstZC(grBlock1Half0, period, &firstZC);
00352       half = 0;
00353       Double_t *tVals = grBlockCalibrated0->GetX();
00354       Double_t lastSample = tVals[SAMPLES_PER_BLOCK-1];
00355       epsilon = -firstZC+lastZC-lastSample+period;
00356       
00357       if(epsilon < -1*period) epsilon+=period;
00358       if(epsilon > +1*period) epsilon-=period;
00359 
00360       epsilonTree->Fill();
00361       lastZCCount = findLastZC(grBlock0Half1, period, &lastZC);
00362       firstZCCount = findFirstZC(grBlock1Half1, period, &firstZC);
00363       half = 1;
00364       epsilon = -firstZC+lastZC-lastSample+period;
00365 
00366       if(epsilon < -1*period) epsilon+=period;
00367       if(epsilon > +1*period) epsilon-=period;
00368       epsilonTree->Fill();
00369       if(grBlock0Half0) delete grBlock0Half0;
00370       if(grBlock0Half1) delete grBlock0Half1;
00371       if(grBlock1Half0) delete grBlock1Half0;
00372       if(grBlock1Half1) delete grBlock1Half1;
00373       
00374       if(grBlockCalibrated0) delete grBlockCalibrated0;
00375       if(grBlock0) delete grBlock0;
00376       if(grBlockCalibrated1) delete grBlockCalibrated1;
00377       if(grBlock1) delete grBlock1;
00378     }//block
00379     
00380     delete gr;
00381     delete grZeroMean;
00382     
00383   }//entry
00384   std::cerr << "\n";
00385 
00386   //Calculate actual epsilon from Tree
00387 
00388   for(capArray=0;capArray<2;capArray++){
00389     char varexp[100];
00390     char selection[100];
00391     char name[100];
00392     sprintf(varexp, "epsilon>>tempHist");
00393     sprintf(selection, "thisCapArray==%i&&epsilon>-1.&&epsilon<1.", capArray);
00394     sprintf(name, "epsilon_hist_capArray_%i", capArray);
00395     epsilonTree->Draw(varexp, selection);
00396     histEpsilon[capArray] = (TH1D*) gPad->GetPrimitive("tempHist");
00397     histEpsilon[capArray]->SetName(name);
00398     histEpsilon[capArray]->SetTitle(name);
00399     histEpsilon[capArray]->Write();
00400     epsilon_times[capArray] = histEpsilon[capArray]->GetMean(1);
00401   }//capArray
00402 
00403   save_inter_sample_times(outFileName, dda, chan);
00404   save_epsilon_times(outFileName, dda, chan);
00405 
00406   outFile->Write();
00407 
00408   return 0;
00409 }
00410 
00411 
00412 
00413 TGraph* zeroMean(TGraph* gr){
00414   Double_t *xVals=gr->GetX();
00415   Double_t *yVals=gr->GetY();
00416   Int_t maxN = gr->GetN();
00417 
00418   if(maxN<1) return NULL;
00419 
00420   Double_t mean=0;
00421   for(int i=0;i<maxN; i++){
00422     mean+=yVals[i]/maxN;
00423   }
00424   Double_t *yValsNew = new Double_t[maxN];
00425   for(int i=0;i<maxN; i++){
00426     yValsNew[i]=yVals[i]-mean;
00427   }
00428   TGraph *grZeroMeaned = new TGraph(maxN, xVals, yValsNew);
00429   
00430   delete yValsNew;
00431   return grZeroMeaned;
00432   
00433 }
00434 
00435 TGraph *getBlockGraph(TGraph *fullEventGraph, Int_t block){
00436   Int_t numSamples = fullEventGraph->GetN();
00437   Int_t numBlocks = numSamples / SAMPLES_PER_BLOCK;
00438   if(block > numBlocks) return NULL;
00439   Double_t *fullX = fullEventGraph->GetX();
00440   Double_t *fullY = fullEventGraph->GetY();  
00441   Double_t *blockX = new Double_t[SAMPLES_PER_BLOCK];
00442   Double_t *blockY = new Double_t[SAMPLES_PER_BLOCK];
00443   for(int sample=0;sample<SAMPLES_PER_BLOCK; sample++){
00444     blockY[sample] = fullY[sample + block*SAMPLES_PER_BLOCK];
00445     blockX[sample] = fullX[sample];
00446   }
00447   TGraph *blockGraph = new TGraph(SAMPLES_PER_BLOCK, blockX, blockY);
00448   delete blockX;
00449   delete blockY;
00450   return blockGraph;
00451 }
00452 
00453 TGraph *getHalfGraph(TGraph *fullGraph, Int_t half){
00454   Int_t numSamples = fullGraph->GetN();
00455   Double_t *xFull  = fullGraph->GetX();
00456   Double_t *yFull  = fullGraph->GetY();
00457   Double_t *newX = new Double_t[numSamples/2];
00458   Double_t *newY = new Double_t[numSamples/2];
00459 
00460   for(Int_t sample=0;sample<numSamples;sample++){
00461     if(sample%2!=half) continue;
00462     newX[sample/2]=xFull[sample];
00463     newY[sample/2]=yFull[sample];
00464     //printf("half %i sample/2 %i sample %i\n", half, sample/2, sample);
00465 
00466   }
00467   TGraph *halfGraph = new TGraph(numSamples/2, newX, newY);
00468   // for(int sample=0;sample<numSamples/2;sample++){
00469   //   printf("sample %i newX[%i] %f newY[%i] %f\n", sample, sample, newX[sample], sample, newY[sample]);
00470   // }
00471    
00472   delete newX;
00473   delete newY;
00474   return halfGraph;
00475   
00476 }
00477 
00478 TGraph* apply_bin_calibration(TGraph* grBlock, Int_t capArray){
00479   Int_t numSamples = grBlock->GetN();
00480   if(numSamples!=SAMPLES_PER_BLOCK){
00481 
00482     fprintf(stderr, "%s : wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK);
00483     return NULL;
00484 
00485   }
00486   
00487   Double_t *yVals = grBlock->GetY();
00488   Double_t *xVals = new Double_t[SAMPLES_PER_BLOCK];
00489   
00490   for(Int_t sample=0;sample<SAMPLES_PER_BLOCK;sample++){
00491     xVals[sample] = inter_sample_times[capArray][sample%2][sample/2];
00492   }//sample
00493   //FIXME -- need to take into account the ordering of samples
00494   //Maybe make a note in the calibration file name
00495   
00496   TGraph *grBlockCalibrated = new TGraph(SAMPLES_PER_BLOCK, xVals, yVals);
00497   delete xVals;
00498 
00499   return grBlockCalibrated;
00500 }
00501 Int_t save_inter_sample_times(char* name, Int_t dda, Int_t channel){
00502 
00503   char outName[180];
00504   sprintf(outName, "%s_sample_timing.txt", name);
00505   std::ofstream OutFile(outName);
00506   Int_t capArray, sample;
00507 
00508 
00509   for(int capArray=0;capArray<2;capArray++) {
00510     OutFile << dda << "\t" << channel << "\t" << capArray << "\t";   
00511     for(sample=0;sample<SAMPLES_PER_BLOCK;sample++) {
00512       //Index values
00513       if(sample%2==0)
00514         OutFile << inter_sample_index[capArray][0][sample/2]*2 << " ";
00515       if(sample%2)
00516         OutFile << inter_sample_index[capArray][1][sample/2]*2+1 << " ";
00517     }
00518     OutFile << "\n";
00519     OutFile << dda << "\t" << channel << "\t" << capArray << "\t";   
00520     for(int sample=0;sample<SAMPLES_PER_BLOCK;sample++) {
00521       //time values
00522       if(sample%2==0)
00523         OutFile << inter_sample_times[capArray][0][sample/2] << " ";
00524       if(sample%2)
00525         OutFile << inter_sample_times[capArray][1][sample/2] << " ";
00526     }
00527     OutFile << "\n";
00528   }
00529   OutFile.close();
00530 
00531 
00532   return 0;
00533 
00534 
00535 }
00536 
00537 
00538 Int_t estimate_phase(TGraph *gr, Double_t period, Double_t *meanPhase, Int_t *totalZCs){
00539   Double_t *yVals = gr->GetY();
00540   Double_t *xVals = gr->GetX();
00541   Int_t numSamples = gr->GetN();
00542   if(numSamples != SAMPLES_PER_BLOCK/2){
00543     fprintf(stderr, "%s : Wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK/2);
00544     return -1;
00545   }
00546   Double_t phase=0;
00547   Int_t numZCs=0;
00548 
00549   for(int sample=0;sample<numSamples-1;sample++){
00550     Double_t y1=yVals[sample];
00551     Double_t y2=yVals[sample+1];
00552     if(y1<0 && y2>0){
00553       Double_t x1=xVals[sample]; 
00554       Double_t x2=xVals[sample+1]; 
00555       Double_t zc=((0-y1)/(y2-y1))*(x2-x1)+x1;
00556       //      if(zc<0.6) printf("sample %i y1 %f y2 %f x1 %f x2 %f\n", sample, y1, y2, x1, x2);
00557       phase+=zc-numZCs*period;
00558       //      printf("zc num %i val %f adjusted val %f\n", numZCs, zc, zc-numZCs*period);
00559       numZCs++;
00560     }
00561   }//sample
00562 
00563   if(!numZCs)
00564     phase=0;
00565   else phase=phase/numZCs;
00566   
00567   *totalZCs = numZCs;
00568   *meanPhase = phase;
00569   return 0;
00570   
00571   
00572 }
00573 
00574 Int_t findFirstZC(TGraph *graph, Double_t period, Double_t *lastAvZC){
00575   Double_t *postWrap[2], thisZC=0, lastZC=0, meanZC=0;
00576   Int_t noZCs=0;
00577   Int_t noSamples=graph->GetN();
00578   postWrap[0]=graph->GetX();
00579   postWrap[1]=graph->GetY();
00580 
00581   //  if(debug>1)  printf("Finding last ZC\n");
00582 
00583   for(Int_t Sample=0; Sample<noSamples-1; Sample++){
00584     if(postWrap[1][Sample]<0&&postWrap[1][Sample+1]>0){
00585       Double_t x1=postWrap[0][Sample];
00586       Double_t x2=postWrap[0][Sample+1];
00587       Double_t y1=postWrap[1][Sample];
00588       Double_t y2=postWrap[1][Sample+1];
00589       thisZC=y1*(x1-x2)/(y2-y1)+(x1);  
00590       if(!noZCs){
00591         lastZC=thisZC;
00592       }
00593       //      printf("zc %i position %f adj position %f\n", noZCs+1, thisZC, thisZC-noZCs*period);
00594       thisZC-=noZCs*period;
00595       meanZC+=thisZC;
00596       noZCs++;
00597     }
00598   }
00599   
00600   //  if(debug>1)  printf("Average value is %f\n", meanZC/noZCs);
00601   
00602   if(noZCs){
00603     *lastAvZC=meanZC/noZCs;
00604     return noZCs;
00605   }
00606   return -1;
00607 }
00608 Int_t findLastZC(TGraph *graph, Double_t period, Double_t *lastAvZC){
00609   Double_t *preWrap[2], thisZC=0, lastZC=0, meanZC=0;
00610   Int_t noZCs=0;
00611   Int_t noSamples=graph->GetN();
00612   preWrap[0]=graph->GetX();
00613   preWrap[1]=graph->GetY();
00614 
00615   //  if(debug>1)  printf("Finding last ZC\n");
00616 
00617   for(Int_t Sample=noSamples-1; Sample>0; --Sample){
00618     if(preWrap[1][Sample-1]<0&&preWrap[1][Sample]>0){
00619       Double_t x1=preWrap[0][Sample-1];
00620       Double_t x2=preWrap[0][Sample];
00621       Double_t y1=preWrap[1][Sample-1];
00622       Double_t y2=preWrap[1][Sample];
00623      
00624       thisZC=y1*(x1-x2)/(y2-y1)+(x1);  
00625       if(!noZCs){
00626         lastZC=thisZC;
00627       }
00628       //      printf("zc %i position %f adj position %f\n", noZCs+1, thisZC, thisZC+noZCs*period);
00629       thisZC+=noZCs*period;
00630       meanZC+=thisZC;
00631       noZCs++;
00632     }
00633   }
00634   
00635   //  if(debug>1)  printf("Average value is %f\n", meanZC/noZCs);
00636   
00637   if(noZCs){
00638     *lastAvZC=meanZC/noZCs;
00639     return noZCs;
00640   }
00641   return -1;
00642 }
00643 
00644 
00645 Int_t save_epsilon_times(char* name, Int_t dda, Int_t channel){
00646 
00647   char outName[180];
00648   sprintf(outName, "%s_epsilon_timing.txt", name);
00649   std::ofstream OutFile(outName);
00650   Int_t capArray, sample;
00651 
00652   for(int capArray=0;capArray<2;capArray++){
00653     OutFile <<  dda << "\t"
00654             << channel << "\t" 
00655             << capArray << "\t";
00656     OutFile << epsilon_times[capArray] << "\n";
00657 
00658   }
00659 
00660   OutFile.close();
00661  
00662   return 0;
00663  
00664 }

Generated on Tue Jul 16 16:58:02 2013 for ARA ROOT v3.10 Software by doxygen 1.4.7