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