ARA ROOT v3.3 Software

AraEvent/AraEventCalibrator.cxx

00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include "AraEventCalibrator.h"
00010 #include "UsefulIcrrStationEvent.h"
00011 #include "UsefulAtriStationEvent.h"
00012 #include "AraGeomTool.h"
00013 #include "TMath.h"
00014 #include "TGraph.h"
00015 #include "FFTtools.h"
00016 #include <iostream>
00017 #include <fstream>
00018 #include <cstring>
00019 #include <zlib.h>
00020 #include <cstdlib>
00021 
00022 
00023 Bool_t AraCalType::hasCableDelays(AraCalType::AraCalType_t calType)
00024 { 
00025   if(calType==kFirstCalibPlusCables || calType==kSecondCalibPlusCables || calType==kSecondCalibPlusCablesUnDiplexed)
00026     return kTRUE;
00027   return kFALSE;
00028 }
00029 
00030 Bool_t AraCalType::hasInterleaveCalib(AraCalType::AraCalType_t calType)
00031 { 
00032   if(calType==kFirstCalibPlusCables || calType==kSecondCalibPlusCables ||
00033      calType==kFirstCalib || calType==kSecondCalib || calType==kSecondCalibPlusCablesUnDiplexed)
00034     return kTRUE;
00035   return kFALSE;
00036 }
00037 
00038 Bool_t AraCalType::hasBinWidthCalib(AraCalType::AraCalType_t calType)
00039 { 
00040   if(calType>=kFirstCalib)
00041     return kTRUE;
00042   return kFALSE;
00043 }
00044 
00045 
00046 Bool_t AraCalType::hasClockAlignment(AraCalType::AraCalType_t calType)
00047 { 
00048   if(calType==kSecondCalibPlusCables ||
00049      calType==kSecondCalib || calType==kSecondCalibPlusCablesUnDiplexed)
00050     return kTRUE;
00051   return kFALSE;
00052 }
00053 
00054 
00055 
00056 Bool_t AraCalType::hasPedestalSubtraction(AraCalType::AraCalType_t calType)
00057 { 
00058   if(calType==kNoCalib) return kFALSE;
00059   return kTRUE;
00060 }
00061 
00062 Bool_t AraCalType::hasCommonMode(AraCalType::AraCalType_t calType)
00063 {
00064   return kFALSE;
00065   if(calType==kNoCalib) return kFALSE;
00066   return kTRUE;
00067 }
00068 
00069 Bool_t AraCalType::hasUnDiplexing(AraCalType::AraCalType_t calType)
00070 {
00071   if(calType==kSecondCalibPlusCablesUnDiplexed) return kTRUE;
00072   return kFALSE;
00073 }
00074 
00075 
00076 
00077 ClassImp(AraEventCalibrator);
00078 
00079 AraEventCalibrator * AraEventCalibrator::fgInstance=0;
00080 
00081 
00082 AraEventCalibrator::AraEventCalibrator() 
00083 {
00084 
00085   //Loop through and set the got ped / calib flags to zero. This assumes stationId's first n elements
00086   //are for ICRR stations and the remaining N-n are ATRI
00087   for(AraStationId_t station=0;station<ICRR_NO_STATIONS+ATRI_NO_STATIONS;station++){
00088     //    fprintf(stderr, "AraEventCalibrator::AraEventCalibrator()  -- station %i\n", station);
00089     if(AraGeomTool::isIcrrStation(station)){
00090       gotIcrrPedFile[AraGeomTool::getStationCalibIndex(station)]=0;
00091       gotIcrrCalibFile[AraGeomTool::getStationCalibIndex(station)]=0;
00092     }
00093     if(AraGeomTool::isAtriStation(station)){
00094       fGotAtriPedFile[AraGeomTool::getStationCalibIndex(station)]=0;
00095       fGotAtriCalibFile[AraGeomTool::getStationCalibIndex(station)]=0;
00096     }
00097 
00098   }
00099 
00100 }
00101 
00102 AraEventCalibrator::~AraEventCalibrator() {
00103   //Default Destructor
00104 }
00105 
00106 //______________________________________________________________________________
00107 AraEventCalibrator*  AraEventCalibrator::Instance()
00108 {
00109   
00110   //  printf("AraEventCalibrator::Instance() creating an instance of the calibrator\n");
00111   //static function
00112   if(fgInstance)
00113     return fgInstance;
00114 
00115   fgInstance = new AraEventCalibrator();
00116 
00117   return fgInstance;
00118 }
00119 
00120 
00121 void AraEventCalibrator::setPedFile(char fileName[], AraStationId_t stationId)
00122 {
00123 
00124   strncpy(IcrrPedFile[stationId],fileName,FILENAME_MAX);
00125   if(stationId==ARA_TESTBED){
00126     fprintf(stdout, "AraEventCalibrator::setPedFile() setting TestBed IcrrPedFile to %s\n",  IcrrPedFile[stationId]);
00127     gotIcrrPedFile[0]=1;
00128   }
00129 
00130   if(stationId==ARA_STATION1){
00131     fprintf(stdout, "AraEventCalibrator::setPedFile() setting Station1 IcrrPedFile to %s\n", IcrrPedFile[stationId]);
00132     gotIcrrPedFile[1]=1;
00133   }
00134 
00135   
00136   loadIcrrPedestals(stationId);
00137 }
00138 
00139 void AraEventCalibrator::loadIcrrPedestals(AraStationId_t stationId)
00140 {
00141 
00142   if(stationId==ARA_TESTBED&&gotIcrrPedFile[0]==0){
00143     //populate the IcrrPedFile[0] variable
00144     char *pedFileEnv = getenv( "ARA_PEDESTAL_FILE" );
00145     if ( pedFileEnv == NULL ) {
00146       char calibDir[FILENAME_MAX];
00147       char *calibEnv=getenv("ARA_CALIB_DIR");
00148       if(!calibEnv) {
00149         char *utilEnv=getenv("ARA_UTIL_INSTALL_DIR");
00150         if(!utilEnv) {
00151           sprintf(calibDir,"calib");
00152         } else {
00153           sprintf(calibDir,"%s/share/araCalib",utilEnv);
00154         }
00155       }
00156       else {
00157         strncpy(calibDir,calibEnv,FILENAME_MAX);
00158       }
00159       sprintf(IcrrPedFile[0],"%s/ICRR/TestBed/peds_1294924296.869787.run001202.dat",calibDir);
00160     } // end of IF-block for pedestal file not specified by environment variable
00161     else {
00162       strncpy(IcrrPedFile[0],pedFileEnv,FILENAME_MAX);
00163     } // end of IF-block for pedestal file specified by environment variable
00164 
00165 
00166   }
00167   if(stationId==ARA_STATION1&&gotIcrrPedFile[1]==0){
00168     //populate the IcrrPedFile[1] variable
00169     char *pedFileEnv = getenv( "ARA_PEDESTAL_FILE" );
00170     if ( pedFileEnv == NULL ) {
00171       char calibDir[FILENAME_MAX];
00172       char *calibEnv=getenv("ARA_CALIB_DIR");
00173       if(!calibEnv) {
00174         char *utilEnv=getenv("ARA_UTIL_INSTALL_DIR");
00175         if(!utilEnv) {
00176           sprintf(calibDir,"calib");
00177         } else {
00178           sprintf(calibDir,"%s/share/araCalib",utilEnv);
00179         }
00180       }
00181       else {
00182         strncpy(calibDir,calibEnv,FILENAME_MAX);
00183       }
00184       sprintf(IcrrPedFile[1],"%s/ICRR/Station1/peds_1326108401.602169.run003747.dat",calibDir);
00185     } // end of IF-block for pedestal file not specified by environment variable
00186     else {
00187       strncpy(IcrrPedFile[1],pedFileEnv,FILENAME_MAX);
00188     } // end of IF-block for pedestal file specified by environment variable
00189 
00190   }
00191   //Finished populating the IcrrPedFile variables
00192 
00193   FullLabChipPedStruct_t peds;
00194 
00195   //Now we load in the pedestals
00196   if(stationId==ARA_TESTBED){
00197     gzFile inPedTB = gzopen(IcrrPedFile[0],"r");
00198     if( !inPedTB ){
00199       fprintf(stderr,"ERROR - Failed to open pedestal file for TestBed %s.\n",IcrrPedFile[0]);
00200       return;
00201     }
00202     int nRead = gzread(inPedTB,&peds,sizeof(FullLabChipPedStruct_t));
00203     if( nRead != sizeof(FullLabChipPedStruct_t)){
00204       int numErr;
00205       fprintf(stderr,"ERROR - Error reading pedestal file %s; %s\n",IcrrPedFile[0],gzerror(inPedTB,&numErr));
00206       gzclose(inPedTB);
00207       return;
00208     }
00209     
00210     int chip,chan,samp;
00211     for(chip=0;chip<LAB3_PER_ICRR;++chip) {
00212       for(chan=0;chan<CHANNELS_PER_LAB3;++chan) {
00213         int chanIndex = chip*CHANNELS_PER_LAB3+chan;
00214         for(samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;++samp) {
00215           pedestalData[0][chip][chan][samp]=peds.chan[chanIndex].pedMean[samp];
00216         }
00217       }
00218     }
00219     gzclose(inPedTB);
00220     
00221     gotIcrrPedFile[0]=1;
00222     gotIcrrPedFile[1]=0;
00223     //last thing is to set the gotIcrrPedFile flags appropriately
00224   }
00225   if(stationId==ARA_STATION1){
00226     gzFile inPedAra1 = gzopen(IcrrPedFile[1],"r");
00227     if( !inPedAra1 ){
00228       fprintf(stderr,"ERROR - Failed to open pedestal file for Station1 %s\n",IcrrPedFile[1]);
00229       return;
00230     }
00231     
00232     int nRead = gzread(inPedAra1,&peds,sizeof(FullLabChipPedStruct_t));
00233     if( nRead != sizeof(FullLabChipPedStruct_t)){
00234       int numErr;
00235       fprintf(stderr,"ERROR - Error reading pedestal file %s; %s\n",IcrrPedFile[1],gzerror(inPedAra1,&numErr));
00236       gzclose(inPedAra1);
00237       return;
00238     }
00239     
00240     int chip, chan, samp;
00241     for(chip=0;chip<LAB3_PER_ICRR;++chip) {
00242       for(chan=0;chan<CHANNELS_PER_LAB3;++chan) {
00243         int chanIndex = chip*CHANNELS_PER_LAB3+chan;
00244         for(samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;++samp) {
00245           pedestalData[1][chip][chan][samp]=peds.chan[chanIndex].pedMean[samp];
00246         }
00247       }
00248     }
00249     gzclose(inPedAra1);
00250     gotIcrrPedFile[0]=0;
00251     gotIcrrPedFile[1]=1;
00252     //last thing is to set the gotIcrrPedFile flags appropriately
00253   }
00254   //Finished loading pedestals
00255 
00256 
00257 
00258 }
00259 
00260 int AraEventCalibrator::doBinCalibration(UsefulIcrrStationEvent *theEvent, int chanIndex,int overrideRCO)
00261 {
00262   AraStationId_t stationId=theEvent->stationId;
00263 
00264   int nChip=theEvent->chan[chanIndex].chanId/CHANNELS_PER_LAB3;
00265   int nChan=theEvent->chan[chanIndex].chanId%CHANNELS_PER_LAB3;
00266   int rco=overrideRCO;
00267   if(overrideRCO!=0 && overrideRCO!=1) 
00268     rco=theEvent->getRCO(chanIndex);
00269   int hbwrap = theEvent->chan[chanIndex].chipIdFlag&0x08;
00270   char hbextra = (theEvent->chan[chanIndex].chipIdFlag&0xf0)>>4;
00271   short hbstart=theEvent->chan[chanIndex].firstHitbus;
00272   short hbend=theEvent->chan[chanIndex].lastHitbus+hbextra;
00273  
00274   double calTime=0;
00275   for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;++samp){
00276     rawadc[samp]=theEvent->chan[chanIndex].data[samp];
00277     if(theEvent->chan[chanIndex].data[samp]==0){
00278       calwv[samp]=0;
00279       pedsubadc[samp]=0;
00280     }else{
00281       pedsubadc[samp]=rawadc[samp]-pedestalData[stationId][nChip][nChan][samp];
00282       calwv[samp]=pedsubadc[samp]*ADCMV;
00283       if(calwv[samp]>SATURATION) calwv[samp]=SATURATION;
00284       if(calwv[samp]<-1*SATURATION) calwv[samp]=-1*SATURATION;
00285     }
00286   }
00287     
00288 
00289   if(nChan==0 || (overrideRCO==0 || overrideRCO==1)) {
00290     //Do the calibration for each chip
00291     calTime=0;
00292     int ir=0;
00293     if(hbwrap){ // Wrapped hitbus
00294       for(int samp=hbstart+1;samp<hbend;++samp) {
00295         tempTimeNums[ir++]=calTime;
00296         calTime+=binWidths[stationId][nChip][rco][samp];
00297       }
00298     }
00299     else{
00300       for(int samp=hbend+1;samp<MAX_NUMBER_SAMPLES_LAB3;++samp) {
00301         tempTimeNums[ir++]=calTime;
00302         calTime+=binWidths[stationId][nChip][1-rco][samp];
00303       }
00304       //Now add epsilon
00305       calTime+=epsilonVals[stationId][nChip][rco];  //Need to check if this is rco or 1-rco
00306       for(int samp=0;samp<hbstart;++samp) {
00307         tempTimeNums[ir++]=calTime;
00308         calTime+=binWidths[stationId][nChip][rco][samp];
00309       }
00310     }
00311     for(int samp=ir;samp<MAX_NUMBER_SAMPLES_LAB3;++samp) {
00312       tempTimeNums[samp]=calTime;
00313       calTime+=1;
00314     }    
00315     TMath::Sort(MAX_NUMBER_SAMPLES_LAB3,tempTimeNums,indexNums,kFALSE);
00316   }
00317   // ... and rotate
00318   int ir=0;
00319   int numValid=0;
00320   if(hbwrap){ // Wrapped hitbus
00321     for(int samp=hbstart+1;samp<hbend;++samp)
00322       v[ir++]=calwv[samp];
00323   }else{
00324     for(int samp=hbend+1;samp<MAX_NUMBER_SAMPLES_LAB3;++samp) 
00325       v[ir++]=calwv[samp];
00326     for(int samp=0;samp<hbstart;++samp)
00327       v[ir++]=calwv[samp];
00328   }
00329   numValid=ir;
00330   // Fill in remaining bins with zeros
00331   for(int samp=ir;samp<MAX_NUMBER_SAMPLES_LAB3;++samp)
00332     v[samp]=0;
00333   
00334   //Now we just have to make sure the times are monotonically increasing
00335   for(int i=0;i<MAX_NUMBER_SAMPLES_LAB3;i++) {
00336     calTimeNums[i]=tempTimeNums[indexNums[i]];
00337     calVoltNums[i]=v[indexNums[i]];
00338     //      std::cout << i << "\t" << indexNums[i] << "\t" << calTimeNums[i] << "\t" << calVoltNums[i] << "\n";
00339   }
00340   return numValid;
00341 }
00342 
00343 
00344 void AraEventCalibrator::calibrateEvent(UsefulIcrrStationEvent *theEvent, AraCalType::AraCalType_t calType) 
00345 {
00346 
00347   AraStationId_t stationId=theEvent->stationId;
00348   if(gotIcrrPedFile[stationId]==0)
00349     {
00350       loadIcrrPedestals(stationId); //This will only load the pedestals once
00351     }
00352   
00353   if(gotIcrrCalibFile[stationId]==0){
00354     loadIcrrCalib(stationId); //This will only load the calib values once
00355   }
00356 
00357   if(AraCalType::hasBinWidthCalib(calType))
00358     theEvent->guessRCO(0); //Forces the calculation of the RCO phase from the clock
00359   //FIXME -- should this be a zero or do we want the actual channel number?
00360 
00361 
00362   //set the number of rfchans in the event
00363 
00364   if(stationId==ARA_TESTBED) theEvent->numRFChans=RFCHANS_TESTBED;
00365   else theEvent->numRFChans=RFCHANS_STATION1;
00366 
00367   // Calibrate waveforms
00368   for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;++samp){
00369     sampNums[samp]=samp;
00370     timeNums[samp]=samp*NSPERSAMP;
00371   }
00372   
00373   for(int  chanIndex = 0; chanIndex < NUM_DIGITIZED_ICRR_CHANNELS; ++chanIndex ){
00374     int numValid=doBinCalibration(theEvent,chanIndex);
00375 
00376     //Now we stuff it back into the UsefulIcrrStationEvent object
00377          
00378     if(calType==AraCalType::kNoCalib) {
00379       theEvent->fNumPoints[chanIndex]=MAX_NUMBER_SAMPLES_LAB3;
00380       for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;samp++) {
00381         theEvent->fVolts[chanIndex][samp]=rawadc[samp];
00382         theEvent->fTimes[chanIndex][samp]=sampNums[samp];
00383       }
00384     }
00385     if(calType==AraCalType::kJustUnwrap || calType==AraCalType::kADC) {
00386       theEvent->fNumPoints[chanIndex]=numValid;
00387       for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;samp++) {
00388         theEvent->fTimes[chanIndex][samp]=sampNums[samp];
00389         if(samp<numValid) {
00390           theEvent->fVolts[chanIndex][samp]=v[samp];
00391         }
00392         else {
00393           theEvent->fVolts[chanIndex][samp]=0;
00394         }
00395       }
00396     }
00397     if(calType==AraCalType::kVoltageTime) {
00398       theEvent->fNumPoints[chanIndex]=numValid;
00399       for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;samp++) {
00400         theEvent->fTimes[chanIndex][samp]=timeNums[samp];
00401         if(samp<numValid) {
00402           theEvent->fVolts[chanIndex][samp]=v[samp];
00403         }
00404         else {
00405           theEvent->fVolts[chanIndex][samp]=0;
00406         }
00407       }
00408     }    
00409     if(calType==AraCalType::kJustPed) {
00410       theEvent->fNumPoints[chanIndex]=MAX_NUMBER_SAMPLES_LAB3;
00411       for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;samp++) {
00412         theEvent->fVolts[chanIndex][samp]=pedsubadc[samp];
00413         theEvent->fTimes[chanIndex][samp]=sampNums[samp];
00414       }
00415     }
00416     if(AraCalType::hasBinWidthCalib(calType)) {
00417       //Almost always want this
00418       theEvent->fNumPoints[chanIndex]=numValid;
00419       for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;samp++) {
00420         theEvent->fTimes[chanIndex][samp]=calTimeNums[samp];
00421         if(samp<numValid) {
00422           theEvent->fVolts[chanIndex][samp]=calVoltNums[samp];
00423         }
00424         else {
00425           theEvent->fVolts[chanIndex][samp]=0;
00426         }
00427       }
00428     }    
00429   }
00430 
00431   //Now we have done the initial bin-by-bin and epsilon calibrations
00432   //Next up is to do the clock alignment
00433   if(AraCalType::hasClockAlignment(calType)) {
00434     //All of the higher calibrations do some form of clock alignment
00435     calcClockAlignVals(theEvent,calType);    
00436     for(int  chanIndex = 0; chanIndex < NUM_DIGITIZED_ICRR_CHANNELS; ++chanIndex ){
00437       int nChip=theEvent->chan[chanIndex].chanId/CHANNELS_PER_LAB3;
00438       for(int samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;samp++) {
00439         theEvent->fTimes[chanIndex][samp]+=clockAlignVals[stationId][nChip];
00440       }
00441     }
00442   }
00443   
00444   //For now we just have the one calibration type for interleaving
00445   AraGeomTool *tempGeom = AraGeomTool::Instance();
00446 
00447   for(int  rfchan = 0; rfchan < (theEvent->numRFChans); ++rfchan ){
00448     //   printf("AraEventCalibrator::calibrateEvent() rfchan %i stationId %i\n", rfchan, stationId);
00449     
00450     memset(theEvent->fVoltsRF[rfchan],0,sizeof(Double_t)*2*MAX_NUMBER_SAMPLES_LAB3);
00451     memset(theEvent->fTimesRF[rfchan],0,sizeof(Double_t)*2*MAX_NUMBER_SAMPLES_LAB3);
00452 
00453 
00454     if(tempGeom->getNumLabChansForChan(rfchan, stationId)==2) {
00455       //      printf("interleaved channel\n");
00456       // std::cout << rfchan << "\t"
00457         //      << tempGeom->getFirstLabChanIndexForChan(rfchan, stationId) << "\t"
00458         //              << tempGeom->getSecondLabChanIndexForChan(rfchan, stationId) << "\n";
00459 
00460       //printf("rfchan %i stationId %i labChip %i FirstLabChan %i SecondLabChan %i numLabChans %i labChans[0] %i labChans[1] %i\n", rfchan, stationId, tempGeom->getLabChipForChan(rfchan, stationId), tempGeom->getFirstLabChanForChan(rfchan, stationId), tempGeom->getSecondLabChanForChan(rfchan, stationId),tempGeom->getNumLabChansForChan(rfchan, stationId), tempGeom->getFirstLabChanIndexForChan(rfchan, stationId), tempGeom->getSecondLabChanIndexForChan(rfchan, stationId));
00461 
00462       int ci1=tempGeom->getFirstLabChanIndexForChan(rfchan, stationId);
00463       int ci2=tempGeom->getSecondLabChanIndexForChan(rfchan, stationId);
00464       theEvent->fNumPointsRF[rfchan]=theEvent->fNumPoints[ci1]+theEvent->fNumPoints[ci2];
00465       //Need to zero mean, maybe should do this in each half seperately?
00466       //RJN hack 13/01/11
00467       double mean=0;
00468       for(int i=0;i<theEvent->fNumPoints[ci1];i++) {
00469         mean+=theEvent->fVolts[ci1][i];
00470       }
00471       for(int i=0;i<theEvent->fNumPoints[ci2];i++) {
00472         mean+=theEvent->fVolts[ci2][i];
00473       }
00474       mean/=theEvent->fNumPointsRF[rfchan];
00475 
00476       //Only the interleaved types have any special calib here
00477       if(AraCalType::hasInterleaveCalib(calType)) {
00478         int i1=0;
00479         int i2=0;
00480         for(int i=0;i<theEvent->fNumPointsRF[rfchan];i++) {
00481           if(i1<theEvent->fNumPoints[ci1] && i2<theEvent->fNumPoints[ci2]) {
00482             //Both in play
00483             if(theEvent->fTimes[ci1][i1]<(theEvent->fTimes[ci2][i2]+interleaveVals[stationId][rfchan])) {
00484               theEvent->fTimesRF[rfchan][i]=theEvent->fTimes[ci1][i1];
00485               theEvent->fVoltsRF[rfchan][i]=theEvent->fVolts[ci1][i1]-mean;
00486               //              std::cout << "A: " << i << "\t" << theEvent->fTimesRF[rfchan][i] << "\n";
00487 
00488               i1++;
00489               continue;
00490             }
00491             else {
00492               theEvent->fTimesRF[rfchan][i]=theEvent->fTimes[ci2][i2]+interleaveVals[stationId][rfchan];
00493               theEvent->fVoltsRF[rfchan][i]=theEvent->fVolts[ci2][i2]-mean;
00494               //              std::cout << "B: " << i << "\t" << theEvent->fTimesRF[rfchan][i] << "\n";
00495               i2++;
00496               continue;
00497             }
00498           }
00499           else if(i1<theEvent->fNumPoints[ci1]) {
00500             theEvent->fTimesRF[rfchan][i]=theEvent->fTimes[ci1][i1];
00501             theEvent->fVoltsRF[rfchan][i]=theEvent->fVolts[ci1][i1]-mean;
00502             i1++;
00503             //        std::cout << "C: " << i << "\t" << theEvent->fTimesRF[rfchan][i] << "\n";
00504             continue;
00505           }
00506           else if(i2<theEvent->fNumPoints[ci2]) {
00507 
00508             theEvent->fTimesRF[rfchan][i]=theEvent->fTimes[ci2][i2]+interleaveVals[stationId][rfchan];
00509             theEvent->fVoltsRF[rfchan][i]=theEvent->fVolts[ci2][i2]-mean;
00510             //      std::cout << "D: " << i << "\t" << theEvent->fTimesRF[rfchan][i] << "\n";
00511             i2++;
00512             continue;
00513           }
00514         }
00515       }//interleaved
00516       
00517       else {    
00518         for(int i=0;i<theEvent->fNumPointsRF[rfchan];i++) {
00519           if(i%2==0) {
00520             //ci2 
00521             theEvent->fVoltsRF[rfchan][i]=theEvent->fVolts[ci2][i/2];
00522             theEvent->fTimesRF[rfchan][i]=theEvent->fTimes[ci2][i/2];     
00523           }
00524           else {
00525             //ci1 
00526             theEvent->fVoltsRF[rfchan][i]=theEvent->fVolts[ci1][i/2];
00527             theEvent->fTimesRF[rfchan][i]=theEvent->fTimes[ci1][i/2]+0.5*NSPERSAMP;       
00528           }
00529           
00530         }
00531       }
00532       
00533     }//numLabChans==2
00534     
00535     //Perform undiplexing of channels (ARA1 has surface antennae diplexed in to HPOL channels)
00536     else if(tempGeom->isDiplexed(rfchan, stationId)==0||!(AraCalType::hasUnDiplexing(calType))){
00537       //    printf("non-interleaved non-diplexed channel\n");
00538       //      std::cout << rfchan << "\t"
00539       //      << tempGeom->getFirstLabChanIndexForChan(rfchan) << "\n";
00540       int ci=tempGeom->getFirstLabChanIndexForChan(rfchan, stationId);
00541       theEvent->fNumPointsRF[rfchan]=theEvent->fNumPoints[ci];   
00542 
00543       //Need to zero mean the data
00544       double mean=0;
00545       for(int i=0;i<theEvent->fNumPoints[ci];i++) {
00546         mean+=theEvent->fVolts[ci][i];
00547       }
00548       mean/=theEvent->fNumPoints[ci];
00549 
00550       for(int i=0;i<theEvent->fNumPointsRF[rfchan];i++) {
00551         theEvent->fVoltsRF[rfchan][i]=theEvent->fVolts[ci][i]-mean;
00552         theEvent->fTimesRF[rfchan][i]=theEvent->fTimes[ci][i];
00553         
00554       }
00555     }//numLabChannels!=2&&isDiplexed==0
00556     
00557     else if(tempGeom->isDiplexed(rfchan, stationId)==1&&(AraCalType::hasUnDiplexing(calType))){
00558       //  printf("non-interleaved diplexed channel\n");
00559       //now need to work out how to do a diplexed channel
00560       //find lab channel from which to derived rfchan
00561       int ci=tempGeom->getFirstLabChanIndexForChan(rfchan,stationId);
00562       int noPoints=theEvent->fNumPoints[ci];
00563       Double_t lowPassFreq=tempGeom->getLowPassFilter(rfchan,stationId);
00564       Double_t highPassFreq=tempGeom->getHighPassFilter(rfchan,stationId);
00565 
00566       TGraph *gUnfiltered = new TGraph(noPoints, theEvent->fTimes[ci], theEvent->fVolts[ci]);
00567   
00568       TGraph *gFiltered = FFTtools::simplePassBandFilter(gUnfiltered, highPassFreq, lowPassFreq);
00569       Double_t *filteredT = gFiltered->GetX();
00570       Double_t *filteredV = gFiltered->GetY();
00571 
00572 
00573       for(int i=0;i<noPoints;i++){
00574         theEvent->fTimesRF[rfchan][i]=filteredT[i];
00575         theEvent->fVoltsRF[rfchan][i]=filteredV[i];
00576       }
00577       theEvent->fNumPointsRF[rfchan]=noPoints;
00578 
00579       delete gFiltered;
00580       delete gUnfiltered;
00581 
00582     }//isDiplexed==1
00583    
00584     if(AraCalType::hasCableDelays(calType)) {
00585       Double_t delay=tempGeom->fStationInfo[stationId].fAntInfo[rfchan].cableDelay;
00586       //      delay-=180; //Just an arbitrary offset
00587       for(int i=0;i<theEvent->fNumPointsRF[rfchan];i++) {
00588         theEvent->fTimesRF[rfchan][i]-=delay;
00589       }
00590     }
00591       
00592  
00593   }
00594 
00595 }
00596 
00597 void AraEventCalibrator::loadIcrrCalib(AraStationId_t stationId)
00598 {
00599   char calibDir[FILENAME_MAX];
00600   char binWidthFileName[FILENAME_MAX];
00601   char epsilonFileName[FILENAME_MAX];
00602   char interleaveFileName[FILENAME_MAX];
00603   char *calibEnv=getenv("ARA_CALIB_DIR");
00604   if(!calibEnv) {
00605     char *utilEnv=getenv("ARA_UTIL_INSTALL_DIR");
00606     if(!utilEnv)
00607       sprintf(calibDir,"calib");
00608     else
00609       sprintf(calibDir,"%s/share/araCalib",utilEnv);
00610   }
00611   else {
00612     strncpy(calibDir,calibEnv,FILENAME_MAX);
00613   }  
00614   //Populate the binWidthFileName, epsilonFileName and interleaveFileName variables
00615   if(stationId==ARA_TESTBED&&gotIcrrCalibFile[0]==0){
00616     sprintf(binWidthFileName,"%s/ICRR/TestBed/binWidths.txt",calibDir);
00617     sprintf(epsilonFileName,"%s/ICRR/TestBed/epsilonFile.txt",calibDir);
00618     sprintf(interleaveFileName,"%s/ICRR/TestBed/interleaveFile.txt",calibDir);
00619   }
00620   if(stationId==ARA_STATION1&&gotIcrrCalibFile[1]==0){
00621     sprintf(binWidthFileName,"%s/ICRR/Station1/binWidths.txt",calibDir);
00622     sprintf(epsilonFileName,"%s/ICRR/Station1/epsilonFile.txt",calibDir);
00623     sprintf(interleaveFileName,"%s/ICRR/Station1/interleaveFile.txt",calibDir);
00624   }
00625   //load the calibration files
00626 
00627   std::ifstream binWidthFile(binWidthFileName);
00628   std::ifstream epsilonFile(epsilonFileName);
00629   std::ifstream interleaveFile(interleaveFileName);
00630   int chip, rco, chan;
00631   double width, epsilon, interleave;
00632   while(binWidthFile >> chip >> rco){
00633     for(int samp=0; samp<MAX_NUMBER_SAMPLES_LAB3;++samp){
00634       binWidthFile >> width;
00635       binWidths[stationId][chip][rco][samp]=width;
00636     }
00637   }
00638   binWidthFile.close();
00639   while(epsilonFile >> chip >> rco >> epsilon){
00640     epsilonVals[stationId][chip][rco]=epsilon;
00641   }
00642   epsilonFile.close();
00643   while(interleaveFile >> chip >> chan >> interleave){
00644     interleaveVals[stationId][chan+4*chip]=interleave;
00645   }
00646   interleaveFile.close();
00647   
00648   //Set the gotIcrrCalibFile flags
00649   if(stationId==ARA_TESTBED){
00650     gotIcrrCalibFile[0]=1;
00651     gotIcrrCalibFile[1]=0;
00652   }
00653   if(stationId==ARA_STATION1){
00654     gotIcrrCalibFile[0]=0;
00655     gotIcrrCalibFile[1]=1;
00656   }
00657 
00658 
00659 }
00660 
00661 
00662 void AraEventCalibrator::calcClockAlignVals(UsefulIcrrStationEvent *theEvent, AraCalType::AraCalType_t calType)
00663 {
00664   AraStationId_t stationId=theEvent->stationId;
00665 
00666   if(!AraCalType::hasClockAlignment(calType)) return;
00667   TGraph *grClock[LAB3_PER_ICRR]={0};
00668   Double_t lag[LAB3_PER_ICRR]={0};
00669   for(int chip=0;chip<LAB3_PER_ICRR;chip++) {
00670     clockAlignVals[stationId][chip]=0;    
00671     int chanIndex=ICRR1_CLOCK_CHANNEL+CHANNELS_PER_LAB3*chip; 
00672     grClock[chip]=theEvent->getGraphFromElecChan(chanIndex);
00673     lag[chip]=estimateClockLag(grClock[chip]);
00674     delete grClock[chip];
00675 
00676     if(chip>0) {
00677       //Then can actually do some alignment
00678       clockAlignVals[stationId][chip]=lag[0]-lag[chip];
00679       //The below fudge factors were "tuned" using pulser data 
00680       // to try and remove period ambiguities resulting from wrong cycle lag
00681       if(lag[chip]<8 && lag[0]>9) 
00682         clockAlignVals[stationId][chip]-=25;
00683       if(lag[chip]>9 && lag[0]<7) 
00684         clockAlignVals[stationId][chip]+=25;
00685       //      std::cout << "clockAlignVals[ " << chip << "] = " << clockAlignVals[chip] << "\n";
00686     }
00687     
00688   }  
00689 }
00690 
00691 
00692 Double_t AraEventCalibrator::estimateClockLag(TGraph *grClock)
00693 {
00694   // This funciton estimates the clock lag (i.e. phase but expressed in terms of a deltaT between 0 and 1 period) by just using all the negative-positive zero crossing
00695 
00696   Double_t period=ICRR1_CLOCK_PERIOD;
00697   Double_t mean=grClock->GetMean(2);
00698   Int_t numPoints=grClock->GetN();
00699   if(numPoints<3) return 0;
00700   Double_t *tVals=grClock->GetX();
00701   Double_t *vVals=grClock->GetY();
00702 
00703   Double_t zc[1000]={0}; 
00704   Double_t rawZc[1000]={0}; 
00705   int countZC=0;
00706   for(int i=2;i<numPoints;i++) {
00707     if(vVals[i-1]<0 && vVals[i]>0) {
00708       Double_t x1=tVals[i-1];
00709       Double_t x2=tVals[i];
00710       Double_t y1=vVals[i-1]-mean;
00711       Double_t y2=vVals[i]-mean;      
00712       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
00713       zc[countZC]=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00714       rawZc[countZC]=zc[countZC];
00715       countZC++;
00716       //      if(countZC==1)
00717       //      break;
00718     }
00719        
00720   }
00721 
00722   Double_t firstZC=zc[0];
00723   while(firstZC>period) firstZC-=period;
00724   Double_t meanZC=0;
00725   Double_t meanZC2=0;
00726   for(int i=0;i<countZC;i++) {
00727     while((zc[i])>period) zc[i]-=period;
00728     if(TMath::Abs((zc[i]-period)-firstZC)<TMath::Abs(zc[i]-firstZC))
00729       zc[i]-=period;
00730     if(TMath::Abs((zc[i]+period)-firstZC)<TMath::Abs(zc[i]-firstZC))
00731       zc[i]+=period;
00732     meanZC+=zc[i];
00733     meanZC2+=zc[i]*zc[i];
00734     //     std::cout << i << "\t" << zc[i] << "\t" << rawZc[i] << "\n";     
00735   }
00736   meanZC/=countZC;
00737   meanZC2/=countZC;
00738   //  Double_t rms=TMath::Sqrt(meanZC2-meanZC*meanZC);
00739   //  std::cout << meanZC << "\t" << rms << "\n";
00740   return meanZC;
00741 
00742 }
00743 
00744 void AraEventCalibrator::fillRCOGuessArray(UsefulIcrrStationEvent *theEvent, int rcoGuess[LAB3_PER_ICRR])
00745 {
00746   for(int chip=0;chip<LAB3_PER_ICRR;chip++) {
00747     int chanIndex=ICRR1_CLOCK_CHANNEL+CHANNELS_PER_LAB3*chip;
00748     rcoGuess[chip]=theEvent->getRawRCO(chanIndex);
00749     
00750     Double_t period[2]={0};
00751     Double_t rms[2]={0};
00752     for(int rcoTest=0;rcoTest<2;rcoTest++) {
00753       int numValid=doBinCalibration(theEvent,chanIndex,rcoTest);
00754       period[rcoTest]=estimateClockPeriod(numValid,rms[rcoTest]);
00755     }
00756     
00757     Double_t periodTest=(TMath::Abs(period[0]-ICRR1_CLOCK_PERIOD)-TMath::Abs(period[1]-ICRR1_CLOCK_PERIOD));
00758     Double_t rmsTest=(rms[0]-rms[1]);
00759     if(periodTest<0) {
00760       rcoGuess[chip]=0;
00761       if(TMath::Abs(periodTest)<0.5 && rmsTest>0) 
00762         rcoGuess[chip]=1;
00763     }    
00764     else {
00765       rcoGuess[chip]=1;
00766       if(TMath::Abs(periodTest)<0.5 && rmsTest<0) {
00767         rcoGuess[chip]=0;       
00768       }
00769     }
00770     if(rms[rcoGuess[chip]]>4)  {
00771       rcoGuess[chip]=theEvent->getRawRCO(chanIndex);
00772     }
00773 
00774     //    std::cout << "AraEventCalibrator:\t" << period[0] << "\t" << period[1] << "\n";
00775     //    std::cout << "AraEventCalibrator:\t" << periodTest << "\t" << rmsTest << "\t" << rcoGuess[chip] << "\n";
00776   }
00777 }
00778 
00779 
00780 
00781 Double_t AraEventCalibrator::estimateClockPeriod(Int_t numPoints, Double_t &rms)
00782 {
00783   // This funciton estimates the period by just using all the negative-positive zero crossing
00784   if(numPoints<3) return 0;
00785   Double_t mean=0;
00786   Double_t vVals[MAX_NUMBER_SAMPLES_LAB3]={0};
00787   for(int i=0;i<numPoints;i++) {
00788     mean+=calVoltNums[i];
00789   }
00790   mean/=numPoints;
00791   for(int i=0;i<numPoints;i++) {
00792     vVals[i]=calVoltNums[i]-mean;
00793   }
00794 
00795 
00796   Double_t zc[1000]={0};
00797   Double_t periods[1000]={0};
00798   int countZC=0;
00799   for(int i=2;i<numPoints;i++) {
00800     if(vVals[i-1]<0 && vVals[i]>0) {
00801       Double_t x1=calTimeNums[i-1];
00802       Double_t x2=calTimeNums[i];
00803       Double_t y1=vVals[i-1];
00804       Double_t y2=vVals[i];      
00805       //      std::cout << i << "\t" << y2 << "\t" << y1 << "\t" << (y2-y1) << "\n";
00806       Double_t zcTime=(((0-y1)/(y2-y1))*(x2-x1))+x1;
00807       if(countZC>0) {
00808         if((zcTime-zc[countZC-1])<10)
00809           continue;
00810       }
00811       zc[countZC]=zcTime;
00812       countZC++;
00813       //      if(countZC==1)
00814       //      break;
00815     }    
00816   }
00817 
00818   if(countZC<2) return 0;
00819   
00820   
00821   int countPeriods=0;
00822   Double_t meanPeriod=0;
00823   Double_t meanPeriodSq=0;
00824   for(int i=1;i<countZC;i++) {
00825     periods[countPeriods]=zc[i]-zc[i-1];
00826     meanPeriod+=periods[countPeriods];
00827     meanPeriodSq+=periods[countPeriods]*periods[countPeriods];
00828     countPeriods++;
00829   }
00830   meanPeriod/=countPeriods;
00831   meanPeriodSq/=countPeriods;
00832   rms=TMath::Sqrt(meanPeriodSq-meanPeriod*meanPeriod);
00833   
00834   return meanPeriod;
00835 }
00836 
00837 void AraEventCalibrator::calibrateEvent(UsefulAtriStationEvent *theEvent, AraCalType::AraCalType_t calType) 
00838 {
00839 
00840   //FIXME -- this is where we force the loading of the calib and pedestals
00841   AraStationId_t thisStationId = theEvent->stationId;
00842   loadAtriCalib(thisStationId);
00843   loadAtriPedestals(thisStationId);
00844 
00845   //Step one is loop over the blocks
00846 
00847   std::vector<RawAtriStationBlock>::iterator blockIt;
00848   std::vector< std::vector<UShort_t> >::iterator vecVecIt;
00849   std::map< Int_t, std::vector <Double_t> >::iterator timeMapIt;
00850   std::map< Int_t, std::vector <Double_t> >::iterator voltMapIt;
00851   std::map< Int_t, std::vector <Double_t> >::iterator voltMapIt2;
00852   std::vector< UShort_t >::iterator shortIt;
00853   for(blockIt = theEvent->blockVec.begin(); 
00854       blockIt!=theEvent->blockVec.end();
00855       blockIt++) {
00856     //Step two is determine the channel Ids
00857     Int_t irsChan[8];
00858     Int_t numChans=0;
00859     for(Int_t bit=0;bit<8;bit++) {
00860       Int_t mask=(1<<bit);
00861       if((blockIt->channelMask)&mask) {
00862         irsChan[numChans]=bit;
00863         numChans++;
00864       }
00865     }
00866     //    std::cout << "Got numChans " << numChans << "\n";
00867 
00868     //Step three is loop over the channels within a block
00869     Int_t uptoChan=0;
00870     for(vecVecIt=blockIt->data.begin();
00871         vecVecIt!=blockIt->data.end();
00872         vecVecIt++) {
00873       Int_t chanId=irsChan[uptoChan] | ((blockIt->channelMask&0x300)>>5);
00874       Int_t chan=irsChan[uptoChan];
00875       Int_t dda=blockIt->getDda();
00876       Int_t block=blockIt->getBlock();
00877       Int_t capArray=blockIt->getCapArray();
00878       //      std::cout << "Got chanId " << chanId << "\t" << irsChan[uptoChan] << "\t" 
00879       //                << dda << "\t" << block << "\t" << RawAtriStationEvent::getPedIndex(dda,block,chan,0) << "\n";
00880       uptoChan++;
00881 
00882       //Step four is to check if we have already got this chanId
00883 
00884       timeMapIt=theEvent->fTimes.find(chanId);
00885       Double_t time=0;
00886       Int_t firstTime=1;
00887       if(timeMapIt==theEvent->fTimes.end()) {
00888         //First time round for this channel
00889         std::vector <Double_t> tempTimes;
00890         std::vector <Double_t> tempVolts;       
00891         //Now need to insert empty vector into map
00892         theEvent->fTimes.insert( std::pair< Int_t, std::vector <Double_t> >(chanId,tempTimes));
00893         theEvent->fVolts.insert( std::pair< Int_t, std::vector <Double_t> >(chanId,tempVolts));
00894         theEvent->fNumChannels++;
00895       }
00896       else {
00897         //Just get the last time
00898         time=timeMapIt->second.back();
00899         //      if(dda==1 &&chan==1)
00900           //      std::cout << "Last time " << time << "\t" << timeMapIt->second.size() << "\n";
00901         firstTime=0;
00902       }
00903       
00904       //Set the iterators to point to the correct channel in the map
00905       timeMapIt=theEvent->fTimes.find(chanId);
00906       voltMapIt=theEvent->fVolts.find(chanId);
00907 
00908       int samp=0;
00909       int index=0;
00910       //Now loop over the 64 samples
00911       Double_t tempTimes[SAMPLES_PER_BLOCK];
00912       Double_t tempVolts[SAMPLES_PER_BLOCK];
00913       Int_t voltIndex[SAMPLES_PER_BLOCK];
00914 
00915       //Here is the Epsilon calibration
00916       if(AraCalType::hasBinWidthCalib(calType)) {
00917         if(!firstTime) {
00918           //Add on the time between blocks
00919           time+=fAtriEpsilonTimes[dda][chan][capArray];
00920           //      if(dda==1 && chan==1)
00921           //        std::cout << "Block " << time << "\t" << fAtriEpsilonTimes[dda][chan][capArray] << "\n";
00922 
00923         }
00924       }
00925 
00926       for(shortIt=vecVecIt->begin();
00927           shortIt!=vecVecIt->end();
00928           shortIt++) {
00929         
00930         if(AraCalType::hasBinWidthCalib(calType)) {
00931 
00932           //Now need to work out where to place the sample
00933           voltIndex[samp]=fAtriSampleIndex[dda][chan][capArray][samp];
00934 
00935           //Now get the time
00936           tempTimes[samp]=time+fAtriSampleTimes[dda][chan][capArray][samp];
00937 
00938           //      if(dda==1 && chan==1) {
00939           //      //        std::cout << dda << "\t" << chan << "\t" << capArray << "\t" << samp << "\t" << fAtriSampleTimes[dda][chan][capArray][samp] << "\n";
00940           //        std::cout << index << "\t" << tempTimes[samp] << "\t" << time << "\n";
00941           //      }
00942           
00943         }
00944         else {
00945           time+=1;      
00946           tempTimes[samp]=time;
00947           voltIndex[samp]=samp;
00948         }
00949         
00950         //Now the voltage part
00951         if(!AraCalType::hasPedestalSubtraction(calType))
00952           tempVolts[samp]=(*shortIt); 
00953         else {
00954           tempVolts[samp]=(*shortIt)-(Int_t)fAtriPeds[RawAtriStationEvent::getPedIndex(dda,block,chan,samp)]; 
00955           //      if(dda==3 && samp<2) {
00956           //        std::cout << (*shortIt)  << "\t" << (Int_t)fAtriPeds[RawAtriStationEvent::getPedIndex(dda,block,chan,samp)] << "\n";
00957           //      }
00958         }
00959         samp++;
00960       }
00961 
00962       for(samp=0;samp<SAMPLES_PER_BLOCK;samp++) {
00963         timeMapIt->second.push_back(tempTimes[samp]); 
00964         voltMapIt->second.push_back(tempVolts[voltIndex[samp]]); //Filling with volts
00965         
00966       }
00967       
00968             
00969     }
00970   }
00971   
00972   if(hasCommonMode(calType)) {
00973     //Then we need to do a common mode correction
00974     //loop over dda
00975     //loop over chan 
00976     //loop over times and subtract one
00977     
00978     for(int dda=0;dda<DDA_PER_ATRI;dda++) {
00979       for(Int_t chan=0;chan<5;chan++) {
00980         Int_t chanId=chan+RFCHAN_PER_DDA*dda;
00981         voltMapIt=theEvent->fVolts.find(chanId);
00982         Int_t chanId2=5+RFCHAN_PER_DDA*dda;
00983         voltMapIt2=theEvent->fVolts.find(chanId2);
00984         
00985         Int_t numPoints=(voltMapIt->second).size();
00986         for(int samp=0;samp<numPoints;samp++) {
00987           voltMapIt->second[samp]-=voltMapIt2->second[samp];
00988         }
00989       }
00990     }
00991 
00992   }
00993 
00994   //  fprintf(stderr, "AraEventCalibrator::CalibrateEvent() -- finished calibrating event\n");//DEBUG
00995             
00996 }
00997 
00998 
00999 
01000 void AraEventCalibrator::setAtriPedFile(char *filename, AraStationId_t stationId)
01001 {
01002   Int_t calibIndex = AraGeomTool::getStationCalibIndex(stationId);
01003   
01004   strncpy(fAtriPedFile[calibIndex],filename,FILENAME_MAX);
01005   fGotAtriPedFile[calibIndex]=0; //This will ensure that we load this new pedFile -- This is in case we already have one loaded for this station
01006   loadAtriPedestals(stationId);
01007 }
01008 
01009 void AraEventCalibrator::loadAtriPedestals(AraStationId_t stationId)
01010 {  
01011   Int_t calibIndex = AraGeomTool::getStationCalibIndex(stationId);
01012   if(calibIndex==-1){
01013     fprintf(stderr, "AraEventCalibrator::loadAtriPedestals -- ERROR Unknown stationId %i\n", stationId);
01014     return;
01015   }
01016 
01017   //Check if we need to load the pedFile
01018   if(fGotAtriPedFile[calibIndex]){
01019     //    fprintf(stderr, "AraEventCalibrator::loadAtriPedestals -- Pedestal already loaded for station %i\n", stationId);//DEBUG
01020     return;
01021   }  
01022   char *pedFileEnv = getenv( "ARA_ONE_PEDESTAL_FILE" ); //FIXME
01023   if ( pedFileEnv == NULL ) {
01024     char calibDir[FILENAME_MAX];
01025     char *calibEnv=getenv("ARA_CALIB_DIR");
01026     if(!calibEnv) {
01027       char *utilEnv=getenv("ARA_UTIL_INSTALL_DIR");
01028       if(!utilEnv) {
01029         sprintf(calibDir,"calib");
01030         fprintf(stdout,"AraEventCalibrator::loadAtriPedestals(): INFO - Pedestal file [from ./calib]");
01031       } else {
01032         sprintf(calibDir,"%s/share/araCalib",utilEnv);
01033         fprintf(stdout,"AraEventCalibrator::loadAtriPedestals(): INFO - Pedestal file [from ARA_UTIL_INSTALL_DIR/share/calib]");
01034       }
01035     }
01036     else {
01037       strncpy(calibDir,calibEnv,FILENAME_MAX);
01038       fprintf(stdout,"AraEventCalibrator::loadAtriPedestals(): INFO - Pedestal file [from ARA_CALIB_DIR]");
01039     }
01040     sprintf(fAtriPedFile[calibIndex],"%s/ATRI/araAtriStation%iPedestals.txt",calibDir, stationId);
01041     fprintf(stdout," = %s\n",fAtriPedFile[calibIndex]);
01042   } // end of IF-block for pedestal file not specified by environment variable
01043   else {
01044     strncpy(fAtriPedFile[calibIndex],pedFileEnv,FILENAME_MAX);
01045     fprintf(stdout,"AraEventCalibrator::loadAtriPedestals(): INFO - Pedestal file [from ARA_ONE_PEDESTAL_FILE] = %s\n",fAtriPedFile[calibIndex]);
01046   } // end of IF-block for pedestal file specified by environment variable
01047 
01048   //Pedestal file
01049   std::ifstream PedFile(fAtriPedFile[calibIndex]);
01050   Int_t dda,block,chan;
01051   UShort_t pedVal;
01052   
01053   //If there are pedestals in memory already then we need to delete them and replace with this station's
01054   if(fAtriPeds) delete fAtriPeds;
01055 
01056   fAtriPeds = new UShort_t [DDA_PER_ATRI*BLOCKS_PER_DDA*RFCHAN_PER_DDA*SAMPLES_PER_BLOCK];
01057   if(!fAtriPeds) {
01058     std::cerr << "Can not allocate memory for pedestal file\n";
01059     exit(0);
01060   }
01061       
01062   while(PedFile >> dda >> block >> chan) {
01063     for(int samp=0;samp<SAMPLES_PER_BLOCK;samp++) {
01064       PedFile >> pedVal;
01065       fAtriPeds[RawAtriStationEvent::getPedIndex(dda,block,chan,samp)]=pedVal;
01066     }
01067   }  
01068   
01069   //Now we set the gotPedFile flags to indicate which station we have in memory
01070   
01071   for(AraStationId_t station=0;station<ICRR_NO_STATIONS+ATRI_NO_STATIONS;station++){
01072     if(AraGeomTool::isAtriStation(station)){
01073       calibIndex=AraGeomTool::getStationCalibIndex(station);
01074       if(station==stationId) fGotAtriPedFile[calibIndex]=1;
01075       else fGotAtriPedFile[calibIndex]=0;
01076     }
01077   }
01078 
01079 
01080 }
01081 
01082 
01083 void AraEventCalibrator::loadAtriCalib(AraStationId_t stationId)
01084 {
01085   Int_t calibIndex = AraGeomTool::getStationCalibIndex(stationId);
01086   if(calibIndex==-1){
01087     fprintf(stderr, "AraEventCalibrator::loadAtriCalib -- ERROR Unknown stationId %i\n", stationId);
01088     return;
01089   }
01090 
01091   //Check if we need to load the calibFile -- Are the calib values already in memory?
01092   if(fGotAtriCalibFile[calibIndex]){
01093     //    fprintf(stderr, "AraEventCalibrator::loadAtriCalib -- Calib already loaded for stationId %i\n", stationId);//DEBUG
01094     return;
01095   }
01096   //If we got here then we are either loading calib for the first time, or replacing the existing calib with that for this station
01097 
01098   char calibFile[FILENAME_MAX];
01099   char calibDir[FILENAME_MAX];
01100   char *calibEnv=getenv("ARA_CALIB_DIR");
01101   if(!calibEnv) {
01102     char *utilEnv=getenv("ARA_UTIL_INSTALL_DIR");
01103     if(!utilEnv)
01104       sprintf(calibDir,"calib");
01105     else
01106       sprintf(calibDir,"%s/share/araCalib",utilEnv);
01107   }
01108   else {
01109     strncpy(calibDir,calibEnv,FILENAME_MAX);
01110   }  
01111 
01112   int dda,chan,sample,capArray;
01113   sprintf(calibFile,"%s/ATRI/araAtriStation%iSampleTiming.txt",calibDir, stationId);
01114   fprintf(stdout, "AraEventCalibrator::loadAtriCalib(): INFO - Calibration file = %s\n", calibFile);//DEBUG
01115 
01116   std::ifstream SampleFile(calibFile);
01117   for(dda=0;dda<DDA_PER_ATRI;dda++) {
01118     for(chan=0;chan<RFCHAN_PER_DDA;chan++) {
01119       for(capArray=0;capArray<2;capArray++) {
01120         for(sample=0;sample<SAMPLES_PER_BLOCK;sample++) {
01121           fAtriSampleTimes[dda][chan][capArray][sample]=sample/3.2;
01122           fAtriSampleIndex[dda][chan][capArray][sample]=sample;
01123         }
01124         fAtriEpsilonTimes[dda][chan][capArray]=1/3.2;
01125       }
01126     }
01127   }
01128 
01129   Double_t value;
01130   Int_t index;
01131   while(SampleFile >> dda >> chan >> capArray) {
01132     //    std::cout <<  dda << "\t" << chan << "\t" << capArray << "\t";
01133     for(sample=0;sample<SAMPLES_PER_BLOCK;sample++) {
01134       SampleFile >> index;
01135       fAtriSampleIndex[dda][chan][capArray][sample]=index;
01136       //      std::cout << fAtriSampleIndex[dda][chan][ << " ";    
01137     }
01138 
01139     SampleFile >> dda >> chan >> capArray;
01140     for(sample=0;sample<SAMPLES_PER_BLOCK;sample++) {
01141       SampleFile >> value;
01142       fAtriSampleTimes[dda][chan][capArray][sample]=value;
01143       //      std::cout << fAtriSampleTimes[dda][chan][capArray][sample] << " ";    
01144     }
01145     //    std::cout << "\n";
01146   }
01147 
01148   
01149   //Now we set the gotCalibFile flags to indicate which station we have in memory
01150   
01151   for(AraStationId_t station=0;station<ICRR_NO_STATIONS+ATRI_NO_STATIONS;station++){
01152     if(AraGeomTool::isAtriStation(station)){
01153       calibIndex=AraGeomTool::getStationCalibIndex(station);
01154       if(station==stationId) fGotAtriCalibFile[calibIndex]=1;
01155       else fGotAtriCalibFile[calibIndex]=0;
01156     }
01157   }
01158 
01159 }
01160 

Generated on Tue Sep 11 19:51:09 2012 for ARA ROOT v3.3 Software by doxygen 1.4.7