ARA ROOT v3.10 Software

calibration/ATRI/rjnCalib.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 Double_t half_inter_sample_times[DDA_PER_ATRI][RFCHAN_PER_DDA][2][2][SAMPLES_PER_BLOCK]={{{{{0}}}}}; //[dda][chan][capArray][half][sample]
00034 Int_t inter_sample_index[DDA_PER_ATRI][RFCHAN_PER_DDA][2][SAMPLES_PER_BLOCK*2]={{{{0}}}}; //[dda][chan][capArray][sample]
00035 Double_t epsilon_times[DDA_PER_ATRI][RFCHAN_PER_DDA][2]={{{0}}}; //[dda][chan][capArray]
00036 Double_t epsilon_times_half[DDA_PER_ATRI][RFCHAN_PER_DDA][2][2]={{{{0}}}}; //[dda][chan][capArray][2]
00037 
00038 
00039 //Prototype Functions
00040 TGraph* zeroMean(TGraph*);
00041 Int_t estimate_phase(TGraph*, Double_t, Double_t*, Int_t*);
00042 Int_t estimate_phase_two_blocks(TGraph*, Double_t, Double_t*, Int_t*);
00043 TGraph *getBlockGraph(TGraph*, Int_t);
00044 TGraph *getTwoBlockGraph(TGraph*, Int_t);
00045 
00046 TGraph* apply_bin_calibration(TGraph*, Int_t, Int_t, Int_t);
00047 TGraph* apply_bin_calibration_half(TGraph*, Int_t, Int_t, Int_t, Int_t);
00048 TGraph* apply_bin_calibration_two_blocks(TGraph*, Int_t, Int_t, Int_t);
00049 Int_t save_inter_sample_times(char*, int goodSampleTiming[DDA_PER_ATRI][RFCHAN_PER_DDA]);
00050 Int_t save_epsilon_times(char*, int goodSampleTiming[DDA_PER_ATRI][RFCHAN_PER_DDA]);
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   sprintf(baseName, argv[1]);
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(baseName, 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, 2);
00120   
00121   //General output stuff
00122   char outFileName[FILENAME_MAX];
00123   sprintf(outFileName, "%s/root/run%i/calibRJN.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]={{0}}; //[dda][chan];
00171   TH1* histEpsilonHalf[DDA_PER_ATRI][RFCHAN_PER_DDA][2]={{{0}}}; //[dda][chan];
00172   epsilonTree->Branch("dda",&dda,"dda/I");
00173   epsilonTree->Branch("chan",&chan,"chan/I");
00174   epsilonTree->Branch("block",&block,"block/I");
00175   epsilonTree->Branch("epsilon",&epsilon,"epsilon/D");
00176   epsilonTree->Branch("firstZC",&firstZC,"firstZC/D");
00177   epsilonTree->Branch("lastZC",&lastZC,"lastZC/D");
00178   epsilonTree->Branch("lastZCCount",&lastZCCount,"lastZCCount/I");
00179   epsilonTree->Branch("firstZCCount",&firstZCCount,"firstZCCount/I");
00180   epsilonTree->Branch("half", &half, "half/I");
00181   epsilonTree->Branch("atriBlock", &atriBlock, "atriBlock/I");
00182   for(dda=0;dda<DDA_PER_ATRI;dda++){
00183     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00184       sprintf(histName,"epsilon_hist_dda%i_chan%i",dda, chan);
00185       histEpsilon[dda][chan] = new TH1F(histName,histName,600,-3.0,3.0);
00186       for(int half=0;half<2;half++) {
00187         sprintf(histName,"epsilon_hist_dda%i_chan%i_half%d",dda, chan,half);
00188         histEpsilonHalf[dda][chan][half] = new TH1F(histName,histName,600,-3.0,3.0);
00189       }
00190     }
00191   }
00192   
00193 
00194 
00195   Double_t time=0;
00196   Int_t index=0;
00197   TTree *binWidthsTree = new TTree("binWidthsTree", "binWidthsTree");
00198   binWidthsTree->Branch("dda", &dda, "dda/I");
00199   binWidthsTree->Branch("chan", &chan, "chan/I");
00200   binWidthsTree->Branch("capArray", &capArray, "capArray/I");
00201   binWidthsTree->Branch("sample", &sample, "sample/I");
00202   binWidthsTree->Branch("time", &time, "time/D");
00203   binWidthsTree->Branch("index", &index, "index/I");
00204   binWidthsTree->Branch("epsilon", &epsilon, "epsilon/D");
00205   
00206 
00207   //BinWidth Calibration
00208   for(int entry=0;entry<numEntries;entry++){
00209     if(entry%starEvery==0) std::cerr <<"*";
00210     eventTree->GetEntry(entry);
00211     UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed);
00212     capArray = evPtr->blockVec[0].getCapArray(); //capArray of first block
00213     
00214     for(dda=0;dda<DDA_PER_ATRI;dda++){
00215       for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00216         chanIndex=chan+RFCHAN_PER_DDA*dda;
00217         TGraph *gr = realEvent.getGraphFromElecChan(chanIndex);
00218         TGraph *grZeroMean = zeroMean(gr);
00219         Int_t numSamples = grZeroMean->GetN();
00220         Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK;
00221         
00222         
00223         for(block=0; block<numBlocks-1; block++){ //FIXME -- Only use blocks > 0
00224           if(block%2) thisCapArray=1-capArray;
00225           else thisCapArray=capArray;
00226           if(thisCapArray==1) continue;
00227           TGraph *grTwoBlock = getTwoBlockGraph(grZeroMean, block);
00228           numEvents[dda][chan]++;
00229           for(half=0;half<2;half++){
00230             TGraph *grHalf = getHalfGraphTwoBlocks(grTwoBlock, half);
00231             Double_t *yVals = grHalf->GetY();
00232             for(sample=0;sample<SAMPLES_PER_BLOCK-1;sample++){
00233               Double_t val1 = yVals[sample];
00234               Double_t val2 = yVals[sample+1];
00235               if(val1<0 && val2>0){
00236                 histBinWidth[dda][chan][half]->Fill(sample);
00237               }
00238               else if(val1>0 && val2<0){
00239                 histBinWidth[dda][chan][half]->Fill(sample);
00240               }
00241               else if(val1==0 || val2==0){
00242                 histBinWidth[dda][chan][half]->Fill(sample, 0.5);
00243               }
00244             }//sample
00245             if(grHalf) delete grHalf;
00246           }//half      
00247           if(grTwoBlock) delete grTwoBlock;
00248         }//block
00249         
00250         delete gr;
00251         delete grZeroMean;
00252       }//chan
00253     }//dda
00254 
00255   }//entry
00256   std::cerr << "\n";
00257 
00258   //Scale the ZC and calculate the bin widths
00259 
00260   for(dda=0;dda<DDA_PER_ATRI;dda++){
00261     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00262       for(half=0;half<2;half++){
00263         histBinWidth[dda][chan][half]->Scale(1./numEvents[dda][chan]);
00264         histBinWidth[dda][chan][half]->Scale(0.5*period);
00265         histBinWidth[dda][chan][half]->Write();
00266       }//half
00267     }//chan
00268   }//dda
00269 
00270   for(dda=0;dda<DDA_PER_ATRI;dda++){
00271     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00272       for(half=0;half<2;half++){
00273         time=0;
00274         for(capArray=0;capArray<2;capArray++){
00275           for(sample=0;sample<SAMPLES_PER_BLOCK/2;sample++){
00276             half_inter_sample_times[dda][chan][capArray][half][sample]=time;
00277             inter_sample_times[dda][chan][capArray][2*sample+half]=time;   
00278             inter_sample_index[dda][chan][capArray][2*sample+half]=2*sample+half;
00279             if(debug&&dda==0&&chan==3) printf("capArray %i half %i sample %i index %d time %f\n", capArray, half, sample, 2*sample+half, time);
00280             time+=histBinWidth[dda][chan][half]->GetBinContent(sample+SAMPLES_PER_BLOCK/2*capArray+1);
00281             if(debug&&dda==0&&chan==0) printf("dda %i chan %i capArray %i half %d time %f\n", dda, chan, capArray,half, half_inter_sample_times[dda][chan][capArray][half][sample]);
00282           }//sample
00283         }//capArray
00284       }//half
00285     }//chan
00286   }//dda
00287 
00288   //Interleave Calibration
00289   //if(debug) numEntries=10;
00290   for(int entry=0;entry<numEntries;entry++){
00291     if(entry%starEvery==0) std::cerr <<"*";
00292     eventTree->GetEntry(entry);
00293     UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed);
00294     capArray = evPtr->blockVec[0].getCapArray(); //capArray of first block
00295     
00296     for(dda=0;dda<DDA_PER_ATRI;dda++){
00297       for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00298         chanIndex=chan+RFCHAN_PER_DDA*dda;
00299         TGraph *gr = realEvent.getGraphFromElecChan(chanIndex);
00300         TGraph *grZeroMean = zeroMean(gr);
00301         Int_t numSamples = grZeroMean->GetN();
00302         Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK;
00303         
00304         for(block=0; block<numBlocks-1; block++){ //FIXME -- only use blocks>0
00305           if(block%2) thisCapArray=1-capArray;
00306           else thisCapArray=capArray;
00307           if(thisCapArray==1) continue;
00308           TGraph *grBlock = getTwoBlockGraph(grZeroMean, block);
00309           TGraph *grBlockCalibrated = apply_bin_calibration_two_blocks(grBlock, thisCapArray, dda, chan);
00310           TGraph *grHalf0 = getHalfGraphTwoBlocks(grBlockCalibrated, 0);
00311           TGraph *grHalf1 = getHalfGraphTwoBlocks(grBlockCalibrated, 1);
00312           Int_t retVal = estimate_phase_two_blocks(grHalf0, period, &lag0, &noZCs[0]);
00313           if(retVal==0){
00314             retVal = estimate_phase_two_blocks(grHalf1, period, &lag1, &noZCs[1]);
00315             if(retVal==0){
00316               deltaLag = lag0-lag1;//FIXME
00317               while(TMath::Abs(deltaLag-period)<TMath::Abs(deltaLag))
00318                 deltaLag-=period;
00319               while(TMath::Abs(deltaLag+period)<TMath::Abs(deltaLag))
00320                 deltaLag+=period;
00321               lagTree->Fill();
00322               if(TMath::Abs(noZCs[0]-noZCs[1])==0) lagHist[dda][chan]->Fill(deltaLag);
00323             }
00324           }
00325           if(grHalf0) delete grHalf0;
00326           if(grHalf1) delete grHalf1;
00327           if(grBlockCalibrated) delete grBlockCalibrated;
00328           if(grBlock) delete grBlock;
00329         }//block
00330         
00331         
00332         delete gr;
00333         delete grZeroMean;
00334       }//chan
00335     }//dda
00336 
00337   }//entry
00338   std::cerr << "\n";
00339 
00340   char varexp[100];
00341   char selection[100];
00342   char name[100];
00343   //Now calculate the lag
00344   Int_t goodSampleTiming[DDA_PER_ATRI][RFCHAN_PER_DDA]={{0}};
00345 
00346 
00347   for(dda=0;dda<DDA_PER_ATRI;dda++){
00348     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00349       lag[dda][chan] = lagHist[dda][chan]->GetMean(1);
00350       if((lagHist[dda][chan]->GetRMS())>0.1) {
00351         printf("dda %i chan %i rms %f\n", dda, chan, lagHist[dda][chan]->GetRMS());
00352         goodSampleTiming[dda][chan]=0;
00353       }
00354       else {
00355         goodSampleTiming[dda][chan]=1;
00356       }
00357       //        printf("dda %i chan %i capArray %i lag %f\n", dda, chan, capArray ,lag[dda][chan][capArray]);   
00358     }//chan
00359   }//dda
00360   
00361   
00362   for(dda=0;dda<DDA_PER_ATRI;dda++){
00363     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00364       for(capArray=0;capArray<2;capArray++){
00365         for(half=0;half<2;half++){
00366           Double_t time=0;
00367           for(sample=0;sample<SAMPLES_PER_BLOCK/2;sample++){
00368 
00369             if(lag[dda][chan]>0) {
00370               inter_sample_times[dda][chan][capArray][2*sample+half]=inter_sample_times[dda][chan][capArray][2*sample+half]+(lag[dda][chan])*half;
00371               half_inter_sample_times[dda][chan][capArray][half][sample]+=(lag[dda][chan])*half;
00372             }
00373             else {
00374               inter_sample_times[dda][chan][capArray][2*sample+half]=inter_sample_times[dda][chan][capArray][2*sample+half]-(lag[dda][chan])*(1-half);
00375               half_inter_sample_times[dda][chan][capArray][half][sample]-=(lag[dda][chan])*(1-half);
00376             }
00377           }//sample
00378         }//half
00379       }//capArray
00380     }//chan
00381   }//dda
00382 
00383   //now sort the times
00384   for(dda=0;dda<DDA_PER_ATRI;dda++){
00385     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00386       for(capArray=0;capArray<2;capArray++){
00387         TMath::Sort(SAMPLES_PER_BLOCK,inter_sample_times[dda][chan][capArray],inter_sample_index[dda][chan][capArray],kFALSE);
00388         
00389         if(debug&&dda==0&&chan==3) {
00390           for(sample=0;sample<SAMPLES_PER_BLOCK;sample++){
00391             int index=inter_sample_index[dda][chan][capArray][sample];
00392             double time=inter_sample_times[dda][chan][capArray][index];
00393             printf("capArray %i sample %i index %d time %f\n", capArray, sample, index,time);
00394           }
00395         }
00396       }
00397     }
00398   }
00399 
00400   //Now revert the inter_sample times to start at zero for each capArray;
00401   //This here is all buggered because quite regular the firstTime in the capArray is index [1] not index [0]
00402   Double_t firstTime=0;
00403   Double_t secondTime=0;
00404   Double_t lastTime=0;
00405   Double_t penultimateTime=0;
00406   for(dda=0;dda<DDA_PER_ATRI;dda++){
00407     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00408       firstTime=inter_sample_times[dda][chan][1][0];
00409       secondTime=inter_sample_times[dda][chan][1][0];
00410       if(secondTime<firstTime) firstTime=secondTime;
00411       lastTime=inter_sample_times[dda][chan][0][SAMPLES_PER_BLOCK-1];
00412       penultimateTime=inter_sample_times[dda][chan][0][SAMPLES_PER_BLOCK-2];
00413       if(lastTime<penultimateTime) lastTime=penultimateTime;
00414       epsilon_times[dda][chan][1]=firstTime-lastTime;
00415       for(sample=0;sample<SAMPLES_PER_BLOCK;sample++){
00416         inter_sample_times[dda][chan][1][sample]-=firstTime;
00417       }
00418       if(debug ) {
00419         std::cout << "epsilon_times[ " << dda << "][" << chan << "][1]= " << epsilon_times[dda][chan][1] << "\n";
00420       }
00421       for(int half=0;half<2;half++) {
00422         Double_t firstTime=half_inter_sample_times[dda][chan][1][half][0];
00423         Double_t lastTime=half_inter_sample_times[dda][chan][0][half][(SAMPLES_PER_BLOCK/2)-1];
00424         epsilon_times_half[dda][chan][1][half]=firstTime-lastTime;
00425         for(int sample=0;sample>(SAMPLES_PER_BLOCK/2);sample++) {
00426           half_inter_sample_times[dda][chan][capArray][half][sample]-=firstTime;
00427         }
00428       }
00429       
00430     }
00431   }
00432   
00433   //  if(debug) numEntries=10;
00434  
00435   char graphName[180];
00436 
00437   //Now calculate epsilon
00438   for(int entry=0;entry<numEntries;entry++){
00439     if(entry%starEvery==0) std::cerr <<"*";
00440     eventTree->GetEntry(entry);
00441     UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed);
00442     capArray = evPtr->blockVec[0].getCapArray(); //capArray of first block
00443     atriBlock = evPtr->blockVec[0].getBlock();
00444 
00445     for(dda=0;dda<DDA_PER_ATRI;dda++){
00446       for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00447         chanIndex=chan+RFCHAN_PER_DDA*dda;
00448         
00449         TGraph *gr = realEvent.getGraphFromElecChan(chanIndex);
00450         TGraph *grZeroMean = zeroMean(gr);
00451         Int_t numSamples = grZeroMean->GetN();
00452         Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK;
00453         
00454         for(block=0; block<numBlocks-1; block++){ 
00455           if(block%2) thisCapArray=1-capArray;
00456           else thisCapArray=capArray;
00457           if(thisCapArray==0) continue;
00458           TGraph *grBlock0 = getBlockGraph(grZeroMean, block);
00459           TGraph *grBlockCalibrated0 = apply_bin_calibration(grBlock0, thisCapArray, dda, chan);
00460           TGraph *grBlock1 = getBlockGraph(grZeroMean, block+1);
00461           TGraph *grBlockCalibrated1 = apply_bin_calibration(grBlock1, 1-thisCapArray, dda, chan);
00462           
00463           //      TCanvas *can = new TCanvas();
00464           //      grBlockCalibrated0->Draw("al");
00465           //      grBlockCalibrated1->SetLineColor(kBlue+2);
00466           //      grBlockCalibrated1->Draw("l");
00467 //        if(dda==0 &&chan==0) {
00468 //          sprintf(graphName,"grBlock0_%d_%d",entry,block);
00469 //          grBlockCalibrated0->SetName(graphName);
00470 //          grBlockCalibrated0->Write();
00471 //          sprintf(graphName,"grBlock1_%d_%d",entry,block);
00472 //          grBlockCalibrated1->SetName(graphName);
00473 //          grBlockCalibrated1->Write();
00474 //        }
00475 
00476           half=-1;
00477           lastZCCount = findLastZC(grBlockCalibrated0, period, &lastZC);
00478           firstZCCount = findFirstZC(grBlockCalibrated1, period, &firstZC);
00479           Double_t *tVals = grBlockCalibrated0->GetX(); //FIXME -- is this really the last sample?
00480           Double_t lastSample = tVals[SAMPLES_PER_BLOCK-1];
00481           epsilon = -firstZC+lastZC-lastSample+period;
00482           if(epsilon < -0.5*period) epsilon+=period;
00483           if(epsilon > +0.5*period) epsilon-=period;
00484           if(TMath::Abs(lastZCCount-firstZCCount)==0) histEpsilon[dda][chan]->Fill(epsilon);
00485           epsilonTree->Fill();
00486 
00487           for(half=0;half<2;half++) {
00488             TGraph *grBlock0Half = getHalfGraph(grBlock0, half);
00489             TGraph *grBlock1Half = getHalfGraph(grBlock1, half);
00490             TGraph *grFirst=apply_bin_calibration_half(grBlock0Half,thisCapArray,dda,chan,half);
00491             TGraph *grSecond=apply_bin_calibration_half(grBlock1Half,1-thisCapArray,dda,chan,half);
00492             
00493             lastZCCount = findLastZC(grFirst, period, &lastZC);
00494             firstZCCount = findFirstZC(grSecond, period, &firstZC);
00495 
00496             Double_t *tVals = grFirst->GetX(); //FIXME -- is this really the last sample?
00497             Double_t lastSample = tVals[(SAMPLES_PER_BLOCK/2)-1];
00498           //   if(dda==0 && chan==0) {
00499 //            std::cout <<entry << "\t" << half << "\t" <<lastZC << "\t" << lastSample << "\t" << grFirst->GetN() << "\n";
00500 
00501 //          }
00502             epsilon = -firstZC+lastZC-lastSample+period;
00503             if(epsilon < -0.5*period) epsilon+=period;
00504             if(epsilon > +0.5*period) epsilon-=period;
00505             epsilonTree->Fill();
00506             if(TMath::Abs(lastZCCount-firstZCCount)==0) histEpsilonHalf[dda][chan][half]->Fill(epsilon);
00507 
00508             if(grBlock0Half) delete grBlock0Half;
00509             if(grBlock1Half) delete grBlock1Half;
00510             if(grFirst) delete grFirst;
00511             if(grSecond) delete grSecond;
00512           }
00513 
00514 
00515           
00516 
00517           
00518           if(grBlockCalibrated0) delete grBlockCalibrated0;
00519           if(grBlock0) delete grBlock0;
00520           if(grBlockCalibrated1) delete grBlockCalibrated1;
00521           if(grBlock1) delete grBlock1;
00522         }//block
00523         
00524         delete gr;
00525         delete grZeroMean;
00526       }//chan
00527     }//dda
00528   }//entry
00529   std::cerr << "\n";
00530 
00531   //zCalculate actual epsilon from Tree
00532   for(dda=0;dda<DDA_PER_ATRI;dda++){
00533     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00534       //      for(capArray=0;capArray<2;capArray++){
00535       //        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]]);
00536         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]]);
00537         deltaT=0; //FIXME
00538         epsilon_times[dda][chan][0] = histEpsilon[dda][chan]->GetMean(1)+deltaT; //FIXME -- only using one half here
00539         if((histEpsilon[dda][chan]->GetRMS())>0.1) printf("Bad full dda %i chan %i  half 0 rms %f\n", dda, chan, histEpsilon[dda][chan]->GetRMS());
00540         for(int half=0;half<2;half++) {
00541           epsilon_times_half[dda][chan][0][half]= histEpsilonHalf[dda][chan][half]->GetMean();
00542           if((histEpsilonHalf[dda][chan][half]->GetRMS())>0.1) printf("Bad half dda %i chan %i  half %d rms %f\n", dda, chan,half, histEpsilonHalf[dda][chan][half]->GetRMS());
00543         }
00544         //      }//capArray
00545     }//chan
00546   }//dda
00547   
00548   
00549   
00550   //Assume that within the same half everything is fine
00551   //So we will include all the even samples and some of the odd
00552   for(int dda=0;dda<DDA_PER_ATRI;dda++){
00553     for(int chan=0;chan<6;chan++){
00554       for(int capArray=0;capArray<2;capArray++) {
00555         Double_t minDiff=1e9;
00556         Int_t badIndexArray[SAMPLES_PER_BLOCK]={0};
00557         Int_t numBadPoints=0;
00558         for(int sample=1;sample<SAMPLES_PER_BLOCK;sample++) {
00559           Int_t index1=inter_sample_index[dda][chan][capArray][sample];
00560           Int_t index0=inter_sample_index[dda][chan][capArray][sample-1];
00561           Double_t time1=inter_sample_times[dda][chan][capArray][index1];
00562           Double_t time0=inter_sample_times[dda][chan][capArray][index0];
00563           if((time1-time0)<minDiff) minDiff=time1-time0;
00564           
00565           if((time1-time0)<0.1) {
00566             //Arbitrary for now
00567             if(index0%2==1) {
00568               if(!badIndexArray[index0]) {
00569                 badIndexArray[index0]=1;
00570                 numBadPoints++;
00571               }
00572             }
00573             else if(index1%2==1) {
00574               if(!badIndexArray[index1]) {
00575                 badIndexArray[index1]=1;
00576                 numBadPoints++;
00577               }
00578             }
00579           }
00580         }
00581         std::cout <<  "Min Diff " << dda << "\t" << chan << "\t" << capArray << "\t" << minDiff << "\t" << numBadPoints << "\n";
00582       }
00583     }
00584   }
00585   
00586   
00587   
00588 
00589 
00590 
00591 
00592   save_inter_sample_times(outFileName,goodSampleTiming);
00593   save_epsilon_times(outFileName,goodSampleTiming);
00594   
00595   for(dda=0;dda<DDA_PER_ATRI;dda++){
00596     for(chan=0;chan<RFCHAN_PER_DDA;chan++){
00597       for(capArray=0;capArray<2;capArray++){
00598         for(sample=0;sample<SAMPLES_PER_BLOCK;sample++){
00599           time=inter_sample_times[dda][chan][capArray][inter_sample_index[dda][chan][capArray][sample]];
00600           index=inter_sample_index[dda][chan][capArray][sample];
00601           epsilon=epsilon_times[dda][chan][capArray];
00602           binWidthsTree->Fill();
00603         }//sample
00604       }//capArray
00605     }//chan
00606   }//dda
00607 
00608 
00609 
00610   outFile->Write();
00611 
00612   return 0;
00613 }
00614 
00615 
00616 
00617 TGraph* zeroMean(TGraph* gr){
00618   Double_t *xVals=gr->GetX();
00619   Double_t *yVals=gr->GetY();
00620   Int_t maxN = gr->GetN();
00621 
00622   if(maxN<1) return NULL;
00623 
00624   Double_t mean=0;
00625   for(int i=0;i<maxN; i++){
00626     mean+=yVals[i]/maxN;
00627   }
00628   Double_t *yValsNew = new Double_t[maxN];
00629   for(int i=0;i<maxN; i++){
00630     yValsNew[i]=yVals[i]-mean;
00631   }
00632   TGraph *grZeroMeaned = new TGraph(maxN, xVals, yValsNew);
00633   
00634   delete yValsNew;
00635   return grZeroMeaned;
00636   
00637 }
00638 
00639 TGraph *getBlockGraph(TGraph *fullEventGraph, Int_t block){
00640   Int_t numSamples = fullEventGraph->GetN();
00641   Int_t numBlocks = numSamples / SAMPLES_PER_BLOCK;
00642   if(block > numBlocks) return NULL;
00643   Double_t *fullX = fullEventGraph->GetX();
00644   Double_t *fullY = fullEventGraph->GetY();  
00645   Double_t *blockX = new Double_t[SAMPLES_PER_BLOCK];
00646   Double_t *blockY = new Double_t[SAMPLES_PER_BLOCK];
00647   for(int sample=0;sample<SAMPLES_PER_BLOCK; sample++){
00648     blockY[sample] = fullY[sample + block*SAMPLES_PER_BLOCK];
00649     blockX[sample] = fullX[sample];
00650   }
00651   TGraph *blockGraph = new TGraph(SAMPLES_PER_BLOCK, blockX, blockY);
00652   delete blockX;
00653   delete blockY;
00654   return blockGraph;
00655 }
00656 
00657 TGraph *getTwoBlockGraph(TGraph *fullEventGraph, Int_t block){
00658   Int_t numSamples = fullEventGraph->GetN();
00659   Int_t numBlocks = numSamples / SAMPLES_PER_BLOCK;
00660   if(block >= numBlocks-1) return NULL;
00661   Double_t *fullX = fullEventGraph->GetX();
00662   Double_t *fullY = fullEventGraph->GetY();  
00663   Double_t *blockX = new Double_t[SAMPLES_PER_BLOCK*2];
00664   Double_t *blockY = new Double_t[SAMPLES_PER_BLOCK*2];
00665   for(int sample=0;sample<SAMPLES_PER_BLOCK*2; sample++){
00666     blockY[sample] = fullY[sample + block*SAMPLES_PER_BLOCK];
00667     blockX[sample] = fullX[sample];
00668   }
00669   TGraph *blockGraph = new TGraph(SAMPLES_PER_BLOCK*2, blockX, blockY);
00670   delete blockX;
00671   delete blockY;
00672   return blockGraph;
00673 }
00674 
00675 
00676 TGraph *getHalfGraph(TGraph *fullGraph, Int_t half){
00677   Int_t numSamples = fullGraph->GetN();
00678   Double_t *xFull  = fullGraph->GetX();
00679   Double_t *yFull  = fullGraph->GetY();
00680   Double_t *newX = new Double_t[numSamples/2];
00681   Double_t *newY = new Double_t[numSamples/2];
00682 
00683   for(Int_t sample=0;sample<numSamples;sample++){
00684     if(sample%2!=half) continue;
00685     newX[sample/2]=xFull[sample];
00686     newY[sample/2]=yFull[sample];
00687   }
00688   TGraph *halfGraph = new TGraph(numSamples/2, newX, newY);
00689    
00690   delete newX;
00691   delete newY;
00692   return halfGraph;
00693   
00694 }
00695 
00696 TGraph *getHalfGraphTwoBlocks(TGraph *fullGraph, Int_t half){
00697   Int_t numSamples = fullGraph->GetN();
00698   Double_t *xFull  = fullGraph->GetX();
00699   Double_t *yFull  = fullGraph->GetY();
00700 
00701   if(numSamples != 2*SAMPLES_PER_BLOCK){
00702     fprintf(stderr, "Wrong number of samples got %d expected %d\n", numSamples, SAMPLES_PER_BLOCK);
00703     return NULL;
00704   }
00705 
00706   Double_t *newX = new Double_t[SAMPLES_PER_BLOCK];
00707   Double_t *newY = new Double_t[SAMPLES_PER_BLOCK];
00708 
00709   for(Int_t sample=0;sample<2*SAMPLES_PER_BLOCK;sample++){
00710     if(sample%2!=half) continue;
00711     newX[sample/2]=xFull[sample];
00712     newY[sample/2]=yFull[sample];
00713   }
00714   TGraph *halfGraph = new TGraph(SAMPLES_PER_BLOCK, newX, newY);
00715    
00716   delete newX;
00717   delete newY;
00718   return halfGraph;
00719   
00720 }
00721 
00722 
00723 
00724 TGraph* apply_bin_calibration_half(TGraph* grBlock, Int_t capArray, Int_t dda, Int_t chan, Int_t half){
00725   Int_t numSamples = grBlock->GetN();
00726   if(numSamples!=SAMPLES_PER_BLOCK/2){
00727 
00728     fprintf(stderr, "%s : wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK/2);
00729     return NULL;
00730 
00731   }
00732   
00733   Double_t *yVals = grBlock->GetY();
00734   Double_t *xVals = new Double_t[SAMPLES_PER_BLOCK/2];
00735   
00736   for(Int_t sample=0;sample<SAMPLES_PER_BLOCK/2;sample++){
00737     xVals[sample] = half_inter_sample_times[dda][chan][capArray][half][sample] + epsilon_times_half[dda][chan][capArray][half];  //RJN do we need to add the epsilon???
00738   }//sample
00739   
00740   TGraph *grHalfCalibrated = new TGraph(SAMPLES_PER_BLOCK/2, xVals, yVals);
00741   delete xVals;
00742 
00743   return grHalfCalibrated;
00744 }
00745 
00746 
00747 TGraph* apply_bin_calibration(TGraph* grBlock, Int_t capArray, Int_t dda, Int_t chan){
00748   Int_t numSamples = grBlock->GetN();
00749   if(numSamples!=SAMPLES_PER_BLOCK){
00750 
00751     fprintf(stderr, "%s : wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK);
00752     return NULL;
00753 
00754   }
00755   
00756   Double_t *yValsIn = grBlock->GetY();
00757   Double_t *xVals = new Double_t[SAMPLES_PER_BLOCK];
00758   Double_t *yVals = new Double_t[SAMPLES_PER_BLOCK];
00759   
00760   for(Int_t sample=0;sample<SAMPLES_PER_BLOCK;sample++){
00761     int index=inter_sample_index[dda][chan][capArray][sample];
00762     xVals[sample] = inter_sample_times[dda][chan][capArray][index] + epsilon_times[dda][chan][capArray];  //RJN do we need to add the epsilon???
00763     yVals[sample] = yValsIn[index];
00764   }//sample
00765   //FIXME -- need to take into account the ordering of samples
00766   //Maybe make a note in the calibration file name
00767   
00768   TGraph *grBlockCalibrated = new TGraph(SAMPLES_PER_BLOCK, xVals, yVals);
00769   delete xVals;
00770   delete yVals;
00771 
00772   return grBlockCalibrated;
00773 }
00774 
00775 TGraph* apply_bin_calibration_two_blocks(TGraph* grBlock, Int_t capArray, Int_t dda, Int_t chan){
00776   //RJN -- Note this will not work if inter_sample_index is not in order as it doesn't switch the yVals at the same time
00777   //Will come back and fix this if necessary
00778 
00779   Int_t numSamples = grBlock->GetN();
00780   if(numSamples!=SAMPLES_PER_BLOCK*2){
00781 
00782     fprintf(stderr, "%s : wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK*2);
00783     return NULL;
00784 
00785   }
00786   
00787   Double_t *yVals = grBlock->GetY();
00788   Double_t *xVals = new Double_t[SAMPLES_PER_BLOCK*2];
00789   
00790   for(Int_t sample=0;sample<SAMPLES_PER_BLOCK;sample++){
00791     xVals[sample] = inter_sample_times[dda][chan][capArray][inter_sample_index[dda][chan][capArray][sample]];
00792   }//sample
00793 
00794   for(Int_t sample=0;sample<SAMPLES_PER_BLOCK;sample++){
00795     xVals[sample+SAMPLES_PER_BLOCK] = inter_sample_times[dda][chan][1-capArray][inter_sample_index[dda][chan][1-capArray][sample]];
00796   }//sample
00797   
00798   TGraph *grBlockCalibrated = new TGraph(2*SAMPLES_PER_BLOCK, xVals, yVals);
00799   delete xVals;
00800 
00801   return grBlockCalibrated;
00802 }
00803 
00804 Int_t save_inter_sample_times(char* name, int goodSampleTiming[DDA_PER_ATRI][RFCHAN_PER_DDA]){
00805 
00806   char outName[180];
00807   sprintf(outName, "%s_sample_timing.txt", name);
00808   std::ofstream OutFile(outName);
00809   Int_t capArray, sample;
00810   
00811   int lastGoodChan=-1;
00812   for(int dda=0;dda<DDA_PER_ATRI;dda++){
00813     for(int chan=0;chan<RFCHAN_PER_DDA;chan++){
00814       for(int capArray=0;capArray<2;capArray++) {
00815         OutFile << dda << "\t" << chan << "\t" << capArray << "\t"  << SAMPLES_PER_BLOCK << "\t";   
00816         
00817         int useDda=dda;
00818         int useChan=chan;
00819         if(goodSampleTiming[dda][chan]) lastGoodChan=chan;
00820         else {
00821           useChan=lastGoodChan;
00822           std::cout << "Replacing " << dda << "," << chan << " with " << useDda << "," << useChan << "\n";
00823         }
00824 
00825         for(sample=0;sample<SAMPLES_PER_BLOCK;sample++) {
00826           //Index values
00827           OutFile << inter_sample_index[useDda][useChan][capArray][sample] << " ";
00828         }
00829         OutFile << "\n";
00830         OutFile << dda << "\t" << chan << "\t" << capArray << "\t"  << SAMPLES_PER_BLOCK << "\t";   
00831         for(int sample=0;sample<SAMPLES_PER_BLOCK;sample++) {
00832           //time values
00833             OutFile << inter_sample_times[useDda][useChan][capArray][inter_sample_index[useDda][useChan][capArray][sample]] << " ";
00834         }
00835         OutFile << "\n";
00836       }
00837     }
00838   }
00839   OutFile.close();
00840 
00841   return 0;
00842 }
00843 
00844 
00845 
00846 
00847 Int_t estimate_phase(TGraph *gr, Double_t period, Double_t *meanPhase, Int_t *totalZCs){
00848   Double_t *yVals = gr->GetY();
00849   Double_t *xVals = gr->GetX();
00850   Int_t numSamples = gr->GetN();
00851   if(numSamples != SAMPLES_PER_BLOCK/2){
00852     fprintf(stderr, "%s : Wrong number of samples %i expected %i\n", __FUNCTION__, numSamples, SAMPLES_PER_BLOCK/2);
00853     return -1;
00854   }
00855   Double_t phase=0;
00856   Int_t numZCs=0;
00857 
00858   for(int sample=0;sample<numSamples-1;sample++){
00859     Double_t y1=yVals[sample];
00860     Double_t y2=yVals[sample+1];
00861     if(y1<0 && y2>0){
00862       Double_t x1=xVals[sample]; 
00863       Double_t x2=xVals[sample+1]; 
00864       Double_t zc=((0-y1)/(y2-y1))*(x2-x1)+x1;
00865       //      if(zc<0.6) //printf("sample %i y1 %f y2 %f x1 %f x2 %f\n", sample, y1, y2, x1, x2);
00866       phase+=zc-numZCs*period;
00867       //printf("zc num %i val %f adjusted val %f\n", numZCs, zc, zc-numZCs*period);
00868       numZCs++;
00869       //return zc;
00870     }
00871   }//sample
00872 
00873   if(!numZCs)
00874     phase=0;
00875   else phase=phase/numZCs;
00876   
00877   *totalZCs = numZCs;
00878   *meanPhase = phase;
00879   return 0;
00880   
00881   
00882 }
00883 
00884 Int_t estimate_phase_two_blocks(TGraph *gr, Double_t period, Double_t *meanPhase, Int_t *totalZCs){
00885   Double_t *yVals = gr->GetY();
00886   Double_t *xVals = gr->GetX();
00887   Int_t numSamples = gr->GetN();
00888   Double_t phase=0;
00889   Int_t numZCs=0;
00890 
00891 
00892   Double_t zcArray[1000]={0};
00893   Int_t countZC=0;
00894   
00895   for(int sample=0;sample<numSamples-1;sample++){
00896     Double_t y1=yVals[sample];
00897     Double_t y2=yVals[sample+1];
00898     if(y1<0 && y2>0){
00899       Double_t x1=xVals[sample]; 
00900       Double_t x2=xVals[sample+1]; 
00901       Double_t zc=((0-y1)/(y2-y1))*(x2-x1)+x1;
00902       //      if(zc<0.6) //printf("sample %i y1 %f y2 %f x1 %f x2 %f\n", sample, y1, y2, x1, x2);
00903       zcArray[countZC]=zc;
00904       countZC++;
00905       phase+=zc-numZCs*period;
00906       //printf("zc num %i val %f adjusted val %f\n", numZCs, zc, zc-numZCs*period);
00907       numZCs++;
00908       //return zc;
00909     }
00910   }//sample
00911 
00912 
00913   Double_t meanZC=0;
00914   
00915   if(countZC>0) {
00916     Double_t firstZC=zcArray[0];
00917     while(firstZC>period) firstZC-=period;
00918     for(int i=0;i<countZC;i++) {
00919       while((zcArray[i]-firstZC)>period) zcArray[i]-=period;
00920       if(TMath::Abs((zcArray[i]-period)-firstZC)<TMath::Abs(zcArray[i]-firstZC))
00921         zcArray[i]-=period;
00922       meanZC+=zcArray[i];
00923       //     std::cout << i << "\t" << zc[i] << "\n";     
00924     }
00925     meanZC/=countZC;
00926   }
00927   *totalZCs = countZC;
00928   *meanPhase = meanZC;
00929   return 0;
00930 }
00931 
00932 
00933 Int_t findFirstZC(TGraph *grIn, Double_t period, Double_t *firstAvZC){
00934 
00935 
00936   Int_t numPoints=grIn->GetN();
00937   if(numPoints<3) return 0;
00938   Double_t *tVals=grIn->GetX();
00939   Double_t *vVals=grIn->GetY();
00940 
00941   Double_t zc[1000]={0};
00942   Double_t rawZc[1000]={0};
00943   int countZC=0;
00944   for(int i=2;i<numPoints;i++) {
00945     if(vVals[i-1]<0 && vVals[i]>0) {
00946       Double_t x1=tVals[i-1];
00947       Double_t x2=tVals[i];
00948       Double_t y1=vVals[i-1];
00949       Double_t y2=vVals[i];      
00950       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
00951       zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00952       rawZc[countZC]=zc[countZC];
00953       countZC++;
00954       //      if(countZC==1)
00955       //      break;
00956     }
00957   }
00958 
00959   Double_t firstZC=zc[0];
00960   while(firstZC>period) firstZC-=period;
00961   Double_t meanZC=0;
00962   for(int i=0;i<countZC;i++) {
00963      while((zc[i]-firstZC)>period) zc[i]-=period;
00964      if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC))
00965        zc[i]-=period;
00966      meanZC+=zc[i];
00967      //std::cout << i << "\t" << zc[i] << "\n";     
00968   }
00969   //  TCanvas *can = new TCanvas();
00970   //  TGraph *gr = new TGraph(countZC,rawZc,zc);
00971   //  gr->Draw("ap");
00972 
00973   //  std::cout << "\n";
00974   meanZC/=countZC;
00975   
00976   //  std::cout << zc << "\n";
00977   
00978   *firstAvZC=meanZC;
00979   return countZC;
00980 
00981 }
00982 
00983 Int_t findLastZC(TGraph *grIn, Double_t period, Double_t *lastAvZC){
00984 
00985  // This funciton estimates the lag by just using all the negative-positive zero crossing
00986  
00987   Int_t numPoints=grIn->GetN();
00988   if(numPoints<3) return 0;
00989   Double_t *tVals=grIn->GetX();
00990   Double_t *vVals=grIn->GetY();
00991 
00992   Double_t zc[1000]={0};
00993   Double_t rawZc[1000]={0};
00994   int countZC=0;
00995   for(int i=numPoints-1;i>1;i--) {
00996     if(vVals[i-1]<0 && vVals[i]>0) {
00997       Double_t x1=tVals[i-1];
00998       Double_t x2=tVals[i];
00999       Double_t y1=vVals[i-1];
01000       Double_t y2=vVals[i];      
01001       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
01002       zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1;
01003       rawZc[countZC]=zc[countZC];
01004       countZC++;
01005       //      if(countZC==1)
01006       //      break;
01007     }
01008   }
01009 
01010   Double_t lastZC=zc[0];
01011   while(lastZC<(tVals[numPoints-1]-period)) lastZC+=period;
01012   Double_t meanZC=0;
01013   for(int i=0;i<countZC;i++) {
01014      while((lastZC-zc[i])>period) zc[i]+=period;
01015      // if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC))
01016      //   zc[i]-=period;]
01017      if((zc[i]+period-lastZC)<(lastZC-zc[i]))
01018        zc[i]+=period;
01019     
01020      meanZC+=zc[i];
01021      //     std::cout << i << "\t" << zc[i] << "\n";     
01022   }
01023   //  TCanvas *can = new TCanvas();
01024   //  TGraph *gr = new TGraph(countZC,rawZc,zc);
01025   //  gr->Draw("ap");
01026 
01027   //  std::cout << "\n";
01028   meanZC/=countZC;
01029   *lastAvZC=meanZC;
01030 
01031   //  std::cout << lastZC << "\t" << meanZC << "\n";
01032   return countZC;
01033   
01034 }
01035 
01036 
01037 
01038 Int_t save_epsilon_times(char* name,int goodSampleTiming[DDA_PER_ATRI][RFCHAN_PER_DDA]){
01039 
01040   char outName[180];
01041   sprintf(outName, "%s_epsilon_timing.txt", name);
01042   std::ofstream OutFile(outName);
01043   Int_t capArray, sample;
01044 
01045   int lastGoodChan=-1;
01046   for(Int_t dda=0;dda<DDA_PER_ATRI;dda++){
01047     for(Int_t chan=0;chan<RFCHAN_PER_DDA;chan++){
01048       for(int capArray=0;capArray<2;capArray++){
01049         OutFile <<  dda << "\t"
01050                 << chan << "\t" 
01051                 << capArray << "\t";
01052         
01053         int useDda=dda;
01054         int useChan=chan;
01055         if(goodSampleTiming[dda][chan]) lastGoodChan=chan;
01056         else {
01057           useChan=lastGoodChan;
01058           //      std::cout << "Replacing " << dda << "," << chan << " with " << useDda << "," << useChan << "\n";
01059         }
01060         
01061         
01062         OutFile << epsilon_times[useDda][useChan][capArray] << "\n";
01063       }
01064     }
01065   }
01066   OutFile.close();
01067  
01068   return 0;
01069  
01070 }

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