12 #include "TMatrixDSym.h" 31 : m_LogLevel(ps.
get<
int>(
"LogLevel")),
32 m_SignalFlag(ps.
get<
Index>(
"SignalFlag")),
33 m_SignalIterationLimit(ps.
get<
Index>(
"SignalIterationLimit")),
34 m_SignalTool(ps.
get<
string>(
"SignalTool")),
35 m_DecayTime(ps.
get<double>(
"DecayTime")) ,
37 m_PedDegree(ps.
get<
int>(
"PedDegree")),
38 m_PedTick0(ps.
get<
Index>(
"PedTick0")),
41 m_IncludeChannelRanges(ps.
get<
NameVector>(
"IncludeChannelRanges")),
42 m_ExcludeChannelRanges(ps.
get<
NameVector>(
"ExcludeChannelRanges")),
43 m_useChannelRanges(false),
44 m_nowarnStatuses(m_NoWarnStatuses.
begin(), m_NoWarnStatuses.
end()),
45 m_pSignalTool(nullptr) {
46 const string myname =
"ExpTailPedRemover::ctor: ";
49 cout << myname <<
"WARNING: Invalid SignalFlag value " <<
m_SignalFlag 50 <<
" reset to " << signalFlag <<
"." <<
endl;
55 if ( ptm ==
nullptr ) {
56 cout << myname <<
"ERROR: Unable to retrieve tool manager." <<
endl;
60 cout << myname <<
"ERROR: Signal finding tool not found: " <<
m_SignalTool <<
endl;
68 if ( pcrt ==
nullptr ) {
69 cout << myname <<
"ERROR: IndexRangeTool not found: channelRanges" <<
endl;
77 cout << myname <<
"WARNING: Ignoring invalid include channel range " << crn <<
endl;
91 cout << myname <<
"WARNING: Ignoring invalid exclude channel range " << crn <<
endl;
101 cout << myname <<
"WARNING: Pedestal degree reduced from m_PedDegree to 2." <<
endl;
109 cout << myname <<
"WARNING: No fit parameters." <<
endl;
112 cout << myname <<
"ERROR: MaxTick = " <<
m_MaxTick <<
" is less than nfit = " << nfit <<
endl;
130 for (
Index isam=0; isam<nsam; ++isam ) {
138 for (
Index isam=0; isam<nsam; ++isam ) {
140 vec[isam] = dsam*dsam;
146 float twopi = 2.0*acos(-1.0);
150 for (
Index isam=0; isam<nsam; ++isam ) {
151 cvec[isam] = cos(twopi*frq*isam);
152 svec[isam] = sin(twopi*frq*isam);
160 cout << myname <<
"ERROR: Unexpected pedestal vector size: " <<
m_pedVectors.size() <<
endl;
164 cout << myname <<
"Parameters:" <<
endl;
173 cout << myname <<
" PedFreqs: [";
176 if ( first ) first =
false;
181 cout << myname <<
" IncludeChannelRanges: [";
184 if ( first ) first =
false;
189 cout << myname <<
" NoWarnStatuses: [";
192 if ( first ) first =
false;
197 cout << myname <<
" ExcludeChannelRanges: [";
200 if ( first ) first =
false;
206 cout << myname <<
"Channel checking enabled for " << nchaCheck <<
" channel" 207 << ( nchaCheck == 1 ?
"" :
"s") <<
"." << endl;
209 cout << myname <<
"Channel checking disabled." <<
endl;
218 const string myname =
"ExpTailPedRemover::update: ";
224 Index nsam = samples.size();
238 Index ncof = ntai + nped;
240 cout << myname <<
"WARNING: Data for channel " << acd.
channel() <<
" has " 241 << ( nsam==0 ?
"no" :
"too few" ) <<
" ticks: " << nsam <<
" < " << ncof <<
endl;
242 return ret.setStatus(1);
245 cout << myname <<
"WARNING: Data for channel " << acd.
channel() <<
" has too many ticks:" 246 << nsam <<
" > " <<
m_MaxTick <<
". Please increase MaxTick." <<
endl;
247 return ret.setStatus(1);
250 if (
m_LogLevel >= 3 ) cout << myname <<
"Correcting run " << acd.
run() <<
" event " << acd.
event()
255 bool checkSignalDefault =
true;
256 bool findSignal =
false;
258 checkSignalDefault =
false;
260 if ( acd.
signal.size() < nsam ) {
261 cout << mychan <<
"WARNING: Data is missing signal flags--padding from " << acd.
signal.size()
262 <<
" to " << nsam <<
" samples." <<
endl;
266 cout << mychan <<
"WARNING: Signal-finding tool is missing. Using all signals." <<
endl;
267 checkSignalDefault =
false;
280 while ( niter < maxiter ) {
286 cout << myname <<
"WARNING: Signal-finding failed for event " << acd.
event()
290 if ( acd.
signal == signalLast && niter > 0 ) {
291 if (
m_LogLevel >=3 ) cout << mychan <<
"Signal is unchanged. Exiting loop." <<
endl;
296 bool checkSignal = checkSignalDefault;
299 for (
Index isam=0; isam<nsam; ++isam ) {
300 if ( isam < acd.
signal.size() && acd.
signal[isam] )
continue;
301 if ( ++nchk >= ncof )
break;
305 string sstat = cstat==0 ?
"Good" : cstat==1 ?
"Bad" : cstat==2 ?
"Noisy" :
"Unknown";
307 if ( dowarn ) cout << myname <<
"WARNING: " << sstat <<
" channel " << acd.
channel()
308 <<
": Not-signal sample count of " << nchk
309 <<
" is not sufficient to evaluate the " << ncof <<
" fit parameters.";
311 if ( dowarn ) cout <<
" Using all samples." <<
endl;
314 if ( dowarn ) cout <<
" Exiting loop." <<
endl;;
321 using DoubleVectorVector = std::vector<DoubleVector>;
322 DoubleVectorVector cofvecs(ncof,
DoubleVector(nsam, 0.0));
326 cout << mychan <<
"ERROR: SampleTailer is invalid. Exiting." <<
endl;
339 for (
Index iped=0; iped<nped; ++iped ) {
353 TMatrixDSym kmat(ncof);
356 for (
Index isam=0; isam<nsam; ++isam ) {
357 if ( checkSignal && isam < acd.
signal.size() && acd.
signal[isam] )
continue;
358 double cd = cdat[isam];
359 for ( icof=0; icof<ncof; ++icof ) {
360 double ci = cofvecs[icof][isam];
362 for (
Index jcof=0; jcof<=icof; ++jcof ) {
363 double cj = cofvecs[jcof][isam];
364 kmat[icof][jcof] += ci*cj;
370 for ( icof=0; icof<ncof; ++icof ) {
371 for (
Index jcof=0; jcof<icof; ++jcof ) {
372 kmat[jcof][icof] = kmat[icof][jcof];
378 if ( ! kmat.IsValid() || det == 0.0 ) {
380 cout << myname <<
"WARNING: Channel " << acd.
channel() <<
": Unable to invert K-matrix with " 381 << nsamFit <<
" of " << nsam <<
" samples--stopping iteration for channel " 383 for ( icof=0; icof<ncof; ++icof ) {
385 for (
Index jcof=0; jcof<ncof; ++jcof ) {
386 cout <<
setw(20) << kmat[icof][jcof];
395 for ( icof=0; icof<ncof; ++icof ) {
396 cofs[icof] = kvec[icof];
400 float tau =
useTail() ? cofs[0] : 0.0;
403 for (
Index iped=0; iped<nped; ++iped ) {
404 for (
Index isam=0; isam<nsam; ++isam ) {
415 for (
Index isam=0; isam<nsam; ++isam ) {
416 if ( checkSignal && isam < acd.
signal.size() && acd.
signal[isam] )
continue;
417 double sig = sta.
signal()[isam];
420 Index ndof = nsamFit > ncof ? nsamFit - ncof : 1;
421 noise = sqrt(sumsq/ndof);
424 cout << mychan <<
"Iteration " << niter <<
": nsamfit=" << nsamFit
427 for (
float cof : cofs ) {
428 if ( first ) first =
false;
439 cout << mychan <<
"Iteration count: " << niter <<
endl;
440 cout << mychan <<
"Noise: " << noise;
441 if ( sampleUnit.size() ) cout <<
" " << sampleUnit;
442 cout <<
" from " << nsamFit <<
"/" << nsam <<
" samples" <<
endl;
445 for (
float cof : cofs ) {
446 if ( first ) first =
false;
454 acd.
metadata[
"uscPedestal"] = cofs[1];
457 for (
Index icof=0; icof<cofs.size(); ++icof ) {
459 ret.setFloat(name, cofs[icof]);
461 ret.setFloat(
"uscNoise", noise);
462 ret.setInt(
"uscNsamFit", nsamFit);
463 ret.setInt(
"uscNiteration", niter);
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
ExpTailPedRemover(fhicl::ParameterSet const &ps)
IndexSet m_nowarnStatuses
int setData(const FloatVector &inData)
bool usePolynomial() const
IndexVector m_NoWarnStatuses
NameVector m_ExcludeChannelRanges
int setPedestal(float val)
int setBeta(float val, bool cancelSignal)
const FloatVector & signal() const
std::vector< double > DoubleVector
AdcChannelTool * m_pSignalTool
static constexpr double ps
std::vector< Index > IndexVector
Q_EXPORT QTSManip setw(int w)
Index m_SignalIterationLimit
int setPedestalVector(const FloatVector *pval)
Index channelStatus() const
std::vector< bool > m_checkChannels
std::vector< bool > AdcFilterVector
FloatVectorVector m_pedVectors
std::vector< Name > NameVector
std::vector< AdcSignal > AdcSignalVector
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
auto const & get(AssnsNode< L, R, D > const &r)
DataMap update(AdcChannelData &acd) const override
NameVector m_IncludeChannelRanges
std::string to_string(ModuleType const mt)
QTextStream & endl(QTextStream &s)
std::vector< float > FloatVector
int setDataZero(Index nsam)