mEmcDCMToRaw.c

Go to the documentation of this file.
00001 /*:>--------------------------------------------------------------------
00002 **: compile with cc
00003 **: FILE:       mEmcDCMToRaw.c.template
00004 **: HISTORY:
00005 **:             00jan93-v000a-hpl- Created by stic Version
00006 **:  Id: idl.y,v 1.3 1998/04/20 19:56:35 leitch Exp  
00007 **:<------------------------------------------------------------------*/
00008 #include "mEmcDCMToRaw.h"
00009 #include "emlLib.h"
00010 
00018 long mEmcDCMToRaw_(
00019   TABLE_HEAD_ST    *dEmcDCMData_h,    DEMCDCMDATA_ST      *dEmcDCMData ,
00020   TABLE_HEAD_ST    *dEmcRawData_h,    DEMCRAWDATA_ST      *dEmcRawData )
00021 {
00022 /*:>--------------------------------------------------------------------
00023 **: ROUTINE:    mEmcDCMToRaw_
00024 **: DESCRIPTION: Physics Analysis Module ANSI C template.
00025 **:             This is an ANSI C Physics Analysis Module template
00026 **:             automatically generated by stic from mEmcDCMToRaw.idl.
00027 **:             Please edit comments and code.
00028 **: AUTHOR:     hpl - H.P. Lovecraft, hplovecraft@cthulhu.void
00029 **: ARGUMENTS:
00030 **:       IN:
00031 **:        dEmcDCMData    - PLEASE FILL IN DESCRIPTION HERE
00032 **:       dEmcDCMData_h   - header Structure for dEmcDCMData
00033 **:    INOUT:
00034 **:      OUT:
00035 **:        dEmcRawData    - PLEASE FILL IN DESCRIPTION HERE
00036 **:       dEmcRawData_h   - header Structure for dEmcRawData
00037 **: RETURNS:    STAF Condition Value
00038 **:>------------------------------------------------------------------*/
00039 
00040   short i,j,k;
00041   long l;
00042   int   i_rawdata;
00043   unsigned short ievno,imod,ikey,imodule,isector,ismoffset,itower,ichan;
00044   unsigned short isec,iarm;
00045 
00046   static long ichany,ichanz,imody,imodz;
00047   
00048   
00049   int icode,igain,itype,iswkey;
00050   short iconst,ismodz,ismody,tower,iz,iy;
00051   
00052     
00053   long rawdata[5][4608][4][2];   /* Max value for PbGl, PbSc is 2592 only */
00054   /* 0: time, 1: high post 2: low post, 3: high pre 4: low pre */
00055 
00056   int tempsize;
00057 
00058 #define r_lowgain_convfac  0.001   
00059 #define r_highgain_convfac  0.008   
00060 #define i_highgain_convfac  8   
00061 #define r_tdc_convfac  0.05
00062 #define i_low_ped 4000
00063 #define i_high_ped 4000
00064 #define i_minvalue 100
00065 
00066 
00067   /* -------------  Executable ----------------------------------*/
00068 
00069 
00070   if(dEmcDCMData_h->nok <= 0)
00071     {
00072       printf("Error in mEmcDCMToRaw: DCM data table empty \n");
00073       return ( STAFCV_BAD);
00074     }
00075 
00076   for (l = 0; l < sizeof(rawdata)/sizeof(rawdata[0][0][0][0]); l++)
00077     {
00078       *((long *)rawdata + l) = 0;
00079     }
00080   i_rawdata = 0;          /* row counter on output */
00081   ievno = dEmcDCMData[0].DCM[2] & 0x0000FFFF;
00082   
00083   for ( i = 0; i < dEmcDCMData_h->nok; i++)
00084     {
00085       imodule = dEmcDCMData[i].DCM[1] & 0x0000FFFF;
00086       /*      isector = (imodule & 0xE000) / 0x1FFF; */
00087       /* New scheme, Jan. 4, 2000, G. David
00088       isector = ( (imodule & 0xE000) >> 13);
00089       imod = imodule & 0x1FFF;
00090       ismoffset = imod * 144;
00091       */      
00092       if(imodule != dEmcDCMData[i].packetID - 8001)
00093         printf("Error in mEmcDCMToRaw: FEM and packetID mismatch \n");
00094       if(imodule < 108)
00095         {
00096           isector = imodule / 18;
00097           iarm = 0;
00098           if(isector > 3)
00099             {
00100               iarm = 1;
00101               isector = isector - 2;
00102             }
00103           imodule = imodule % 18;
00104           ismody = imodule / 6;
00105           ismodz = imodule % 6;
00106         }
00107       else
00108         {
00109           iarm = 1;
00110           imodule = imodule - 108;
00111           isector = imodule / 32;
00112           imodule = imodule % 32;
00113           ismody = imodule / 8;
00114           ismodz = imodule % 8;
00115         }
00116       
00117       ismoffset = imodule * 144;
00118 
00119       /* Now loop over variable length data input */
00120       j = 8;
00121       /*      icode = (dEmcDCMData[i].data[j] & 0x000F0000) / 0x0000FFFF; */
00122       icode = ( (dEmcDCMData[i].DCM[j] & 0x000F0000) >> 16);
00123 
00124       while( (icode == 9 || icode == 10 || icode == 12) && j < 450 )
00125         {
00126           /* Get channel number within FEE module by masking bits 27-20 */
00127           /*      ichan = (dEmcDCMData[i].data[j] & 0x0FF00000) / 0x000FFFFF; */
00128           ichan = ((dEmcDCMData[i].DCM[j] & 0x0FF00000) >> 20);
00129           /* Get tower within sector by adding SM lower left offset */
00130           itower = ismoffset + ichan;
00131           /* Get low-high gain bit */
00132           /*      igain = (dEmcDCMData[i].data[j] & 0x0008000) / 0x00007FFF; */
00133 
00134           igain = ((dEmcDCMData[i].DCM[j] & 0x0008000) >> 15);
00135 
00136           switch ( icode + 10 * igain)
00137             {
00138             case 9:             /* Time */
00139               rawdata[0][itower][isector][iarm] = 
00140                 dEmcDCMData[i].DCM[j] & 0x00000FFF;
00141               break;
00142             case 10:            /* Low gain pre, force high gain pre */
00143               rawdata[4][itower][isector][iarm] = 
00144                 dEmcDCMData[i].DCM[j] & 0x00000FFF;
00145               rawdata[3][itower][isector][iarm] = i_high_ped;
00146               break;
00147             case 12:            /* Low gain post */
00148               rawdata[2][itower][isector][iarm] = 
00149                 dEmcDCMData[i].DCM[j] & 0x00000FFF;
00150               break;
00151             case 20:            /* High gain pre, force low gain pre */
00152               rawdata[3][itower][isector][iarm] = 
00153                 dEmcDCMData[i].DCM[j] & 0x00000FFF;
00154               rawdata[4][itower][isector][iarm] = i_low_ped;
00155               break;
00156             case 22:            /* High gain post */
00157               rawdata[1][itower][isector][iarm] = 
00158                 dEmcDCMData[i].DCM[j] & 0x00000FFF;
00159               break;
00160             default:
00161               printf("Error in mEmcDCMToRaw: unrecognized data \n");
00162               break;
00163             }
00164           j++;
00165           /*      icode = (dEmcDCMData[i].data[j] & 0x000F0000) / 0x0000FFFF; */
00166           icode = ( (dEmcDCMData[i].DCM[j] & 0x000F0000) >> 16);
00167           
00168         }  /* icode = 9, 10 or 12, valid, real data  */
00169       
00170     }     /* Loop over i, all rows of dEmcDCMData table */
00171 
00172   /* Now write out raw data  */
00173 
00174   /* RawData indexing changed, Jan 4, 2000, G. David */
00175 
00176   for ( isector = 0; isector < 4; isector++)
00177     {
00178       for ( iarm = 0; iarm < 2; iarm++)
00179         {
00180           
00181           for ( itower = 0; itower < 4608; itower++ )
00182             {
00183               /* If low or high gain post is nonzero, write new RawData */
00184               if ( rawdata[1][itower][isector][iarm] > 0 ||
00185                    rawdata[2][itower][isector][iarm] > 0 )
00186                 {
00187                   
00188                   /* Generate missing low or high */
00189                   if (rawdata[1][itower][isector][iarm] == 0) /* High gain missing */
00190                     {
00191                       rawdata[1][itower][isector][iarm] = 
00192                         rawdata[3][itower][isector][iarm] +
00193                         (rawdata[2][itower][isector][iarm] - 
00194                          rawdata[4][itower][isector][iarm]) / i_highgain_convfac;
00195                     }
00196 
00197                     /* Low gain missing */
00198                   if (rawdata[2][itower][isector][iarm] == 0) 
00199                     {
00200                       rawdata[2][itower][isector][iarm] = i_minvalue;
00201                     }
00202                   dEmcRawData[i_rawdata].id = i_rawdata + 1;
00203                   dEmcRawData[i_rawdata].evno = ievno;
00204                   
00205                   /* New indexing scheme - replaceable with PbSc/PbGlindexer
00206                      - relies only on what we call "software key"
00207                      Jan 4. 2000, G. David */
00208 
00209                   ichan = itower % 144;
00210                   ismoffset = itower - ichan;
00211                   imodule = ismoffset / 144;
00212                   /* Relative indices within SM */
00213                   ichany = ichan / 12;
00214                   ichanz = ichan % 12;
00215                   
00216                   if(iarm == 0 || isector > 1)
00217                     {
00218                       imody = imodule / 6;
00219                       imodz = imodule % 6;
00220                       iy = 12 * imody + ichany;
00221                       iz = 12 * imodz + ichanz;
00222                       itype = 1;
00223                     }
00224                   else            
00225                     {
00226                       imody = imodule / 8;
00227                       imodz = imodule % 8;
00228                       iy = 12 * imody + ichany;
00229                       iz = 12 * imodz + ichanz;
00230                       itype = 2;
00231                     }
00232 
00233                   dEmcRawData[i_rawdata].type = itype;
00234                   
00235                   iswkey = 100000 * iarm + 10000 * isector + 100 * iy + iz;
00236                   dEmcRawData[i_rawdata].swkey = iswkey;
00237                   
00238                   
00239                   dEmcRawData[i_rawdata].adclopre = 
00240                     rawdata[4][itower][isector][iarm];
00241                   dEmcRawData[i_rawdata].adclopost = 
00242                     rawdata[2][itower][isector][iarm];
00243                   dEmcRawData[i_rawdata].adchipre = 
00244                     rawdata[3][itower][isector][iarm];
00245                   dEmcRawData[i_rawdata].adchipost = 
00246                     rawdata[1][itower][isector][iarm];
00247                   dEmcRawData[i_rawdata].tdc = 
00248                     rawdata[0][itower][isector][iarm];
00249                   
00250                   i_rawdata = i_rawdata + 1;   /* Incerement row counter */
00251                 }  /* endif low/high post > 0 : New row in dEmcRawData */
00252               
00253             }  /* Loop over itower, all towers */
00254         }  /* Loop over iarm */
00255       
00256     }  /* Loop over isector, all sectors */
00257   
00258   dEmcRawData_h->nok = i_rawdata;
00259 
00260   return STAFCV_OK;
00261 }