AdcRoiViewer.h
Go to the documentation of this file.
1 // AdcRoiViewer.h
2 //
3 // David Adams
4 // October 2017
5 //
6 // Tool to extract information about the ROIs in an ADC channel.
7 //
8 // Configuration:
9 // LogLevel - Logging level: 0=none, 1=init, 2=call, ...
10 // SigThresh - if <0, then only keep ROIs with a tick below this value
11 // if >0, then only keep ROIs with a tick above this value
12 // TickBorder - if > 0, only keep ROIs forwhich there are this many ticks or more
13 // before the start and after the end of the ROI.
14 // RoiHistOpt - histo option: 0 - No histograms
15 // 1 - sample vs. tick
16 // 2 - raw vs. tick
17 // 10 + i - As above for i except vs. tick - tick0
18 // FitOpt - ROI fitting option
19 // 0 - no fit
20 // 1 - fit with coldelecReponse
21 // RoiPlotOpt - 0 = none, 1 = for separate for each event, 2 = multi-event
22 // StartTime - Offset for time meaurements in sec since 1970.
23 // PulserStepCharge - Charge per unit step in a pulser run
24 // PulserDacOffset - Offset in pulser: Qin = PulserStepCharge*(DAC - PulserDacOffset)
25 // PulserChargeUnit - Unit for the pulser charge (ke, fC, ...)
26 // MaxRoiPlots - Maximum # of ROI plots (<0 is no limit, 0 is no plots)
27 // RoiPlotPadX - Number of pad columns in ROI plots. No plots if 0.
28 // RoiPlotPadY - Number of pad rows in ROI plots. No plots if 0.
29 // SumHists - Array of summary histogram specifiers. See below.
30 // SumNegate - If true, the following variable replacements are made for all sum hists:
31 // fitHeight --> fitHeightNeg
32 // SumPlotPadX - Number of pad columns in summary plots.
33 // SumPlotPadY - Number of pad rows in summary plots.
34 // ChannelRanges - Ranges of channels for channel summary plots.
35 // Obtained from IndexRangeTool channelRanges.
36 // ChanSumHists - Array of specifiers for the channel summary histograms.
37 // ChannelLineModulus - Repeat spacing for horizontal lines in summary plots
38 // ChannelLinePattern - Pattern for horizontal lines in summary plots
39 // RunDataTool - Name for the run data tool. If found and pulser is on, then each
40 // ROI is assigned a charge corresponding to the pulser setting.
41 // TickOffsetTool - Name of the tool that provides the tick offset.
42 // RoiRootFileName - Name of file to which the ROI histograms are copied.
43 // SumRootFileName - Name of file to which the summary histograms are copied.
44 // ChanSumRootFileName - Name of file to which the channel summary histograms are copied.
45 // PlotLabels - Array of strings that may be used whe constructin plot titles.
46 // The are referencesd as %LAB0%, %LAB1%, ... %LAB9%
47 //
48 // Summary histograms
49 // ------------------
50 // Summary histograms show the number of ROIs in bins of some ROI variable such
51 // as tick or pulse height.
52 // A summary histogram specifier is a parameter set with the following fields:
53 // var: Name of the variable to draw:
54 // sigArea - Raw area
55 // sigWidth - Raw width
56 // fitHeight - Height from ROI fit
57 // fitHeightNeg - Negative of height from ROI fit
58 // fitHeightGain - Height/(pulser charge)
59 // fitWidth - Shaping time from ROI fit
60 // fitPos - Postion [ticks] from ROI fit
61 // fitPosRem - remainder(fitPos, 1)
62 // fitToffPulser - fmod(fitPos + toff, Tpulser) where
63 // toff is the timing offset, i.e. the time for tick 0 and
64 // Tpulser is the period of the pulser (e.g. 497 ticks)
65 // fitToffPulserMod10 - fmod(fitToffPulser, 10)
66 // fitChiSquare - Chi-square from ROI fit (as reported by TF1)
67 // fitChiSquareDof - Chi-square/DOF from ROI fit (both from TF1)
68 // fitCSNorm - Chi-square/(ped RMS)^2
69 // fitCSNormDof - Chi-square/DOF/(ped RMS)^2
70 // timingPhase_fitToffPulserMod10 - 2D plot of timing phase (0 to 1) vs offset tick
71 // timeSec, timeHour, timeDay - DAQ time since StartTime
72 // procEvent - event number filled once per event
73 // procTimeSec, procTimeHour, procTimeDay - DAQ time since StartTime filled once/event
74 // name: Name of the histogram. Include %CHAN% to get separate histos for each channel
75 // title: Histogram title
76 // nbin: # bins
77 // xmin: Lower edge of the first bin
78 // xmax: Upper edge of the last bin
79 // fit: Name of function used to fit the distribution, e.g. "gaus"
80 // plot: Name of file where plot should be created. E.g. myvar%CHAN%.png
81 // pwid: Plot only includes region of this width around the peak.
82 // If xmin < xmax and xmin > 0, the range will have width xmin centered on the median
83 // value for the first event. If xmax > 0, the lower edge is rounded to that value.
84 // If xmin <= xmax otherwise (e.g. xmin = xmax = 0), Root will do autoscaling of the axis.
85 // E.g.: {var:fitHeight name:"hfh_chan%0CHAN" title:"Fit height for channel %CHAN%"
86 // nbin:50 xmin:0.0 xmax:5.0}
87 // Unless otherwise noted, the histograms are filled once for each ROI.
88 // For variable proc*, the histogram is filled once each time a channel is processed.
89 //
90 // Channel summary histograms
91 // --------------------------
92 // Channel summary histograms hold a metric for each channel derived from a summary histogram.
93 // The following fields specify a channel summary histogram:
94 // name - name for the summary histogram (%CRNAME% is replaced with the channel range name)
95 // title - histogram title (substitutions for %CRLABEL%, %RUN%, ...)
96 // valHist - Name of the summary histogram template from which the metric is derived (should include %CHAN%)
97 // valType - Specifies the metric to be extracted and used to set the bin content for each channe:
98 // entries - Root GetEntries() (Includes under and overflow. The following do not.)
99 // count - Root Integral()
100 // mean - Root GetMean()
101 // peak - Root GetMaximumBin()
102 // rms - Root GetRMS()
103 // rmsFF - Root GetRMS()
104 // fitXXX - Parameter XXX from the fit made to the summary histogram, e.g. Mean for gaus.
105 // fitratXXX - Ratio of parameter XXX from the fit to the mean from the fit.
106 // errType - Specifies the metric used to set the bin error for each channel. Any of the value options or:
107 // none - Do not set error
108 // rms - Root GetRMS()
109 // meanError - Root GetMeanError()
110 // rmsError - Root GetRMSError()
111 // rmsFF - max(Root GetRMS(), FF)
112 // zero - Set the error to zero
113 // bins - if > 0, plot # channels vs. variable in nbins bins
114 // if 0, plot variable vs channel
115 // pran - Range of y axis: ymin:ymax:yscal
116 // yscal = pamp: Multiply range by pulserAmplitude
117 // yscal = pampg14: Multiply range by pulserAmplitude*pulserGain/14.0
118 // plot - Name of the file where the histogram should be plotted.
119 // The histogram name is substituted for %HNAME%.
120 // cr - Name of the channel range to plot. If "list", each value in ChannelRanges.
121 //
122 // Output data map for view:
123 // int roiRun - Run number
124 // int roiSubRun - SubRun number
125 // int roiEvent - Event number
126 // int roiChannel - Channel number
127 // int roiRawCount - # ROI (nROI) before selection
128 // int roiCount - # ROI (nROI)
129 // int roiNTickChannel - # ticks in the ADC channel
130 // int[nROI] roiTick0s - First tick for each ROI
131 // int[nROI] roiNTicks - # ticks for each ROI
132 // int[nROI] roiNUnderflows - # bins with ADC underflow in each ROI
133 // int[nROI] roiNOverflows - # bins with ADC overflow in each ROI
134 // int[nROI] roiTickMins - tick-tick0 for signal minimum for each ROI
135 // int[nROI] roiTickMaxs - tick-tick0 for signal maximum for each ROI
136 // float[nROI] roiSigMins - Signal minimum for each ROI
137 // float[nROI] roiSigMaxs - Signal maximum for each ROI
138 // float[nROI] roiSigAreas - Signal area for each ROI
139 // TH1*[nROI] roiHists - Histogram of samples or raw for each ROI
140 // If fit is done:
141 // float[nROI] roiFitHeights - Height in signal units from fit
142 // float[nROI] roiFitWidths - Shaping time in ticks from fit
143 // float[nROI] roiFitPositions - T0 in ticks from fit
144 // float[nROI] roiFitChiSquares - Chi-square from fit
145 // float[nROI] roiFitChiSquareDofs - Chi-square/DOF from fit
146 // int[nROI] roiFitStats - Return status from fit
147 // If run data is found:
148 // float tpcAmpGain - Nominal preamp gain setting [mV/fC]
149 // float tpcShapingTime - Nominal preamp shaping time [us]
150 // int pulserIndex - Pulser gain setting (0, 1, ..., 63)
151 // int pulserInternal - 1 if pulser on ADC ASIC is used, 0 for FEMB pulser
152 // If fit, run data ane non-zero pulser index:
153 // float(nRoi]
154 //
155 // Output data map for viewMap:
156 // int roiCount - # ROI (nROI)
157 // int roiChannelCount - # channels
158 // int roiFailedChannelCount - # failed channels
159 // int[nfail] failedChannels - List of failed channels
160 //
161 // Lines are draw at N*ChannelLineModulus + ChannelLinePattern[i] for any
162 // integer N and any value if i in range of the array which are within
163 // the drawn channel range.
164 // If ChannelLineModulus is zero, then lines are drawn for the channels in
165 // ChannelLinePattern.
166 
167 
168 #ifndef AdcRoiViewer_H
169 #define AdcRoiViewer_H
170 
172 #include "fhiclcpp/ParameterSet.h"
177 #include <iostream>
178 
180 class RunDataTool;
181 class TimeOffsetTool;
182 class IndexRangeTool;
183 class TH1;
184 
186 
187 public:
188 
189  using Index = unsigned int;
190  using IndexVector = std::vector<Index>;
191  using Name = std::string;
192  using NameVector = std::vector<Name>;
193  using HistVector = std::vector<TH1*>;
194  using HistMap = std::map<Name, TH1*>;
195  using HistVectorMap = std::map<Name, HistVector>;
196  using NameMap = std::map<Name, Name>;
197  using NameVectorMap = std::map<Name, NameVector>;
199  using ChannelRangeMap = std::map<Name, ChannelRange>;
200  using FloatMap = std::map<Name, float>;
201  using IndexByIndexMap = std::map<Index, Index>;
202  using IndexByNameMap = std::map<Name, Index>;
203  using TpmPtr = std::unique_ptr<TPadManipulator>;
204  using TpmMap = std::map<Index, TpmPtr>;
205  using TpmNameMap = std::map<Index, Name>;
206  using TpmCountMap = std::map<Index, Index>;
207 
208  // Subclass that associates a variable name with a histogram.
209  // vary != "" ==> 2D histo
210  class HistInfo {
211  public:
212  TH1* ph = nullptr;
217  float plotWidth;
218  };
219 
220  using HistInfoMap = std::map<Name, HistInfo>;
221 
222  // This subclass carries the state for this tool, i.e. data that can change
223  // after initialization.
224  class State {
225  public:
226  // ROI plots.
230  // Summary histogram templates.
232  // Summary histograms.
233  HistMap sumHists; // Histograms indexed by histogram name.
234  HistVectorMap sumPlotHists; // Histograms for each plot indexed by plot template name.
235  NameMap sumFitNames; // Fit name for each plotted histogram indexed by hist name.
236  NameMap sumPlotNames; // File names for each plotted histogram indexed by hist name.
237  // The first file name is used for plots with multiple hists.
238  FloatMap sumPlotWidths; // Plot width for each plotted histogram indexed by hist name.
239  IndexByNameMap sumHistChannels; // Channel for each sum histogram
240  // Channel summary histograms.
242  NameMap chanSumHistTemplateNames; // Sum template name indexed by chansum name
243  NameMap chanSumHistVariableTypes; // Variable type indexed by chansum name.
244  NameMap chanSumHistErrorTypes; // Error type indexed by chansum name.
245  IndexByNameMap chanSumHistTypes; // Type (0=var vs. chan, 1=#ROI vs var)
246  NameMap chanSumPlotNames; // Plot name indexed by chansum name
247  FloatMap chanSumPlotYMins; // Min value of y for plot.
248  FloatMap chanSumPlotYMaxs; // Max value of y for plot.
249  NameMap chanSumPlotYOpts; // Y scaling option for plot ("", "pamp")
250  IndexByNameMap chanSumChaBegin; // First channel for each histogram.
251  IndexByNameMap chanSumChaEnd; // Last channel for each histogram.
252  ~State();
253  // Fetch properties indexed by a histogram name.
254  TH1* getSumHist(Name hnam);
255  Name getSumFitName(Name hnam) const;
256  Name getSumPlotName(Name hnam) const;
257  float getSumPlotWidth(Name hnam) const;
258  Name getChanSumHistTemplateName(Name hnam) const;
259  Name getChanSumHistVariableType(Name hnam) const;
260  Name getChanSumHistErrorType(Name hnam) const;
261  Name getChanSumPlotName(Name hnam) const;
262  Index getChannelStatus(Index icha) const;
263  Index getChannelStatus(Name hnam) const; // Argument is a chansum histogram name
264  Index cachedRunCount = 0; // Increment each time run number changes.
267  Index nRoiPlot =0;
268  IndexByIndexMap channelStatuses; // Status indexed by channel number
269  // Run data.
271  // Count doView calls.
272  Index callCount =0; // Total
273  IndexByIndexMap eventCallCount; // For each event. Size of this is the event count.
274  Index closeCount = 0;
275  };
276 
277  using StatePtr = std::shared_ptr<State>;
278 
280 
281  ~AdcRoiViewer() override;
282 
283  // AdcChannelTool methods.
284  DataMap view(const AdcChannelData& acd) const override;
285  DataMap viewMap(const AdcChannelDataMap& acds) const override;
286  bool updateWithView() const override { return true; }
287  DataMap close(const DataMap* dmin) override;
288 
289  // Internal methods where most of the work is done.
290 
291  // Read ROIs, fit them and put results in data map.
292  using DataMapVector = std::vector<DataMap>;
293  int doView(const AdcChannelData& acd, int dbg, DataMap& dm) const;
294 
295  // Save the ROI histograms to the ROI Root file.
296  void writeRoiHists(const DataMap& res, int dbg) const;
297  void writeRoiHists(const DataMapVector& res, int dbg) const;
298 
299  // Plot the ROIs for an event/channel.
300  // ADC channel data is used to build plot names.
301  void writeRoiPlots(const HistVector& hists, const AdcChannelData& acd) const;
302 
303  // Return the state.
304  State& getState() const { return *m_state; }
305 
306  // Fill the summary histograms for one channel.
307  void fillSumHists(const AdcChannelData& acd, const DataMap& dm) const;
308 
309  // Fit the summary histograms to the summary Root file.
310  void fitSumHists() const;
311 
312  // Fill the channel summary histograms.
313  void fillChanSumHists() const;
314 
315  // Write the summary histograms to the summary Root files
316  // and summary plots to the plot files.
317  void writeSumHists() const;
318  void writeSumPlots(const DataMap* pdmin) const;
319  void writeChanSumHists() const;
320  void writeChanSumPlots() const;
321 
322  // Replace %LABX% with the corresponding plot label.
323  void setPlotLabels(Name& sttl) const;
324 
325 private:
326 
327  // Configuration data.
329  float m_SigThresh;
332  int m_FitOpt;
337  time_t m_StartTime;
348  Name m_ChannelRangeTool ="channelRanges";
354 
355  // Derived from configuration.
358 
359  // Shared pointer so we can make sure only one reference is out at a time.
361 
362  // Tools.
364  const RunDataTool* m_pRunDataTool =nullptr;
367 
368 };
369 
370 
371 #endif
Index m_TickBorder
Definition: AdcRoiViewer.h:330
Index m_SumPlotPadX
Definition: AdcRoiViewer.h:342
const TimeOffsetTool * m_pTickOffsetTool
Definition: AdcRoiViewer.h:365
void writeChanSumPlots() const
const AdcChannelStringTool * m_adcStringBuilder
Definition: AdcRoiViewer.h:363
std::map< Index, Index > TpmCountMap
Definition: AdcRoiViewer.h:206
HistInfoMap sumHistTemplates
Definition: AdcRoiViewer.h:231
IndexByIndexMap channelStatuses
Definition: AdcRoiViewer.h:268
Index m_ChannelLineModulus
Definition: AdcRoiViewer.h:344
void writeSumPlots(const DataMap *pdmin) const
void writeSumHists() const
const RunDataTool * m_pRunDataTool
Definition: AdcRoiViewer.h:364
void fitSumHists() const
std::map< Name, HistInfo > HistInfoMap
Definition: AdcRoiViewer.h:220
void writeRoiPlots(const HistVector &hists, const AdcChannelData &acd) const
bool dbg
unsigned int Index
std::string string
Definition: nybbler.cc:12
StatePtr m_state
Definition: AdcRoiViewer.h:360
IndexVector m_ChannelLinePattern
Definition: AdcRoiViewer.h:345
std::map< Name, Name > NameMap
Definition: AdcRoiViewer.h:196
Name m_RoiRootFileName
Definition: AdcRoiViewer.h:349
IndexByNameMap sumHistChannels
Definition: AdcRoiViewer.h:239
IndexByNameMap chanSumChaBegin
Definition: AdcRoiViewer.h:250
TpmNameMap roiPadNames
Definition: AdcRoiViewer.h:228
NameVector m_PlotLabels
Definition: AdcRoiViewer.h:353
std::map< Name, NameVector > NameVectorMap
Definition: AdcRoiViewer.h:197
float m_SigThresh
Definition: AdcRoiViewer.h:329
void fillChanSumHists() const
std::vector< Index > IndexVector
Definition: AdcRoiViewer.h:190
FloatMap chanSumPlotYMins
Definition: AdcRoiViewer.h:247
Name m_PulserChargeUnit
Definition: AdcRoiViewer.h:340
void writeRoiHists(const DataMap &res, int dbg) const
std::vector< DataMap > DataMapVector
Definition: AdcRoiViewer.h:292
std::vector< TH1 * > HistVector
Definition: AdcRoiViewer.h:193
DataMap close(const DataMap *dmin) override
NameVector m_ChannelRanges
Definition: AdcRoiViewer.h:352
std::map< Index, Name > TpmNameMap
Definition: AdcRoiViewer.h:205
NameMap chanSumHistErrorTypes
Definition: AdcRoiViewer.h:244
DataMap viewMap(const AdcChannelDataMap &acds) const override
std::vector< Name > NameVector
Definition: AdcRoiViewer.h:192
std::map< Name, TH1 * > HistMap
Definition: AdcRoiViewer.h:194
Index m_RoiPlotOpt
Definition: AdcRoiViewer.h:333
Name m_ChanSumRootFileName
Definition: AdcRoiViewer.h:351
NameMap chanSumHistTemplateNames
Definition: AdcRoiViewer.h:242
FloatMap chanSumPlotYMaxs
Definition: AdcRoiViewer.h:248
AdcRoiViewer(fhicl::ParameterSet const &ps)
std::map< Index, TpmPtr > TpmMap
Definition: AdcRoiViewer.h:204
float m_PulserDacOffset
Definition: AdcRoiViewer.h:339
Index m_SumPlotPadY
Definition: AdcRoiViewer.h:343
int doView(const AdcChannelData &acd, int dbg, DataMap &dm) const
IndexByNameMap chanSumHistTypes
Definition: AdcRoiViewer.h:245
HistVectorMap sumPlotHists
Definition: AdcRoiViewer.h:234
static constexpr double ps
Definition: Units.h:99
IndexByNameMap chanSumChaEnd
Definition: AdcRoiViewer.h:251
DataMap view(const AdcChannelData &acd) const override
Index m_RoiPlotPadX
Definition: AdcRoiViewer.h:335
std::map< Name, float > FloatMap
Definition: AdcRoiViewer.h:200
NameMap m_plotLabelSubs
Definition: AdcRoiViewer.h:357
std::map< Index, Index > IndexByIndexMap
Definition: AdcRoiViewer.h:201
time_t m_StartTime
Definition: AdcRoiViewer.h:337
std::map< Name, ChannelRange > ChannelRangeMap
Definition: AdcRoiViewer.h:199
State & getState() const
Definition: AdcRoiViewer.h:304
void setPlotLabels(Name &sttl) const
std::map< Name, Index > IndexByNameMap
Definition: AdcRoiViewer.h:202
~AdcRoiViewer() override
Name m_RunDataTool
Definition: AdcRoiViewer.h:346
std::unique_ptr< TPadManipulator > TpmPtr
Definition: AdcRoiViewer.h:203
NameMap chanSumHistVariableTypes
Definition: AdcRoiViewer.h:243
Index m_RoiPlotPadY
Definition: AdcRoiViewer.h:336
float m_PulserStepCharge
Definition: AdcRoiViewer.h:338
bool updateWithView() const override
Definition: AdcRoiViewer.h:286
std::map< Name, HistVector > HistVectorMap
Definition: AdcRoiViewer.h:195
void writeChanSumHists() const
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
std::shared_ptr< State > StatePtr
Definition: AdcRoiViewer.h:277
void fillSumHists(const AdcChannelData &acd, const DataMap &dm) const
ChannelRangeMap m_crmap
Definition: AdcRoiViewer.h:356
std::string Name
Definition: AdcRoiViewer.h:191
static Index badIndex()
IndexByIndexMap eventCallCount
Definition: AdcRoiViewer.h:273
const IndexRangeTool * m_pChannelRangeTool
Definition: AdcRoiViewer.h:366
Name m_SumRootFileName
Definition: AdcRoiViewer.h:350
TpmCountMap roiPadCounts
Definition: AdcRoiViewer.h:229
Name m_ChannelRangeTool
Definition: AdcRoiViewer.h:348
Name m_TickOffsetTool
Definition: AdcRoiViewer.h:347