mEmcPRDFToRaw.C

Go to the documentation of this file.
00001 #include "mEmcPRDFToRaw.h"
00002 #include "dEmcRawDataWrapper.h"
00003 
00004 #include "Event.h"
00005 
00006 #include "phool.h"
00007 #include "PHIODataNode.h"
00008 #include "PHCompositeNode.h"
00009 #include "PHNodeIterator.h"
00010 #include <iostream>
00011 using namespace std;
00012 
00013 typedef PHDataNode<Event> EventNode_t;
00014 typedef PHIODataNode<PHTable> TableNode_t;
00015 typedef PHDataNode<dEmcRawDataWrapper> dEmcRawDataNode_t;
00016 
00017 static const int FeeToSoftwareMap[144] = 
00018 { 121, 120, 133, 132,  97,  96, 109, 108, 73, 72, 85, 84,
00019    49,  48,  61,  60,  25,  24,  37,  36,  1,  0, 13, 12,
00020   123, 122, 135, 134,  99,  98, 111, 110, 75, 74, 87, 86,
00021    51,  50,  63,  62,  27,  26,  39,  38,  3,  2, 15, 14,
00022   125, 124, 137, 136, 101, 100, 113, 112, 77, 76, 89, 88,
00023    53,  52,  65,  64,  29,  28,  41,  40,  5,  4, 17, 16,
00024   127, 126, 139, 138, 103, 102, 115, 114, 79, 78, 91, 90,
00025    55,  54,  67,  66,  31,  30,  43,  42,  7,  6, 19, 18,
00026   129, 128, 141, 140, 105, 104, 117, 116, 81, 80, 93, 92,
00027    57,  56,  69,  68,  33,  32,  45,  44,  9,  8, 21, 20,
00028   131, 130, 143, 142, 107, 106, 119, 118, 83, 82, 95, 94,
00029    59,  58,  71,  70,  35,  34,  47,  46, 11, 10, 23, 22
00030 };
00031 
00032 int mEmcPRDFToRaw(PHCompositeNode* topNode)
00033 {
00034   PHNodeIterator iter(topNode), *j;
00035   PHNode *n1,*n2;
00036 
00037   // Find the node with the RawData object
00038   n2 = iter.findFirst("PHCompositeNode", "EMC");
00039   if (!n2) {
00040     cout << "mEmcPRDFToRAW: error, couldn't find EMC Node" << endl;
00041     return 0;
00042   }
00043   PHCompositeNode *emcNode = static_cast<PHCompositeNode*>(n2);
00044   dEmcRawDataWrapper *demcraw;
00045   j = new PHNodeIterator(emcNode);
00046 
00047   dEmcRawDataNode_t *demcrawNode = static_cast<dEmcRawDataNode_t*>(j->findFirst("PHIODataNode","dEmcRawData"));
00048   if (!demcrawNode) {
00049     cout << "mEmcPRDFToRAW: error, couldn't find dEmcRawData" << endl;
00050     return 0;
00051   }
00052   demcraw = static_cast<dEmcRawDataWrapper*>(demcrawNode->getData());
00053   delete j;
00054 
00055   // Find the node with the Event object; it is called "PRDF".
00056   n1 = iter.findFirst("PHDataNode", "PRDF");
00057   if (!n1) {
00058     return 1;
00059   }
00060 
00061   // Extract the Event object from the node.
00062   EventNode_t* node = static_cast<EventNode_t*>(n1);
00063   Event* event = node->getData();
00064   if (!event) {
00065     return 1;
00066   }
00067 
00068   // Get the relevant packets from the Event object and transfer the
00069   // data to the subsystem-specific table.
00070   Packet* p;
00071   static const int id_base     = 8000;
00072   static const int max_packets = 172;
00073   short id = 0; // unique identifier of the RAW entry
00074 
00075   for (int ipkt = 1; ipkt <= max_packets; ipkt++)
00076     {
00077       int pktid = id_base + ipkt;
00078 
00079       // get packet
00080       p = event->getPacket(pktid);
00081       if ( p != 0 )
00082         {
00083           // go through all channels, save those that have a hit
00084           for (int ifemch=0; ifemch<144; ifemch++)
00085             {
00086               // check if there is a hit (ie, if all data values are 0)
00087               if ( (p->iValue(ifemch, 0) + p->iValue(ifemch, 0) + p->iValue(ifemch, 0) +
00088                     p->iValue(ifemch, 0) + p->iValue(ifemch, 0)) == 0 )
00089                 {
00090                   continue;
00091                 }
00092 
00093               // now get the swkey and detector type (hwkey is not used?)
00094               short detector_type = -1;
00095               int arm, sector, iy, iz;
00096               int swkey = -1;
00097               int module = p->iValue(0, "MODULE");
00098               if (module != ipkt)
00099                 {
00100                   cout << "mEmcPRDFToRaw: error, FEM and packetid mismatch" << endl;
00101                 }
00102 
00103               // module number from packet should always match packetid-8000, but if not
00104               // we use the packetid as the correct identity
00105               if (ipkt <= 72)
00106                 {
00107                   arm = 0;
00108                   sector = (ipkt-1)/18;
00109                 }
00110               else
00111                 {
00112                   arm = 1;
00113                   if (ipkt <= 108)
00114                     {
00115                       sector = (ipkt-73)/18 + 2;
00116                       iy = ((ipkt-(sector*18)-1)/6)*12 + FeeToSoftwareMap[ifemch]/12;
00117                       iz = ((ipkt-(sector*18)-1)%6)*12 + FeeToSoftwareMap[ifemch]%12;
00118                     }
00119                   else             sector = (ipkt-109)/32;
00120                 }
00121 
00122               if (ipkt <= 108)
00123                 {
00124                   iy = ( ((ipkt-1)%18)/6 )*12 + FeeToSoftwareMap[ifemch]/12;
00125                   iz = ((ipkt - 1)%6)*12 + FeeToSoftwareMap[ifemch]%12;
00126                 }
00127               else
00128                 {
00129                   iy = ( ((ipkt-109)%32)/8 )*12 + FeeToSoftwareMap[ifemch]/12;
00130                   iz = ((ipkt - 109)%8)*12 + FeeToSoftwareMap[ifemch]%12;
00131                 }
00132 
00133               swkey = arm*100000 + sector*10000 + iy*100 + iz;
00134 
00135               if (ipkt < 109) detector_type = 1;        // PbSc
00136               else            detector_type = 2;        // PbGl
00137 
00138               demcraw->set_id(id, id+1);        // starts counting at 1
00139               demcraw->set_evno(id, p->iValue(0,"EVTNR") );
00140               demcraw->set_hwkey(id, 0);
00141               demcraw->set_swkey(id, swkey);
00142               demcraw->set_type(id, detector_type);
00143               demcraw->set_adclopre(id,  p->iValue(ifemch, 3) ^ 0xfff );
00144               demcraw->set_adclopost(id, p->iValue(ifemch, 1) ^ 0xfff );
00145               demcraw->set_adchipre(id,  p->iValue(ifemch, 4) ^ 0xfff );
00146               demcraw->set_adchipost(id, p->iValue(ifemch, 2) ^ 0xfff );
00147               demcraw->set_tdc(id,       p->iValue(ifemch, 0) ^ 0xfff );
00148 
00149               id++;
00150             }
00151 
00152           delete p;     // free the sucker
00153         }
00154 
00155     }
00156 
00157   demcraw->SetRowCount( id );
00158 
00159   return 0;
00160 }
00161