DispHistogram.cpp

00001 #include "../../include/ScanResultWriter/dataTypes.h"
00002 
00003 unsigned short GetWord(FILE* file){
00004 
00005   unsigned char  lo_char, hi_char;
00006   unsigned short word;
00007 
00008   if(file==NULL){
00009     printf("bad file\n");
00010     return 0;
00011   }
00012 
00013   // Read two bytes
00014   lo_char = fgetc(file);
00015   hi_char = fgetc(file);
00016 
00017   if(lo_char == EOF || hi_char == EOF) {
00018     printf("end of file\n");
00019     return 0;
00020   }
00021 
00022   // Swap bytes to form 16 bit word
00023   word = (hi_char<<8)+lo_char;
00024 
00025   return word;
00026 }
00027 
00028 unsigned long GetLWord(FILE* file){
00029 
00030   unsigned char  lo_char, hi_char;
00031   unsigned char  lo_char2, hi_char2;
00032   unsigned long word;
00033 
00034   if(file==NULL){
00035     printf("bad file\n");
00036     return 0;
00037   }
00038 
00039   // Read four bytes
00040   lo_char = fgetc(file);
00041   hi_char = fgetc(file);
00042   lo_char2 = fgetc(file);
00043   hi_char2 = fgetc(file);
00044 
00045   if(lo_char == EOF || hi_char == EOF) {
00046     printf("end of file\n");
00047     return 0;
00048   }
00049 
00050   // Swap bytes to form 16 bit word
00051   word = (hi_char2<<24)+(lo_char2<<16) + (hi_char<<8)+lo_char;
00052 
00053   return word;
00054 }
00055 
00056 void DispHistogram(char* infname, int occ=0) { // , float start, float step, int bursts) {
00057 
00058   // Convert ROD histograms to SCTDAQ format
00059 
00060   char htitle[128];
00061   char hname[32];
00062 
00063   int i,j;
00064   unsigned short header[8];
00065   unsigned short trailer[8];
00066   unsigned long *data; // [1024*2];
00067   unsigned short chan,chip;
00068 
00069   TH1F* h_scan_evcnt;
00070   TH1F* h_scan_errcnt;
00071   TH2F* h_scan0;
00072   TH2F* h_scan1;
00073 
00074   TH2F* h_occCount[12];
00075 
00076   FILE* infile;
00077   //  TFile* outfile;
00078 
00079   infile = fopen(infname,"rb");
00080 
00081   if(infile==NULL) {
00082     printf("Could not open file %s\n",infname);
00083     return;
00084   }  
00085 
00086   int dontSkip = 0;
00087 
00088   short version = GetWord(infile);          // version
00089   short length = GetWord(infile);
00090   dontSkip += 1;
00091 
00092   int run = GetLWord(infile);
00093   int scan = GetLWord(infile);
00094   dontSkip += 2;
00095 
00096   char moduleName[16];
00097   for(int i=0; i<16; i++) {
00098     moduleName[i] = fgetc(infile);
00099   }
00100   dontSkip +=4;
00101 
00102   if(version == 1 || version == 2 ) {
00103     char startTime[16];
00104     for(int i=0; i<16; i++) {
00105       startTime[i] = fgetc(infile);
00106     }
00107     dontSkip +=4;
00108     printf("Started: %s\n", startTime);
00109 
00110     char endTime[16];
00111     for(int i=0; i<16; i++) {
00112       endTime[i] = fgetc(infile);
00113     }
00114     dontSkip +=4;
00115 
00116     printf("Finished: %s\n", endTime);
00117   }
00118 
00119   short scanType = GetWord(infile);           // scanType
00120   short nBins = GetWord(infile);
00121   dontSkip ++;
00122 
00123   int size = GetLWord(infile);
00124   cout << "size: " << size << endl;
00125 
00126   dontSkip ++;
00127   short type = GetWord(infile);
00128   short width = GetWord(infile);
00129 
00130   if(!(width == 32 || width == 16)) {
00131     cout << "Bad data width: " << width << endl;
00132     return;
00133   } else {
00134     cout << "Data width: " << width << endl;
00135   }
00136 
00137   int histWidth;
00138 
00139   if(type == SR_DT_RAWHIST) {
00140     histWidth = size / (width==32?2:1) / 2 / nBins;
00141     data = new unsigned long[histWidth*2];
00142   } else {
00143     histWidth = 768;
00144     data = new unsigned long[1024*2];
00145   }
00146 
00147 
00148   dontSkip ++;
00149 
00150   int bursts = nBins;
00151 
00152   dontSkip +=4;            // Dont skip pointers
00153 
00154   for(int i=0; i<length/4-dontSkip; i++) {
00155     unsigned long dummy = GetLWord(infile);
00156   }
00157 
00158   unsigned long pointer = GetLWord(infile);     // Pointer to thresholds
00159   unsigned long pEvents = GetLWord(infile);
00160   unsigned long pErrors = GetLWord(infile);
00161   unsigned long pData = GetLWord(infile);       // Pointer to data
00162 
00163   for(int i=0; i<(pointer-length)/4; i++) {   // Presumably 0
00164     unsigned long dummy = GetLWord(infile);
00165   }
00166 
00167   printf("nbins = %d\n", nBins);
00168   float *threshold = new float[bursts];
00169   unsigned long *eventCount = new unsigned long[bursts];
00170   unsigned long *errorCount = new unsigned long[bursts];
00171 
00172   // Scan points
00173   for(int i=0; i<bursts; i++) {
00174     unsigned long word = GetLWord(infile);
00175     threshold[i] = *((float*)(&word));
00176   }
00177 
00178   // Event counts
00179   for(int i=0; i<bursts; i++) {
00180     unsigned long events = GetLWord(infile);
00181     eventCount[i] = events;
00182   }
00183 
00184   // Error counts
00185   for(int i=0; i<bursts; i++) {
00186     unsigned long errors = GetLWord(infile);
00187     errorCount[i] = errors;
00188   }
00189 
00190   double hist_start;
00191   double hist_stop;
00192 
00193   //  hist_start = start-(step/2);
00194   //  hist_stop =  start+(112*step)+(step/2);
00195   float step = threshold[1] - threshold[0];
00196   if(threshold[0] < threshold[bursts-1]) {
00197     hist_start = threshold[0] - step/2.; // start;
00198     hist_stop =  threshold[bursts-1] + step/2.; // start+(bursts*step);
00199   } else {
00200     hist_start =  threshold[bursts-1] + step/2.; // start+(bursts*step);
00201     hist_stop = threshold[0] - step/2.; // start;
00202   }
00203   printf("start %3.2f stop %3.2f\n",hist_start, hist_stop);
00204 
00205   sprintf(htitle,"ROD Events Requested");
00206   sprintf(hname,"h_scan_evcnt");
00207   h_scan_evcnt = new TH1F(hname, htitle, bursts, hist_start, hist_stop);
00208   h_scan_evcnt->SetFillColor(46);
00209 
00210   sprintf(htitle,"ROD Errors Counted");
00211   sprintf(hname,"h_scan_errcnt");
00212   h_scan_errcnt = new TH1F(hname, htitle, bursts, hist_start, hist_stop);
00213   h_scan_errcnt->SetFillColor(46);
00214 
00215   sprintf(htitle, "%s Stream 0 THRESHOLD Scan", moduleName);
00216   sprintf(hname,"h_scan0_0");
00217 
00218   h_scan0 = new TH2F(hname,htitle, histWidth, 0.5, histWidth + 0.5, bursts, hist_start, hist_stop);
00219   h_scan0->SetXTitle("Channel Number");
00220   h_scan0->SetYTitle("THRESHOLD (mV)");
00221 
00222   sprintf(htitle, "%s Stream 1 THRESHOLD Scan", moduleName);
00223   sprintf(hname,"h_scan1_0");
00224   h_scan1 = new TH2F(hname,htitle, histWidth, 0.5, histWidth + 0.5, bursts, hist_start, hist_stop);
00225   h_scan1->SetXTitle("Channel Number");
00226   h_scan1->SetYTitle("THRESHOLD (mV)");
00227 
00228   printf("Creating chip occupancy histos\n");
00229 
00230   Double_t xBins[1+128/4 + 1];
00231   xBins[0] = 0;
00232   for(int i=0; i<128/4 + 1; i++) {
00233     xBins[i+1] = i*4+1.0;
00234   }
00235 
00236   for(int c=0; c<12; c++) {
00237     sprintf(htitle, "%s Chip %d Occupancy counts", moduleName, c);
00238     sprintf(hname,"h_occCount_%d", c);
00239     // char *name,char *title,Int_t nbinsx,const Double_t *xbins ,Int_t nbinsy,Axis_t ylow,Axis_t yup
00240     h_occCount[c] = new TH2F(hname,htitle, 33, xBins, bursts, hist_start, hist_stop);
00241     h_occCount[c]->SetXTitle("Channel count");
00242     h_occCount[c]->SetYTitle("THRESHOLD (mV)");
00243   }
00244 
00245   printf("Filling event counts...\n");
00246 
00247   for(j=0;j<bursts;j++) {
00248     h_scan_evcnt->Fill(threshold[j],eventCount[j]);
00249     h_scan_errcnt->Fill(threshold[j],errorCount[j]);
00250   }
00251 
00252   int slice = 1;
00253   int chipNumber;
00254   Axis_t channelAxis;
00255   int value;
00256   if(slice) {
00257     printf("Building slice histogram...\n");
00258 
00259     for(j=0;j<bursts;j++){
00260       int words, skip;
00261       if(type == SR_DT_RAWHIST || type == SR_DT_SLICE_COMPRESSED) {
00262         words = histWidth*2; skip = 0;
00263       } else {
00264         words = 1024*2; skip = 1024-768;
00265       }
00266       for(i=0;i<words;i++){
00267         // data
00268         if(width == 32) {
00269           data[i]=GetLWord(infile);
00270         } else if(width == 16) {
00271           data[i]=GetWord(infile);
00272         }
00273 
00274         if(i>0    && i<histWidth)
00275           h_scan0->Fill(i,threshold[j],data[i]);
00276 
00277         if(i>(histWidth + skip) && i<(words-skip))
00278           h_scan1->Fill(i-(histWidth + skip),threshold[j],data[i]);
00279 
00280         if(occ && !(type == SR_DT_RAWHIST || type == SR_DT_SLICE_COMPRESSED)) {
00281           if((i>=768 && i<768 + 32 * 6) || (i>=(768 + 1024) && i<1024 + 768 + 32 * 6)) {
00282             // Also calculate 0 from sum of others?
00283 
00284             if(i<1024) {
00285               chipNumber = (i-768)/32;
00286               channelAxis = (i-768) - 32*chipNumber;
00287               channelAxis = channelAxis * 4 + 2;
00288             } else {
00289               chipNumber = 6+(i-(1024+768))/32;
00290 
00291               channelAxis = (i-(1024+768)) - 32*(chipNumber-6);
00292               channelAxis = channelAxis * 4 + 2;
00293             }
00294 
00295             value = data[i];
00296             h_occCount[chipNumber]->Fill(channelAxis, threshold[j], value);
00297           }
00298         }
00299       }
00300 //      printf("%s\n", (char *)&data[i-8]);
00301     }
00302   } else {
00303     for(i=0;i<512;i++){
00304 
00305       // For each channel we get 100 hex data words:
00306       //    
00307       // Event format
00308       // Header - 8 16 bit words
00309       // 00 - Chan Chip
00310       // 04 - 0000 0000
00311       // 08 - BBBB BBBB
00312       // 0C - BBBB BBBB
00313       //
00314       // Now the real data
00315       //
00316       // Trailer - 8 16 bit words
00317       // F0 - FFFF FFFF 
00318       // F4 - FFFF FFFF 
00319       // F8 - FFFF FFFF 
00320       // FC - FFFF FFFF
00321 
00322       // header
00323       for(j=0;j<8;j++){
00324         header[j]=GetWord(infile);
00325       }
00326 
00327       // data
00328       for(j=0;j<bursts;j++){
00329         data[j]=GetWord(infile);
00330       }
00331 
00332       // trailer
00333       for(j=0;j<8;j++){
00334         trailer[j]=GetWord(infile);
00335       }
00336 
00337       // Fill SCTDAQ scan Histogram
00338       chan = (header[1]*128)+header[0];
00339 
00340       for(j=0;j<bursts;j++){
00341         h_scan0->Fill(chan,threshold[j],data[j]);
00342       }
00343     
00344     } 
00345 
00346   }
00347 
00348   fclose(infile);
00349 
00350   // outfile = new TFile("rod.root","RECREATE");
00351 
00352   // h_scan0->Write("h_scan0");
00353   // h_scan1->Write("h_scan1");
00354   // h_scan_evcnt->Write("h_scan_evcnt");
00355 
00356   // outfile->Close();
00357 
00358   printf("Draw scan0\n");
00359 
00360   TCanvas *c1 = new TCanvas;
00361   h_scan_evcnt->Print();
00362   h_scan0->Print();
00363   h_scan0->Draw("colz");
00364 
00365   printf("Draw evcnt\n");
00366   TCanvas *cev = new TCanvas;
00367   h_scan_evcnt->Draw();
00368 
00369   printf("Draw scan0\n");
00370 
00371   TCanvas *c2 = new TCanvas;
00372   h_scan1->Print();
00373   h_scan1->Draw("colz");
00374 
00375   if(occ) {
00376    TCanvas *c3 = new TCanvas;
00377    c3->Divide(4, 3);
00378    for(int c=0; c<12; c++) {
00379      c3->cd(c+1);
00380      //    h_occCount[c]->Print();
00381      //  printf("Draw histo %d\n", c);
00382      h_occCount[c]->Draw("colz");
00383    }
00384   }
00385 }

Generated on Mon Feb 6 14:01:19 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6