ARA ROOT v3.10 Software

calibration/ICRR/TestBed/testClockCal.cxx

00001 #include <iostream>
00002 #include <fstream>
00003 
00004 //Event Reader Includes
00005 #include "UsefulAraTestBedStationEvent.h"
00006 #include "RawAraTestBedStationEvent.h"
00007 #include "AraGeomTool.h"
00008 #include "AraEventCalibrator.h"
00009 #include "araTestbedDefines.h"
00010 #include "FFTtools.h"
00011 
00012 //ROOT Includes
00013 #include "TROOT.h"
00014 #include "TCanvas.h"
00015 #include "TTree.h"
00016 #include "TFile.h"
00017 #include "TH1.h"
00018 #include "TGraphErrors.h"
00019 #include "TF1.h"
00020 #include "TTree.h"
00021 #include "TTreeIndex.h"
00022 #include "TButton.h"
00023 #include "TGroupButton.h"
00024 #include "TThread.h"
00025 #include "TEventList.h"
00026 #include "TMath.h"
00027 #include "TAxis.h"
00028 #include <TGClient.h>
00029 
00030 #include <zlib.h>
00031 
00032 Double_t estimateLag(TGraph *grIn, Double_t freq, Double_t &rms);
00033 Double_t estimatePeriod(TGraph *grIn, Double_t &rms);
00034 Double_t getSimpleDeltat(TGraph *gr1, TGraph *gr2, Double_t period) ;
00035 TGraph *justPositive(TGraph *gr1);
00036 TGraph *generateCombFunction(Double_t period, Double_t dt, Int_t numPoints);
00037 TGraph *combFilter(TGraph *grIn, TGraph *grComb, Double_t period);
00038 int debug=0;
00039 
00040 int main(int argc, char **argv)
00041 {
00042 
00043   if(argc<2) {
00044     std::cout << "Usage\n" << argv[0] << " <input file>\n";
00045     return 0;
00046   }
00047 
00048   TFile *fp = TFile::Open(argv[1]);
00049   if(!fp) {
00050     std::cerr << "Can't open file\n";
00051      return -1;
00052    }
00053    TTree *eventTree = (TTree*) fp->Get("eventTree");
00054    if(!eventTree) {
00055      std::cerr << "Can't find eventTree\n";
00056      return -1;
00057    }
00058    //  AraEventCalibrator::Instance()->setPedFile("/Users/rjn/ara/data/frozen_daqbox_calibration/Minus54C/pedestal_files/peds_1291239657/peds_1291239657/peds_1291239657.193855.dat");
00059    //   AraEventCalibrator::Instance()->setPedFile("/unix/anita1/ara/data/frozen_daqbox_calibration/Minus54C/pedestal_files/peds_1291239657/peds_1291239657/peds_1291239657.193855.dat");
00060    RawAraTestBedStationEvent *evPtr=0;
00061    eventTree->SetBranchAddress("event",&evPtr);
00062    
00063    
00064    char outName[180];
00065    sprintf(outName,"testClockPeriod.root");
00066    TFile *fpOut = new TFile(outName,"RECREATE");
00067    Int_t firstSamp[3];
00068    Int_t rco[3];
00069    Int_t myRco[3];
00070    Double_t period[2][3];
00071    Double_t rms[2][3];
00072    TTree *clockTree = new TTree("clockTree","Tree of clock calib fun");
00073    clockTree->Branch("period",period,"period[2][3]/D");
00074    clockTree->Branch("rms",rms,"rms[2][3]/D");
00075    clockTree->Branch("firstSamp",firstSamp,"firstSamp[3]/i");
00076    clockTree->Branch("rco",rco,"rco[3]/i");
00077    clockTree->Branch("myRco",myRco,"myRco[3]/i");
00078 
00079    AraGeomTool *tempGeom = AraGeomTool::Instance();
00080    AraEventCalibrator *fCalibrator = AraEventCalibrator::Instance();
00081 
00082    Long64_t numEntries=eventTree->GetEntries();
00083    numEntries=10000;
00084    Long64_t starEvery=numEntries/80;
00085    if(starEvery==0) starEvery++;
00086    //   numEntries=2;
00087 
00088    for(Long64_t event=0;event<numEntries;event++) {
00089      
00090      if(event%starEvery==0) {
00091        std::cerr << "*";       
00092      }
00093      eventTree->GetEntry(event);
00094      
00095      firstSamp[0]=evPtr->getEarliestSample(8);
00096      firstSamp[1]=evPtr->getEarliestSample(17);
00097      firstSamp[2]=evPtr->getEarliestSample(26);
00098 
00099      UsefulAraTestBedStationEvent realEvent(evPtr,AraCalType::kFirstCalib);
00100 
00101 
00102      TGraph *grClock[3];
00103      for(int chip=0;chip<3;chip++) {
00104          int chanIndex=8+9*chip;
00105          rco[chip]=realEvent.getRCO(chanIndex);
00106          //      std::cout << rco[chip] << "\t" << realEvent.getRawRCO(chanIndex) << "\n";
00107          
00108 
00109          for(int rcoGuess=0;rcoGuess<2;rcoGuess++) {
00110            int numValid=fCalibrator->doBinCalibration(&realEvent,chanIndex,rcoGuess);      
00111            grClock[chip]=new TGraph(numValid,fCalibrator->calTimeNums,fCalibrator->calVoltNums);
00112            //      std::cout << event << "\t" << chip << "\t" << rcoGuess << "\n";
00113            period[rcoGuess][chip]=estimatePeriod(grClock[chip],rms[rcoGuess][chip]);
00114            //      char canName[180];
00115            //      sprintf(canName,"can%d_%d_%d",event,chip,rcoGuess);
00116            //      new TCanvas(canName,canName);
00117            //      grClock[chip]->Draw("alp");
00118 
00119            delete grClock[chip];
00120          }     
00121          Double_t testVal=(TMath::Abs(period[0][chip]-25)-TMath::Abs(period[1][chip]-25))+0.5*(rms[0][chip]-rms[1][chip]);
00122                  
00123          if(testVal<=0) {
00124            myRco[chip]=0;
00125          }
00126          else {
00127            myRco[chip]=1;
00128          }
00129          if(myRco[chip]!=rco[chip]) {
00130            std::cout << event << "\t" << realEvent.head.eventNumber << "\t" << chip << "\n";
00131            std::cout << period[0][chip] << "\t" << period[1][chip] << "\n";
00132            std::cout << rms[0][chip] << "\t" << rms[1][chip] << "\n";
00133          }
00134      }
00135      clockTree->Fill();     
00136    }
00137    std::cerr << "\n";
00138 
00139    clockTree->AutoSave();
00140    fpOut->Close();
00141 
00142 }
00143 
00144 Double_t getSimpleDeltat(TGraph *gr1, TGraph *gr2, Double_t period) 
00145 {
00146   static int counter=0;
00147   TGraph *grCor = FFTtools::getCorrelationGraph(gr1,gr2);
00148   Int_t peakBin=FFTtools::getPeakBin(grCor);
00149   Double_t *dtVals=grCor->GetX();
00150   Double_t dt=dtVals[peakBin];
00151   while(dt>0.5*period) dt-=period;
00152   while(dt<-0.5*period) dt+=period;
00153 
00154   if(debug) {
00155     char graphName[180];
00156     sprintf(graphName,"grCor%d_%d",counter/3,counter%3);
00157     grCor->SetName(graphName);
00158     grCor->SetTitle(graphName);
00159     grCor->Write();
00160   }
00161   counter++;
00162   delete grCor;
00163   return dt;
00164 }
00165 
00166 
00167 
00168 TGraph *justPositive(TGraph *gr1)
00169 {
00170   Int_t numPoints=gr1->GetN();
00171   Double_t *newY= new Double_t[numPoints];
00172   Double_t *oldY=gr1->GetY();
00173   Double_t *oldX=gr1->GetX();
00174   for(int i=0;i<numPoints;i++) {
00175     newY[i]=oldY[i];
00176     if(newY[i]<0) newY[i]=0;
00177   }
00178 
00179   TGraph *gr = new TGraph(numPoints,oldX,newY);
00180   delete [] newY;
00181   return gr;
00182 }
00183 
00184 TGraph *generateCombFunction(Double_t period, Double_t dt, Int_t numPoints)
00185 {
00186   Double_t *xVals = new Double_t[numPoints];
00187   Double_t *yVals = new Double_t[numPoints];
00188   
00189   for(int i=0;i<numPoints;i++) {
00190     xVals[i]=dt*i;
00191     yVals[i]=0;
00192     Double_t temp=(xVals[i]+0.5*period);
00193     //Calculate remainder must be a more efficient way
00194     temp/=period;
00195     Int_t tempi=Int_t(temp);
00196     temp-=tempi;
00197     temp*=period;
00198     if(temp<0.5*dt || TMath::Abs(temp-period)<0.5*dt) {
00199       //      std::cout << i << "\t" << xVals[i] << "\t" << temp << "\n";
00200       yVals[i]=1;
00201     }
00202   }
00203   TGraph *grComb = new TGraph(numPoints,xVals,yVals);
00204   delete [] xVals;
00205   delete [] yVals;
00206   return grComb;
00207 
00208 }
00209 
00210 TGraph *combFilter(TGraph *grIn, TGraph *grComb, Double_t period)
00211 {
00212   TGraph *grCor = FFTtools::getCorrelationGraph(grIn,grComb);
00213   Double_t *xVals=grCor->GetX();
00214   Int_t zeroBin=(0-xVals[0])/(xVals[1]-xVals[0]);
00215   Int_t periodBins=period/(xVals[1]-xVals[0]);
00216   
00217   Int_t peakBin=FFTtools::getPeakBin(grCor,zeroBin-0.5*periodBins,zeroBin+0.5*periodBins);
00218   //  std::cout << peakBin << "\t" << peakBin-zeroBin << "\t" << xVals[peakBin] << "\n";
00219   Int_t offset=peakBin-zeroBin;
00220 
00221   Double_t *rawX = grIn->GetX();
00222   Double_t *rawY = grIn->GetY();
00223   Int_t numPoints=grIn->GetN();
00224   Int_t numPointsComb=grComb->GetN();
00225   Double_t *combY=grComb->GetY();
00226   Double_t *combX=grComb->GetX();
00227   Double_t *newY = new Double_t[numPoints];
00228   Int_t *mapY = new Int_t[numPoints];
00229   for(int i=0;i<numPoints;i++) {    
00230     mapY[i]=0;
00231     int combBin=i-offset;
00232     if(combBin>=0 && combBin<numPointsComb) {
00233       if(combY[combBin]>0) {
00234         //Got one
00235         for(int j=i;j>=i-10;j--) {
00236           if(j<0) break;
00237           mapY[j]=1;
00238         }
00239         int tempi=i;
00240         for(i=tempi;i<tempi+10;i++) {
00241           if(i>=numPoints) break;
00242           mapY[i]=1;
00243         }
00244         if(i<numPoints) mapY[i]=0;
00245       }
00246     }
00247   }
00248 
00249 
00250 
00251   static int firstOne=1;
00252 
00253   for(int i=0;i<numPoints;i++) {
00254     int combBin=i-offset;
00255     newY[i]=0;
00256     //    if(firstOne) {
00257     //      std::cout << i << "\t" << rawX[i] << "\t" << rawY[i] << "\t" << mapY[i] << "\t" << combY[combBin] << "\n";
00258     //    }
00259     if(mapY[i]) newY[i]=rawY[i];
00260   }
00261   
00262   TGraph *grRet = new TGraph(numPoints,rawX,newY);
00263   delete [] newY ;
00264   delete [] mapY ;
00265   delete grCor;
00266   firstOne=0;
00267   return grRet;
00268 }
00269 
00270 
00271 Double_t estimateLag(TGraph *grIn, Double_t freq, Double_t &rms)
00272 {
00273   // This funciton estimates the lag by just using all the negative-positive zero crossing
00274  
00275   Double_t mean=grIn->GetMean(2);
00276   Int_t numPoints=grIn->GetN();
00277   if(numPoints<3) return 0;
00278   Double_t period=1./freq;
00279   Double_t *tVals=grIn->GetX();
00280   Double_t *vVals=grIn->GetY();
00281 
00282   Double_t zc[1000]={0};
00283   Double_t rawZc[1000]={0};
00284   int countZC=0;
00285   for(int i=2;i<numPoints;i++) {
00286     if(vVals[i-1]<0 && vVals[i]>0) {
00287       Double_t x1=tVals[i-1];
00288       Double_t x2=tVals[i];
00289       Double_t y1=vVals[i-1]-mean;
00290       Double_t y2=vVals[i]-mean;      
00291       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
00292       Double_t zcTime=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00293       if(countZC>0) {
00294         if((zcTime-zc[countZC-1])<10)
00295           continue;
00296       }
00297       zc[countZC]=zcTime;
00298       rawZc[countZC]=zc[countZC];
00299       countZC++;
00300       //      if(countZC==1)
00301       //      break;
00302     }
00303        
00304   }
00305 
00306   Double_t firstZC=zc[0];
00307   while(firstZC>period) firstZC-=period;
00308   Double_t meanZC=0;
00309   Double_t meanZC2=0;
00310   for(int i=0;i<countZC;i++) {
00311      while((zc[i]-firstZC)>period) zc[i]-=period;
00312      if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC))
00313        zc[i]-=period;
00314      if(TMath::Abs((zc[i]+period)-firstZC)<TMath::Abs(zc[i]-firstZC))
00315        zc[i]+=period;
00316      meanZC+=zc[i];
00317      meanZC2+=zc[i]*zc[i];
00318      //     std::cout << i << "\t" << zc[i] << "\t" << rawZc[i] << "\n";     
00319   }
00320   //  TCanvas *can = new TCanvas();
00321   //  TGraph *gr = new TGraph(countZC,rawZc,zc);
00322   //  gr->Draw("ap");
00323 
00324   //  std::cout << "\n";
00325   meanZC/=countZC;
00326   meanZC2/=countZC;
00327   rms=TMath::Sqrt(meanZC2-meanZC*meanZC);
00328   //  std::cout << meanZC << "\t" << rms << "\n";
00329   return meanZC;
00330 
00331 }
00332 
00333 
00334 
00335 Double_t estimatePeriod(TGraph *grIn, Double_t &rms)
00336 {
00337   // This funciton estimates the period by just using all the negative-positive zero crossing
00338  
00339   Double_t mean=grIn->GetMean(2);
00340   Int_t numPoints=grIn->GetN();
00341   if(numPoints<3) return 0;
00342   Double_t *tVals=grIn->GetX();
00343   Double_t *vVals=grIn->GetY();
00344 
00345   Double_t zc[1000]={0};
00346   Double_t periods[1000]={0};
00347   int countZC=0;
00348   for(int i=2;i<numPoints;i++) {
00349     Double_t x1=tVals[i-1];
00350     Double_t x2=tVals[i];
00351     Double_t y1=vVals[i-1]-mean;
00352     Double_t y2=vVals[i]-mean;      
00353     if(vVals[i-1]<0 && vVals[i]>0) {
00354 
00355       Double_t zcTime=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00356       // std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\t" << zcTime << "\n";
00357       if(countZC>0) {
00358         if((zcTime-zc[countZC-1])<10)
00359           continue;
00360       }
00361       zc[countZC]=zcTime;
00362       //      zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00363       countZC++;
00364       //      if(countZC==1)
00365       //      break;
00366     }
00367        
00368   }
00369 
00370   if(countZC<2) return 0;
00371   
00372   
00373   int countPeriods=0;
00374   Double_t meanPeriod=0;
00375   Double_t meanPeriodSq=0;
00376   for(int i=1;i<countZC;i++) {
00377     periods[countPeriods]=zc[i]-zc[i-1];
00378     meanPeriod+=periods[countPeriods];
00379     meanPeriodSq+=periods[countPeriods]*periods[countPeriods];
00380     //std::cout << countPeriods << "\t" << periods[countPeriods] << "\n";
00381     countPeriods++;
00382 
00383 
00384   }
00385   meanPeriod/=countPeriods;
00386   meanPeriodSq/=countPeriods;
00387   rms=TMath::Sqrt(meanPeriodSq-meanPeriod*meanPeriod);
00388   
00389   return meanPeriod;
00390 
00391 }

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