Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:23

0001 /** \file
0002  *
0003  *  $Date: 2015/07/10 
0004  *  \author M.C Fouz 
0005  *  Updated from class DTROS8FileReader to a new class: DTNewROS8FileReader  
0006  *    to read ROS8 for MB1 GIF++ 2015 data  (for CMSSW_5X versions)
0007  *    include also the PU Data (Chamber Trigger info)
0008  *  
0009  */
0010 
0011 #include <IORawData/DTCommissioning/plugins/DTNewROS8FileReader.h>
0012 #include <IORawData/DTCommissioning/plugins/DTFileReaderHelpers.h>
0013 
0014 #include <DataFormats/FEDRawData/interface/FEDHeader.h>
0015 #include <DataFormats/FEDRawData/interface/FEDTrailer.h>
0016 #include <DataFormats/FEDRawData/interface/FEDNumbering.h>
0017 
0018 #include "DataFormats/Provenance/interface/EventID.h"
0019 #include <DataFormats/Provenance/interface/Timestamp.h>
0020 #include <DataFormats/FEDRawData/interface/FEDRawData.h>
0021 #include <DataFormats/FEDRawData/interface/FEDRawDataCollection.h>
0022 
0023 #include <FWCore/ParameterSet/interface/ParameterSet.h>
0024 #include <FWCore/Utilities/interface/Exception.h>
0025 
0026 #include <string>
0027 #include <iosfwd>
0028 #include <iostream>
0029 #include <algorithm>
0030 
0031 using namespace std;
0032 using namespace edm;
0033 
0034 DTNewROS8FileReader::DTNewROS8FileReader(const edm::ParameterSet& pset) : runNumber(1), eventNum(0) {
0035   const string& filename = pset.getUntrackedParameter<string>("fileName");
0036 
0037   inputFile.open(filename.c_str());
0038   if (inputFile.fail()) {
0039     throw cms::Exception("InputFileMissing")
0040         << "DTNewROS8FileReader: the input file: " << filename << " is not present";
0041   }
0042 
0043   produces<FEDRawDataCollection>();
0044 }
0045 
0046 DTNewROS8FileReader::~DTNewROS8FileReader() { inputFile.close(); }
0047 
0048 int DTNewROS8FileReader::fillRawData(Event& e,
0049                                      //               Timestamp& tstamp,
0050                                      FEDRawDataCollection*& data) {
0051   EventID eID = e.id();
0052   data = new FEDRawDataCollection();
0053 
0054   try {
0055     /* Structure of the DATA 
0056 
0057      1.- NUMBER OF WORDS (it includes this word and the last counter)
0058      2.- HEADER: 8 Words
0059            Header word 0: run number
0060            Header word 1: spill number
0061            Header word 2: event number
0062            Header word 3: reserved
0063            Header word 4: ROS data offset
0064            Header word 5: PU data offset
0065            Header word 6: reserved
0066            Header word 7: reserved
0067      3.- ROS DATA ==> DECODE BY THE DTROS8Unpacker.cc in EventFilter 
0068            3.1 NUMBER OF ROS WORDS (it includes this counter)
0069            3.2 Lock status Word
0070            3.3 ROS DATA
0071                  3.3.1 ROS BOARD ID/ROS CHANNEL - 1 word
0072                  3.3.2 GLOBAL HEADER - 1 word
0073                  3.3.3 TDC Data Words - X words (depends on event)
0074                  3.3.4 GLOBAL TRAILER
0075      4.- PU DATA (trigger) ==> DECODE BY THE DTROS8Unpacker.cc in EventFilter  
0076                  Not always. If chamber is autotriggered doesn't have PU data except
0077                              the Number of PU words
0078            4.1.- NUMBER OF WORDS (it includes this word, always present 
0079                                   even if there is not PU data)
0080            4.2.- PATTERN UNIT ID - 1 word (data counter & PU-ID)
0081            4.3.- DATA - X Words ?????
0082      5.- NUMBER OF WORDS (include this word and the first counter)
0083     */
0084 
0085     if (checkEndOfFile())
0086       throw 1;
0087 
0088     // Number of words in the header, including the first word of header that is the number of words in the event
0089     int numberEventHeadWords = 8;
0090 
0091     //1.- Get the total NUMBER OF WORDs from the 1st word in the payload
0092     int numberOfWords = 0;
0093     int nread = 0;
0094     nread = inputFile.read(dataPointer<int>(&numberOfWords), ros8WordLenght);  // ros8WordLength=4
0095     if (nread <= 0)
0096       throw 1;
0097 
0098     // inputFile.ignore(4*(numberEventHeadWords-1)); // Skip the header. The first word of header has been already read
0099 
0100     //2.- Get the HEADER  ============================================================================
0101     int datahead[numberEventHeadWords];
0102     for (int iih = 0; iih < numberEventHeadWords; iih++) {
0103       nread = inputFile.read(dataPointer<int>(&datahead[iih]), ros8WordLenght);
0104     }
0105 
0106     //3.- ROS DATA  &  4.- PU DATA (Trigger)   =======================================================
0107     // Get the event data (all words but the header and the first and the last counter)
0108     int numberOfDataWords = numberOfWords - numberEventHeadWords - 2;
0109     int* eventData = new int[numberOfDataWords];
0110     nread = inputFile.read(dataPointer<int>(eventData), numberOfDataWords * ros8WordLenght);
0111     if (nread <= 0)
0112       throw 1;
0113 
0114     //5.- Get the total NUMBER OF WORDs from the last word in the payload
0115     // Check that the event data size corresponds to the 1st word datum
0116     int LastCounter = 0;
0117     nread = inputFile.read(dataPointer<int>(&LastCounter), ros8WordLenght);
0118     if (nread <= 0)
0119       throw 1;
0120 
0121     if (LastCounter != numberOfWords) {
0122       cout << "[DTNewROS8FileReader]: word counter mismatch exception: " << numberOfWords << " " << LastCounter << endl;
0123       throw 99;
0124     }
0125 
0126     //The first word in the header is the run number
0127     runNumber = datahead[0];
0128     cout << "[DTNewROS8FileReader]: Run Number: " << dec << runNumber << endl;
0129 
0130     //The third word in the header is the event number (without any reset)
0131     //eventNum= datahead[2];  //francos system
0132     eventNum = datahead[1];  //linux system
0133     //cout<<"Ć«ventNum  "<<dec<<eventNum<<endl;
0134     if (eventNum < 1)
0135       eventNum = 1;  // Event number must start at 1 but at TDCs it starts at cero,if not the program crashes
0136                      // files used for testing start in 1, but... just in case...
0137 
0138     eID = EventID(runNumber, 1U, eventNum);
0139 
0140     //cout << " EEEEE eID: " << eID << endl;
0141     // Even if we have introducing the correct runNumber when running the runNumber appears always as =1
0142 
0143     int eventDataSize = numberOfDataWords * ros8WordLenght;
0144     int adjustment = (eventDataSize / 4) % 2 == 1 ? 4 : 0;
0145 
0146     // The FED ID is always the first in the DT range
0147     FEDRawData& fedRawData = data->FEDData(FEDNumbering::MINDTFEDID);
0148     fedRawData.resize(eventDataSize + adjustment);  // the size must be multiple of 8 bytes
0149 
0150     // Passing the data to the Event
0151     copy(reinterpret_cast<unsigned char*>(eventData),
0152          reinterpret_cast<unsigned char*>(eventData) + eventDataSize,
0153          fedRawData.data());
0154 
0155     // needed to get rid of memory leaks (?)
0156     delete[] eventData;
0157 
0158     return true;
0159 
0160   } catch (int i) {
0161     if (i == 1) {
0162       cout << "[DTNewROS8FileReader]: END OF FILE REACHED. "
0163            << "No information read for the requested event" << endl;
0164       delete data;
0165       data = nullptr;
0166       return false;
0167     } else {
0168       cout << "[DTNewROS8FileReader]: PROBLEM WITH EVENT INFORMATION ON THE FILE. "
0169            << "EVENT DATA READING FAILED  code= " << i << endl;
0170       delete data;
0171       data = nullptr;
0172       return false;
0173     }
0174   }
0175 }
0176 
0177 void DTNewROS8FileReader::produce(Event& e, EventSetup const& es) {
0178   edm::Handle<FEDRawDataCollection> rawdata;
0179   FEDRawDataCollection* fedcoll = nullptr;
0180   fillRawData(e, fedcoll);
0181   std::unique_ptr<FEDRawDataCollection> bare_product(fedcoll);
0182   e.put(std::move(bare_product));
0183 }
0184 
0185 bool DTNewROS8FileReader::checkEndOfFile() {
0186   bool retval = false;
0187   if (inputFile.eof())
0188     retval = true;
0189   return retval;
0190 }