Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#include <EventFilter/DTRawToDigi/plugins/DTDigiToRaw.h>
#include <DataFormats/DTDigi/interface/DTDDUWords.h>

#include <cmath>
#include <iostream>

using namespace edm;
using namespace std;

DTDigiToRaw::DTDigiToRaw(const edm::ParameterSet& ps) : pset(ps) {
  debug = pset.getUntrackedParameter<bool>("debugMode", false);
  if (debug)
    cout << "[DTDigiToRaw]: constructor" << endl;
}

DTDigiToRaw::~DTDigiToRaw() {
  if (debug)
    cout << "[DTDigiToRaw]: destructor" << endl;
}

FEDRawData* DTDigiToRaw::createFedBuffers(const DTDigiCollection& digis, edm::ESHandle<DTReadOutMapping>& map) {
  int NROS = 12;
  int NROB = 25;

  vector<uint32_t> words;

  uint32_t fakeROSHeaderWord = DTROSWordType::headerControlWord << WORDCONTROLSHIFT | DTROSWordType::rosTypeWord
                                                                                          << WORDTYPESHIFT;

  uint32_t fakeROSTrailerWord = DTROSWordType::trailerControlWord << WORDCONTROLSHIFT | DTROSWordType::rosTypeWord
                                                                                            << WORDTYPESHIFT;

  int NWords = 2;
  words.push_back(0);
  words.push_back(0);

  DTDigiCollection::DigiRangeIterator detUnitIt;
  detUnitIt = digis.begin();

  bool b_ros[12] = {false, false, false, false, false, false, false, false, false, false, false, false};
  vector<uint32_t> w_ROBROS[12][25];

  for (detUnitIt = digis.begin(); detUnitIt != digis.end(); ++detUnitIt) {
    const DTLayerId layerId = (*detUnitIt).first;
    const DTDigiCollection::Range& digiRange = (*detUnitIt).second;
    // Loop over all digis in the given range

    for (DTDigiCollection::const_iterator digi = digiRange.first; digi != digiRange.second; digi++) {
      int dduId = -1, rosId = -1, robId = -1, tdcId = -1, channelId = -1;

      int layer = layerId.layer();
      DTSuperLayerId superlayerID = layerId.superlayerId();
      int superlayer = superlayerID.superlayer();
      DTChamberId chamberID = superlayerID.chamberId();
      int station = chamberID.station();
      int wheel = chamberID.wheel();
      int sector = chamberID.sector();

      int searchstatus = map->geometryToReadOut(wheel,
                                                station,
                                                sector,
                                                superlayer,
                                                layer,
                                                (*digi).wire(),  //"input"
                                                dduId,
                                                rosId,
                                                robId,
                                                tdcId,
                                                channelId);  //"output"

      if (searchstatus == 1 && debug)
        cout << "[DTDigiToRaw]: warning, geometryToReadOut status = 1" << endl;

      //create FED corresponding to current ddu
      if (dduID_ != dduId)
        continue;

      DTTDCMeasurementWord dttdc_mw;
      uint32_t word;
      int ntdc = (*digi).countsTDC();
      dttdc_mw.set(word, 0, 0, 0, tdcId, channelId, ntdc * 4);  //FIXME

      //provisional fix
      DTTDCMeasurementWord tdcMeasurementWord(word);
      int tdcIDCheck = tdcMeasurementWord.tdcID();
      int tdcChannelCheck = tdcMeasurementWord.tdcChannel();
      int tdcCountsCheck = tdcMeasurementWord.tdcTime();
      if (tdcIDCheck == tdcId && channelId == tdcChannelCheck && ntdc == tdcCountsCheck) {
        if (rosId <= NROS && rosId > 0)
          b_ros[rosId - 1] = true;
        else if (debug) {
          cout << "[DTDigiToRaw]: invalid value for rosId" << endl;
        }

        w_ROBROS[rosId - 1][robId].push_back(word);
      }
    }
  }

  uint32_t therosList = 0;
  for (int i_ros = 0; i_ros < NROS; i_ros++) {
    if (b_ros[i_ros])
      therosList += uint32_t(pow(2.0, i_ros));
  }

  if (debug)
    cout << "[DTDigiToRaw]: therosList = " << therosList << endl;

  for (int i_ros = 0; i_ros < NROS; i_ros++) {
    if (b_ros[i_ros]) {
      words.push_back(fakeROSHeaderWord);
      NWords++;
    }

    for (int i_rob = 0; i_rob < NROB; i_rob++) {
      vector<uint32_t>::const_iterator i_robros;
      if (w_ROBROS[i_ros][i_rob].begin() != w_ROBROS[i_ros][i_rob].end()) {
        uint32_t word = 0;
        DTROBHeaderWord rob_header;
        rob_header.set(word, i_rob, 0, 0);
        //static void set(uint32_t &word, int rob_id, int event_id, int bunch_id)
        words.push_back(word);
        NWords++;

        int n_robros = 0;
        for (i_robros = w_ROBROS[i_ros][i_rob].begin(); i_robros != w_ROBROS[i_ros][i_rob].end(); i_robros++) {
          NWords++;
          words.push_back((*i_robros));
          n_robros++;
        }

        NWords++;
        DTROBTrailerWord rob_trailer;
        rob_trailer.set(word, i_rob, 0, n_robros + 2);
        //static void set(uint32_t &word, int rob_id, int event_id, int word_count)
        words.push_back(word);
      }
    }

    if (b_ros[i_ros]) {
      words.push_back(fakeROSTrailerWord);
      NWords++;
    }
  }

  if (NWords % 2 == 1) {
    words.push_back(0);
    NWords++;
  }

  words.push_back(0);
  words.push_back(0);
  //include rosList in raw data information
  uint32_t secondstatusword = therosList << 16;
  words.push_back(secondstatusword);

  words.push_back(0);
  words.push_back(0);
  words.push_back(0);

  // Write Raw Data
  int dataSize = words.size() * sizeof(Word32);
  FEDRawData* rawData = new FEDRawData(dataSize);
  Word64* word64 = reinterpret_cast<Word64*>(rawData->data());
  for (unsigned int i = 0; i < words.size(); i += 2) {
    *word64 = (Word64(words[i]) << 32) | words[i + 1];
    word64++;
  }

  return rawData;
}

void DTDigiToRaw::SetdduID(int x) { dduID_ = x; }