calibration/ATRI/estimate_sampling_speed.cxx
00001 00002 00003 00004 00005 00006 00007 00008 00009 #include <iostream> 00010 #include <fstream> 00011 #include <stdlib.h> 00012 00013 //Event Reader Includes 00014 #include "UsefulAtriStationEvent.h" 00015 #include "RawAtriStationEvent.h" 00016 #include "AraEventCalibrator.h" 00017 #include "araSoft.h" 00018 00019 //ROOT Includes 00020 #include "TCanvas.h" 00021 #include "TTree.h" 00022 #include "TFile.h" 00023 #include "TH1.h" 00024 00025 TGraph* zeroMean(TGraph*); 00026 TGraph *getBlockGraph(TGraph*, Int_t); 00027 TGraph* getHalfGraph(TGraph*, Int_t); 00028 00029 int estimate_sampling_speed(char *runFile, char *outFile, char* pedFile, Double_t freq, Int_t dda, Int_t chan, bool debug=false); 00030 00031 using namespace std; 00032 int main(int argc, char **argv) 00033 { 00034 if(argc<7) { 00035 std::cerr << "Usage: " << argv[0] << " <runFile> <outFile> <pedFile> <freq in GHz> <dda> <chan>\n"; 00036 return 1; 00037 } 00038 return estimate_sampling_speed(argv[1], argv[2], argv[3], atof(argv[4]),atoi(argv[5]),atoi(argv[6]), atoi(argv[7])); 00039 } 00040 00041 int estimate_sampling_speed(char *runFile, char *outFile, char *pedFile, Double_t frequency,Int_t dda, Int_t chan, bool debug) 00042 { 00043 printf("RunFile %s Frequency %f dda %i channel %i\n", runFile, frequency, dda, chan); 00044 00045 Int_t chanIndex=chan+RFCHAN_PER_DDA*dda; 00046 Double_t period = 1./frequency; 00047 Double_t average_bin_width=0; 00048 00049 //1.Set up the input and output files 00050 char inName[180]; 00051 sprintf(inName,"%s", runFile); 00052 00053 TFile *fp = new TFile(inName); 00054 if(!fp) { 00055 std::cerr << "Can't open file\n"; 00056 return -1; 00057 } 00058 TTree *eventTree = (TTree*) fp->Get("eventTree"); 00059 if(!eventTree) { 00060 std::cerr << "Can't find eventTree\n"; 00061 return -1; 00062 } 00063 RawAtriStationEvent *evPtr=0; 00064 eventTree->SetBranchAddress("event",&evPtr); 00065 Int_t numEntries = eventTree->GetEntries(); 00066 Int_t starEvery = numEntries / 80; 00067 00068 //Now set the pedfile 00069 Int_t stationId=0; 00070 eventTree->GetEntry(0); 00071 stationId= evPtr->stationId; 00072 // std::cerr << "stationId " << stationId << "\n"; 00073 AraEventCalibrator *calib = AraEventCalibrator::Instance(); 00074 calib->setAtriPedFile(pedFile, stationId);//FIXME 00075 00076 char histName[180]; 00077 sprintf(histName, "%s", outFile); 00078 TFile *histFile = new TFile(histName,"RECREATE"); 00079 00080 //2. Set up the trees and variables 00081 00082 TTree *binWidthsTree = new TTree("binWidthsTree", "binWidthsTree"); 00083 Int_t capArray=0, half=0, sample=0, noZCs[2]={0}, noSWs[2]={0}; 00084 Int_t block, thisCapArray; 00085 Double_t binWidth=0; 00086 binWidthsTree->Branch("capArray", &capArray, "capArray/I"); 00087 binWidthsTree->Branch("half", &half, "half/I"); 00088 binWidthsTree->Branch("sample", &sample, "sample/I"); 00089 binWidthsTree->Branch("binWidth", &binWidth, "binWidth/D"); 00090 Int_t numEvents[2]={0}; //used to scale the bin widths 00091 Double_t zcCount[2][2][32]={{{0}}}; 00092 00093 TH1F *histBinWidth[2][2]; 00094 for(int capArray=0;capArray<2;capArray++) { 00095 for(int half=0;half<2;half++) { 00096 sprintf(histName,"histBinWidth%d_%d",capArray,half); 00097 histBinWidth[capArray][half] = new TH1F(histName,histName,SAMPLES_PER_BLOCK/2,-0.5,(SAMPLES_PER_BLOCK/2)-0.5); 00098 } 00099 } 00100 00101 00102 for(int entry=0;entry<numEntries;entry++){ 00103 if(entry%starEvery==0) std::cerr <<"*"; 00104 eventTree->GetEntry(entry); 00105 UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed); 00106 capArray = evPtr->blockVec[dda].getCapArray(); //capArray of first block 00107 00108 TGraph *gr = realEvent.getGraphFromElecChan(chanIndex); 00109 TGraph *grZeroMean = zeroMean(gr); 00110 Int_t numSamples = grZeroMean->GetN(); 00111 Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK; 00112 00113 for(block=1; block<numBlocks; block++){ //FIXME -- Only use blocks > 0 00114 if(block%2) thisCapArray=1-capArray; 00115 else thisCapArray=capArray; 00116 TGraph *grBlock = getBlockGraph(grZeroMean, block); 00117 numEvents[thisCapArray]++; 00118 for(half=0;half<2;half++){ 00119 TGraph *grHalf = getHalfGraph(grBlock, half); 00120 Double_t *yVals = grHalf->GetY(); 00121 for(sample=0;sample<(SAMPLES_PER_BLOCK)/2-1;sample++){ 00122 Double_t val1 = yVals[sample]; 00123 Double_t val2 = yVals[sample+1]; 00124 if(val1<0 && val2>0){ 00125 histBinWidth[thisCapArray][half]->Fill(sample); 00126 zcCount[thisCapArray][half][sample]+=1; 00127 } 00128 else if(val1>0 && val2<0){ 00129 histBinWidth[thisCapArray][half]->Fill(sample); 00130 zcCount[thisCapArray][half][sample]+=1; 00131 } 00132 else if(val1==0 || val2==0){ 00133 histBinWidth[thisCapArray][half]->Fill(sample, 0.5); 00134 zcCount[thisCapArray][half][sample]+=0.5; 00135 } 00136 }//sample 00137 if(grHalf) delete grHalf; 00138 }//half 00139 if(grBlock) delete grBlock; 00140 }//block 00141 00142 delete gr; 00143 delete grZeroMean; 00144 00145 }//entry 00146 std::cerr << "\n"; 00147 00148 //Scale the ZC and calculate the bin widths 00149 for(capArray=0;capArray<2;capArray++){ 00150 for(half=0;half<2;half++){ 00151 histBinWidth[capArray][half]->Scale(1./numEvents[capArray]); 00152 histBinWidth[capArray][half]->Scale(0.5*period); 00153 histBinWidth[capArray][half]->Write(); 00154 }//half 00155 }//capArray 00156 00157 for(capArray=0;capArray<2;capArray++){ 00158 for(half=0;half<2;half++){ 00159 for(sample=0;sample<(SAMPLES_PER_BLOCK/2)-1;sample++){ //we only calculate 31 intersample times 00160 binWidth = zcCount[capArray][half][sample]/numEvents[capArray]; 00161 binWidth = binWidth*0.5*period; 00162 average_bin_width += binWidth/(2*(SAMPLES_PER_BLOCK-1)); 00163 // printf("capArray %i half %i sample %i binWidth %f\n", capArray, half, sample, binWidth); 00164 binWidthsTree->Fill(); 00165 }//sample 00166 }//half 00167 }//capArray 00168 00169 00170 // save_inter_sample_times(outFile, dda, chan); 00171 00172 00173 binWidthsTree->AutoSave(); 00174 histFile->Write(); 00175 00176 //We have calculated the binWidths between adjacent event or adjacent odd samples, so the nominal 00177 //inter-sample width is half this 00178 printf("Average binWidth %f ns\tAverage sampling speed %f GSamples/s\n", 0.5*average_bin_width, 2./average_bin_width); 00179 00180 cout << "\n"; 00181 return 0; 00182 } 00183 00184 TGraph* zeroMean(TGraph* gr){ 00185 Double_t *xVals=gr->GetX(); 00186 Double_t *yVals=gr->GetY(); 00187 Int_t maxN = gr->GetN(); 00188 00189 if(maxN<1) return NULL; 00190 00191 Double_t mean=0; 00192 for(int i=0;i<maxN; i++){ 00193 mean+=yVals[i]/maxN; 00194 } 00195 Double_t *yValsNew = new Double_t[maxN]; 00196 for(int i=0;i<maxN; i++){ 00197 yValsNew[i]=yVals[i]-mean; 00198 } 00199 TGraph *grZeroMeaned = new TGraph(maxN, xVals, yValsNew); 00200 00201 delete yValsNew; 00202 return grZeroMeaned; 00203 00204 } 00205 00206 TGraph *getBlockGraph(TGraph *fullEventGraph, Int_t block){ 00207 Int_t numSamples = fullEventGraph->GetN(); 00208 Int_t numBlocks = numSamples / SAMPLES_PER_BLOCK; 00209 if(block > numBlocks) return NULL; 00210 Double_t *fullX = fullEventGraph->GetX(); 00211 Double_t *fullY = fullEventGraph->GetY(); 00212 Double_t *blockX = new Double_t[SAMPLES_PER_BLOCK]; 00213 Double_t *blockY = new Double_t[SAMPLES_PER_BLOCK]; 00214 for(int sample=0;sample<SAMPLES_PER_BLOCK; sample++){ 00215 blockY[sample] = fullY[sample + block*SAMPLES_PER_BLOCK]; 00216 blockX[sample] = fullX[sample + block*SAMPLES_PER_BLOCK]; 00217 } 00218 TGraph *blockGraph = new TGraph(SAMPLES_PER_BLOCK, blockX, blockY); 00219 delete blockX; 00220 delete blockY; 00221 return blockGraph; 00222 } 00223 00224 TGraph *getHalfGraph(TGraph *fullGraph, Int_t half){ 00225 Int_t numSamples = fullGraph->GetN(); 00226 Double_t *xFull = fullGraph->GetX(); 00227 Double_t *yFull = fullGraph->GetY(); 00228 Double_t *newX = new Double_t[numSamples/2]; 00229 Double_t *newY = new Double_t[numSamples/2]; 00230 00231 for(Int_t sample=0;sample<numSamples;sample++){ 00232 if(sample%2!=half) continue; 00233 newX[sample/2]=xFull[sample]; 00234 newY[sample/2]=yFull[sample]; 00235 // printf("half %i sample/2 %i sample %i\n", half, sample/2, sample); 00236 00237 } 00238 TGraph *halfGraph = new TGraph(numSamples/2, newX, newY); 00239 delete newX; 00240 delete newY; 00241 return halfGraph; 00242 00243 }
Generated on Mon Mar 18 16:00:44 2013 for ARA ROOT v3.4 Software by
