Belle II Software  release-05-01-25
AsicBackgroundDetector.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost, Sasha Glazov *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/trackFindingCDC/findlets/minimal/AsicBackgroundDetector.h>
11 #include <tracking/trackFindingCDC/eventdata/hits/CDCWireHit.h>
12 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
13 #include <framework/core/ModuleParamList.templateDetails.h>
14 #include <cdc/dataobjects/CDCHit.h>
15 #include <framework/logging/Logger.h>
16 #include <iostream>
17 
18 using namespace Belle2;
19 using namespace TrackFindingCDC;
20 using std::vector;
21 using std::map;
22 using std::pair;
23 using std::sort;
24 
26 {
28  // database:
29  m_channelMapFromDB = std::make_unique<DBArray<CDCChannelMap>> ();
30 
31  if ((*m_channelMapFromDB).isValid()) {
32  B2DEBUG(100, "CDC Channel map is valid");
33  } else {
34  B2FATAL("CDC Channel map is not valid");
35  }
36 }
37 
39 {
41 
42  // Load map from DB:
43  for (const auto& cm : (*m_channelMapFromDB)) {
44  const int isl = cm.getISuperLayer();
45  const int il = cm.getILayer();
46  const int iw = cm.getIWire();
47  const int iBoard = cm.getBoardID();
48  const int iCh = cm.getBoardChannel();
49  const WireID wireId(isl, il, iw);
50  m_map[wireId.getEWire()] = std::pair<int, int>(iBoard, iCh);
51  }
52 }
53 
55 {
56  return "Marks hits as background using ASIC-based filter.";
57 }
58 
59 void AsicBackgroundDetector::apply(std::vector<CDCWireHit>& wireHits)
60 {
61 
62  map< pair<int, int>, vector<CDCWireHit*>> groupedByAsic;
63  for (CDCWireHit& wireHit : wireHits) {
64  auto eWire = wireHit.getWireID().getEWire();
65  B2ASSERT("Channel map NOT found for the channel", m_map.count(eWire) > 0);
66  auto board = m_map[eWire].first;
67  auto channel = m_map[eWire].second;
68  auto asicID = pair<int, int>(board, channel / 8); // ASIC are groups of 8 channels
69  groupedByAsic[asicID].push_back(&wireHit);
70  };
71  for (auto& [asicID, asicList] : groupedByAsic) {
72  applyAsicFilter(asicList);
73  };
74 
75  return;
76 }
77 
78 void AsicBackgroundDetector::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
79 {
80  Super::exposeParameters(moduleParamList, prefix);
81  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalHitNumberASIC"),
83  "Required number of hits per ASIC for background check",
85  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "deviationFromMedianTDCinASIC"),
87  "Flag hits as cross talk if TDC does not deviate from median more than this value",
89  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalNSignalASIC"),
91  "Flag background hits only when ASIC contains no more than this number of signal hits",
93 }
94 
95 void AsicBackgroundDetector::applyAsicFilter(std::vector<CDCWireHit*>& wireHits)
96 {
97 
98  if (wireHits.size() < m_minimal_hit_number) {
99  return;
100  };
101 
102  if (wireHits.size() > 8) {
103  B2ERROR("Number of hits per asic should not exceed 8, observe too many hits." << LogVar("nHits", wireHits.size()));
105  for (auto& hit : wireHits) {
106  (*hit)->setBackgroundFlag();
107  (*hit)->setTakenFlag();
108  }
109  return;
110  }
111 
112  B2ASSERT("Number of hits per asic should be above 0 and can not exceed 8",
113  (wireHits.size() <= 8) && (wireHits.size() > 0));
114 
115  // compute median time:
116  vector<short> times;
117  for (auto& hit : wireHits) {
118  short tdc = hit->getHit()->getTDCCount();
119  times.push_back(tdc);
120  }
121  sort(times.begin(), times.end());
122  int mid = times.size() / 2;
123  double median = times.size() % 2 == 0 ? (times[mid] + times[mid - 1]) / 2 : times[mid];
124 
125 
126  size_t nbg = 0;
127  int adcOnMedian = 0;
128  int adcOffMedian = 0;
129  for (auto& hit : wireHits) {
130  int adc = hit->getHit()->getADCCount();
131  if (abs(hit->getHit()->getTDCCount() - median) < m_deviation_from_median) {
132  nbg++;
133  if (adc > adcOnMedian) adcOnMedian = adc;
134  } else {
135  if (adc > adcOffMedian) adcOffMedian = adc;
136  }
137  }
138 
139  if ((nbg < times.size()) // at least 1 hit with different TDC ("signal")
140  && (nbg > 1) // more than one candidate hits
141  && (nbg > times.size() - m_nsignal_max) // a few background hits
142  && (adcOnMedian < adcOffMedian) // triggered by large ADC "signal"
143  ) {
144 
145  // mark hits too close to the median time as background:
146  for (auto& hit : wireHits) {
147  if (abs(hit->getHit()->getTDCCount() - median) < m_deviation_from_median) {
148  (*hit)->setBackgroundFlag();
149  (*hit)->setTakenFlag();
150  }
151  }
152  }
153 }
Belle2::WireID
Class to identify a wire inside the CDC.
Definition: WireID.h:44
Belle2::TrackFindingCDC::AsicBackgroundDetector::m_deviation_from_median
double m_deviation_from_median
distance from median TDC, to be considered as bg.
Definition: AsicBackgroundDetector.h:76
Belle2::WireID::getEWire
unsigned short getEWire() const
Getter for encoded wire number.
Definition: WireID.h:164
Belle2::TrackFindingCDC::AsicBackgroundDetector::m_minimal_hit_number
size_t m_minimal_hit_number
min. number of hits in ASIC for background check
Definition: AsicBackgroundDetector.h:73
Belle2::TrackFindingCDC::AsicBackgroundDetector::m_channelMapFromDB
std::unique_ptr< DBArray< CDCChannelMap > > m_channelMapFromDB
Channel map retrieved from DB.
Definition: AsicBackgroundDetector.h:67
Belle2::TrackFindingCDC::AsicBackgroundDetector::apply
virtual void apply(std::vector< CDCWireHit > &wireHits) final
Main algorithm marking hit as background.
Definition: AsicBackgroundDetector.cc:59
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::initialize
void initialize() override
Receive and dispatch signal before the start of the event processing.
Definition: CompositeProcessingSignalListener.cc:17
Belle2::TrackFindingCDC::AsicBackgroundDetector::exposeParameters
virtual void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
Definition: AsicBackgroundDetector.cc:78
Belle2::TrackFindingCDC::AsicBackgroundDetector::m_map
std::map< int, std::pair< int, int > > m_map
map from ewire to board/channel ID
Definition: AsicBackgroundDetector.h:70
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::Findlet< CDCWireHit & >::exposeParameters
virtual void exposeParameters(ModuleParamList *moduleParamList __attribute__((unused)), const std::string &prefix __attribute__((unused)))
Forward prefixed parameters of this findlet to the module parameter list.
Definition: Findlet.h:79
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::beginRun
void beginRun() override
Receive and dispatch signal for the beginning of a new run.
Definition: CompositeProcessingSignalListener.cc:25
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
Belle2::TrackFindingCDC::AsicBackgroundDetector::getDescription
std::string getDescription() final
Short description of the findlet.
Definition: AsicBackgroundDetector.cc:54
Belle2::TrackFindingCDC::AsicBackgroundDetector::m_nsignal_max
size_t m_nsignal_max
max. number of signal-like hits in ASIC for background check
Definition: AsicBackgroundDetector.h:79
Belle2::TrackFindingCDC::CDCWireHit
Class representing a hit wire in the central drift chamber.
Definition: CDCWireHit.h:65
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::TrackFindingCDC::AsicBackgroundDetector::beginRun
void beginRun() final
Reload channel map if needed.
Definition: AsicBackgroundDetector.cc:38
Belle2::TrackFindingCDC::AsicBackgroundDetector::initialize
void initialize() final
Access database here:
Definition: AsicBackgroundDetector.cc:25
Belle2::TrackFindingCDC::AsicBackgroundDetector::applyAsicFilter
void applyAsicFilter(std::vector< CDCWireHit * > &wireHits)
Algorithm marking hit as background for each CDC ASIC.
Definition: AsicBackgroundDetector.cc:95