Belle II Software  release-08-01-10
AsicBackgroundLibraryCreator.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <tracking/trackFindingCDC/findlets/complete/AsicBackgroundLibraryCreator.h>
9 #include <tracking/trackFindingCDC/eventdata/hits/CDCWireHit.h>
10 #include <tracking/trackFindingCDC/eventdata/tracks/CDCTrack.h>
11 #include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectory2D.h>
12 #include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectorySZ.h>
13 #include <tracking/trackFindingCDC/topology/CDCWire.h>
14 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
15 #include <framework/core/ModuleParamList.templateDetails.h>
16 #include <cdc/dataobjects/CDCHit.h>
17 #include <framework/logging/Logger.h>
18 #include <iostream>
19 #include <TTree.h>
20 #include <TFile.h>
21 #include <TBranch.h>
22 using namespace Belle2;
23 using namespace TrackFindingCDC;
24 using std::vector;
25 using std::map;
26 using std::pair;
27 using std::sort;
28 using std::min;
29 using std::max;
30 
32 
33 float getDist2D(const TrackFindingCDC::CDCTrajectory3D& trajectory, const TrackFindingCDC::CDCWireHit* wireHit)
34 {
35  const TrackFindingCDC::CDCTrajectory2D& trajectory2D = trajectory.getTrajectory2D();
36  const TrackFindingCDC::CDCTrajectorySZ& trajectorySZ = trajectory.getTrajectorySZ();
37  TrackFindingCDC::Vector2D recoPos2D;
38  if (wireHit->isAxial()) {
39  recoPos2D = wireHit->reconstruct2D(trajectory2D);
40  } else {
41  const TrackFindingCDC::CDCWire& wire = wireHit->getWire();
42  const TrackFindingCDC::Vector2D& posOnXYPlane = wireHit->reconstruct2D(trajectory2D);
43  const double arcLength = trajectory2D.calcArcLength2D(posOnXYPlane);
44  const double z = trajectorySZ.mapSToZ(arcLength);
45  const TrackFindingCDC::Vector2D& wirePos2DAtZ = wire.getWirePos2DAtZ(z);
46  const TrackFindingCDC::Vector2D& recoPosOnTrajectory = trajectory2D.getClosest(wirePos2DAtZ);
47  const double driftLength = wireHit->getRefDriftLength();
48  TrackFindingCDC::Vector2D disp2D = recoPosOnTrajectory - wirePos2DAtZ;
49  disp2D.normalizeTo(driftLength);
50  recoPos2D = wirePos2DAtZ + disp2D;
51  }
52  const float distanceToHit = trajectory2D.getDist2D(recoPos2D);
53  return abs(distanceToHit);
54 }
55 
57 {
59  // database:
60  m_channelMapFromDB = std::make_unique<DBArray<CDCChannelMap>> ();
61 
62  if ((*m_channelMapFromDB).isValid()) {
63  B2DEBUG(29, "CDC Channel map is valid");
64  } else {
65  B2FATAL("CDC Channel map is not valid");
66  }
67 
68  // Library for writing
69 
70  auto leavesCreator = [this](TTree & tree) {
71  if (m_write_extra_vars) {
72  tree.Branch("Dist", &m_dist_signal, "DistSig/f:DistBg/f");
73  tree.Branch("ADC", &m_adc_sig, "ADC_Sig/S:ADC_bg/S");
74  tree.Branch("Track", &m_n_hit_track, "tr_nhit/S");
75  }
76  tree.Branch("Board", &m_board, "board/b");
77  tree.Branch("Channel", &m_channel, "channel/b");
78  tree.Branch("Nhit", &m_n_hit, "nhit/b");
79  tree.Branch("Asic", &m_asic_info[0],
80  "TDC0/S:ADC0/S:TOT0/S:TDC1/S:ADC1/S:TOT1/S:TDC2/S:ADC2/S:TOT2/S:TDC3/S:ADC3/S:TOT3/S:TDC4/S:ADC4/S:TOT4/S:TDC5/S:ADC5/S:TOT5/S:TDC6/S:ADC6/S:TOT6/S:TDC7/S:ADC7/S:TOT7/S");
81  return;
82  };
83 
84  m_recorder.reset(new Recorder(
85  leavesCreator
87  , "ASIC"));
88 }
89 
91 {
93  // Load map from DB:
94  for (const auto& cm : (*m_channelMapFromDB)) {
95  const int isl = cm.getISuperLayer();
96  const int il = cm.getILayer();
97  const int iw = cm.getIWire();
98  const int iBoard = cm.getBoardID();
99  const int iCh = cm.getBoardChannel();
100  const WireID wireId(isl, il, iw);
101  m_map[wireId.getEWire()] = std::pair<int, int>(iBoard, iCh);
102  }
103 }
104 
106 {
107  return "Finds suitable ASICs with a single hit attached to a track and uses them to create the library";
108 }
109 
110 void AsicBackgroundLibraryCreator::apply(const std::vector<CDCWireHit>& wireHits, const std::vector<CDCTrack>& tracks)
111 {
112 
113  map< pair<int, int>, vector<const CDCWireHit*>> groupedByAsic;
114  for (const CDCWireHit& wireHit : wireHits) {
115  auto eWire = wireHit.getWireID().getEWire();
116  B2ASSERT("Channel map NOT found for the channel", m_map.count(eWire) > 0);
117  auto board = m_map[eWire].first;
118  auto channel = m_map[eWire].second;
119  auto asicID = pair<int, int>(board, channel / 8); // ASIC are groups of 8 channels
120  groupedByAsic[asicID].push_back(&wireHit);
121  };
122  for (auto& asicList : groupedByAsic) {
123  selectAsic(asicList.second, tracks);
124  };
125 
126  return;
127 }
128 
129 void AsicBackgroundLibraryCreator::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
130 {
131  Super::exposeParameters(moduleParamList, prefix);
132  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalHitNumberASIC"),
134  "Required number of hits per ASIC for library creation",
136 
137  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "AsicLibraryFileName"),
139  "ASIC library file name",
141 
142  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalDistanceSignal"),
144  "maximal distance in cm from track to signal hit",
146 
147  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalDistanceBackground"),
149  "minimal distance in cm from track to background hit",
151 
152  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "useAxialHitsOnly"),
154  "use axial layers only",
156 
157  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "writeExtraVars"),
159  "Write extra variables to the library",
161 
162  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalHitsOnTrack"),
164  "Required number of hits on track for library creation",
166 
167  // set some defaults:
168  moduleParamList->getParameter<std::string>("inputTracks").setDefaultValue("CDCTrackVector");
169  moduleParamList->getParameter<std::string>("inputWireHits").setDefaultValue("CDCWireHitVector");
170 }
171 
173 {
174  m_recorder->write();
176 }
177 
178 void AsicBackgroundLibraryCreator::selectAsic(const std::vector<const CDCWireHit*>& wireHits, const std::vector<CDCTrack>& tracks)
179 {
180 
181  if (wireHits.size() < m_minimal_hit_number) {
182  return;
183  };
184 
185  if (wireHits.size() > 8) {
186  B2ERROR("Number of hits per asic should not exceed 8, observe too many hits." << LogVar("nHits", wireHits.size()));
188  return;
189  }
190 
191 
192  // count taken non-background hits:
193  int count = 0;
194  const CDCWireHit* signal = nullptr;
195  for (auto& hit : wireHits) {
196  if (!(*hit)->hasBackgroundFlag() && (*hit)->hasTakenFlag()) {
197  count += 1;
198  signal = hit;
199  }
200  }
201 
202  // require one and only one taken hit
203  if (count != 1) {
204  return;
205  }
206 
207 
208  // check if only axial hits are used:
209 
210  if ((!signal->isAxial()) && m_use_axial_hits_only) {
211  return;
212  }
213 
214  // find the track to which this hit belongs
215  const CDCTrack* signalTrack = nullptr;
216  for (auto& track : tracks) {
217  for (auto& hit : track) {
218  if (&hit.getWireHit() == signal) {
219  signalTrack = &track;
220  break;
221  }
222  }
223  if (signalTrack != nullptr) {
224  break;
225  }
226  }
227 
228  if (signalTrack == nullptr) {
229  B2DEBUG(29, "AsicBackgroundLibraryCreator::No track found for the signal hit");
230  return;
231  }
232 
233  m_n_hit_track = signalTrack->size();
234 
236 
237  // check the distance from the track to each signal hit
238  const auto& trajectory = signalTrack->getStartTrajectory3D();
239 
240  m_dist_bg = 1000.;
241 
242  for (auto& hit : wireHits) {
243 
244  const float dist = getDist2D(trajectory, hit);
245 
246 
247  if (hit == signal) {
248  m_dist_signal = dist;
249  if (dist > m_distance_signal_max) return;
250  } else {
251  m_dist_bg = min(m_dist_bg, dist);
252  if (dist < m_distance_background_min) return;
253  }
254  }
255 
256 
257  // Ok, passes all cuts !
258 
259  // reset the library entries
260  for (auto& channel : m_asic_info) {
261  channel.TDC = -1;
262  channel.ADC = -1;
263  channel.TOT = -1;
264  }
265 
266  // add to the library
267 
268  m_n_hit = wireHits.size();
269 
270  m_adc_max_bg = 0;
271  for (auto& hit : wireHits) {
272  auto eWire = hit->getWireID().getEWire();
273  auto channel = m_map[eWire].second;
274  auto asicCH = channel % 8;
275  m_asic_info[asicCH].ADC = hit->getHit()->getADCCount();
276  m_asic_info[asicCH].TDC = hit->getHit()->getTDCCount();
277  m_asic_info[asicCH].TOT = hit->getHit()->getTOT();
278 
279  if (hit != signal) {
280  m_adc_max_bg = max(m_adc_max_bg, m_asic_info[asicCH].ADC);
281  }
282  }
283  // also signal hit info
284  auto eWire = signal->getWireID().getEWire();
285  m_channel = m_map[eWire].second;
286  m_board = m_map[eWire].first;
287  m_adc_sig = signal->getHit()->getADCCount();
288 
289  // make sure that ADC of the signal is >= than ADC of the background:
290  if (m_adc_sig < m_adc_max_bg) return;
291 
292  // write out
293  m_recorder->capture();
294 }
The Module parameter list class.
double m_distance_signal_max
maximal distance from track to signal hit
std::unique_ptr< DBArray< CDCChannelMap > > m_channelMapFromDB
Channel map retrieved from DB.
void initialize() final
Access database here, open library for writing:
size_t m_minimal_hits_on_track
min. number of hits on the track
std::map< int, std::pair< int, int > > m_map
map from ewire to board/channel ID
std::string getDescription() final
Short description of the findlet.
void selectAsic(const std::vector< const CDCWireHit * > &wireHits, const std::vector< CDCTrack > &tracks)
Algorithm to select suitable ASIC for library creation.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
UChar_t m_n_hit
For debuging, store also number of channels with hits.
std::unique_ptr< Recorder > m_recorder
Recorder for the root output.
double m_distance_background_min
minimal distance from track to background hit
size_t m_minimal_hit_number
min. number of hits in ASIC for background check
void apply(const std::vector< CDCWireHit > &wireHits, const std::vector< CDCTrack > &tracks) final
Main algorithm marking hit as background.
Class representing a sequence of three dimensional reconstructed hits.
Definition: CDCTrack.h:41
Particle trajectory as it is seen in xy projection represented as a circle.
double calcArcLength2D(const Vector2D &point) const
Calculate the travel distance from the start position of the trajectory.
Vector2D getClosest(const Vector2D &point) const
Calculates the closest approach on the trajectory to the given point.
double getDist2D(const Vector2D &point) const
Calculates the distance from the point to the trajectory as seen from the xy projection.
Particle full three dimensional trajectory.
CDCTrajectory2D getTrajectory2D() const
Getter for the two dimensional trajectory.
CDCTrajectorySZ getTrajectorySZ() const
Getter for the sz trajectory.
Linear trajectory in sz space.
double mapSToZ(const double s=0) const
Translates the travel distance to the z coordinate.
Class representing a hit wire in the central drift chamber.
Definition: CDCWireHit.h:55
const WireID & getWireID() const
Getter for the WireID of the wire the hit is located on.
Definition: CDCWireHit.h:185
Class representing a sense wire in the central drift chamber.
Definition: CDCWire.h:58
Vector2D getWirePos2DAtZ(const double z) const
Gives the xy projected position of the wire at the given z coordinate.
Definition: CDCWire.h:192
void initialize() override
Receive and dispatch signal before the start of the event processing.
void terminate() override
Receive and dispatch Signal for termination of the event processing.
void beginRun() override
Receive and dispatch signal for the beginning of a new run.
virtual void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Forward prefixed parameters of this findlet to the module parameter list.
Definition: Findlet.h:69
Class to fill a tree from a set of variables.
Definition: Recorder.h:29
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:35
double normalizeTo(const double toLength)
Normalizes the vector to the given length.
Definition: Vector2D.h:325
Class to identify a wire inside the CDC.
Definition: WireID.h:34
unsigned short getEWire() const
Getter for encoded wire number.
Definition: WireID.h:154
Class to store variables with their name which were sent to the logging service.
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.
Short_t TOT
Time over threshold.