calibration/ICRR/Station1/doEpsilon.cxx
00001 //Written by Jonathan Davies - UCL 00002 //doEpsilon.cxx used to calculate the wrap around widths for ARA experiment 00003 00004 #include <iostream> 00005 #include <fstream> 00006 #include <cmath> 00007 00008 //General Includes 00009 #include <zlib.h> 00010 00011 //ROOT Includes 00012 #include "TSystem.h" 00013 #include "TFile.h" 00014 #include "TTree.h" 00015 #include "TH1.h" 00016 #include "TCanvas.h" 00017 #include "TStyle.h" 00018 #include "TGraph.h" 00019 #include "TChain.h" 00020 00021 //AraRoot Includes -- using version 3.0 00022 //#include "RawAraEvent.h" 00023 #include "RawIcrrStationEvent.h" 00024 //#include "AraRawRFChannel.h" 00025 #include "AraRawIcrrRFChannel.h" 00026 //#include "araDefines.h" 00027 #include "araIcrrDefines.h" 00028 00029 00030 Int_t fDebug=0; 00031 int gotPedFile=0; 00032 char pedFile[FILENAME_MAX]; 00033 float pedestalData[LAB3_PER_ICRR][CHANNELS_PER_LAB3][MAX_NUMBER_SAMPLES_LAB3]; 00034 00035 int doEpsilon(char[FILENAME_MAX], char[FILENAME_MAX], char[FILENAME_MAX], char[FILENAME_MAX], int, int, int, int flag=0); 00036 Int_t findLastZC(TGraph*, Double_t, Double_t*); 00037 Int_t findFirstZC(TGraph*, Double_t, Double_t*); 00038 void loadPedestals(); 00039 00040 using namespace std; 00041 00042 int main(int argc, char *argv[]){ 00043 int minRun, maxRun, freq; 00044 00045 char runDir[200], baseDir[200], temp[200], pedFile[200]; 00046 sprintf(runDir, "/Users/jdavies/ara/data/ara_station1_ICRR_calibration/root/Station1Test"); 00047 sprintf(baseDir, "/Users/jdavies/ara/calibration/ara_station1_ICRR/testing/testing"); 00048 sprintf(pedFile, "/Users/jdavies/ara/data/ara_station1_ICRR_calibration/data/peds/run_003747/peds_1326108401/peds_1326108401.602169.run003747.dat"); 00049 00050 // if(argc<5){ 00051 // printf("Correct usage : ./doBinWidths <frequency> <minRun> <maxRun> <Minus40 / Minus20 / Minus5 / RoomTemp>\n"); 00052 // return -1; 00053 // } 00054 // freq=atoi(argv[1]); 00055 // minRun=atoi(argv[2]); 00056 // maxRun=atoi(argv[3]); 00057 // strcpy(temp, argv[4]); 00058 00059 //jd to comment out 00060 freq=175; 00061 minRun=3763; 00062 maxRun=3769; 00063 sprintf(temp, "Minus20"); 00064 00065 printf("freq %i minRun %i maxRun %i temp %s\n", freq, minRun, maxRun, temp); 00066 00067 doEpsilon(runDir, baseDir, temp, pedFile, freq, minRun, maxRun); 00068 00069 return 0; 00070 } 00071 00072 00073 00074 00075 int doEpsilon(char runDir[FILENAME_MAX], char baseDir[FILENAME_MAX], char temp[FILENAME_MAX], char ped[FILENAME_MAX], Int_t frequency, int minRun, int maxRun, int flag){ 00076 00077 if(flag){ 00078 fDebug=flag; 00079 printf("In debuggin mode level %i\n", fDebug); 00080 } 00081 00082 // sprintf(pedFile, "/Users/jdavies/ara/data/ara_station1_ICRR_calibration/data/peds/run_002263/peds_1324969832/peds_1324969832.905582.run002263.dat"); 00083 strcpy(pedFile, ped); 00084 gotPedFile=1; 00085 00086 //1.0 Define variables needed 00087 00088 Int_t Chip=0, RCO=0, Sample=0; 00089 Double_t BinSize=0; 00090 Double_t period=1./(0.001*frequency); 00091 00092 //1.1 Open the bin widths file and populate a time array 00093 00094 Double_t calibTime[LAB3_PER_ICRR][2][MAX_NUMBER_SAMPLES_LAB3]={{{0}}}; 00095 char calibName[FILENAME_MAX]; 00096 sprintf(calibName, "%s/root/%s/BinWidths%iMHzRun%iTo%i.root", baseDir, temp, frequency, minRun, maxRun); 00097 00098 if(fDebug) printf("Opening Bin widths file\n%s\n", calibName); 00099 00100 TFile *calibFile = TFile::Open(calibName); 00101 TTree *calibTree = (TTree*)calibFile->Get("binWidths"); 00102 calibTree->SetBranchAddress("Chip", &Chip); 00103 calibTree->SetBranchAddress("Sample", &Sample); 00104 calibTree->SetBranchAddress("BinSize", &BinSize); 00105 calibTree->SetBranchAddress("RCO", &RCO); 00106 00107 for(int entry=0;entry<calibTree->GetEntries();++entry){ 00108 calibTree->GetEntry(entry); 00109 calibTime[Chip][RCO][Sample]=BinSize; 00110 } 00111 00112 calibFile->Close(); 00113 00114 if(fDebug){ 00115 for(Chip=0;Chip<LAB3_PER_ICRR;++Chip){ 00116 for(RCO=0;RCO<2;++RCO){ 00117 for(Sample=0;Sample<MAX_NUMBER_SAMPLES_LAB3;++Sample){ 00118 printf("%i\t%i\t%i\t%f\n", Chip, RCO, Sample, calibTime[Chip][RCO][Sample]); 00119 } 00120 } 00121 } 00122 } 00123 00124 //2. Open run files into a TChain 00125 00126 TChain *eventTree = new TChain("eventTree"); 00127 char runName[FILENAME_MAX]; 00128 for(int run=minRun; run<=maxRun;run++){ 00129 sprintf(runName, "%s/run%i/event%i.root", runDir, run, run); 00130 if(fDebug) printf("Adding run file %s\n", runName); 00131 eventTree->Add(runName); 00132 } 00133 00134 00135 // RawAraEvent *evPtr=0; 00136 // AraRawRFChannel chanOut; 00137 RawIcrrStationEvent *evPtr=0; 00138 AraRawIcrrRFChannel chanOut; 00139 00140 eventTree->SetBranchAddress("event", &evPtr); 00141 00142 //3. Open Pedestals 00143 loadPedestals(); 00144 00145 //4. Create an output Tree format and interesting variables 00146 TGraph *preWrapGraph[NUM_DIGITIZED_ICRR_CHANNELS]; 00147 TGraph *postWrapGraph[NUM_DIGITIZED_ICRR_CHANNELS]; 00148 Double_t preWrap[2][MAX_NUMBER_SAMPLES_LAB3]; 00149 Double_t postWrap[2][MAX_NUMBER_SAMPLES_LAB3]; 00150 Int_t Channel=0, firstHitBus=0, lastHitBus=0, wrapped=0, RCO1=0,RCO2=0, hitCut=0, noSamples=0, offsetCount=0, eventNo=0, entry=0, skipCount=0; 00151 Double_t deltaT=0, rawDeltaT=0, lastZC=0, firstZC=0, offset=0, cumalativeTime=0; 00152 00153 char outName[FILENAME_MAX]; 00154 if(fDebug) sprintf(outName, "%s/root/%s/WrapAroundDebugRun%iTo%i.root", baseDir, temp, minRun, maxRun); 00155 00156 else sprintf(outName, "%s/root/%s/WrapAroundRun%iTo%i.root", baseDir, temp, minRun, maxRun); 00157 if(fDebug) printf("Creating outPut File and Tree %s\n", outName); 00158 00159 00160 TTree *wrapTree = new TTree("wrapTree", "wrapTree"); 00161 00162 wrapTree->Branch("Chip", &Chip, "Chip/I"); 00163 wrapTree->Branch("RCO", &RCO, "RCO/I"); 00164 wrapTree->Branch("Channel", &Channel, "Channel/I"); 00165 wrapTree->Branch("firstHitBus", &firstHitBus, "firstHitBus/I"); 00166 wrapTree->Branch("lastHitBus", &lastHitBus, "lastHitBus/I"); 00167 wrapTree->Branch("wrapped", &wrapped, "wrapped/I"); 00168 wrapTree->Branch("RCO1", &RCO1, "RCO1/I"); 00169 wrapTree->Branch("entry", &entry, "entry/I"); 00170 wrapTree->Branch("hitCut", &hitCut, "hitCut/I"); 00171 wrapTree->Branch("noSamples", &noSamples, "noSamples/I"); 00172 wrapTree->Branch("eventNo", &eventNo, "eventNo/I"); 00173 wrapTree->Branch("skipCount", &skipCount, "skipCount/I"); 00174 00175 // wrapTree->Branch("A", &A, "A/I"); 00176 wrapTree->Branch("deltaT", &deltaT, "deltaT/D"); 00177 wrapTree->Branch("rawDeltaT", &rawDeltaT, "rawDeltaT/D"); 00178 00179 wrapTree->Branch("firstZC", &firstZC, "firstZC/D"); 00180 wrapTree->Branch("lastZC", &lastZC, "lastZC/D"); 00181 // wrapTree->Branch("A", &A, "A/D"); 00182 00183 //4.1 Create a bunch of histograms in which to store deltaT's 00184 TH1D *histDeltaT[LAB3_PER_ICRR][2]; 00185 char histName[FILENAME_MAX]; 00186 for(Chip=0;Chip<LAB3_PER_ICRR;Chip++){ 00187 for(RCO=0;RCO<2;RCO++){ 00188 sprintf(histName, "histDeltaTChip%iRCO%i", Chip, RCO); 00189 histDeltaT[Chip][RCO] = new TH1D(histName,histName, 1000, 2, 6); 00190 } 00191 } 00192 00193 00194 00195 00196 //5. Event loop 00197 int minEntry=0, maxEntry=eventTree->GetEntries(), minChan=0, maxChan=CHANNELS_PER_LAB3-1, starEvery=maxEntry/80; 00198 printf("No of entries in the run Tree is %i\n", maxEntry); 00199 if(fDebug){ 00200 minEntry=46186; 00201 maxEntry=46187; 00202 minChan=0; 00203 // maxChan=3; 00204 } 00205 for(entry=minEntry;entry<maxEntry;++entry){ 00206 if(entry%starEvery==0) fprintf(stderr, "*"); 00207 00208 eventTree->GetEntry(entry); 00209 eventNo=evPtr->head.eventNumber; 00210 00211 for(int logChan=minChan;logChan<NUM_DIGITIZED_ICRR_CHANNELS;++logChan){ 00212 00213 //5.1 Get all the necessary stuff from the event for that channel 00214 chanOut=evPtr->chan[logChan]; 00215 // Chip=evPtr->getLabChip(logChan); 00216 // Chip=chanOut.getLabChip(); 00217 Chip=logChan/9; 00218 Channel=logChan%9; 00219 if(Channel>maxChan) continue; 00220 firstHitBus=evPtr->getFirstHitBus(logChan); 00221 lastHitBus=evPtr->getLastHitBus(logChan); 00222 wrapped=evPtr->getWrappedHitBus(logChan); 00223 RCO2=evPtr->getRCO(logChan); 00224 RCO1=1-RCO2; 00225 00226 00227 if(fDebug){ 00228 for(int i=0;i<80;i++) printf("-"); 00229 printf("\nEvent %i Chip %i Channel %i RCO2%i\n", entry, Chip, Channel, RCO2); 00230 } 00231 00232 00233 //5.2 This is a cut made in first version of wrapAround.cxx 00234 if(firstHitBus<20||firstHitBus>236||lastHitBus<20||lastHitBus>236){ 00235 // continue; 00236 hitCut=1; 00237 } 00238 else hitCut=0; 00239 00240 //5.3 Calculate the offset 00241 offset=0; 00242 offsetCount=0; 00243 for(Sample=0; Sample<firstHitBus; Sample++){ 00244 offset += chanOut.data[Sample]-pedestalData[Chip][Channel][Sample]; 00245 offsetCount++; 00246 } 00247 for(Sample=lastHitBus+1; Sample <259; Sample++){ 00248 offset += chanOut.data[Sample]-pedestalData[Chip][Channel][Sample]; 00249 offsetCount++; 00250 } 00251 offset=offset/offsetCount; 00252 00253 //5.4 Fill pre and post wrap arrays 00254 cumalativeTime=0; 00255 noSamples=0; 00256 for(Sample=lastHitBus+1;Sample<256;++Sample){ 00257 preWrap[0][Sample-lastHitBus-1]=cumalativeTime; 00258 preWrap[1][Sample-lastHitBus-1]=(chanOut.data[Sample]-pedestalData[Chip][Channel][Sample])-offset; 00259 cumalativeTime+=calibTime[Chip][RCO1][Sample]; 00260 ++noSamples; 00261 } 00262 00263 cumalativeTime-=calibTime[Chip][RCO1][255]; 00264 00265 preWrapGraph[logChan] = new TGraph(noSamples, preWrap[0], preWrap[1]); 00266 noSamples=0; 00267 00268 for(Sample=0;Sample<firstHitBus;Sample++){ 00269 postWrap[0][Sample]=cumalativeTime; 00270 postWrap[1][Sample]=(chanOut.data[Sample]-pedestalData[Chip][Channel][Sample])-offset; 00271 cumalativeTime+=calibTime[Chip][RCO2][Sample]; 00272 ++noSamples; 00273 } 00274 00275 postWrapGraph[logChan] = new TGraph(noSamples, postWrap[0], postWrap[1]); 00276 00277 //5.5 Calculate wrapAround / deltaT 00278 00279 firstZC=0; 00280 lastZC=0; 00281 00282 if(findFirstZC(postWrapGraph[logChan], period, &firstZC)==-1) 00283 continue; 00284 if(findLastZC(preWrapGraph[logChan], period, &lastZC)==-1) 00285 continue; 00286 deltaT=(lastZC-firstZC)+period; 00287 rawDeltaT=deltaT; 00288 // need to insert some more stringent requirement here 00289 00290 if(deltaT<0) deltaT+=period; 00291 00292 // deltaT=lastZC-firstZC; 00293 // rawDeltaT=deltaT+period; 00294 // while(TMath::Abs(deltaT+period)<TMath::Abs(deltaT)) 00295 // deltaT+=period; 00296 // while(TMath::Abs(deltaT-period)<TMath::Abs(deltaT)) 00297 // deltaT-=period; 00298 00299 00300 if(fDebug) printf("5.5 deltaT is %f rawDeltaT is %f\tfirstZC %f\tlastZC %f\n",deltaT,rawDeltaT, firstZC, lastZC); 00301 00302 //5.6 Fill the Tree and or histograms 00303 00304 skipCount=0; 00305 if(entry<10) 00306 histDeltaT[Chip][RCO1]->Fill(deltaT); 00307 else if(TMath::Abs(histDeltaT[Chip][RCO1]->GetMean()-deltaT)<.5) 00308 histDeltaT[Chip][RCO1]->Fill(deltaT); 00309 else skipCount=1; 00310 RCO=RCO1; 00311 wrapTree->Fill(); 00312 00313 00314 if(!fDebug){ 00315 delete preWrapGraph[logChan]; 00316 delete postWrapGraph[logChan]; 00317 } 00318 }//logChan 00319 }//entry 00320 00321 printf("\n"); 00322 00323 //6. Create a Canvas and other useful things before saving and closing the files 00324 if(fDebug) printf("6. Create a Canvas and other useful things before saving and closing the files\n"); 00325 TFile *outFile = new TFile(outName, "RECREATE"); 00326 TCanvas *canvas[NUM_DIGITIZED_ICRR_CHANNELS]; 00327 00328 00329 wrapTree->Write(); 00330 00331 if(fDebug){ 00332 char canvasName[FILENAME_MAX]; 00333 for(Int_t logChan=0;logChan<NUM_DIGITIZED_ICRR_CHANNELS;logChan++){ 00334 Chip=logChan/9; 00335 Channel=logChan%9; 00336 sprintf(canvasName, "Event%iChip%iChannel%i", minEntry, Chip, Channel); 00337 canvas[logChan] = new TCanvas(canvasName,canvasName); 00338 canvas[logChan]->Divide(1,2); 00339 canvas[logChan]->cd(1); 00340 preWrapGraph[logChan]->SetTitle("preWrap"); 00341 preWrapGraph[logChan]->GetXaxis()->SetTitle("Time (ns)"); 00342 preWrapGraph[logChan]->GetXaxis()->SetLabelSize(0.04); 00343 preWrapGraph[logChan]->GetYaxis()->SetTitle("mV (ish)"); 00344 preWrapGraph[logChan]->Draw("AL*"); 00345 canvas[logChan]->cd(2); 00346 postWrapGraph[logChan]->SetTitle("postWrap"); 00347 postWrapGraph[logChan]->GetXaxis()->SetTitle("Time (ns)"); 00348 postWrapGraph[logChan]->GetXaxis()->SetLabelSize(0.04); 00349 postWrapGraph[logChan]->GetYaxis()->SetTitle("mV (ish)"); 00350 postWrapGraph[logChan]->Draw("AL*"); 00351 } 00352 } 00353 else{ 00354 char canvasName[FILENAME_MAX]; 00355 sprintf(canvasName, "histDeltaT"); 00356 canvas[0] = new TCanvas(canvasName,canvasName); 00357 canvas[0]->Divide(3,2); 00358 canvas[0]->cd(1); 00359 // wrapTree->Draw("deltaT", "wrapped==0&&hitCut==0&&Channel<8&&Chip==0&&RCO==0"); 00360 histDeltaT[0][0]->Draw(); 00361 canvas[0]->cd(2); 00362 // wrapTree->Draw("deltaT", "wrapped==0&&hitCut==0&&Channel<8&&Chip==1&&RCO==0"); 00363 histDeltaT[1][0]->Draw(); 00364 canvas[0]->cd(3); 00365 // wrapTree->Draw("deltaT", "wrapped==0&&hitCut==0&&Channel<8&&Chip==2&&RCO==0"); 00366 histDeltaT[2][0]->Draw(); 00367 canvas[0]->cd(4); 00368 // wrapTree->Draw("deltaT", "wrapped==0&&hitCut==0&&Channel<8&&Chip==0&&RCO==1"); 00369 histDeltaT[0][1]->Draw(); 00370 canvas[0]->cd(5); 00371 // wrapTree->Draw("deltaT", "wrapped==0&&hitCut==0&&Channel<8&&Chip==1&&RCO==1"); 00372 histDeltaT[1][1]->Draw(); 00373 canvas[0]->cd(6); 00374 // wrapTree->Draw("deltaT", "wrapped==0&&hitCut==0&&Channel<8&&Chip==2&&RCO==1"); 00375 histDeltaT[2][1]->Draw(); 00376 canvas[0]->Write(); 00377 canvas[0]->Close(); 00378 sprintf(canvasName, "rawDeltaT"); 00379 canvas[1] = new TCanvas(canvasName,canvasName); 00380 canvas[1]->Divide(3,2); 00381 canvas[1]->cd(1); 00382 wrapTree->Draw("rawDeltaT", "wrapped==0&&firstHitBus>20&&Channel<8&&Chip==0&&RCO==0"); 00383 canvas[1]->cd(2); 00384 wrapTree->Draw("rawDeltaT", "wrapped==0&&firstHitBus>20&&Channel<8&&Chip==1&&RCO==0"); 00385 canvas[1]->cd(3); 00386 wrapTree->Draw("rawDeltaT", "wrapped==0&&firstHitBus>20&&Channel<8&&Chip==2&&RCO==0"); 00387 canvas[1]->cd(4); 00388 wrapTree->Draw("rawDeltaT", "wrapped==0&&firstHitBus>20&&Channel<8&&Chip==0&&RCO==1"); 00389 canvas[1]->cd(5); 00390 wrapTree->Draw("rawDeltaT", "wrapped==0&&firstHitBus>20&&Channel<8&&Chip==1&&RCO==1"); 00391 canvas[1]->cd(6); 00392 wrapTree->Draw("rawDeltaT", "wrapped==0&&firstHitBus>20&&Channel<8&&Chip==2&&RCO==1"); 00393 canvas[1]->Write(); 00394 canvas[1]->Close(); 00395 } 00396 00397 00398 00399 //7. Save everything then print the epsilon values 00400 00401 for(Chip=0;Chip<LAB3_PER_ICRR;Chip++){ 00402 for(RCO=0;RCO<2;RCO++){ 00403 histDeltaT[Chip][RCO]->Write(); 00404 } 00405 } 00406 Double_t epsilon=0; 00407 00408 ofstream epsilonFile; 00409 00410 if(fDebug) cout << endl << "Epsilon values\n"; 00411 00412 ofstream textOut; 00413 char textOutName[FILENAME_MAX]; 00414 sprintf(textOutName, "%s/calib/%s/epsilonFile.txt", baseDir, temp); 00415 textOut.open(textOutName); 00416 for(Chip=0;Chip<LAB3_PER_ICRR;Chip++){ 00417 for(RCO=0;RCO<2;RCO++){ 00418 epsilon=-(histDeltaT[Chip][RCO]->GetMean())+calibTime[Chip][RCO][255]+calibTime[Chip][RCO][256]+calibTime[Chip][RCO][257]+calibTime[Chip][RCO][258]; 00419 textOut << Chip << "\t" << RCO << "\t" << epsilon << endl; 00420 if(fDebug) cout << Chip << "\t" << RCO << "\t" << histDeltaT[Chip][RCO]->GetMean() << "\t" << epsilon << endl; 00421 } 00422 } 00423 textOut.close(); 00424 outFile->Write(); 00425 // outFile->Close(); 00426 00427 00428 00429 return 0; 00430 } 00431 00432 // Int_t findLastZC(TGraph *graph, Double_t period, Double_t *lastZC){ 00433 // Double_t lastZCEst=0, phase=0, phase0=0, *preWrap[2]; 00434 // Int_t noZCs=0; 00435 00436 // Int_t noSamples=graph->GetN(); 00437 // preWrap[0]=graph->GetX(); 00438 // preWrap[1]=graph->GetY(); 00439 00440 // if(fDebug>1) printf("Finding first ZC\n"); 00441 00442 // for(Int_t Sample=noSamples-1; Sample>0; --Sample){ 00443 // if(preWrap[1][Sample-1]<0&&preWrap[1][Sample]>0){ 00444 // Double_t x1=preWrap[0][Sample-1]; 00445 // Double_t x2=preWrap[0][Sample]; 00446 // Double_t y1=preWrap[1][Sample-1]; 00447 // Double_t y2=preWrap[1][Sample]; 00448 00449 // // time=(preWrap[1][Sample-1])*(preWrap[0][Sample-1]-preWrap[0][Sample])/(preWrap[1][Sample]-preWrap[1][Sample-1])+preWrap[0][Sample-1]; 00450 00451 // phase=y1*(x1-x2)/(y2-y1)+(x1); 00452 00453 // if(fDebug>1) printf("zc no %i found at %f, relocated to %f\n", noZCs+1, phase, phase+noZCs*period); 00454 // lastZCEst+=phase+noZCs*period; 00455 // noZCs++; 00456 00457 // } 00458 // } 00459 00460 // if(fDebug>1) printf("Average value is %f\n", lastZCEst/noZCs); 00461 00462 // if(noZCs){ 00463 // *lastZC=lastZCEst/noZCs; 00464 // return 0; 00465 // } 00466 // return -1; 00467 // } 00468 00469 // Int_t findFirstZC(TGraph *graph, Double_t period, Double_t *firstZC){ 00470 // Double_t firstZCEst=0, phase=0, lastPhase=0, *postWrap[2]; 00471 // Int_t noZCs=0, noSamples=0; 00472 00473 // noSamples=graph->GetN(); 00474 // postWrap[0]=graph->GetX(); 00475 // postWrap[1]=graph->GetY(); 00476 00477 // if(fDebug>1) printf("Finding first ZC\n"); 00478 00479 // for(Int_t Sample=0; Sample<noSamples-1; ++Sample){ 00480 // if(postWrap[1][Sample]<0&&postWrap[1][Sample+1]>0){ 00481 // Double_t x1=postWrap[0][Sample]; 00482 // Double_t x2=postWrap[0][Sample+1]; 00483 // Double_t y1=postWrap[1][Sample]; 00484 // Double_t y2=postWrap[1][Sample+1]; 00485 00486 // // time=(postWrap[1][Sample])*(postWrap[0][Sample]-postWrap[0][Sample+1])/(postWrap[1][Sample+1]-postWrap[1][Sample])+postWrap[0][Sample]; 00487 00488 // phase=y1*(x1-x2)/(y2-y1)+(x1); 00489 00490 // if(fDebug>1) printf("zc no %i found at %f, relocated to %f phase-lastPhase %f\n", noZCs+1, phase, phase-noZCs*period, phase-lastPhase); 00491 00492 // firstZCEst+=phase-noZCs*period; 00493 // noZCs++; 00494 // lastPhase=phase; 00495 // } 00496 // } 00497 00498 // if(fDebug>1) printf("Average value is %f\n", firstZCEst/noZCs); 00499 00500 // if(noZCs){ 00501 // *firstZC=firstZCEst/noZCs; 00502 // return 0; 00503 // } 00504 // return -1; 00505 // } 00506 00507 void loadPedestals(){ 00508 if(!gotPedFile) { 00509 char calibDir[FILENAME_MAX]; 00510 char *calibEnv=getenv("ARA_CALIB_DIR"); 00511 if(!calibEnv) { 00512 char *utilEnv=getenv("ARA_UTIL_INSTALL_DIR"); 00513 if(!utilEnv) 00514 sprintf(calibDir,"calib"); 00515 else 00516 sprintf(calibDir,"%s/share/araCalib",utilEnv); 00517 } 00518 else { 00519 strncpy(calibDir,calibEnv,FILENAME_MAX); 00520 } 00521 sprintf(pedFile,"%s/peds_1286989711.394723.dat",calibDir); 00522 } 00523 FullLabChipPedStruct_t peds; 00524 gzFile inPed = gzopen(pedFile,"r"); 00525 if( !inPed ){ 00526 fprintf(stderr,"Failed to open pedestal file %s.\n",pedFile); 00527 return; 00528 } 00529 00530 int nRead = gzread(inPed,&peds,sizeof(FullLabChipPedStruct_t)); 00531 if( nRead != sizeof(FullLabChipPedStruct_t)){ 00532 int numErr; 00533 fprintf(stderr,"Error reading pedestal file %s; %s\n",pedFile,gzerror(inPed,&numErr)); 00534 gzclose(inPed); 00535 return; 00536 } 00537 00538 int chip,chan,samp; 00539 for(chip=0;chip<LAB3_PER_ICRR;++chip) { 00540 for(chan=0;chan<CHANNELS_PER_LAB3;++chan) { 00541 int chanIndex = chip*CHANNELS_PER_LAB3+chan; 00542 for(samp=0;samp<MAX_NUMBER_SAMPLES_LAB3;++samp) { 00543 pedestalData[chip][chan][samp]=peds.chan[chanIndex].pedMean[samp]; 00544 } 00545 } 00546 } 00547 gzclose(inPed); 00548 } 00549 00550 00551 //Modified versions of the findLast and findFirst ZC functions 00552 00553 Int_t findLastZC(TGraph *graph, Double_t period, Double_t *lastAvZC){ 00554 Double_t *preWrap[2], thisZC=0, lastZC=0, meanZC=0; 00555 Int_t noZCs=0; 00556 Int_t noSamples=graph->GetN(); 00557 preWrap[0]=graph->GetX(); 00558 preWrap[1]=graph->GetY(); 00559 00560 if(fDebug>1) printf("Finding last ZC\n"); 00561 00562 for(Int_t Sample=noSamples-1; Sample>0; --Sample){ 00563 if(preWrap[1][Sample-1]<0&&preWrap[1][Sample]>0){ 00564 Double_t x1=preWrap[0][Sample-1]; 00565 Double_t x2=preWrap[0][Sample]; 00566 Double_t y1=preWrap[1][Sample-1]; 00567 Double_t y2=preWrap[1][Sample]; 00568 00569 thisZC=y1*(x1-x2)/(y2-y1)+(x1); 00570 if(!noZCs) lastZC=thisZC; 00571 while(TMath::Abs((lastZC-thisZC)-period)<TMath::Abs(lastZC-thisZC)) thisZC+=period; 00572 meanZC+=thisZC; 00573 noZCs++; 00574 } 00575 } 00576 00577 if(fDebug>1) printf("Average value is %f\n", meanZC/noZCs); 00578 00579 if(noZCs){ 00580 *lastAvZC=meanZC/noZCs; 00581 return 0; 00582 } 00583 return -1; 00584 } 00585 00586 Int_t findFirstZC(TGraph *graph, Double_t period, Double_t *firstAvZC){ 00587 Double_t *postWrap[2], thisZC=0, meanZC=0, firstZC=0; 00588 Int_t noZCs=0; 00589 00590 Int_t noSamples=graph->GetN(); 00591 postWrap[0]=graph->GetX(); 00592 postWrap[1]=graph->GetY(); 00593 00594 if(fDebug>1) printf("Finding first ZC\n"); 00595 00596 for(Int_t Sample=0; Sample<noSamples-1; ++Sample){ 00597 if(postWrap[1][Sample]<0&&postWrap[1][Sample+1]>0){ 00598 Double_t x1=postWrap[0][Sample]; 00599 Double_t x2=postWrap[0][Sample+1]; 00600 Double_t y1=postWrap[1][Sample]; 00601 Double_t y2=postWrap[1][Sample+1]; 00602 00603 thisZC=y1*(x1-x2)/(y2-y1)+(x1); 00604 00605 if(!noZCs) firstZC=thisZC; 00606 00607 while(TMath::Abs((firstZC-thisZC)+period)<TMath::Abs(firstZC-thisZC)) thisZC-=period; 00608 meanZC+=thisZC; 00609 noZCs++; 00610 00611 } 00612 } 00613 00614 if(fDebug>1) printf("Average value is %f\n", meanZC/noZCs); 00615 00616 if(noZCs){ 00617 *firstAvZC=meanZC/noZCs; 00618 return 0; 00619 } 00620 return -1; 00621 }
Generated on Tue Jul 16 16:58:02 2013 for ARA ROOT v3.10 Software by
