Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
calib::ADCShapeFitTable Class Reference

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-10-23/Calibrator/func/ADCShapeFit.h"

Public Member Functions

 ADCShapeFitTable (const std::string &fname, bool isND, bool isMC, int mode)
 
 ~ADCShapeFitTable ()
 
double TNS (double tns0, int16_t adc1, int16_t adc2, int16_t adc3, double &adcpeak, bool &goodTime, double &base) const
 
double TableHitFraction () const
 What fraction of calls to TNS went via the table? More...
 

Static Public Attributes

static const int kADC3Max = 800
 
static const int kADC1Min = -10
 
static const int kADC2RelMin = -140
 
static const int kADC2RelMax = +10
 
static const unsigned int kNumTableEntries
 

Protected Member Functions

 ADCShapeFitTable (const ADCShapeFitTable &)=delete
 
ADCShapeFitTableoperator= (const ADCShapeFitTable &)=delete
 
uint16_t Table (int i) const
 Access the table entry at index i. More...
 

Protected Attributes

bool fIsND
 
bool fIsMC
 
int fMode
 
std::unordered_set< intfIsBad
 
int fNHit
 
int fNMiss
 
std::vector< std::vector< char > > fCompressedTable
 Chunks of LZ4-compressed data containing the lookup table. More...
 

Detailed Description

Definition at line 32 of file ADCShapeFit.h.

Constructor & Destructor Documentation

calib::ADCShapeFitTable::ADCShapeFitTable ( const std::string &  fname,
bool  isND,
bool  isMC,
int  mode 
)

Definition at line 202 of file ADCShapeFit.cxx.

References ana::assert(), compressedSize, om::cout, allTimeWatchdog::endl, MakeMiniprodValidationCuts::f, fCompressedTable, fIsBad, plot_validation_datamc::fname, update_sam_good_runs_metadata::good, MECModelEnuComparisons::i, calib::kChunk, kNumTableEntries, LZ4_compress_HC(), LZ4_compressBound(), LZ4HC_CLEVEL_DEFAULT, getGoodRuns4SAM::n, PandAna.reco_validation.add_data::offset, getGoodRuns4SAM::table, and make_root_from_grid_output::tr.

