CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/EventFilter/CastorRawToDigi/src/CastorPacker.cc

Go to the documentation of this file.
00001 #include "EventFilter/CastorRawToDigi/interface/CastorPacker.h"
00002 #include "EventFilter/CastorRawToDigi/interface/CastorCollections.h"
00003 #include "EventFilter/HcalRawToDigi/interface/HcalHTRData.h"
00004 #include "EventFilter/HcalRawToDigi/interface/HcalDCCHeader.h"
00005 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00008 #include "FWCore/Utilities/interface/CRC16.h"
00009 
00010 template <class Coll, class DetIdClass> 
00011 int process(const Coll* pt, const DetId& did, unsigned short* buffer, int& presamples) {
00012   if (pt==0) return 0;
00013   int size=0;
00014   typename Coll::const_iterator i=pt->find(DetIdClass(did));
00015   if (i!=pt->end()) {
00016     presamples=i->presamples();
00017     size=i->size();
00018     for (int j=0; j<size; j++) 
00019       buffer[j]=(*i)[j].raw();
00020   }
00021   return size;
00022 }
00023 
00024 int CastorPacker::findSamples(const DetId& did, const CastorCollections& inputs,
00025                             unsigned short* buffer, int &presamples) {
00026 
00027   if (did.det()!=DetId::Calo) return 0;
00028   int size=0;
00029   HcalCastorDetId genId(did);
00030 
00031   size=process<CastorDigiCollection,HcalCastorDetId>(inputs.castorCont,did,buffer,presamples);
00032 
00033   return size;
00034 }
00035 
00036 void CastorPacker::pack(int fedid, int dccnumber,
00037                       int nl1a, int orbitn, int bcn,
00038                       const CastorCollections& inputs, 
00039                       const CastorElectronicsMap& emap,
00040                       FEDRawData& output) {
00041   std::vector<unsigned short> precdata(HcalHTRData::CHANNELS_PER_SPIGOT*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL);
00042   std::vector<unsigned short> trigdata(HcalHTRData::CHANNELS_PER_SPIGOT*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL);
00043   std::vector<unsigned char> preclen(HcalHTRData::CHANNELS_PER_SPIGOT);
00044   std::vector<unsigned char> triglen(HcalHTRData::CHANNELS_PER_SPIGOT);
00045   static const int HTRFormatVersion=3;
00046 
00047   HcalHTRData spigots[15];
00048   // loop over all valid channels in the given dcc, spigot by spigot.
00049   for (int spigot=0; spigot<15; spigot++) {
00050     spigots[spigot].allocate(HTRFormatVersion);
00051     CastorElectronicsId exampleEId;
00052     int npresent=0;
00053     int presamples=-1, samples=-1;
00054     for (int fiber=1; fiber<=8; fiber++) 
00055       for (int fiberchan=0; fiberchan<3; fiberchan++) {
00056         int linear=(fiber-1)*3+fiberchan;
00057         HcalQIESample chanSample(0,0,fiber,fiberchan,false,false);
00058         unsigned short chanid=chanSample.raw()&0xF800;
00059         preclen[linear]=0;
00060 
00061         CastorElectronicsId partialEid(fiberchan,fiber,spigot,dccnumber);
00062         // does this partial id exist?
00063         CastorElectronicsId fullEid;
00064         HcalGenericDetId genId;
00065         if (!emap.lookup(partialEid,fullEid,genId)) continue;
00066 
00067         // next, see if there is a digi with this id
00068         unsigned short* database=&(precdata[linear*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL]);
00069         int mypresamples;
00070         int mysamples=findSamples(genId,inputs,database,mypresamples);
00071 
00072         if (mysamples>0) {
00073           if (samples<0) samples=mysamples;
00074           else if (samples!=mysamples) {
00075             edm::LogError("CASTOR") << "Mismatch of samples in a single HTR (unsupported) " << mysamples << " != " << samples;
00076             continue;
00077           }
00078           if (presamples<0) {
00079             presamples=mypresamples;
00080             exampleEId=fullEid;
00081           } else if (mypresamples!=presamples) {
00082             edm::LogError("CASTOR") << "Mismatch of presamples in a single HTR (unsupported) " << mypresamples << " != " << presamples;
00083             continue;       
00084           }
00085           for (int ii=0; ii<samples; ii++)
00086             database[ii]=(database[ii]&0x7FF)|chanid;
00087           preclen[linear]=(unsigned char)(samples);
00088           npresent++;
00089         }       
00090       }
00092     if (npresent>0) {
00093       spigots[spigot].pack(&(preclen[0]),&(precdata[0]),
00094                            &(triglen[0]),&(trigdata[0]),
00095                            false);
00096       static const int pipeline=0x22;
00097       static const int firmwareRev=0;
00098       int submodule=exampleEId.htrTopBottom()&0x1;
00099       submodule|=(exampleEId.htrSlot()&0x1F)<<1;
00100       submodule|=(exampleEId.readoutVMECrateId()&0x1f)<<6;
00101       spigots[spigot].packHeaderTrailer(nl1a,
00102                                         bcn,
00103                                         submodule,
00104                                         orbitn,
00105                                         pipeline,
00106                                         samples,
00107                                         presamples,
00108                                         firmwareRev);
00109       
00110     }
00111   }
00112   // calculate the total length, and resize the FEDRawData
00113   int theSize=0;
00114   for (int spigot=0; spigot<15; spigot++) {
00115     theSize+=spigots[spigot].getRawLength()*sizeof(unsigned short);
00116   }
00117   theSize+=sizeof(HcalDCCHeader)+8; // 8 for trailer
00118   theSize+=(8-(theSize%8))%8; // even number of 64-bit words.
00119   output.resize(theSize);
00120   
00121   // construct the bare DCC Header
00122   HcalDCCHeader* dcc=(HcalDCCHeader*)(output.data());
00123   dcc->clear();
00124   dcc->setHeader(fedid,bcn,nl1a,orbitn);
00125 
00126   // pack the HTR data into the FEDRawData block using HcalDCCHeader
00127   for (int spigot=0; spigot<15; spigot++) {
00128     if (spigots[spigot].getRawLength()>0)
00129       dcc->copySpigotData(spigot,spigots[spigot],true,0);
00130   }
00131   // trailer
00132   FEDTrailer fedTrailer(output.data()+(output.size()-8));
00133   fedTrailer.set(output.data()+(output.size()-8),
00134     output.size()/8,
00135     evf::compute_crc(output.data(),output.size()), 0, 0);
00136 
00137 }