ARA ROOT Test BEd Software

calibration/ICRR/TestBed/doEpsilonCal.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 
00011 //ROOT Includes
00012 #include "TROOT.h"
00013 #include "TCanvas.h"
00014 #include "TTree.h"
00015 #include "TFile.h"
00016 #include "TH1.h"
00017 #include "TGraphErrors.h"
00018 #include "TF1.h"
00019 #include "TTree.h"
00020 #include "TTreeIndex.h"
00021 #include "TButton.h"
00022 #include "TGroupButton.h"
00023 #include "TThread.h"
00024 #include "TEventList.h"
00025 #include "TMath.h"
00026 #include <TGClient.h>
00027 
00028 #include <zlib.h>
00029 
00030 Double_t estimateLag(TGraph *grIn, Double_t freq);
00031 Double_t estimateLagFirst(TGraph *grIn, Double_t freq);
00032 Double_t estimateLagLast(TGraph *grIn, Double_t freq);
00033 void loadCalib();
00034 void loadPedestals();   
00035 Double_t mySineWave(Double_t *t, Double_t *par) ;
00036 int gotPedFile=0;
00037 char pedFile[FILENAME_MAX];
00038 float pedestalData[LAB3_PER_TESTBED][CHANNELS_PER_LAB3][MAX_NUMBER_SAMPLES_LAB3];
00039 Double_t binWidths[LAB3_PER_TESTBED][2][MAX_NUMBER_SAMPLES_LAB3];
00040 
00041 int main(int argc, char **argv)
00042 {
00043    char inputFile[FILENAME_MAX];
00044    Double_t ampVal=200;
00045    Double_t freqVal=0.2; //200 Mhz
00046 
00047    if(argc>1) {
00048       strncpy(inputFile,argv[1],FILENAME_MAX);
00049    }
00050    else {
00051       strncpy(inputFile,"/unix/anita1/ara/data/frozen_daqbox_calibration/Minus54C/sine_wave_data/root/event250MHz_331mV.root",FILENAME_MAX);
00052    }
00053    if(argc>2) {
00054       freqVal=atof(argv[2]);
00055    }
00056    if(argc>3) {
00057       ampVal=atof(argv[3]);
00058    }
00059 
00060 
00061    //TFile *fp = new TFile("/Users/rjn/ara/data/root/event_frozen_200MHz.root");
00062    TFile *fp = new TFile(inputFile);
00063   if(!fp) {
00064     std::cerr << "Can't open file\n";
00065     return -1;
00066   }
00067   TTree *eventTree = (TTree*) fp->Get("eventTree");
00068   if(!eventTree) {
00069     std::cerr << "Can't find eventTree\n";
00070     return -1;
00071   }
00072   RawAraTestBedStationEvent *evPtr=0;
00073   eventTree->SetBranchAddress("event",&evPtr);
00074   //strcpy(pedFile,"/Users/rjn/ara/data/frozen_daqbox_calibration/Minus54C/pedestal_files/peds_1291239657/peds_1291239657/peds_1291239657.193855.dat");
00075  strcpy(pedFile,"/unix/anita1/ara/data/frozen_daqbox_calibration/Minus54C/pedestal_files/peds_1291303459/peds_1291303459.323022.dat");
00076 
00077  gotPedFile=1;
00078   loadPedestals();
00079   loadCalib();
00080 
00081   //  Double_t ampVal=200;
00082   //  Double_t freqVal=0.2; //200 Mhz
00083   Double_t period=1./freqVal;
00084   char outName[180];
00085   sprintf(outName,"epsilonFile_%3.0fMHz_%3.0fmV.root",freqVal*1000,ampVal);
00086 
00087   TFile *fpEpsilon = new TFile(outName,"RECREATE");
00088   TH1F *histEpsilon[3][2];
00089   TH1F *histEpsilonChan[3][8][2];
00090   char histName[180];
00091   TTree *epsTree = new TTree("epsTree","Tree of Epsilons");
00092   Int_t chip,chan,rco,firstSamp,lastSamp,otherChip;
00093   Double_t epsilon,lag1,lag2;
00094   epsTree->Branch("rco",&rco,"rco/I");
00095   epsTree->Branch("chan",&chan,"chan/I");
00096   epsTree->Branch("chip",&chip,"chip/I");
00097   epsTree->Branch("epsilon",&epsilon,"epsilon/D");
00098   epsTree->Branch("lag1",&lag1,"lag1/D");
00099   epsTree->Branch("lag2",&lag2,"lag2/D");
00100   epsTree->Branch("firstSamp",&firstSamp,"firstSamp/I");
00101   epsTree->Branch("lastSamp",&lastSamp,"lastSamp/I");
00102   epsTree->Branch("otherChip",&otherChip,"otherChip/I");
00103 
00104   for(int chip=0;chip<3;chip++) {
00105     for(int rco=0;rco<2;rco++) {
00106       sprintf(histName,"histEpsilon_%d_%d",chip,rco);
00107       histEpsilon[chip][rco] = new TH1F(histName,histName,6000,-3,3);
00108       for(int chan=0;chan<8;chan++) {
00109         sprintf(histName,"histEpsilonChan_%d_%d_%d",chip,chan,rco);
00110         histEpsilonChan[chip][chan][rco] = new TH1F(histName,histName,6000,-3,3);
00111       }
00112     }
00113     
00114   }
00115 
00116   Long64_t numEntries=eventTree->GetEntries();
00117   Long64_t starEvery=numEntries/80;
00118   if(starEvery==0) starEvery++;
00119   //  numEntries=10000;
00120   for(Long64_t i=0;i<numEntries;i++) {
00121     if(i%starEvery==0) 
00122       std::cerr << "*";
00123     eventTree->GetEntry(i);
00124     
00125     for(int chanIndex=0;chanIndex<NUM_DIGITIZED_TESTBED_CHANNELS;chanIndex++) {
00126       chip=chanIndex/9;
00127       chan=chanIndex%9;
00128       if(chan==8) continue;
00129       if(chip==2 && chan==7) continue;
00130       rco=evPtr->getRCO(chanIndex);
00131       otherChip=evPtr->getLabChip(chanIndex);
00132       firstSamp=evPtr->getEarliestSample(chanIndex);
00133       lastSamp=evPtr->getLatestSample(chanIndex);
00134 
00135       //To get a cleaner sample of RCO zero and one  uncomment the following line
00136       //      if(firstSamp<20) continue;
00137 
00138       //Check to see which rco phase we and if we need to switch
00139       //      if(lastSamp<8) rco=1-rco;
00140 
00141       Float_t data[260];
00142       Float_t maxVal=-1e9;
00143       Float_t minVal=+1e9;
00144       for(int samp=0;samp<260;samp++) {
00145         data[samp]=evPtr->chan[chanIndex].data[samp]-pedestalData[chip][chan][samp];
00146         if(data[samp]>maxVal) maxVal=data[samp];
00147         if(data[samp]<minVal) minVal=data[samp];
00148       }
00149       //Zero mean the waveform
00150       Float_t mean=TMath::Mean(260,data);
00151       for(int samp=0;samp<260;samp++)
00152         data[samp]-=mean;
00153       //      std::cout << mean << "\t" << minVal << "\t" << maxVal << "\n";
00154 
00155       minVal-=mean;
00156       maxVal-=mean;
00157       Double_t amp=TMath::Abs(minVal);
00158       //Set the amplitude
00159       if(TMath::Abs(maxVal)>amp) amp=TMath::Abs(maxVal);
00160       
00161 
00162       if(firstSamp<lastSamp) {
00163         //One RCO phase
00164         continue;
00165       }
00166       else {
00167         //Two RCO phases
00168         //Do the first phase which runs from firstSamp to 259
00169          Int_t numFirst=260-firstSamp;
00170          Double_t tVals[2][260]={{0}};
00171          Double_t vVals[2][260]={{0}};
00172          Double_t vValErrs[2][260]={{10}};
00173          Double_t curTVal=0;
00174          for(int samp=0;samp<numFirst;samp++) {
00175            int cap=samp+firstSamp;
00176            tVals[0][samp]=curTVal;
00177            vVals[0][samp]=data[cap];
00178            if(samp<(numFirst-1))
00179              curTVal+=binWidths[chip][1-rco][cap];
00180          }
00181 
00182          Int_t numSecond=lastSamp+1;
00183          for(int samp=0;samp<numSecond;samp++) {
00184            int cap=samp;
00185            tVals[1][samp]=curTVal;
00186            vVals[1][samp]=data[cap];
00187            curTVal+=binWidths[chip][rco][cap];
00188          }
00189          TGraphErrors *grFirst = new TGraphErrors(numFirst,tVals[0],vVals[0],0,vValErrs[0]);
00190          TGraphErrors *grSecond = new TGraphErrors(numSecond,tVals[1],vVals[1],0,vValErrs[1]);
00191          //
00192          lag1=estimateLag(grFirst,freqVal);
00193          lag2=estimateLag(grSecond,freqVal);
00194 
00195          
00196          Double_t deltaLag=lag1-lag2;
00197          if(TMath::Abs(deltaLag+period)<TMath::Abs(deltaLag))
00198            deltaLag+=period;
00199          if(TMath::Abs(deltaLag-period)<TMath::Abs(deltaLag))
00200            deltaLag-=period;
00201          epsilon=deltaLag;
00202          if(lag1!=0 & lag2!=0) {
00203             //     epsTree->Fill();
00204            if(firstSamp>20 && firstSamp<250) {
00205               if(i>10) {
00206                  Double_t mean=histEpsilon[chip][rco]->GetMean();
00207                  if(TMath::Abs((deltaLag+period)-mean)<TMath::Abs(deltaLag-mean))
00208                     deltaLag+=period;
00209                  if(TMath::Abs((deltaLag-period)-mean)<TMath::Abs(deltaLag-mean))
00210                     deltaLag-=period;
00211               }
00212               histEpsilon[chip][rco]->Fill(deltaLag);
00213               histEpsilonChan[chip][chan][rco]->Fill(deltaLag);
00214            }
00215          }
00216 //       TCanvas *can = new TCanvas();
00217 //       TH1F *framey = can->DrawFrame(0,-1.2*amp,260,1.2*amp);
00218 //       grFirst->SetMarkerColor(kRed+3);
00219 //       grFirst->Draw("p");
00220 //       grSecond->SetMarkerColor(kBlue+3);
00221 //       grSecond->Draw("p");
00222 
00223          delete grFirst;
00224          delete grSecond;
00225          
00226       }
00227     }   
00228   }
00229   epsTree->AutoSave();
00230   fpEpsilon->Write();
00231   
00232 
00233   sprintf(outName,"epsilonFile_%3.0fMHz_%3.0fmV.txt",freqVal*1000,ampVal);
00234   std::ofstream EpsFile(outName);
00235   for(int chip=0;chip<3;chip++) {
00236     for(int rco=0;rco<2;rco++) {
00237       EpsFile << chip << "\t" << rco << "\t" << histEpsilon[chip][rco]->GetMean() << "\n";
00238     }
00239   }
00240   EpsFile.close();
00241 
00242 }
00243 
00244 
00245 
00246 void loadPedestals()
00247 {
00248   if(!gotPedFile) {
00249     char calibDir[FILENAME_MAX];
00250     char *calibEnv=getenv("ARA_CALIB_DIR");
00251     if(!calibEnv) {
00252       char *utilEnv=getenv("ARA_UTIL_INSTALL_DIR");
00253       if(!utilEnv)
00254         sprintf(calibDir,"calib");
00255       else
00256         sprintf(calibDir,"%s/share/araCalib",utilEnv);
00257     }
00258     else {
00259       strncpy(calibDir,calibEnv,FILENAME_MAX);
00260     }  
00261     sprintf(pedFile,"%s/peds_1286989711.394723.dat",calibDir);
00262   }
00263   FullLabChipPedStruct_t peds;
00264   gzFile inPed = gzopen(pedFile,"r");
00265   if( !inPed ){
00266     fprintf(stderr,"Failed to open pedestal file %s.\n",pedFile);
00267     return;
00268   }
00269 
00270   int nRead = gzread(inPed,&peds,sizeof(FullLabChipPedStruct_t));
00271   if( nRead != sizeof(FullLabChipPedStruct_t)){
00272     int numErr;
00273     fprintf(stderr,"Error reading pedestal file %s; %s\n",pedFile,gzerror(inPed,&numErr));
00274     gzclose(inPed);
00275     return;
00276   }
00277 
00278   int chip,chan,samp;
00279   for(chip=0;chip<LAB3_PER_TESTBED;++chip) {
00280     for(chan=0;chan<CHANNELS_PER_LAB3;++chan) {
00281       int chanIndex = chip*CHANNELS_PER_LAB3+chan;
00282       for(samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;++samp) {
00283         pedestalData[chip][chan][samp]=peds.chan[chanIndex].pedMean[samp];
00284       }
00285     }
00286   }
00287   gzclose(inPed);
00288 }
00289 
00290 
00291 
00292 
00293 Double_t mySineWave(Double_t *t, Double_t *par) 
00294 {
00295   Double_t amp=par[0]; //In mV
00296   Double_t freq=par[1]; //In GHz
00297   Double_t lag=par[2]; //In ns
00298 
00299   return amp*TMath::Sin(TMath::TwoPi()*freq*(t[0]-lag));
00300 
00301 }
00302 
00303 void loadCalib()
00304 {
00305   std::ifstream BinFile("binWidths.txt");
00306   int chip,rco;
00307   double width;
00308   while(BinFile >> chip >> rco) {
00309     for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;samp++) {
00310       BinFile >> width;
00311       binWidths[chip][rco][samp]=width;
00312     }
00313   }
00314 }
00315 
00316 Double_t estimateLagFirst(TGraph *grIn, Double_t freq)
00317 {
00318 
00319   // This funciton estimates the lag by just using the first negative-positive zero crossing
00320   // To resolve quadrant ambiguity in the ASin function, the first zero crossing is used as a test of lag
00321   Int_t numPoints=grIn->GetN();
00322   if(numPoints<3) return 0;
00323   Double_t period=1./freq;
00324   Double_t *tVals=grIn->GetX();
00325   Double_t *vVals=grIn->GetY();
00326 
00327   Double_t zc=0;
00328   for(int i=2;i<numPoints;i++) {
00329     if(vVals[i-1]<0 && vVals[i]>0) {
00330       Double_t x1=tVals[i-1];
00331       Double_t x2=tVals[i];
00332       Double_t y1=vVals[i-1];
00333       Double_t y2=vVals[i];      
00334       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
00335       zc=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00336       break;
00337     }
00338   }
00339   while(zc>period) zc-=period;
00340   //  std::cout << zc << "\n";
00341   return zc;
00342 
00343 }
00344 
00345 Double_t estimateLag(TGraph *grIn, Double_t freq)
00346 {
00347   // This funciton estimates the lag by just using all the negative-positive zero crossing
00348  
00349   Int_t numPoints=grIn->GetN();
00350   if(numPoints<3) return 0;
00351   Double_t period=1./freq;
00352   Double_t *tVals=grIn->GetX();
00353   Double_t *vVals=grIn->GetY();
00354 
00355   Double_t zc[1000]={0};
00356   Double_t rawZc[1000]={0};
00357   int countZC=0;
00358   for(int i=2;i<numPoints;i++) {
00359     if(vVals[i-1]<0 && vVals[i]>0) {
00360       Double_t x1=tVals[i-1];
00361       Double_t x2=tVals[i];
00362       Double_t y1=vVals[i-1];
00363       Double_t y2=vVals[i];      
00364       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
00365       zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00366       rawZc[countZC]=zc[countZC];
00367       countZC++;
00368       //      if(countZC==1)
00369       //      break;
00370     }
00371   }
00372 
00373   Double_t firstZC=zc[0];
00374   while(firstZC>period) firstZC-=period;
00375   Double_t meanZC=0;
00376   for(int i=0;i<countZC;i++) {
00377      while((zc[i]-firstZC)>period) zc[i]-=period;
00378      if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC))
00379        zc[i]-=period;
00380      meanZC+=zc[i];
00381      //     std::cout << i << "\t" << zc[i] << "\n";     
00382   }
00383   //  TCanvas *can = new TCanvas();
00384   //  TGraph *gr = new TGraph(countZC,rawZc,zc);
00385   //  gr->Draw("ap");
00386 
00387   //  std::cout << "\n";
00388   meanZC/=countZC;
00389   
00390   //  std::cout << zc << "\n";
00391   return meanZC;
00392 
00393 }
00394 
00395 
00396 
00397 
00398 
00399 
00400 
00401 
00402 
00403 
00404 
00405 
00406 
00407 
00408 Double_t estimateLagLast(TGraph *grIn, Double_t freq)
00409 {
00410 
00411   // This funciton estimates the lag by just using the first negative-positive zero crossing
00412   // To resolve quadrant ambiguity in the ASin function, the first zero crossing is used as a test of lag
00413   Int_t numPoints=grIn->GetN();
00414   if(numPoints<3) return 0;
00415   Double_t period=1./freq;
00416   Double_t *tVals=grIn->GetX();
00417   Double_t *vVals=grIn->GetY();
00418 
00419   Double_t zc=0;
00420   for(int i=2;i<numPoints;i++) {
00421     if(vVals[i-1]<0 && vVals[i]>0) {
00422       Double_t x1=tVals[i-1];
00423       Double_t x2=tVals[i];
00424       Double_t y1=vVals[i-1];
00425       Double_t y2=vVals[i];      
00426       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
00427       zc=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00428       //      break;
00429     }
00430   }
00431   while(zc>period) zc-=period;
00432   //  std::cout << zc << "\n";
00433   return zc;
00434 
00435 }

Generated on Wed Aug 8 16:20:07 2012 for ARA ROOT Test Bed Software by doxygen 1.4.7