203  : fIsND(isND), fIsMC(isMC), fMode(mode), fNHit(0), fNMiss(0)
204  {
205  TFile f(fname.c_str());
206  assert(!f.IsZombie());
207 
208  TTree* tr = (TTree*)f.Get("shape_lookup");
209  assert(tr);
210 
211  const unsigned int N = tr->GetEntries();
212 
213  assert(N == kNumTableEntries);
214 
215  // Offsets are all two bytes to save space, which is far more resolution
216  // ADCShapeFit needs.
217  uint16_t offset;
218  bool good;
219  tr->SetBranchAddress("offset", &offset);
220  tr->SetBranchAddress("good", &good);
221 
222  std::vector<uint16_t> table(N);
223 
224  std::cout << "Loading ADCShapeFit table from " << fname;
225  for(unsigned int n = 0; n < N; ++n){
226  if(n%(N/8) == 0) std::cout << "." << std::flush;
227 
228  tr->GetEntry(n);
229  table[n] = offset;
230 
231  if(!good) fIsBad.insert(n);
232  }
233  std::cout << std::endl;
234 
235  // The maximum length a compressed block can be
236  const unsigned long bufferLen = LZ4_compressBound(kChunk*sizeof(uint16_t));
237  std::vector<char> buffer(bufferLen);
238 
239  std::cout << "Compressing table";
240  int compressedSize = 0;
241  for(unsigned int i = 0; i < kNumTableEntries/kChunk; ++i){
242  if(i%(kNumTableEntries/kChunk/8) == 0) std::cout << "." << std::flush;
243 
244  // Compress one kChunk-sized chunk of the table into buffer
245  const int outputLen = LZ4_compress_HC((char*)&table[i*kChunk],
246  &buffer.front(),
247  kChunk*sizeof(uint16_t),
248  bufferLen,
250 
251  assert(outputLen > 0);
252 
253  // Store the result into the i'th entry of fCompresssedTable
254  fCompressedTable.emplace_back(buffer.begin(), buffer.begin()+outputLen);
255  fCompressedTable.back().shrink_to_fit();
256 
257  compressedSize += outputLen;
258  }
259  std::cout << std::endl;
260  std::cout << "Shrunk " << N*sizeof(uint16_t)/(1024*1024) << "MB table to "
261  << compressedSize/(1024*1024) << "MB." << std::endl;
262  }
std::vector< std::vector< char > > fCompressedTable
Chunks of LZ4-compressed data containing the lookup table.
Definition: ADCShapeFit.h:79
#define LZ4HC_CLEVEL_DEFAULT
Definition: lz4hc.h:48
static const unsigned int kNumTableEntries
Definition: ADCShapeFit.h:59
::xsd::cxx::tree::buffer< char > buffer
Definition: Database.h:179
char int compressedSize
Definition: lz4.h:458
OStream cout
Definition: OStream.cxx:6
const int kChunk
Definition: ADCShapeFit.cxx:39
std::unordered_set< int > fIsBad
Definition: ADCShapeFit.h:75
int LZ4_compress_HC(const char *src, char *dst, int srcSize, int maxDstSize, int compressionLevel)
Definition: lz4hc.cxx:534
assert(nhit_max >=nhit_nbins)
int LZ4_compressBound(int isize)
Definition: lz4.cxx:395
calib::ADCShapeFitTable::~ADCShapeFitTable ( )

Definition at line 265 of file ADCShapeFit.cxx.

266  {
267  }
calib::ADCShapeFitTable::ADCShapeFitTable ( const ADCShapeFitTable )
protecteddelete

Member Function Documentation

ADCShapeFitTable& calib::ADCShapeFitTable::operator= ( const ADCShapeFitTable )
protecteddelete
uint16_t calib::ADCShapeFitTable::Table ( int  i) const
protected

Access the table entry at index i.

Definition at line 367 of file ADCShapeFit.cxx.

References ana::assert(), fCompressedTable, calib::kChunk, LZ4_decompress_fast(), and gen_flatrecord::size.

Referenced by TNS().

368  {
369  // If anything goes wrong and this compression/decompression doesn't
370  // reproduce the raw table, the checks in Calibrator::GetTNS(), where we
371  // occasionally re-fit a trace even though it's in the table, will catch
372  // it.
373 
374  static uint16_t buffer[kChunk];
375 
376  // Figure out which chunk 'i' is in
377  const int chunkIdx = i/kChunk;
378 
379  // Extract the relevant compressed chunk into our buffer
380  const unsigned int lenUsed =
381  LZ4_decompress_fast(&fCompressedTable[chunkIdx].front(),
382  (char*)buffer,
383  kChunk*sizeof(uint16_t));
384 
385  // Make sure it consumed the whole compressed chunk
386  assert(lenUsed == fCompressedTable[chunkIdx].size());
387 
388  // Return the element of the chunk corresponding to 'i'
389  return buffer[i%kChunk];
390  }
std::vector< std::vector< char > > fCompressedTable
Chunks of LZ4-compressed data containing the lookup table.
Definition: ADCShapeFit.h:79
int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
Definition: lz4.cxx:1261
::xsd::cxx::tree::buffer< char > buffer
Definition: Database.h:179
const int kChunk
Definition: ADCShapeFit.cxx:39
assert(nhit_max >=nhit_nbins)
double calib::ADCShapeFitTable::TableHitFraction ( ) const

What fraction of calls to TNS went via the table?

Definition at line 361 of file ADCShapeFit.cxx.

References fNHit, and fNMiss.

Referenced by calib::Calibrator::~Calibrator().

362  {
363  return fNHit ? fNHit/double(fNHit+fNMiss) : 0;
364  }
double calib::ADCShapeFitTable::TNS ( double  tns0,
int16_t  adc1,
int16_t  adc2,
int16_t  adc3,
double &  adcpeak,
bool &  goodTime,
double &  base 
) const

Returns equivalent of OffsetToTNS(tdc, ADCShapeFit(adc1...)) but in most cases can do so more rapidly via a lookup table. adcpeak is the estimated peak curve value at the best fit time.

Definition at line 270 of file ADCShapeFit.cxx.

References calib::ADCShapeFit(), ana::assert(), fIsBad, fIsMC, fIsND, fMode, fNHit, fNMiss, calib::GetExpectations(), kADC1Min, kADC2RelMax, kADC2RelMin, kADC3Max, calib::kNumFineTimingADCPoints, kNumTableEntries, calib::kZeroOffsetSamples, norm, PandAna.reco_validation.add_data::offset, cet::pow(), getGoodRuns4SAM::t0, and Table().

Referenced by calib::ADCShapeFitAna::analyze(), and calib::Calibrator::GetTNS().

272  {
273  uint16_t offset;
274  double riseTime;
275  double fallTime ;
276  double preAmp;
277  const double sampleTime = fIsND ? 125 : 500; // ns
278 
279  if (fMode == 0){
280  riseTime = (fIsND ? 140 : 380)/sampleTime;
281  fallTime = (fIsND ? 4500 : 7000)/sampleTime;
282  preAmp = 0;
283 
284  if(!fIsND && !fIsMC) riseTime = 460/sampleTime;
285 
286  } else{
287  riseTime = (fIsND ? 116 : 424)/sampleTime;
288  fallTime = (fIsND ? 6729 : 10449)/sampleTime;
289  preAmp = ( fIsND ? 147000 : 110000)/sampleTime;
290 
291  //fd data 410 900
292  if(!fIsND && !fIsMC)
293  { riseTime = 410/sampleTime;
294  fallTime = 9000/sampleTime;
295  }
296  }
297  // These are the conditions for the best offset to be in the table
298  if(adc3 >= 0 && adc3 < kADC3Max &&
299  adc1 >= kADC1Min && adc1 < adc3 &&
300  adc2 >= adc3+kADC2RelMin && adc2 < adc3+kADC2RelMax){
301  ++fNHit;
302 
303  // And this is the offset it'll be at, based on aforementioned geometry
304  const int tableIdx = (kADC2RelMax-kADC2RelMin)* // Wedge thickness
305  (-kADC1Min*(adc3+1)+ // Rectangular part
306  (adc3*(adc3-1))/2+adc1)+ // Triangular part
307  (adc2-adc3-kADC2RelMin); // Distance through wedge
308 
309  assert(tableIdx >= 0 && tableIdx < int(kNumTableEntries));
310 
311  offset = Table(tableIdx);
312  goodTime = (fIsBad.count(tableIdx) == 0);
313 
314  // One time in 400, do the full calculation just to sanity check that the
315  // table (and our indexing into it) agrees. We have about a 5% miss rate
316  // anyway, so this isn't too much additional computation.
317 
318  if(adc3%20 == 0 && adc1%20 == 0){
319 
320  assert(ADCShapeFit(adc1, adc2, adc3, riseTime, fallTime, preAmp, goodTime, fMode) == offset);
321 
322  }
323  }
324  else{
325  // It's not in our table, just do the fit ourselves.
326  ++fNMiss;
327  offset = ADCShapeFit(adc1, adc2, adc3, riseTime, fallTime, preAmp, goodTime, fMode);
328  }
329 
330  // Empirically, if the best-fit t0 is before the first sample, then this is
331  // a bad fit. See docdb 12601.
332  if(offset < -kZeroOffsetSamples/kSamplesPerOffset) goodTime = false;
333 
334  // We need to extract the best normalization, so repeat the relevant part
335  // of the inner loop at that offset.
336  if(goodTime){
337  // The observations, in chronological order
338  const int16_t obs[kNumFineTimingADCPoints] = {0, adc1, adc2, adc3};
339  const double t0 = (double(offset)*kSamplesPerOffset+kZeroOffsetSamples);
340  double junk[kNumFineTimingADCPoints]; // Expectations
341  const double norm = GetExpectations(t0, riseTime, fallTime, preAmp, obs, junk, base, fMode);
342 
343  // The value the peak of the curve would have before scaling by norm
344  // Analytic calculation from the functional form in ReadoutSim
345  static const double peak = pow(riseTime/(riseTime+fallTime), riseTime/fallTime)-
346  pow(riseTime/(riseTime+fallTime), 1+riseTime/fallTime);
347 
348  adcpeak = norm*peak;
349  }
350  else{
351  // Unless the fit failed, in which case we're probably safest just doing
352  // this.
353  adcpeak = adc3;
354  return tns0;
355  }
356 
357  return tns0+(offset*kSamplesPerOffset+kZeroOffsetSamples)*sampleTime;
358  }
constexpr T pow(T x)
Definition: pow.h:75
uint16_t Table(int i) const
Access the table entry at index i.
static const unsigned int kNumTableEntries
Definition: ADCShapeFit.h:59
const XML_Char int const XML_Char int const XML_Char * base
Definition: expat.h:331
static const int kADC2RelMax
Definition: ADCShapeFit.h:55
static const int kADC2RelMin
Definition: ADCShapeFit.h:54
const double kZeroOffsetSamples
Definition: ADCShapeFit.cxx:50
static const int kADC1Min
Definition: ADCShapeFit.h:53
const unsigned int kNumFineTimingADCPoints
Definition: ADCShapeFit.cxx:45
uint16_t ADCShapeFit(int16_t adc1, int16_t adc2, int16_t adc3, double riseTime, double fallTime, double preAmp, bool &goodTime, int fMode)
const double kSamplesPerOffset
Definition: ADCShapeFit.cxx:54
std::unordered_set< int > fIsBad
Definition: ADCShapeFit.h:75
Float_t norm
assert(nhit_max >=nhit_nbins)
double GetExpectations(double t0, double riseTime, double fallTime, double preAmp, const int16_t *obs, double *exps, double &base, int mode)
Helper function for ADCShapeFit inner loop.
Definition: ADCShapeFit.cxx:64
static const int kADC3Max
Definition: ADCShapeFit.h:52

Member Data Documentation

std::vector<std::vector<char> > calib::ADCShapeFitTable::fCompressedTable
protected

Chunks of LZ4-compressed data containing the lookup table.

Definition at line 79 of file ADCShapeFit.h.

Referenced by ADCShapeFitTable(), and Table().

std::unordered_set<int> calib::ADCShapeFitTable::fIsBad
protected

Definition at line 75 of file ADCShapeFit.h.

Referenced by ADCShapeFitTable(), and TNS().

bool calib::ADCShapeFitTable::fIsMC
protected

Definition at line 73 of file ADCShapeFit.h.

Referenced by TNS().

bool calib::ADCShapeFitTable::fIsND
protected

Definition at line 72 of file ADCShapeFit.h.

Referenced by TNS().

int calib::ADCShapeFitTable::fMode
protected

Definition at line 74 of file ADCShapeFit.h.

Referenced by TNS().

int calib::ADCShapeFitTable::fNHit
mutableprotected

Definition at line 76 of file ADCShapeFit.h.

Referenced by TableHitFraction(), and TNS().

int calib::ADCShapeFitTable::fNMiss
mutableprotected

Definition at line 76 of file ADCShapeFit.h.

Referenced by TableHitFraction(), and TNS().

const int calib::ADCShapeFitTable::kADC1Min = -10
static

Definition at line 53 of file ADCShapeFit.h.

Referenced by TNS().

const int calib::ADCShapeFitTable::kADC2RelMax = +10
static

Definition at line 55 of file ADCShapeFit.h.

Referenced by TNS().

const int calib::ADCShapeFitTable::kADC2RelMin = -140
static

Definition at line 54 of file ADCShapeFit.h.

Referenced by TNS().

const int calib::ADCShapeFitTable::kADC3Max = 800
static

Definition at line 52 of file ADCShapeFit.h.

Referenced by TNS().

const unsigned int calib::ADCShapeFitTable::kNumTableEntries
static
Initial value:

Definition at line 59 of file ADCShapeFit.h.

Referenced by ADCShapeFitTable(), and TNS().


The documentation for this class was generated from the following files: