Belle II Software  release-05-01-25
CDCCrossTalkLibrary.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Tracking, CDC group *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <iostream>
13 #include <iomanip>
14 #include <map>
15 #include <TObject.h>
16 #include <TRandom.h>
17 #include <cdc/dbobjects/CDCCrossTalkClasses.h>
18 
19 using std::vector;
20 using std::upper_bound;
21 using std::equal_range;
22 using std::pair;
23 
24 namespace Belle2 {
33  class CDCCrossTalkLibrary: public TObject {
34  public:
35 
39  CDCCrossTalkLibrary() = default;
40 
47  void addAsicRecord(const Short_t channel, const Short_t ADC, const asicChannels& asicInfo)
48  {
49  Short_t ch8 = channel % 8;
50  adcAsicTuple entry{ADC, ch8 , asicInfo};
51  // keep sorted
52  auto place = upper_bound(m_library.begin(), m_library.end(), entry, adc_search());
53  m_library.insert(place, entry);
54  }
55 
56 
67  const vector< pair<Short_t, asicChannel> > getLibraryCrossTalk(Short_t channel, Short_t TDCin, Short_t ADCin, Short_t TOTin,
68  size_t entry = 0, bool insertSignalToOutput = false) const
69  {
70  // output
71  vector< pair<Short_t, asicChannel> > outRec;
72  auto prob = gRandom->Uniform();
73 
74  if (prob > pCrossTalk(ADCin)) {
76  if (insertSignalToOutput)
77  outRec.emplace_back(std::make_pair(channel, asicChannel{TDCin, ADCin, TOTin}));
78  return outRec;
79  }
80 
81  // find a range of possible candidates
82  Short_t ch8 = channel % 8;
83  adcChannelPair query{ADCin, ch8};
84  auto pADC = equal_range(m_library.begin(), m_library.end(), query, adc_search());
85 
86  size_t size = std::distance(pADC.first, pADC.second);
87  size_t val = 0;
88 
89 
90  if (entry != 0) {
91  // select modulo
92  val = entry % size;
93  } else {
94  // select random
95  val = gRandom->Integer(size);
96  }
97 
98  asicChannels rec;
99  bool recNotSet = true;
100  if (size == 0) {
101  if (pADC.first == m_library.end()) {
102  rec = m_library.back().record;
103  recNotSet = false;
104  } else {
105  if (pADC.first->Channel == ch8) {
106  rec = (pADC.first)->record;
107  recNotSet = false;
108  } else { // need to step back, if possible
109  if ((--pADC.first)->Channel == ch8) {
110  rec = (--pADC.first)->record;
111  recNotSet = false;
112  } else {
113  B2WARNING("Could not find CDC Cross talk library entry for channel, ADC: " << channel << " " << ADCin);
114  if (insertSignalToOutput)
115  outRec.emplace_back(std::make_pair(channel, asicChannel{TDCin, ADCin, TOTin}));
116  return outRec;
117  }
118  }
119  }
120  } else {
121  size_t count = 0;
122  for (auto p = pADC.first; p != pADC.second; ++p) {
123  if (count == val) {
124  rec = p->record;
125  recNotSet = false;
126  }
127  count += 1;
128  }
129  }
130 
131  B2ASSERT("CDC cross talk record not set", !recNotSet);
132 
134  Short_t DeltaTDC = TDCin - rec[ch8].TDC;
135 
136  if (abs(DeltaTDC) > 1000) {
137  B2WARNING("Large xTalk DeltaTDC=" << DeltaTDC);
138  }
139 
140  // std::cout << TDCin << " HHH " << rec[ch8].TDC << " " << ch8 << "\n";
141 
142 
143  for (int i = 0; i < 8; i += 1) {
144  if (rec[i].TDC > 0) {
145  if (i == ch8) { // store input values
146  B2ASSERT("CDC Cross talk entry for the selected channel cannot be empty " << ch8 << " " << rec[ch8].TDC , rec[ch8].TDC > -1);
147  if (insertSignalToOutput)
148  outRec.emplace_back(std::make_pair(channel, asicChannel{TDCin, ADCin, TOTin}));
149  } else { // adjust TDC for the cross talk, keep ADC and TOT
150  Short_t TDCout = rec[i].TDC + DeltaTDC;
151  outRec.emplace_back(std::make_pair(i - ch8 + channel , asicChannel{TDCout, rec[i].ADC, rec[i].TOT}));
152  }
153  }
154  }
155  return outRec;
156  }
157 
158 
159 
163  void dump(int verbosity) const
164  {
165  std::cout << "Content of CDCCrossTalkLibrary" << std::endl;
166  std::cout << "Size = " << m_library.size() << "\n";
167  if (verbosity < 1) return;
168 
169  for (size_t i = 0 ; i < m_library.size(); i += 1) {
170  auto [adc, ch, record] = m_library[i];
171  std::cout << "ADC: " << adc << " CH: " << ch << " count: " << i << "\n";
172  std::cout << " TDC ADC TOT \n";
173  for (auto rec : record) {
174  std::cout << " " << rec.TDC << " " << rec.ADC << " " << rec.TOT << "\n";
175  }
176  }
177  std::cout << "Probability of cross talk vs ADC: \n";
178  for (size_t a = 0; a < 8196; a++) {
179  std::cout << "P(" << a << ")=" << m_pCrossTalk[a] << "\n";
180  }
181  }
182 
184  void dumpEntry(size_t entry)
185  {
186 
187  if (entry > m_library.size()) entry = m_library.size() - 1;
188  auto [adc, ch, record] = m_library[entry];
189  std::cout << "ADC:" << adc << " CH: " << ch << " Size: " << m_library.size() << "\n";
190  std::cout << " TDC ADC TOT \n";
191  for (auto rec : record) {
192  std::cout << " " << rec.TDC << " " << rec.ADC << " " << rec.TOT << "\n";
193  }
194 
195  }
196 
200  double pCrossTalk(const Short_t ADC) const
201  {
202  if (ADC < 0) return 0;
203  if (ADC >= 8196) return 1;
204  return m_pCrossTalk[ADC];
205  }
206 
208  void setPCrossTalk(const double* probs)
209  {
210  for (size_t i = 0; i < m_pCrossTalk.size(); i += 1) {
211  m_pCrossTalk[i] = probs[i];
212  }
213  }
214 
215  private:
216  std::vector<adcAsicTuple> m_library;
217  array<float, 8196> m_pCrossTalk;
218 
220  };
221 
223 } // end namespace Belle2
Belle2::CDCCrossTalkLibrary::setPCrossTalk
void setPCrossTalk(const double *probs)
Store x-talk probability.
Definition: CDCCrossTalkLibrary.h:208
Belle2::CDCCrossTalkLibrary::pCrossTalk
double pCrossTalk(const Short_t ADC) const
Get probability of the cross talk.
Definition: CDCCrossTalkLibrary.h:200
Belle2::CDCCrossTalkLibrary::getLibraryCrossTalk
const vector< pair< Short_t, asicChannel > > getLibraryCrossTalk(Short_t channel, Short_t TDCin, Short_t ADCin, Short_t TOTin, size_t entry=0, bool insertSignalToOutput=false) const
Get cross talk record from the library.
Definition: CDCCrossTalkLibrary.h:67
Belle2::adcAsicTuple
tuple to store ADC,Channel -> 8 asicChannels
Definition: CDCCrossTalkClasses.h:39
Belle2::CDCCrossTalkLibrary::m_pCrossTalk
array< float, 8196 > m_pCrossTalk
x-talk probability
Definition: CDCCrossTalkLibrary.h:217
Belle2::asicChannels
array< asicChannel, 8 > asicChannels
fixed sized array of ASIC channels
Definition: CDCCrossTalkClasses.h:30
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CDCCrossTalkLibrary::dumpEntry
void dumpEntry(size_t entry)
Dump single entry, for a given channel.
Definition: CDCCrossTalkLibrary.h:184
Belle2::asicChannel
record to be used to store ASIC info
Definition: CDCCrossTalkClasses.h:23
Belle2::CDCCrossTalkLibrary::CDCCrossTalkLibrary
CDCCrossTalkLibrary()=default
Default constructor.
Belle2::CDCCrossTalkLibrary
Database object for ASIC crosstalk library.
Definition: CDCCrossTalkLibrary.h:33
Belle2::CDCCrossTalkLibrary::addAsicRecord
void addAsicRecord(const Short_t channel, const Short_t ADC, const asicChannels &asicInfo)
Add a new ASIC record to the library.
Definition: CDCCrossTalkLibrary.h:47
Belle2::adcChannelPair
pair ADC, channel
Definition: CDCCrossTalkClasses.h:33
Belle2::CDCCrossTalkLibrary::dump
void dump(int verbosity) const
Print out contents of the library.
Definition: CDCCrossTalkLibrary.h:163
Belle2::adc_search
functions to search in the sorted list of tuples
Definition: CDCCrossTalkClasses.h:47
Belle2::CDCCrossTalkLibrary::ClassDef
ClassDef(CDCCrossTalkLibrary, 2)
ClassDef.
Belle2::CDCCrossTalkLibrary::m_library
std::vector< adcAsicTuple > m_library
Library.
Definition: CDCCrossTalkLibrary.h:216