mEmcRawToLongModulev1.C

Go to the documentation of this file.
00001 #include "mEmcRawToLongModulev1.h"
00002 #include "emcNodeHelper.h"
00003 #include "PHCompositeNode.h"
00004 #include "dEmcRawDataWrapper.h"
00005 #include "dEmcDCMLongDataWrapper.h"
00006 
00007 using std::cout;
00008 using std::cerr;
00009 using std::endl;
00010 
00011 //_____________________________________________________________________________
00012 mEmcRawToLongModulev1::mEmcRawToLongModulev1()
00013 {
00014   name = "mEmcRawToLongModulev1";
00015   fEmcRawNodeName = "dEmcRawData";
00016   fEmcDCMLongNodeName = "dEmcDCMLongData";
00017 }
00018 
00019 //_____________________________________________________________________________
00020 mEmcRawToLongModulev1::~mEmcRawToLongModulev1()
00021 {
00022 }
00023 
00024 //_____________________________________________________________________________
00025 PHBoolean
00026 mEmcRawToLongModulev1::event(PHCompositeNode *root) 
00027 {
00028   return event(root,fEmcRawNodeName.c_str(),fEmcDCMLongNodeName.c_str());
00029 }
00030 
00031 //_____________________________________________________________________________
00032 PHBoolean
00033 mEmcRawToLongModulev1::event(PHCompositeNode *root,
00034                              const char* dEmcRawNodeName,
00035                              const char* dEmcDCMLongNodeName) 
00036 {
00037   PHCompositeNode* emcNode = 
00038     emcNodeHelper::findCompositeNode(root,"EMC");
00039 
00040   if (!emcNode) 
00041     {
00042       emcNode = new PHCompositeNode("EMC");
00043       root->addNode(emcNode);
00044     }
00045   
00046   PHCompositeNode* dcmNode =
00047     emcNodeHelper::findCompositeNode(root,"DCM");
00048   
00049   if (!dcmNode) 
00050     {
00051       dcmNode = new PHCompositeNode("DCM");
00052       root->addNode(dcmNode);
00053     }
00054   
00055   dEmcRawDataWrapper* dEmcRawData = 
00056     emcNodeHelper::getTable<dEmcRawDataWrapper>(dEmcRawNodeName,emcNode);
00057 
00058   if ( !dEmcRawData ) 
00059     {
00060       cerr << __FILE__ << ":" << __LINE__ 
00061            << " Table dEmcRawData not found in node "
00062            << dEmcRawNodeName << " I will create it."
00063            << endl;
00064       dEmcRawData = emcNodeHelper::addTable<dEmcRawDataWrapper>
00065         (emcNode,"dEmcRawData",15000,false,dEmcRawNodeName);
00066     }
00067 
00068   dEmcDCMLongDataWrapper* dEmcDCMLongData =
00069     emcNodeHelper::getTable<dEmcDCMLongDataWrapper>(dEmcDCMLongNodeName,dcmNode);
00070 
00071   if ( !dEmcDCMLongData ) 
00072     {
00073       cerr << __FILE__ << ":" << __LINE__ 
00074            << " Table dEmcDCMLongData not found in node "
00075            << dEmcDCMLongNodeName << " I will create it."
00076            << endl;
00077       dEmcDCMLongData = emcNodeHelper::addTable<dEmcDCMLongDataWrapper>
00078         (dcmNode,"dEmcDCMLongData",500,false,dEmcDCMLongNodeName);
00079     }
00080 
00081   return doit(*dEmcRawData, *dEmcDCMLongData);
00082 }
00083 
00084 //_____________________________________________________________________________
00085 bool
00086 mEmcRawToLongModulev1::doit(const dEmcRawDataWrapper& dEmcRawData,
00087                             dEmcDCMLongDataWrapper& dEmcDCMLongData)
00088 {
00089   //       IN:
00090   //        dEmcRawData    - PLEASE FILL IN DESCRIPTION HERE
00091   //      OUT:
00092   //        dEmcDCMLongData    - PLEASE FILL IN DESCRIPTION HERE
00093 
00094   unsigned short i;
00095   long rawdata[5][4608][4][2];   /* Max value for PbGl, PbSc is 2592 only */
00096   long activemodule[32][4][2];   /* One for each FEM module */
00097   unsigned int ll;
00098   long iarm,isector,iz,iy,itower,imod,ichan,imody,imodz,ichany,ichanz;
00099   long swkey;
00100   long FEMmodule;
00101   int  icount;
00102   int  pkts_per_sector;
00103  
00104   short timecell = 1;
00105   short precell = 2;
00106   short postcell = 3;
00107   short iflag,ievno,iclock,idetid;
00108   short ioffset;
00109   int channel;
00110   int  fem_channel;
00111  
00112   /* this is the map of software tower channel to fem channel
00113      (software map to hardware map) */
00114   const int fem_map[144] = {
00115     21,  20,  45,  44,  69,  68,  93,  92, 117, 116, 141, 140,
00116     23,  22,  47,  46,  71,  70,  95,  94, 119, 118, 143, 142,
00117     17,  16,  41,  40,  65,  64,  89,  88, 113, 112, 137, 136,
00118     19,  18,  43,  42,  67,  66,  91,  90, 115, 114, 139, 138,
00119     13,  12,  37,  36,  61,  60,  85,  84, 109, 108, 133, 132,
00120     15,  14,  39,  38,  63,  62,  87,  86, 111, 110, 135, 134,
00121      9,   8,  33,  32,  57,  56,  81,  80, 105, 104, 129, 128,
00122     11,  10,  35,  34,  59,  58,  83,  82, 107, 106, 131, 130,
00123      5,   4,  29,  28,  53,  52,  77,  76, 101, 100, 125, 124,
00124      7,   6,  31,  30,  55,  54,  79,  78, 103, 102, 127, 126,
00125      1,   0,  25,  24,  49,  48,  73,  72,  97,  96, 121, 120,
00126      3,   2,  27,  26,  51,  50,  75,  74,  99,  98, 123, 122
00127   };
00128 
00129   short i_FEMData;   /* Counter of output rows */
00130 
00131   long userword[8];
00132   
00133   /* ----------  Executable  -------------------------------*/
00134 
00135   if(dEmcRawData.RowCount() <= 0)
00136     {
00137       printf("Error in mEmcRawToFEM: raw data table empty \n");
00138       return false;
00139     }
00140   /*
00141     Read raw data.  Using hwkey restore sector and serial number
00142     of channel within the sector.  Compute FEM module number that goes
00143     with the channel.  Indicate in a table that this FEM has at least
00144     one hit, and store raw data until read-in is finished
00145     We assume that one specific channel has only one raw data entry
00146     in dEmcRawData table
00147   */    
00148 
00149   for (ll = 0; ll < sizeof(rawdata)/sizeof(rawdata[0][0][0][0]); ll++)
00150     {
00151       *((long *)rawdata + ll) = 0;
00152     }
00153   for (ll = 0; ll < sizeof(activemodule)/sizeof(activemodule[0][0][0]); ll++)
00154     {
00155       *((long *)activemodule + ll) = 0;
00156     }
00157   for (ll = 0; ll < sizeof(userword)/sizeof(userword[0]); ll++)
00158     {
00159       *(userword + ll) = 0;
00160     }
00161   /* Take care of proper filling later ! */
00162   
00163   /* Set userwords to 5900 */
00164   for (i=0;i<8;i++) userword[i] = 0x00005900;
00165 
00166   ievno = dEmcRawData.get_evno(0) & 0xffff;   /* Word only, not longword */
00167   iclock = ievno - 1;                     /* Dummy, take care of it ! */
00168   iflag = 0;                              /* Dummy for now */
00169   idetid = 4;
00170   
00171   i_FEMData = 0;
00172   
00173   
00174   for ( i = 0; i < dEmcRawData.RowCount() ; i++)
00175     {
00176       /* Replace this section: derive everything from swkey,
00177          to get more transparent to "Indexer"
00178          Jan. 3, 2000, G. David */
00179 
00180       swkey = dEmcRawData.get_swkey(i);
00181       iarm = swkey / 100000; 
00182       swkey = swkey - 100000 * iarm;
00183       isector = swkey / 10000; 
00184       swkey = swkey - 10000 * isector;
00185       iy = swkey / 100; 
00186       iz = swkey - 100 * iy;
00187 
00188       /* Get supermodule and channel within supermodule */
00189       if(iarm == 0 || isector > 1)
00190         /* This is PbSc */
00191         {
00192           /* Supermodule numbering goes like 6 * y + z */
00193           imody = iy / 12;
00194           imodz = iz / 12;
00195           imod = 6 * imody + imodz;
00196           /* Channel within supermodule goes like 12 * y + z */
00197           ichany = iy % 12;
00198           ichanz = iz % 12;
00199           ichan = 12 * ichany + ichanz;
00200           itower = 144 * imod + ichan;
00201           fem_channel = 144 * imod + fem_map[ichan];
00202         }
00203       else      
00204         /* This is PbGl */
00205         {
00206           /* Super-duper-module numbering goes like 8 * y + z */
00207           imody = iy / 12;
00208           imodz = iz / 12;
00209           imod = 8 * imody + imodz;
00210           /* Channel within supermodule goes like 12 * y + z */
00211           ichany = iy % 12;
00212           ichanz = iz % 12;
00213           ichan = 12 * ichany + ichanz;
00214           itower = 144 * imod + ichan;
00215           fem_channel = 144 * imod + fem_map[ichan];
00216         }
00217       activemodule[imod][isector][iarm] = 1;
00218       
00219       
00220       /* Fake values to time, pre and post AMU cell numbers */
00221       /* we might want to make these random from 0-63, */
00222       /* since that would better correspond to reality */
00223 
00224       timecell = 1;
00225       precell = 2;
00226       postcell = 3;
00227       
00228       /*
00229          put in raw data channels.  note that adclopost is
00230          for high gain data
00231       */
00232       rawdata[0][fem_channel][isector][iarm] = dEmcRawData.get_adclopost(i);
00233       rawdata[1][fem_channel][isector][iarm] = dEmcRawData.get_adclopre(i);
00234       rawdata[2][fem_channel][isector][iarm] = dEmcRawData.get_adchipost(i);
00235       rawdata[3][fem_channel][isector][iarm] = dEmcRawData.get_adchipre(i);
00236       rawdata[4][fem_channel][isector][iarm] = dEmcRawData.get_tdc(i);
00237       
00238     }   /* End for i = 0, loop over RawData rows */
00239 
00240   /*
00241     OK, now let's write out one dEmcFEMData structure for each FEM
00242     that has at least one non-zero channel
00243    */
00244 
00245   for ( isector = 0; isector < 4; isector++)
00246     {
00247       for (iarm = 0; iarm < 2; iarm++)
00248         {
00249           if(iarm == 0 || isector > 1)
00250             {
00251               pkts_per_sector = 18;
00252             }
00253           else
00254             {
00255               pkts_per_sector = 32;
00256             }
00257           for ( imod = 0; imod < pkts_per_sector; imod++)
00258             {         
00259               // write out only the modules that have data
00260               if( activemodule[imod][isector][iarm] > 0 )
00261                 {
00262                   dEmcDCMLongData.set_scheme(i_FEMData,1108);
00263                   
00264                   /* 
00265                      Eventually check how module is encoded!
00266                      Right now it is similar to "hwkey"
00267                   */
00268                   
00269                   /* Encode serially */
00270                   if(iarm == 0)
00271                     {
00272                       FEMmodule = 18 * isector + imod;
00273                     }
00274                   else
00275                     {
00276                       if(isector > 1)
00277                         {
00278                           FEMmodule = 72 + 18 * (isector - 2) + imod;
00279                         }
00280                       else
00281                         {
00282                           FEMmodule = 108 + 32 * isector + imod;
00283                         }
00284                     }
00285                   
00286                   dEmcDCMLongData.set_packetID(i_FEMData,8001 + FEMmodule);
00287                   
00288                   icount = 0;   // keeps track of where we are in the packet
00289                   
00290                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,iflag);
00291                   
00292                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,iflag);
00293                   
00294                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,FEMmodule + 1);
00295                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,ievno);
00296                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,iclock);
00297                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,idetid);
00298                   
00299                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,postcell);
00300                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,precell);
00301                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,timecell);
00302                   
00303                   ioffset = imod * 144;
00304                   
00305                   /* the real data channels are scrambled */
00306                   for ( fem_channel = ioffset; fem_channel < ioffset + 144; fem_channel++)
00307                     {
00308                       channel = (fem_channel - ioffset) << 20;
00309 
00310                       /* in simulated data, there is a hit if the channel data
00311                          are non-zero */
00312                       if ( rawdata[0][fem_channel][isector][iarm] != 0 )
00313                           { 
00314                             dEmcDCMLongData.set_DCM(icount++,i_FEMData,channel |
00315                                    (rawdata[0][fem_channel][isector][iarm] ^ 0xfff) | 0x92000);
00316                             dEmcDCMLongData.set_DCM(icount++,i_FEMData,channel |
00317                                    (rawdata[1][fem_channel][isector][iarm] ^ 0xfff) | 0xa4000);
00318                             dEmcDCMLongData.set_DCM(icount++,i_FEMData,channel |
00319                                    (rawdata[2][fem_channel][isector][iarm] ^ 0xfff) | 0xb3000);
00320                             dEmcDCMLongData.set_DCM(icount++,i_FEMData,channel |
00321                                    (rawdata[3][fem_channel][isector][iarm] ^ 0xfff) | 0xc5000);
00322                             dEmcDCMLongData.set_DCM(icount++,i_FEMData,channel |
00323                                    (rawdata[4][fem_channel][isector][iarm] ^ 0xfff) | 0xd1000);
00324                           }
00325 
00326                     }  /* End loop over itower from ioffset to ioffset + 144 */
00327 
00328                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,0x10080000); /* long. parity */
00329                   dEmcDCMLongData.set_DCM(icount++,i_FEMData,0x10072220); /* summary word */
00330 
00331                   dEmcDCMLongData.set_nWords(i_FEMData,icount); /* now put in length */
00332 
00333                   /* Increment table row count */
00334                   
00335                   i_FEMData = i_FEMData + 1;
00336                 }  /* End if (activemodule[imod][isector] > 0) */
00337               
00338             }   /* End loop over imod, max. number of FEM modules in sector */
00339         }    /* End loop over iarm */
00340       
00341     }    /* End loop over isector, all sectors */
00342   
00343   dEmcDCMLongData.SetRowCount(i_FEMData);
00344 
00345   return true;
00346 }
00347