ARA ROOT v3.10 Software

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

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