ARA ROOT v3.10 Software

debugging/operating_parameters/vadj/estimate_sampling_speed_runList.cxx

00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include <iostream>
00010 #include <fstream>
00011 
00012 //Event Reader Includes
00013 #include "UsefulAtriStationEvent.h"
00014 #include "RawAtriStationEvent.h"
00015 #include "AraEventCalibrator.h"
00016 #include "araSoft.h"
00017 
00018 //ROOT Includes
00019 #include "TCanvas.h"
00020 #include "TTree.h"
00021 #include "TFile.h"
00022 #include "TH1.h"
00023 
00024 TGraph* zeroMean(TGraph*);
00025 TGraph *getBlockGraph(TGraph*, Int_t);
00026 TGraph* getHalfGraph(TGraph*, Int_t);
00027 
00028 Double_t estimate_sampling_speed(char *, char*, Int_t, Int_t, Double_t);
00029 
00030 using namespace std;
00031 int main(int argc, char **argv)
00032 {
00033   if(argc<2) {
00034     std::cerr << "Usage: " << argv[0] << " <runList>\n";
00035     return 1;
00036   }
00037 
00038   printf("runList %s\n", argv[1]);
00039 
00040   std::fstream runDescriptionFile(argv[1]);
00041 
00042   if(!runDescriptionFile){
00043     printf("Unable to open runList\n");
00044     return -1;
00045   }
00046   char *runFileBaseName = new char[100];
00047   char *pedFileBaseName = new char[100];
00048   char *outFileName = new char[200];
00049   Int_t runNumber=0, pedNumber=0, dda=0, channel=0, vadj=0;
00050   Double_t frequency=0;
00051   Double_t speedDda[4][10]={{0}};
00052   Double_t vadjDda[4][10]={{0}};
00053   Int_t loopDda[4]={0};
00054 
00055 
00056   runDescriptionFile >> runFileBaseName >> pedFileBaseName >> outFileName;
00057   while(runDescriptionFile >> runNumber >> pedNumber >> vadj >> frequency >> channel >> dda){
00058 
00059     char runFileName[100], pedFileName[100];
00060     sprintf(runFileName, "%s/run%i/event%i.root", runFileBaseName, runNumber, runNumber);
00061     sprintf(pedFileName, "%s/run_000%i/pedestalValues.run000%i.dat", pedFileBaseName, pedNumber, pedNumber);
00062 
00063     // printf("run %s ped %s\n", runFileName, pedFileName);
00064     //    printf("vadj %i channel %i dda %i frequency %f\n", vadj, channel, dda, frequency);
00065 
00066     Double_t speed = estimate_sampling_speed(runFileName, pedFileName, dda, channel, frequency);
00067     speedDda[dda][loopDda[dda]] = speed;
00068     vadjDda[dda][loopDda[dda]] = vadj;
00069     loopDda[dda]++;
00070 
00071   }
00072   
00073   TFile *fpOut = new TFile(outFileName, "RECREATE");
00074   char grName[100];
00075   char grTitle[100];
00076   for(dda=0;dda<4;dda++){
00077 
00078     TGraph *grDda = new TGraph(loopDda[dda], speedDda[dda],vadjDda[dda]);
00079     sprintf(grName, "grDda%i", dda);
00080     sprintf(grTitle, "DDA%i", dda);
00081     grDda->SetName(grName);
00082     grDda->SetTitle(grTitle);
00083     grDda->GetXaxis()->SetTitle("speed (GS/s)");
00084     grDda->GetYaxis()->SetTitle("vadj DAC");
00085     grDda->Write();
00086     delete    grDda;
00087 
00088   }
00089   
00090   fpOut->Write();
00091 
00092 
00093   return 0;
00094 
00095   //  return estimate_sampling_speed(argv[1]);
00096 }
00097 
00098 Double_t estimate_sampling_speed(char *runFile, char* pedFile, Int_t dda, Int_t chan, Double_t frequency)
00099 {
00100   Int_t chanIndex=chan+RFCHAN_PER_DDA*dda;
00101   Double_t period = 1./frequency;
00102   Double_t average_bin_width=0;
00103   
00104   //1.Set up the input and output files
00105   char inName[180];
00106   sprintf(inName,"%s", runFile);
00107 
00108   TFile *fp = new TFile(inName);
00109   if(!fp) {
00110     std::cerr << "Can't open file\n";
00111     return -1;
00112   }
00113   TTree *eventTree = (TTree*) fp->Get("eventTree");
00114   if(!eventTree) {
00115     std::cerr << "Can't find eventTree\n";
00116     return -1;
00117   }
00118   RawAtriStationEvent *evPtr=0;
00119   eventTree->SetBranchAddress("event",&evPtr);
00120   Int_t numEntries = eventTree->GetEntries();
00121   Int_t starEvery = numEntries / 80;
00122 
00123   //Now set the pedfile
00124   Int_t stationId=0;
00125   eventTree->GetEntry(0);
00126   stationId= evPtr->stationId;
00127   //  std::cerr << "stationId " << stationId << "\n";
00128   AraEventCalibrator *calib = AraEventCalibrator::Instance();
00129   calib->setAtriPedFile(pedFile, stationId);//FIXME
00130 
00131   //2. Set up the trees and variables
00132 
00133   Int_t capArray=0, half=0, sample=0, noZCs[2]={0}, noSWs[2]={0};
00134   Int_t block, thisCapArray;
00135   Double_t binWidth=0;
00136   Int_t numEvents[2]={0}; //used to scale the bin widths
00137   Double_t zcCount[2][2][32]={{{0}}};
00138 
00139     
00140   for(int entry=0;entry<numEntries;entry++){
00141     if(entry%starEvery==0) std::cerr <<"*";
00142     eventTree->GetEntry(entry);
00143     UsefulAtriStationEvent realEvent(evPtr, AraCalType::kJustPed);
00144     capArray = evPtr->blockVec[dda].getCapArray(); //capArray of first block
00145     
00146     TGraph *gr = realEvent.getGraphFromElecChan(chanIndex);
00147     TGraph *grZeroMean = zeroMean(gr);
00148     Int_t numSamples = grZeroMean->GetN();
00149     Int_t numBlocks = numSamples/SAMPLES_PER_BLOCK;
00150 
00151     for(block=1; block<numBlocks; block++){ //FIXME -- Only use blocks > 0
00152       if(block%2) thisCapArray=1-capArray;
00153       else thisCapArray=capArray;
00154       TGraph *grBlock = getBlockGraph(grZeroMean, block);
00155       numEvents[thisCapArray]++;
00156       for(half=0;half<2;half++){
00157         TGraph *grHalf = getHalfGraph(grBlock, half);
00158         Double_t *yVals = grHalf->GetY();
00159         for(sample=0;sample<(SAMPLES_PER_BLOCK)/2-1;sample++){
00160           Double_t val1 = yVals[sample];
00161           Double_t val2 = yVals[sample+1];
00162           if(val1<0 && val2>0){
00163             zcCount[thisCapArray][half][sample]+=1;
00164           }
00165           else if(val1>0 && val2<0){
00166             zcCount[thisCapArray][half][sample]+=1;
00167           }
00168           else if(val1==0 || val2==0){
00169             zcCount[thisCapArray][half][sample]+=0.5;
00170           }
00171         }//sample
00172         if(grHalf) delete grHalf;
00173       }//half      
00174       if(grBlock) delete grBlock;
00175     }//block
00176     
00177     delete gr;
00178     delete grZeroMean;
00179     
00180   }//entry
00181   std::cerr << "\n";
00182 
00183   for(capArray=0;capArray<2;capArray++){
00184     for(half=0;half<2;half++){
00185       for(sample=0;sample<(SAMPLES_PER_BLOCK/2)-1;sample++){ //we only calculate 31 intersample times
00186         binWidth = zcCount[capArray][half][sample]/numEvents[capArray];
00187         binWidth = binWidth*0.5*period;
00188         average_bin_width += binWidth/(2*(SAMPLES_PER_BLOCK-1));
00189 
00190       }//sample
00191     }//half
00192   }//capArray
00193 
00194   //  printf("Average binWidth %f ns\tAverage sampling speed %f GSamples/s\n", 0.5*average_bin_width, 2./average_bin_width);
00195   
00196   cout << "\n";
00197   return 2./average_bin_width;
00198 }
00199 
00200 TGraph* zeroMean(TGraph* gr){
00201   Double_t *xVals=gr->GetX();
00202   Double_t *yVals=gr->GetY();
00203   Int_t maxN = gr->GetN();
00204 
00205   if(maxN<1) return NULL;
00206 
00207   Double_t mean=0;
00208   for(int i=0;i<maxN; i++){
00209     mean+=yVals[i]/maxN;
00210   }
00211   Double_t *yValsNew = new Double_t[maxN];
00212   for(int i=0;i<maxN; i++){
00213     yValsNew[i]=yVals[i]-mean;
00214   }
00215   TGraph *grZeroMeaned = new TGraph(maxN, xVals, yValsNew);
00216   
00217   delete yValsNew;
00218   return grZeroMeaned;
00219   
00220 }
00221 
00222 TGraph *getBlockGraph(TGraph *fullEventGraph, Int_t block){
00223   Int_t numSamples = fullEventGraph->GetN();
00224   Int_t numBlocks = numSamples / SAMPLES_PER_BLOCK;
00225   if(block > numBlocks) return NULL;
00226   Double_t *fullX = fullEventGraph->GetX();
00227   Double_t *fullY = fullEventGraph->GetY();  
00228   Double_t *blockX = new Double_t[SAMPLES_PER_BLOCK];
00229   Double_t *blockY = new Double_t[SAMPLES_PER_BLOCK];
00230   for(int sample=0;sample<SAMPLES_PER_BLOCK; sample++){
00231     blockY[sample] = fullY[sample + block*SAMPLES_PER_BLOCK];
00232     blockX[sample] = fullX[sample + block*SAMPLES_PER_BLOCK];
00233   }
00234   TGraph *blockGraph = new TGraph(SAMPLES_PER_BLOCK, blockX, blockY);
00235   delete blockX;
00236   delete blockY;
00237   return blockGraph;
00238 }
00239 
00240 TGraph *getHalfGraph(TGraph *fullGraph, Int_t half){
00241   Int_t numSamples = fullGraph->GetN();
00242   Double_t *xFull  = fullGraph->GetX();
00243   Double_t *yFull  = fullGraph->GetY();
00244   Double_t *newX = new Double_t[numSamples/2];
00245   Double_t *newY = new Double_t[numSamples/2];
00246 
00247   for(Int_t sample=0;sample<numSamples;sample++){
00248     if(sample%2!=half) continue;
00249     newX[sample/2]=xFull[sample];
00250     newY[sample/2]=yFull[sample];
00251     //    printf("half %i sample/2 %i sample %i\n", half, sample/2, sample);
00252 
00253   }
00254   TGraph *halfGraph = new TGraph(numSamples/2, newX, newY);
00255   delete newX;
00256   delete newY;
00257   return halfGraph;
00258   
00259 }

Generated on Tue Jul 16 16:58:02 2013 for ARA ROOT v3.10 Software by doxygen 1.4.7