ReportHistogram.cxx

00001 #include <iostream>
00002 
00003 #include <TFile.h>
00004 #include <TH1F.h>
00005 #include <TH2F.h>
00006 
00007 using namespace std;
00008 
00009 void ReportHistogram(char* infname);
00010 
00011 int main(int argc, char **argv) {
00012   for(int i=0; i<argc-1; i++) {
00013     ReportHistogram(argv[i+1]);
00014   }
00015 }
00016 
00017 unsigned short GetWord(FILE* file){
00018 
00019   // Need to fit EOF
00020   int lo_char, hi_char;
00021   unsigned short word;
00022 
00023   if(file==NULL){
00024     cout << "bad file\n";
00025     return 0;
00026   }
00027 
00028   // Read two bytes
00029   lo_char = fgetc(file);
00030   hi_char = fgetc(file);
00031 
00032   if(lo_char == EOF || hi_char == EOF) {
00033     cout << "end of file\n";
00034     return 0;
00035   }
00036 
00037   // Swap bytes to form 16 bit word
00038   word = (hi_char<<8)+lo_char;
00039 
00040   return word;
00041 }
00042 
00043 unsigned long GetLWord(FILE* file){
00044   // Fit EOF
00045   int lo_char, hi_char;
00046   int lo_char2, hi_char2;
00047 
00048   // Result
00049   unsigned long word;
00050 
00051   if(file==NULL){
00052     cout << "bad file\n";
00053     return 0;
00054   }
00055 
00056   // Read four bytes
00057   lo_char = fgetc(file);
00058   hi_char = fgetc(file);
00059   lo_char2 = fgetc(file);
00060   hi_char2 = fgetc(file);
00061 
00062   if(lo_char == EOF || hi_char == EOF) {
00063     cout << "end of file\n";
00064     return 0;
00065   }
00066 
00067   // Swap bytes to form 16 bit word
00068   word = (hi_char2<<24)+(lo_char2<<16) + (hi_char<<8)+lo_char;
00069 
00070   return word;
00071 }
00072 
00073 void ReportHistogram(char* infname) {
00074   cout << "File: " << infname;
00075 
00076   // Convert ROD histograms to SCTDAQ format
00077 
00078   char htitle[128];
00079   char hname[32];
00080 
00081   int i,j;
00082   unsigned short header[8];
00083   unsigned short trailer[8];
00084   unsigned long data[1024*2];
00085   unsigned short chan,chip;
00086 
00087   TH1F* h_scan_evcnt;
00088   TH2F* h_scan0;
00089   TH2F* h_scan1;
00090 
00091   FILE* infile;
00092 
00093   infile = fopen(infname,"rb");
00094 
00095   if(infile==NULL) {
00096     cout << "Could not open file\n";
00097     return;
00098   }  
00099 
00100   int dontSkip = 0;
00101 
00102   short version = GetWord(infile);          // version
00103   short length = GetWord(infile);
00104   dontSkip += 1;
00105 
00106   int run = GetLWord(infile);
00107   int scan = GetLWord(infile);
00108   dontSkip += 2;
00109 
00110   char moduleName[16];
00111   for(int i=0; i<16; i++) {
00112     moduleName[i] = fgetc(infile);
00113   }
00114   dontSkip +=4;
00115 
00116   if(version == 1) {
00117     char startTime[16];
00118     for(int i=0; i<16; i++) {
00119       startTime[i] = fgetc(infile);
00120     }
00121     dontSkip +=4;
00122 //     printf("Started: %s\n", startTime);
00123 
00124     char endTime[16];
00125     for(int i=0; i<16; i++) {
00126       endTime[i] = fgetc(infile);
00127     }
00128     dontSkip +=4;
00129 
00130 //     printf("Finished: %s\n", endTime);
00131   }
00132 
00133   short scanType = GetWord(infile);           // scanType
00134 
00135   cout << " Type: ";
00136   cout.width(3);
00137   cout << scanType;
00138 
00139   short nBins = GetWord(infile);
00140   dontSkip ++;
00141 
00142   int size = GetLWord(infile);
00143   dontSkip ++;
00144   short type = GetWord(infile);
00145   short width = GetWord(infile);
00146 
00147   if(!(width == 32 || width == 16)) {
00148     cout << " Bad data width: " << width << endl;
00149     return;
00150   } else {
00151     cout << " Width: " << width;
00152   }
00153 
00154   dontSkip ++;
00155 
00156   int bursts = nBins;
00157 
00158   dontSkip +=4;            // Dont skip pointers
00159 
00160   for(int i=0; i<length/4-dontSkip; i++) {
00161     unsigned long dummy = GetLWord(infile);
00162   }
00163 
00164   unsigned long pointer = GetLWord(infile);     // Pointer to thresholds
00165   unsigned long pEvents = GetLWord(infile);
00166   unsigned long pErrors = GetLWord(infile);
00167   unsigned long pData = GetLWord(infile);       // Pointer to data
00168 
00169   for(unsigned int i=0; i<(pointer-length)/4; i++) {   // Presumably 0
00170     unsigned long dummy = GetLWord(infile);
00171   }
00172 
00173   cout << " nbins = " << nBins;
00174   float *threshold = new float[bursts];
00175 
00176   // Scan points
00177   for(int i=0; i<bursts; i++) {
00178     unsigned long word = GetLWord(infile);
00179     threshold[i] = *((float*)(&word));
00180   }
00181 
00182   // Event counts
00183   for(int i=0; i<bursts; i++) {
00184     unsigned long events = GetLWord(infile);
00185   }
00186 
00187   // Error counts
00188   for(int i=0; i<bursts; i++) {
00189     unsigned long errors = GetLWord(infile);
00190   }
00191 
00192   double hist_start;
00193   double hist_stop;
00194   float fist_start;
00195   float fist_stop;
00196 
00197   //  hist_start = start-(step/2);
00198   //  hist_stop =  start+(112*step)+(step/2);
00199   float step = threshold[1] - threshold[0];
00200   if(threshold[0] < threshold[bursts-1]) {
00201     hist_start = threshold[0]; // start;
00202     hist_stop =  threshold[bursts-1] + step; // start+(bursts*step);
00203   } else {
00204     hist_start =  threshold[bursts-1] + step; // start+(bursts*step);
00205     hist_stop = threshold[0]; // start;
00206   }
00207 
00208   fist_start = hist_start;
00209   fist_stop =  hist_stop;
00210 
00211   sprintf(htitle,"ROD Events Requested");
00212   sprintf(hname,"h_scan_evcnt");
00213   h_scan_evcnt = new TH1F(hname, htitle, bursts, hist_start, hist_stop);
00214   h_scan_evcnt->SetFillColor(46);
00215 
00216   sprintf(htitle, "%s Stream 0 THRESHOLD Scan", moduleName);
00217   sprintf(hname,"h_scan0_0");
00218   h_scan0 = new TH2F(hname,htitle, 768,0.5,768.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, 768,0.5,768.5, bursts, hist_start, hist_stop);
00225   h_scan1->SetXTitle("Channel Number");
00226   h_scan1->SetYTitle("THRESHOLD (mV)");
00227 
00228   for(j=0;j<bursts;j++) {
00229     h_scan_evcnt->Fill(threshold[j],50);  // hardcoded to 50 events
00230   }
00231 
00232   int slice = 1;
00233   if(slice) {
00234     for(j=0;j<bursts;j++){
00235       int words, skip;
00236       if(type == 16 || type == 6) {
00237         words = 768*2; skip = 0;
00238       } else {
00239         words = 1024*2; skip = 1024-768;
00240       }
00241       for(i=0;i<words;i++){
00242         // data
00243         if(width == 32) {
00244           data[i]=GetLWord(infile);
00245         } else if(width == 16) {
00246           data[i]=GetWord(infile);
00247         }
00248 
00249         if(i>0    && i<768)
00250           h_scan0->Fill(i,threshold[j],data[i]);
00251         if(i>(768 + skip) && i<(words-skip))
00252           h_scan1->Fill(i-(768 + skip),threshold[j],data[i]);
00253       }
00254     }
00255   } else {
00256     for(i=0;i<512;i++){
00257 
00258       // For each channel we get 100 hex data words:
00259       //    
00260       // Event format
00261       // Header - 8 16 bit words
00262       // 00 - Chan Chip
00263       // 04 - 0000 0000
00264       // 08 - BBBB BBBB
00265       // 0C - BBBB BBBB
00266       //
00267       // Now the real data
00268       //
00269       // Trailer - 8 16 bit words
00270       // F0 - FFFF FFFF 
00271       // F4 - FFFF FFFF 
00272       // F8 - FFFF FFFF 
00273       // FC - FFFF FFFF
00274 
00275       // header
00276       for(j=0;j<8;j++){
00277         header[j]=GetWord(infile);
00278       }
00279 
00280       // data
00281       for(j=0;j<bursts;j++){
00282         data[j]=GetWord(infile);
00283       }
00284 
00285       // trailer
00286       for(j=0;j<8;j++){
00287         trailer[j]=GetWord(infile);
00288       }
00289 
00290       // Fill SCTDAQ scan Histogram
00291       chan = (header[1]*128)+header[0];
00292 
00293       for(j=0;j<bursts;j++){
00294         h_scan0->Fill(chan,threshold[j],data[j]);
00295       }
00296     
00297     } 
00298 
00299   }
00300 
00301   fclose(infile);
00302 
00303   Stat_t stat0[4];
00304   Stat_t stat1[4];
00305   for(int i=0; i<4; i++) { stat0[i] = stat1[i] = 0; }
00306 
00307   h_scan0->GetStats(stat0);
00308   h_scan1->GetStats(stat1);
00309 
00310   cout << " Sum link 0:  " << stat0[0];
00311   cout << " link 1:  " << stat1[0];
00312   cout << endl;
00313 }

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