mEmcRawToFEM.c

Go to the documentation of this file.
00001 #include "mEmcRawToFEM.h"
00002 #include "emlLib.h"
00003 
00015 long mEmcRawToFEM_(
00016   TABLE_HEAD_ST    *dEmcRawData_h,    DEMCRAWDATA_ST      *dEmcRawData ,
00017   TABLE_HEAD_ST    *dEmcFEMData_h,    DEMCFEMDATA_ST      *dEmcFEMData )
00018 {
00019 /*:>--------------------------------------------------------------------
00020 **: ROUTINE:    mEmcRawToFEM_
00021 **: DESCRIPTION: Physics Analysis Module ANSI C template.
00022 **:             This is an ANSI C Physics Analysis Module template
00023 **:             automatically generated by stic from mEmcRawToFEM.idl.
00024 **:             Please edit comments and code.
00025 **: AUTHOR:     hpl - H.P. Lovecraft, hplovecraft@cthulhu.void
00026 **: ARGUMENTS:
00027 **:       IN:
00028 **:        dEmcRawData    - PLEASE FILL IN DESCRIPTION HERE
00029 **:       dEmcRawData_h   - header Structure for dEmcRawData
00030 **:    INOUT:
00031 **:      OUT:
00032 **:        dEmcFEMData    - PLEASE FILL IN DESCRIPTION HERE
00033 **:       dEmcFEMData_h   - header Structure for dEmcFEMData
00034 **: RETURNS:    STAF Condition Value
00035 **:>------------------------------------------------------------------*/
00036 
00037   short sector;
00038   short i,j,k;
00039   long rawdata[5][4608][4][2];   /* Max value for PbGl, PbSc is 2592 only */
00040   long activemodule[32][4][2];   /* One for each FEM module */
00041   int ll;
00042   long iarm,isector,iz,iy,itower,imod,ichan,imody,imodz,ichany,ichanz;
00043   long swkey;
00044   long FEMmodule;
00045   
00046   static unsigned short header = 0xFFFF;
00047   static short trailer = 0x0000;
00048   
00049   short timecell = 1;
00050   short precell = 2;
00051   short postcell = 3;
00052   short iflag,ievno,iclock,idetid;
00053   short ioffset;
00054   
00055   short i_FEMData;   /* Counter of output rows */
00056 
00057   long userword[8];
00058   
00059 
00060   int tempsize;
00061 
00062   /* ----------  Executable  -------------------------------*/
00063 
00064   if(dEmcRawData_h->nok <= 0)
00065     {
00066       printf("Error in mEmcRawToFEM: raw data table empty \n");
00067       return ( STAFCV_BAD);
00068     }
00069   /*
00070     Read raw data.  Using hwkey restore sector and serial number
00071     of channel within the sector.  Compute FEM module number that goes
00072     with the channel.  Indicate in a table that this FEM has at least
00073     one hit, and store raw data until read-in is finished
00074     We assume that one specific channel has only one raw data entry
00075     in dEmcRawData table
00076   */    
00077 
00078   for (ll = 0; ll < sizeof(rawdata)/sizeof(rawdata[0][0][0][0]); ll++)
00079     {
00080       *((long *)rawdata + ll) = 0;
00081     }
00082   for (ll = 0; ll < sizeof(activemodule)/sizeof(activemodule[0][0][0]); ll++)
00083     {
00084       *((long *)activemodule + ll) = 0;
00085     }
00086   for (ll = 0; ll < sizeof(userword)/sizeof(userword[0]); ll++)
00087     {
00088       *(userword + ll) = 0;
00089     }
00090   /* Take care of proper filling later ! */
00091   
00092   /* Set userwords to 5a00,  Now. 30, 1999 G. David */
00093   for (i=0;i<8;i++) userword[i] = 0x00005a00;
00094   
00095 
00096 
00097   ievno = dEmcRawData[0].evno & 0xFFFF;   /* Word only, not longword */
00098   iclock = ievno - 1;                     /* Dummy, take care of it ! */
00099   iflag = 0x0001;                         /* Dummy for now */
00100   idetid = 0x0080;                        /* Dummy for now */
00101   idetid = 0x0004;
00102   
00103   i_FEMData = 0;
00104   
00105   
00106   for ( i = 0; i < dEmcRawData_h->nok ; i++)
00107     {
00108       /* Replace this section: derive everything from swkey,
00109          to get more transparent to "Indexer"
00110          Jan. 3, 2000, G. David */
00111 
00112       swkey = dEmcRawData[i].swkey;
00113       iarm = swkey / 100000; swkey = swkey - 100000 * iarm;
00114       isector = swkey / 10000; swkey = swkey - 10000 * isector;
00115       iy = swkey / 100; 
00116       iz = swkey - 100 * iy;
00117 
00118       /* Get supermodule and channel within supermodule */
00119       if(iarm == 0 || isector > 1)
00120         /* This is PbSc */
00121         {
00122           /* Supermodule numbering goes like 6 * y + z */
00123           imody = iy / 12;
00124           imodz = iz / 12;
00125           imod = 6 * imody + imodz;
00126           /* Channel within supermodule goes like 12 * y + z */
00127           ichany = iy % 12;
00128           ichanz = iz % 12;
00129           ichan = 12 * ichany + ichanz;
00130           itower = 144 * imod + ichan;
00131         }
00132       else      
00133         /* This is PbGl */
00134         {
00135           /* Super-duper-module numbering goes like 8 * y + z */
00136           imody = iy / 12;
00137           imodz = iz / 12;
00138           imod = 8 * imody + imodz;
00139           /* Channel within supermodule goes like 12 * y + z */
00140           ichany = iy % 12;
00141           ichanz = iz % 12;
00142           ichan = 12 * ichany + ichanz;
00143           itower = 144 * imod + ichan;
00144         }
00145       activemodule[imod][isector][iarm] = 1;
00146       
00147       /* 
00148          In dEmcRawData all but the last 12 bits (11-0) are masked out 
00149          repeat this for safety...
00150          Now you can add high gain flag to bit 15
00151       */
00152 
00153       rawdata[0][itower][isector][iarm] = dEmcRawData[i].tdc & 0x00000FFF;
00154       rawdata[1][itower][isector][iarm] = 
00155         (dEmcRawData[i].adchipost & 0x00000FFF) + 0x00008000;
00156       rawdata[2][itower][isector][iarm] = dEmcRawData[i].adclopost & 0x00000FFF;
00157       rawdata[3][itower][isector][iarm] = 
00158         (dEmcRawData[i].adchipre & 0x00000FFF) + 0x00008000;
00159       rawdata[4][itower][isector][iarm] = dEmcRawData[i].adclopre & 0x00000FFF;
00160       
00161     }   /* End for i = 0, loop over RawData rows */
00162 
00163   /*
00164     OK, now let's write out one dEmcFEMData structure for each FEM
00165     that has at least one non-zero channel
00166    */
00167 
00168   for ( isector = 0; isector < 4; isector++)
00169     {
00170       for (iarm = 0; iarm < 2; iarm++)
00171         {
00172           for ( imod = 0; imod < 32; imod++)
00173             {
00174               if( activemodule[imod][isector][iarm] > 0 )
00175                 {
00176                   dEmcFEMData[i_FEMData].header = header;
00177                   dEmcFEMData[i_FEMData].detid = idetid;
00178                   dEmcFEMData[i_FEMData].evno = ievno;
00179                   /* 
00180                      Eventually check how module is encoded!
00181                      Right now it is similar to "hwkey"
00182                   */
00183                   /*
00184                     dEmcFEMData[i_FEMData].module = isector * 0x2000 + imod;
00185                   */
00186                   /* Encode serially */
00187                   if(iarm == 0)
00188                     {
00189                       FEMmodule = 18 * isector + imod;
00190                     }
00191                   else
00192                     {
00193                       if(isector > 1)
00194                         {
00195                           FEMmodule = 72 + 18 * (isector - 2) + imod;
00196                         }
00197                       else
00198                         {
00199                           FEMmodule = 108 + 32 * isector + imod;
00200                         }
00201                     }
00202                   dEmcFEMData[i_FEMData].module = FEMmodule;
00203 
00204                   
00205                   dEmcFEMData[i_FEMData].flag = iflag;
00206                   dEmcFEMData[i_FEMData].clock = iclock;
00207                   dEmcFEMData[i_FEMData].timecell = timecell;
00208                   dEmcFEMData[i_FEMData].precell = precell;
00209                   dEmcFEMData[i_FEMData].postcell = postcell;
00210                   ioffset = imod * 144;
00211                   i = 0;
00212                   
00213                   for( itower = ioffset; itower < ioffset + 144; itower++)
00214                     {
00215                       for ( j = 0; j < 5; j++)
00216                         {
00217                           dEmcFEMData[i_FEMData].data[i] =
00218                             rawdata[j][itower][isector][iarm] & 0x0000FFFF;
00219                           i = i + 1;
00220                           
00221                           if( i > 720)
00222                             {
00223                               printf("Error in mEmcRawToFEM: index overflow \n");
00224                             }
00225                         }
00226                       
00227                     }  /* End loop over itower from ioffset to ioffset + 144 */
00228                   
00229                   for ( i = 0; i < 8; i++)
00230                     {
00231                       dEmcFEMData[i_FEMData].userword[i] = 
00232                         userword[i] & 0x0000FFFF;
00233                     }
00234                   dEmcFEMData[i_FEMData].longparity = 0;
00235                   dEmcFEMData[i_FEMData].trailer = trailer;
00236                   
00237                   /* Increment table row count */
00238                   
00239                   i_FEMData = i_FEMData + 1;
00240                 }  /* End if activemodule[imod][isector] > 0 */
00241               
00242             }   /* End loop over imod, max. number of FEM modules in sector */
00243         }    /* End loop over iarm */
00244       
00245     }    /* End loop over isector, all sectors */
00246   
00247   dEmcFEMData_h->nok = i_FEMData;
00248   
00249 
00250   return STAFCV_OK;
00251 }