AdcThresholdSignalFinder.cc
Go to the documentation of this file.
2 
7 #include <iostream>
8 
9 using std::string;
10 using std::cout;
11 using std::endl;
12 
13 //**********************************************************************
14 // Local defintitions.
15 //**********************************************************************
16 
17 namespace {
18 
19 string boolToString(bool val) {
20  return val ? "true" : "false";
21 }
22 
23 } // end unnamed namespace
24 
25 //**********************************************************************
26 // Class methods.
27 //**********************************************************************
28 
30 : m_LogLevel(ps.get<int>("LogLevel")),
31  m_Threshold(new RootParFormula("Threshold", ps.get<Name>("Threshold"))),
32  m_BinsBefore(ps.get<unsigned int>("BinsBefore")),
33  m_BinsAfter(ps.get<unsigned int>("BinsAfter")),
34  m_FlagPositive(ps.get<bool>("FlagPositive")),
35  m_FlagNegative(ps.get<bool>("FlagNegative")),
36  m_prdtool(nullptr) {
37  const string myname = "AdcThresholdSignalFinder::ctor: ";
39  if ( pdtm == nullptr ) {
40  cout << myname << "ERROR: Unable to retrieve tool manager." << endl;
41  } else {
42  string stnam = "runDataTool";
43  if ( m_Threshold->npar() ) {
44  m_prdtool = pdtm->getShared<RunDataTool>(stnam);
45  if ( m_prdtool == nullptr ) {
46  cout << myname << "ERROR: RunDataTool " << stnam
47  << " not found. Scale factor formula will not be evaluated." << endl;
48  } else {
49  cout << myname << "RunDataTool retrieved." << endl;
50  }
51  }
52  }
53  if ( m_LogLevel >= 1 ) {
54  cout << myname << "Configuration parameters:" << endl;
55  cout << myname << " LogLevel: " << m_LogLevel << endl;
56  cout << myname << " Threshold: " << m_Threshold->formulaString() << endl;
57  cout << myname << " BinsBefore: " << m_BinsBefore << endl;
58  cout << myname << " BinsAfter: " << m_BinsAfter << endl;
59  cout << myname << " FlagPositive: " << boolToString(m_FlagPositive) << endl;
60  cout << myname << " FlagNegative: " << boolToString(m_FlagNegative) << endl;
61  }
62 }
63 
64 //**********************************************************************
65 
67  const string myname = "AdcThresholdSignalFinder::update: ";
68  DataMap ret;
69  AdcIndex nsam = acd.samples.size();
70  if ( nsam == 0 ) {
71  cout << myname << "ERROR: No samples found in channel " << acd.channel() << endl;
72  acd.signal.clear();
73  acd.rois.clear();
74  return ret.setStatus(1);
75  }
76  if ( m_prdtool != nullptr ) {
77  RunData rdat = m_prdtool->runData(acd.run());
78  if ( ! rdat.isValid() ) cout << myname << "WARNING: RunData not found." << endl;
79  else rdat.setFormulaPars(*m_Threshold);
80  }
81  if ( ! m_Threshold->ready() ) {
82  cout << myname << "WARNING: Using default scale factor " << m_Threshold->defaultEval() << endl;
83  }
84  float thr = m_Threshold->eval();
85  if ( m_LogLevel >= 2 ) cout << myname << "Finding ROIs for channel " << acd.channel() << endl;
86  AdcIndex nsamlo = m_BinsBefore;
87  AdcIndex nsamhi = m_BinsAfter;
88  acd.signal.clear();
89  acd.signal.resize(nsam, false);
90  AdcIndex nbinAbove = 0;
91  AdcIndex isamUnknown = 0; // First sample which is not known to be inside or ouside a ROI.
92  for ( AdcIndex isam=0; isam<nsam; ++isam ) {
93  float val = acd.samples[isam];
94  bool keep = ( m_FlagPositive && val > thr ) ||
95  ( m_FlagNegative && val < -thr );
96  if ( keep ) {
97  ++nbinAbove;
98  AdcIndex jsam1 = isam > nsamlo ? isam - nsamlo : 0;
99  if ( jsam1 < isamUnknown ) jsam1 = isamUnknown;
100  AdcIndex jsam2 = isam + nsamhi + 1;
101  if ( jsam2 > nsam ) jsam2 = nsam;
102  if ( m_LogLevel >= 4 ) cout << myname << "Trigger: sample[" << isam
103  << "] = " << val << " ==> range: ["
104  << jsam1 << ", " << jsam2 << ")" << endl;
105  for ( AdcIndex jsam=jsam1; jsam<jsam2; ++jsam ) acd.signal[jsam] = true;
106  isamUnknown = jsam2;
107  }
108  }
109  acd.roisFromSignal();
110  if ( m_LogLevel >= 3 ) {
111  cout << myname << " # ticks above threshold: " << nbinAbove << endl;
112  cout << myname << " # ROI: " << acd.rois.size() << endl;
113  }
114  ret.setFloat("threshold", thr);
115  ret.setInt("nThresholdBins", nbinAbove);
116  ret.setInt("nroi", acd.rois.size());
117  return ret;
118 }
119 
120 //**********************************************************************
121 
123  AdcChannelData acdtmp;
124  acdtmp.samples = acd.samples;
125  return update(acdtmp);
126 }
127 
128 //**********************************************************************
void setFloat(Name name, float val)
Definition: DataMap.h:133
G4double thr[100]
Definition: G4S2Light.cc:59
DataMap update(AdcChannelData &acd) const override
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
virtual double eval(const Values &vars) const =0
virtual Name formulaString() const =0
virtual bool ready() const
Definition: ParFormula.h:56
bool isValid() const
Definition: RunData.h:48
virtual Index npar() const
Definition: ParFormula.h:44
AdcRoiVector rois
AdcIndex run() const
void setInt(Name name, int val)
Definition: DataMap.h:131
virtual RunData runData(Index run, Index subRun=0) const =0
static constexpr double ps
Definition: Units.h:99
unsigned int AdcIndex
Definition: AdcTypes.h:15
Channel channel() const
AdcFilterVector signal
DataMap view(const AdcChannelData &acd) const override
virtual Value defaultEval() const
Definition: ParFormula.h:59
AdcThresholdSignalFinder(fhicl::ParameterSet const &ps)
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
SetStat setFormulaPars(TFormula *form)
Definition: RunData.h:87
int bool
Definition: qglobal.h:345
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)