Subversion Repositories f9daq

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdio.h>
  2. #include "TH2F.h"
  3. #include "TCanvas.h"
  4. #include "TMath.h"
  5. #include "TStyle.h"
  6. #include "TColor.h"
  7.  
  8. void mypalette(){
  9.  
  10.    Int_t MyPalette[1000];
  11.    double stops[] = {0.00, 0.34, 0.61, 0.84, 1.00};
  12.    double red[]   = {1.00, 0.84, 0.61, 0.34, 0.00};
  13.    double green[] = {1.00, 0.84, 0.61, 0.34, 0.00};
  14.    double blue[]  = {1.00, 0.84, 0.61, 0.34, 0.00};
  15.  
  16.    
  17.    Int_t gs = TColor::CreateGradientColorTable(5, stops, red, green, blue, 999);
  18.    //for (int i=0;i<999;i++) MyPalette[i] = gs+998-i;
  19.  
  20.    for (int i=0;i<999;i++) MyPalette[i] = gs+i;
  21.    gStyle->SetPalette(999, MyPalette);
  22. }
  23.  
  24. int imageread(char *fname,int nchx=28, int nchy=56, int nchz=0, int format=0, int size=0, char *opt=""){
  25. FILE *fp= fopen(fname,"read");
  26. const int maxbuf=360000;
  27. float *fbuf= new float[maxbuf];
  28. unsigned short*ibuf=((unsigned short*) &fbuf[0]);
  29. if (size==0) {
  30.  if (format) size=sizeof(float);
  31.  else size=sizeof(unsigned short);
  32. }
  33. int ncount=0;
  34.  
  35. if(fp){
  36.  
  37. gStyle->SetPalette(100);
  38. mypalette();
  39. TCanvas *c=new TCanvas("c","c",1500,1500);
  40. int sizx=sqrt(nchz);
  41. c->Divide(sizx,TMath::Ceil(nchz/double(sizx)) );
  42. char hname[128];
  43.  
  44. while (!feof(fp)){
  45.  
  46.  
  47. int nb=fread(fbuf,size,nchx*nchy,fp);
  48. printf("nb=%d\n",nb);
  49. if (nb>0){
  50.   sprintf(hname,"slice %d",ncount);
  51.   TH2F *h=new TH2F("h",hname,nchx,-0.5,nchx-0.5,nchy,-0.5,nchy-0.5);
  52.   float val=0;
  53.   for (int i=0 ; i<nb;i++){
  54.     //h->SetBinContent(i%nchx+1, i/nchx+1, fbuf[i]);
  55.     if (format) val=fbuf[i]; else val=float(ibuf[i]);
  56.     h->SetBinContent(i/nchy+1,i%nchy+1, val);
  57.   }
  58.   c->cd(ncount+1);
  59.   h->SetMinimum(0);
  60.   h->Draw(opt);
  61. }
  62.  
  63. ncount++;
  64. }
  65. fclose(fp);
  66. c->Modified();
  67. c->Update();
  68. }
  69.  
  70. delete fbuf;
  71. return 0;
  72. }
  73.  
  74. int interfileread(char *fname, char *opt=""){
  75.   FILE *fp=fopen(fname,"r");
  76.   if (!fp) {
  77.     printf("Cannot open %s\n",fname);
  78.     return 0;
  79.   }  
  80.   int j=0;  
  81.   int ndim=400;
  82.   char *line= new char[ndim];                
  83.   int indx[4]={-1,-1,-1,-1};  
  84.   int dim[5];
  85.   char *cmd,*val,*token;
  86.   char fimage[100];                                                                                      
  87.   while (fgets(line,ndim,fp)!=NULL) {  
  88.      char *v= strstr(line,":=");
  89.      if (v!=NULL) {
  90.        cmd=line;
  91.        v[0]=0;
  92.        val = &v[2];
  93.        val[strlen(val)-1]=0;
  94.        printf("%d #%s#  %s\n",j++, val, cmd);
  95.        int axis=-1;
  96.        if (strstr(cmd,"matrix axis label")!= NULL){
  97.          
  98.          sscanf(cmd,"matrix axis label [%d]", &axis);
  99.          if(axis>0 && axis<5){
  100.            if (strstr(val," x")!= NULL)  indx[axis-1]=1;
  101.            if (strstr(val," y")!= NULL)  indx[axis-1]=0;
  102.            if (strstr(val," z")!= NULL)  indx[axis-1]=3;
  103.  
  104.            if (strstr(val,"view")      != NULL)  indx[axis-1]=0;          
  105.            if (strstr(val,"tangential")!= NULL)  indx[axis-1]=1;
  106.            if (strstr(val,"segment")   != NULL)  indx[axis-1]=2;
  107.            if (strstr(val,"axial")     != NULL)  indx[axis-1]=3;
  108.            printf("-->> %s axis=%d indx=%d\n",val, axis,indx[axis-1]);
  109.            
  110.          }
  111.        }
  112.        if (strstr(cmd,"!matrix size")!= NULL){
  113.            sscanf(cmd,"!matrix size [%d]", &axis);
  114.            if(axis>0 && axis<5){
  115.                if (indx[axis-1]<3){ dim[indx[axis-1]]=atoi(val);}
  116.                else {
  117.                 int sum=0;
  118.                 token = strtok(val,"{,");
  119.                 sum+=atoi(token);
  120.                 while ( (token=strtok(NULL,"{,")) ) sum+=atoi(token);
  121.                 printf("total dim=%d\n",sum);
  122.                 dim[3]=sum;
  123.                };
  124.            }
  125.            printf("axis=%d indx=%d\n",axis,indx[axis-1]);          
  126.        }
  127.        if (strstr(cmd,"name of data file")!= NULL){
  128.           sscanf(val,"%s",fimage);
  129.           printf("image file=%s\n",fimage);
  130.        }
  131.        if (strstr(cmd,"!number of bytes per pixel")!= NULL){
  132.           dim[4]=atoi(val);
  133.           printf("bytes per pixel=%d\n",dim[4]);
  134.        }
  135.        if (strstr(cmd,"!number format")!= NULL){
  136.           if (strstr(val,"float")!=NULL){ dim[5]=1; };
  137.           if (strstr(val,"integer")!=NULL){ dim[5]=0; };
  138.           printf("number format=%d\n",dim[5]);
  139.        }
  140.          
  141.      }
  142.   }
  143.   for (int i=0;i<6;i++) printf ("dim[%d]=%d\n", i,dim[i]);
  144.   fclose(fp);
  145.   imageread(fimage,dim[0], dim[1], dim[3], dim[5], dim[4], opt);
  146.   return 0;
  147. }
  148.  
  149.  
  150. int sinoread(char *fname, char *opt=""){
  151.   return interfileread(fname,opt);
  152. }
  153.