Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

DispHistogram.cpp

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

Generated on Thu Jul 15 09:50:45 2004 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5