CMS 3D CMS Logo

HcalPacker.cc

Go to the documentation of this file.
00001 #include "EventFilter/HcalRawToDigi/interface/HcalPacker.h"
00002 #include "EventFilter/HcalRawToDigi/interface/HcalHTRData.h"
00003 #include "EventFilter/HcalRawToDigi/interface/HcalDCCHeader.h"
00004 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00007 #include "FWCore/Utilities/interface/CRC16.h"
00008 
00009 HcalPacker::Collections::Collections() {
00010   hbhe=0;
00011   hoCont=0;
00012   hfCont=0;
00013   tpCont=0;
00014   zdcCont=0;
00015   calibCont=0;
00016 }
00017 
00018 template <class Coll, class DetIdClass> 
00019 int process(const Coll* pt, const DetId& did, unsigned short* buffer, int& presamples) {
00020   if (pt==0) return 0;
00021   int size=0;
00022   typename Coll::const_iterator i=pt->find(DetIdClass(did));
00023   if (i!=pt->end()) {
00024     presamples=i->presamples();
00025     size=i->size();
00026     for (int j=0; j<size; j++) 
00027       buffer[j]=(*i)[j].raw();
00028   }
00029   return size;
00030 }
00031 
00032 static unsigned char processTrig(const HcalTrigPrimDigiCollection* pt, const HcalTrigTowerDetId& tid, unsigned short* buffer) {
00033   if (pt==0) return 0;
00034   int size=0;
00035   HcalTrigPrimDigiCollection::const_iterator i=pt->find(tid);
00036   if (i!=pt->end()) {
00037     int presamples=i->presamples();
00038     int samples=i->size();
00039 
00040     for (int j=0; j<samples; j++) {
00041        buffer[j]=(*i)[j].raw();
00042        if (j==presamples) buffer[j]|=0x0200;
00043     }
00044   }
00045   return size;
00046 }
00047 
00048 int HcalPacker::findSamples(const DetId& did, const Collections& inputs,
00049                             unsigned short* buffer, int &presamples) {
00050   if (did.det()!=DetId::Hcal) return 0;
00051   int size=0;
00052   HcalGenericDetId genId(did);
00053   
00054   switch (genId.genericSubdet()) {
00055   case(HcalGenericDetId::HcalGenBarrel):
00056   case(HcalGenericDetId::HcalGenEndcap):
00057     size=process<HBHEDigiCollection,HcalDetId>(inputs.hbhe,did,buffer,presamples);
00058     break;
00059   case(HcalGenericDetId::HcalGenOuter):
00060     size=process<HODigiCollection,HcalDetId>(inputs.hoCont,did,buffer,presamples);
00061     break;
00062   case(HcalGenericDetId::HcalGenForward):
00063     size=process<HFDigiCollection,HcalDetId>(inputs.hfCont,did,buffer,presamples);
00064     break;
00065   case(HcalGenericDetId::HcalGenZDC):
00066     size=process<ZDCDigiCollection,HcalZDCDetId>(inputs.zdcCont,did,buffer,presamples);
00067     break;
00068   case(HcalGenericDetId::HcalGenCalibration):
00069     size=process<HcalCalibDigiCollection,HcalCalibDetId>(inputs.calibCont,did,buffer,presamples);
00070     break;
00071   default: size=0;
00072   }
00073   return size;
00074 }
00075 
00076 void HcalPacker::pack(int fedid, int dccnumber,
00077                       int nl1a, int orbitn, int bcn,
00078                       const Collections& inputs, 
00079                       const HcalElectronicsMap& emap,
00080                       FEDRawData& output) {
00081   std::vector<unsigned short> precdata(HcalHTRData::CHANNELS_PER_SPIGOT*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL);
00082   std::vector<unsigned short> trigdata(HcalHTRData::CHANNELS_PER_SPIGOT*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL);
00083   std::vector<unsigned char> preclen(HcalHTRData::CHANNELS_PER_SPIGOT);
00084   std::vector<unsigned char> triglen(HcalHTRData::CHANNELS_PER_SPIGOT);
00085   static const int HTRFormatVersion=3;
00086 
00087   HcalHTRData spigots[15];
00088   // loop over all valid channels in the given dcc, spigot by spigot.
00089   for (int spigot=0; spigot<15; spigot++) {
00090     spigots[spigot].allocate(HTRFormatVersion);
00091     HcalElectronicsId exampleEId;
00092     int npresent=0;
00093     int presamples=-1, samples=-1;
00094     for (int fiber=1; fiber<=8; fiber++) 
00095       for (int fiberchan=0; fiberchan<3; fiberchan++) {
00096         int linear=(fiber-1)*3+fiberchan;
00097         HcalQIESample chanSample(0,0,fiber,fiberchan,false,false);
00098         unsigned short chanid=chanSample.raw()&0xF800;
00099         preclen[linear]=0;
00100 
00101         HcalElectronicsId partialEid(fiberchan,fiber,spigot,dccnumber);
00102         // does this partial id exist?
00103         HcalElectronicsId fullEid;
00104         HcalGenericDetId genId;
00105         if (!emap.lookup(partialEid,fullEid,genId)) continue;
00106 
00107 
00108         // next, see if there is a digi with this id
00109         unsigned short* database=&(precdata[linear*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL]);
00110         int mypresamples;
00111         int mysamples=findSamples(genId,inputs,database,mypresamples);
00112 
00113         if (mysamples>0) {
00114           if (samples<0) samples=mysamples;
00115           else if (samples!=mysamples) {
00116             edm::LogError("HCAL") << "Mismatch of samples in a single HTR (unsupported) " << mysamples << " != " << samples;
00117             continue;
00118           }
00119           if (presamples<0) {
00120             presamples=mypresamples;
00121             exampleEId=fullEid;
00122           } else if (mypresamples!=presamples) {
00123             edm::LogError("HCAL") << "Mismatch of presamples in a single HTR (unsupported) " << mypresamples << " != " << presamples;
00124             continue;       
00125           }
00126           for (int ii=0; ii<samples; ii++)
00127             database[ii]=(database[ii]&0x7FF)|chanid;
00128           preclen[linear]=(unsigned char)(samples);
00129           npresent++;
00130         }       
00131       }
00132     for (int slb=1; slb<=6; slb++) 
00133       for (int slbchan=0; slbchan<=3; slbchan++) {
00134         int linear=(slb-1)*4+slbchan;
00135         HcalTriggerPrimitiveSample idCvt(0,0,slb,slbchan);
00136         unsigned short chanid=idCvt.raw()&0xF800;
00137         triglen[linear]=0;
00138 
00139         HcalElectronicsId partialEid(slbchan,slb,spigot,dccnumber,0,0,0);
00140         // does this partial id exist?
00141         HcalElectronicsId fullEid;
00142         HcalTrigTowerDetId tid;
00143         if (!emap.lookup(partialEid,fullEid,tid)) {
00144 //        std::cout << "TPGPACK : no match for " << partialEid << std::endl;
00145           continue;
00146         }  //else std::cout << "TPGPACK : converted " << partialEid << " to " << fullEid << "/" << tid << std::endl;
00147           
00148         // finally, what about a trigger channel?
00149         if (!tid.null()) {
00150           unsigned short* trigbase=&(trigdata[linear*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL]);
00151           triglen[linear]=processTrig(inputs.tpCont,tid,trigbase);
00152           
00153           for (unsigned char q=0; q<triglen[linear]; q++)
00154             trigbase[q]=(trigbase[q]&0x7FF)|chanid;
00155         }
00156       }
00158     if (npresent>0) {
00159       spigots[spigot].pack(&(preclen[0]),&(precdata[0]),
00160                            &(triglen[0]),&(trigdata[0]),
00161                            false);
00162       static const int pipeline=0x22;
00163       static const int firmwareRev=0;
00164       int submodule=exampleEId.htrTopBottom()&0x1;
00165       submodule|=(exampleEId.htrSlot()&0x1F)<<1;
00166       submodule|=(exampleEId.readoutVMECrateId()&0x1f)<<6;
00167       spigots[spigot].packHeaderTrailer(nl1a,
00168                                         bcn,
00169                                         submodule,
00170                                         orbitn,
00171                                         pipeline,
00172                                         samples,
00173                                         presamples,
00174                                         firmwareRev);
00175       
00176     }
00177   }
00178   // calculate the total length, and resize the FEDRawData
00179   int theSize=0;
00180   for (int spigot=0; spigot<15; spigot++) {
00181     theSize+=spigots[spigot].getRawLength()*sizeof(unsigned short);
00182   }
00183   theSize+=sizeof(HcalDCCHeader)+8; // 8 for trailer
00184   theSize+=(8-(theSize%8))%8; // even number of 64-bit words.
00185   output.resize(theSize);
00186   
00187   // construct the bare DCC Header
00188   HcalDCCHeader* dcc=(HcalDCCHeader*)(output.data());
00189   dcc->clear();
00190   dcc->setHeader(fedid,bcn,nl1a,orbitn);
00191 
00192   // pack the HTR data into the FEDRawData block using HcalDCCHeader
00193   for (int spigot=0; spigot<15; spigot++) {
00194     if (spigots[spigot].getRawLength()>0)
00195       dcc->copySpigotData(spigot,spigots[spigot],true,0);
00196   }
00197   // trailer
00198   FEDTrailer fedTrailer(output.data()+(output.size()-8));
00199   fedTrailer.set(output.data()+(output.size()-8),
00200     output.size()/8,
00201     evf::compute_crc(output.data(),output.size()), 0, 0);
00202 }

Generated on Tue Jun 9 17:34:41 2009 for CMSSW by  doxygen 1.5.4