Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:14

0001 /** \file
0002  *
0003  *  $Date: 2010/03/12 10:04:19 $
0004  *  $Revision: 1.14 $
0005  *  \author M. Zanetti
0006  */
0007 
0008 #include <IORawData/DTCommissioning/plugins/DTROS25FileReader.h>
0009 #include <IORawData/DTCommissioning/plugins/DTFileReaderHelpers.h>
0010 
0011 #include <DataFormats/FEDRawData/interface/FEDHeader.h>
0012 #include <DataFormats/FEDRawData/interface/FEDTrailer.h>
0013 #include <DataFormats/FEDRawData/interface/FEDNumbering.h>
0014 
0015 #include "DataFormats/Provenance/interface/EventID.h"
0016 #include <DataFormats/Provenance/interface/Timestamp.h>
0017 #include <DataFormats/FEDRawData/interface/FEDRawData.h>
0018 #include <DataFormats/FEDRawData/interface/FEDRawDataCollection.h>
0019 
0020 #include <FWCore/ParameterSet/interface/ParameterSet.h>
0021 #include <FWCore/Utilities/interface/Exception.h>
0022 
0023 #include <string>
0024 #include <iosfwd>
0025 #include <iostream>
0026 #include <algorithm>
0027 
0028 using namespace std;
0029 using namespace edm;
0030 
0031 DTROS25FileReader::DTROS25FileReader(const edm::ParameterSet& pset) : runNumber(1), eventNumber(0) {
0032   const string& filename = pset.getUntrackedParameter<string>("fileName");
0033 
0034   inputFile.open(filename.c_str());
0035   if (inputFile.fail()) {
0036     throw cms::Exception("InputFileMissing") << "DTROS25FileReader: the input file: " << filename << " is not present";
0037   }
0038   produces<FEDRawDataCollection>();
0039 }
0040 
0041 DTROS25FileReader::~DTROS25FileReader() { inputFile.close(); }
0042 
0043 int DTROS25FileReader::fillRawData(Event& e,
0044                                    //                  Timestamp& tstamp,
0045                                    FEDRawDataCollection*& data) {
0046   EventID eID = e.id();
0047   data = new FEDRawDataCollection();
0048 
0049   vector<uint32_t> eventData;
0050   size_t estimatedEventDimension = 102400;  // dimensione hardcoded
0051   eventData.reserve(estimatedEventDimension);
0052   uint32_t word = 0;
0053 
0054   try {
0055     bool marked = false;
0056 
0057     // getting the data word by word from the file
0058     // do it until you get the ROS25 trailer
0059     while (!isTrailer(word)) {
0060       // get the first word
0061       int nread = inputFile.read(dataPointer<uint32_t>(&word), rosWordLenght);
0062 
0063       // WARNING!!! ||swapping it|| (Check whether it is necessary)
0064       swap(word);
0065 
0066       if (nread <= 0)
0067         throw 1;
0068 
0069       // get the ROS25 header
0070       if (isHeader(word))
0071         marked = true;
0072 
0073       // from now on fill the eventData with the ROS data
0074       if (marked) {
0075         eventData.push_back(word);
0076       }
0077     }
0078 
0079     // next event reading will start with meaningless trailer+header from DTLocalDAQ
0080     // those will be skipped automatically when seeking for the ROS25 header
0081 
0082     //if (eventData.size() > estimatedEventDimension) throw 2;
0083 
0084     // Setting the Event ID
0085     eID = EventID(runNumber, 1U, eventNumber);
0086 
0087     // eventDataSize = (Number Of Words)* (Word Size)
0088     int eventDataSize = eventData.size() * rosWordLenght;
0089     // It has to be a multiple of 8 bytes. if not, adjust the size of the FED payload
0090     int adjustment = (eventDataSize / 4) % 2 == 1 ? 4 : 0;
0091 
0092     // The FED ID is always the first in the DT range
0093     FEDRawData& fedRawData = data->FEDData(FEDNumbering::MINDTFEDID);
0094     fedRawData.resize(eventDataSize + adjustment);
0095 
0096     copy(reinterpret_cast<unsigned char*>(&eventData[0]),
0097          reinterpret_cast<unsigned char*>(&eventData[0]) + eventDataSize,
0098          fedRawData.data());
0099 
0100     return true;
0101   }
0102 
0103   catch (int i) {
0104     if (i == 1) {
0105       cout << "[DTROS25FileReader]: ERROR! failed to get the trailer" << endl;
0106       delete data;
0107       data = nullptr;
0108       return false;
0109     } else {
0110       cout << "[DTROS25FileReader]:"
0111            << " ERROR! ROS data exceeding estimated event dimension. Event size = " << eventData.size() << endl;
0112       delete data;
0113       data = nullptr;
0114       return false;
0115     }
0116   }
0117 }
0118 
0119 void DTROS25FileReader::produce(Event& e, EventSetup const& es) {
0120   edm::Handle<FEDRawDataCollection> rawdata;
0121   FEDRawDataCollection* fedcoll = nullptr;
0122   fillRawData(e, fedcoll);
0123   std::unique_ptr<FEDRawDataCollection> bare_product(fedcoll);
0124   e.put(std::move(bare_product));
0125 }
0126 
0127 void DTROS25FileReader::swap(uint32_t& word) {
0128   twoNibble* newWorld = reinterpret_cast<twoNibble*>(&word);
0129 
0130   uint16_t msBits_tmp = newWorld->msBits;
0131   newWorld->msBits = newWorld->lsBits;
0132   newWorld->lsBits = msBits_tmp;
0133 }
0134 
0135 bool DTROS25FileReader::isHeader(uint32_t word) {
0136   bool it_is = false;
0137   if ((word >> 24) == 31) {
0138     it_is = true;
0139     ++eventNumber;
0140   }
0141 
0142   return it_is;
0143 }
0144 
0145 bool DTROS25FileReader::isTrailer(uint32_t word) {
0146   bool it_is = false;
0147   if ((word >> 24) == 63) {
0148     it_is = true;
0149   }
0150 
0151   return it_is;
0152 }
0153 
0154 bool DTROS25FileReader::checkEndOfFile() {
0155   bool retval = false;
0156   if (inputFile.eof())
0157     retval = true;
0158   return retval;
0159 }