/**
* Sets up Signal Channel fields
*/
JPetSigCh TimeWindowCreatorTools::generateSigCh(
  double tdcChannelTime, const JPetTOMBChannel& channel,
  map<unsigned int, vector<double>>& timeCalibrationMap,
  map<unsigned int, vector<double>>& thresholdsMap,
  JPetSigCh::EdgeType edge, bool setTHRValuesFromChannels
) {
  JPetSigCh sigCh;
  sigCh.setValue(1000.*(tdcChannelTime
    + UniversalFileLoader::getConfigurationParameter(timeCalibrationMap, channel.getChannel())
  ));
  sigCh.setType(edge);
  sigCh.setTOMBChannel(channel);
  sigCh.setPM(channel.getPM());
  sigCh.setFEB(channel.getFEB());
  sigCh.setTRB(channel.getTRB());
  sigCh.setDAQch(channel.getChannel());
  sigCh.setThresholdNumber(channel.getLocalChannelNumber());
  if(setTHRValuesFromChannels) {
    sigCh.setThreshold(channel.getThreshold());
  } else {
    sigCh.setThreshold(
      UniversalFileLoader::getConfigurationParameter(thresholdsMap, channel.getChannel())
    );
  }
  return sigCh;
}
JPetSigCh TimeWindowCreator::generateSigCh(const JPetTOMBChannel& channel, JPetSigCh::EdgeType edge) const
{
  JPetSigCh sigch;
  sigch.setDAQch(channel.getChannel());
  sigch.setType(edge);
  sigch.setThresholdNumber(channel.getLocalChannelNumber());
  sigch.setThreshold(channel.getThreshold());
  sigch.setPM(channel.getPM());
  sigch.setFEB(channel.getFEB());
  sigch.setTRB(channel.getTRB());
  sigch.setTOMBChannel(channel);
  return sigch;
}
/**
 * Building all Signal Chnnels from one TDC
 */
vector<JPetSigCh> TimeWindowCreatorTools::buildSigChs(
  TDCChannel* tdcChannel, const JPetTOMBChannel& tombChannel,
  map<unsigned int, vector<double>>& timeCalibrationMap,
  map<unsigned int, vector<double>>& thresholdsMap,
  double maxTime, double minTime, bool setTHRValuesFromChannels,
  JPetStatistics& stats, bool saveHistos
){
  vector<JPetSigCh> allTDCSigChs;
  // Loop over all entries on leading edge in current TOMBChannel and create SigCh
  for (int j = 0; j < tdcChannel->GetLeadHitsNum(); j++) {
    auto leadTime = tdcChannel->GetLeadTime(j);
    if (leadTime > maxTime || leadTime < minTime ) { continue; }
    auto leadSigCh = generateSigCh(
      leadTime, tombChannel, timeCalibrationMap, thresholdsMap,
      JPetSigCh::Leading, setTHRValuesFromChannels
    );
    allTDCSigChs.push_back(leadSigCh);
    if (saveHistos){
      stats.getHisto1D(Form("pm_occupation_thr%d", tombChannel.getLocalChannelNumber()))
        ->Fill(tombChannel.getPM().getID());
    }
  }
  // Loop over all entries on trailing edge in current TOMBChannel and create SigCh
  for (int j = 0; j < tdcChannel->GetTrailHitsNum(); j++) {
    auto trailTime = tdcChannel->GetTrailTime(j);
    if (trailTime > maxTime || trailTime < minTime ) { continue; }
    auto trailSigCh = generateSigCh(
      trailTime, tombChannel, timeCalibrationMap, thresholdsMap,
      JPetSigCh::Trailing, setTHRValuesFromChannels
    );
    allTDCSigChs.push_back(trailSigCh);
    if (saveHistos){
      stats.getHisto1D(Form("pm_occupation_thr%d", tombChannel.getLocalChannelNumber()))
        ->Fill(tombChannel.getPM().getID());
    }
  }
  return allTDCSigChs;
}