3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Declares analysis data modules for calculating histograms.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_analysisdata
39 #ifndef GMX_ANALYSISDATA_MODULES_HISTOGRAM_H
40 #define GMX_ANALYSISDATA_MODULES_HISTOGRAM_H
42 #include "../abstractdata.h"
43 #include "../arraydata.h"
44 #include "../datamodule.h"
45 #include "../../utility/uniqueptr.h"
50 class AnalysisHistogramSettings;
53 * Provides "named parameter" idiom for constructing histograms.
55 * \see histogramFromBins()
56 * \see histogramFromRange()
59 * \ingroup module_analysisdata
61 class AnalysisHistogramSettingsInitializer
65 * Creates an empty initializer.
67 * Should not be called directly, but histogramFromRange() or
68 * histogramFromBins() should be used instead.
70 AnalysisHistogramSettingsInitializer();
73 * Sets the first bin location.
75 * Typically should not be called directly, but through
76 * histogramFromBins().
78 AnalysisHistogramSettingsInitializer &start(real min)
79 { min_ = min; return *this; }
81 * Sets the number of bins in the histogram.
83 * If only the first bin location is specified, this value is required
84 * (and automatically provided if histogramFromBins() is used).
85 * If both the first and last bins are specified, either this value or
86 * binWidth() is required.
88 AnalysisHistogramSettingsInitializer &binCount(int binCount)
89 { binCount_ = binCount; return *this; }
91 * Sets the first and last bin locations.
93 * Typically should not be called directly, but through
94 * histogramFromRange().
96 AnalysisHistogramSettingsInitializer &range(real min, real max)
97 { min_ = min; max_ = max; return *this; }
99 * Sets the bin width of the histogram.
101 * If only the first bin location is specified, this value is required
102 * (and automatically provided if histogramFromBins() is used).
103 * If both the first and last bins are specified, either this value or
104 * binCount() is required.
105 * If a bin width is provided with both first and last bin locations,
106 * and the given bin width does not divide the range exactly, the last
107 * bin location is adjusted to match.
109 AnalysisHistogramSettingsInitializer &binWidth(real binWidth)
110 { binWidth_ = binWidth; return *this; }
112 * Indicate that first and last bin locations to specify bin centers.
114 * If set, the first and last bin locations are interpreted as bin
116 * If not set (the default), the first and last bin locations are
117 * interpreted as the edges of the whole histogram.
119 * Cannot be specified together with roundRange().
121 AnalysisHistogramSettingsInitializer &integerBins(bool enabled = true)
122 { bIntegerBins_ = enabled; return *this; }
124 * Round first and last bin locations.
126 * If set, the resulting histogram will cover the range specified, but
127 * the actual bin locations will be rounded such that the edges fall
128 * on multiples of the bin width.
129 * Only implemented when both first and last bin location and bin width
131 * Cannot be specified together with integerBins() or with binCount().
133 AnalysisHistogramSettingsInitializer &roundRange(bool enabled = true)
134 { bRoundRange_ = enabled; return *this; }
136 * Sets the histogram to match all values.
138 * If set, the histogram behaves as if the bins at the ends extended to
141 AnalysisHistogramSettingsInitializer &includeAll(bool enabled = true)
142 { bIncludeAll_ = enabled; return *this; }
153 friend class AnalysisHistogramSettings;
157 * Initializes a histogram using a range and a bin width.
161 inline AnalysisHistogramSettingsInitializer
162 histogramFromRange(real min, real max)
164 return AnalysisHistogramSettingsInitializer().range(min, max);
168 * Initializes a histogram using bin width and the number of bins.
172 inline AnalysisHistogramSettingsInitializer
173 histogramFromBins(real start, int nbins, real binwidth)
175 return AnalysisHistogramSettingsInitializer()
176 .start(start).binCount(nbins).binWidth(binwidth);
181 * Contains parameters that specify histogram bin locations.
184 * \ingroup module_analysisdata
186 class AnalysisHistogramSettings
189 //! Initializes undefined parameters.
190 AnalysisHistogramSettings();
192 * Initializes parameters based on a named parameter object.
194 * This constructor is not explicit to allow initialization of
195 * histograms directly from AnalysisHistogramSettingsInitializer:
197 gmx::AnalysisDataSimpleHistogramModule *hist =
198 new gmx::AnalysisDataSimpleHistogramModule(
199 histogramFromRange(0.0, 5.0).binWidth(0.5));
202 AnalysisHistogramSettings(const AnalysisHistogramSettingsInitializer &settings);
204 //! Returns the left edge of the first bin.
205 real firstEdge() const { return firstEdge_; }
206 //! Returns the right edge of the first bin.
207 real lastEdge() const { return lastEdge_; }
208 //! Returns the number of bins in the histogram.
209 int binCount() const { return binCount_; }
210 //! Returns the width of a bin in the histogram.
211 real binWidth() const { return binWidth_; }
212 //! Whether values beyond the edges are mapped to the edge bins.
213 bool includeAll() const { return bAll_; }
214 //! Returns a zero-based bin index for a value, or -1 if not in range.
215 int findBin(real y) const;
221 real inverseBinWidth_;
230 class BasicHistogramImpl;
232 } // namespace internal
234 class AbstractAverageHistogram;
236 //! Smart pointer to manage an AbstractAverageHistogram object.
237 typedef gmx_unique_ptr<AbstractAverageHistogram>::type
238 AverageHistogramPointer;
241 * Base class for representing histograms averaged over frames.
243 * The averaging module for a per-frame histogram is always created by the
244 * AbstractHistogramModule class, and can be accessed using
245 * AbstractHistogramModule::averager().
246 * The user can alter some properties of the average histogram directly, but
247 * the main use of the object is to postprocess the histogram once the
248 * calculation is finished.
251 * \ingroup module_analysisdata
253 class AbstractAverageHistogram : public AbstractAnalysisArrayData
256 virtual ~AbstractAverageHistogram();
258 //! Returns bin properties for the histogram.
259 const AnalysisHistogramSettings &settings() const { return settings_; }
262 * Creates a copy of the histogram with double the bin width.
264 * The caller is responsible of deleting the returned object.
266 AverageHistogramPointer resampleDoubleBinWidth(bool bIntegerBins) const;
268 * Creates a deep copy of the histogram.
270 * The returned histogram is not necessarily of the same dynamic type
271 * as the original object, but contains the same data from the point of
272 * view of the AbstractAverageHistogram interface.
274 * The caller is responsible of deleting the returned object.
276 AverageHistogramPointer clone() const;
277 //! Normalizes the histogram such that the integral over it is one.
278 void normalizeProbability();
279 //! Scales the value of each bin by an uniform scaling factor.
280 void scale(real norm);
281 //! Scales the value of each bin by a different scaling factor.
282 void scaleVector(real norm[]);
284 * Notifies attached modules of the histogram data.
286 * After this function has been called, it is no longer possible to
287 * alter the histogram.
289 void done() { AbstractAnalysisArrayData::valuesReady(); }
293 * Creates a histogram module with undefined bins.
295 * Bin parameters must be defined with init() before data input is
298 AbstractAverageHistogram();
299 //! Creates a histogram module with defined bin parameters.
300 explicit AbstractAverageHistogram(const AnalysisHistogramSettings &settings);
303 * (Re)initializes the histogram from settings.
305 void init(const AnalysisHistogramSettings &settings);
308 AnalysisHistogramSettings settings_;
310 // Copy and assign disallowed by base.
315 * Data module for per-frame histograms.
317 * Output data contains the same number of frames as the input data.
318 * Each frame contains the histogram for the points in that frame.
319 * All input columns are averaged into the same histogram.
320 * The number of columns equals the number of bins in the histogram.
323 * \ingroup module_analysisdata
325 class AnalysisDataSimpleHistogramModule : public AbstractAnalysisData,
326 public AnalysisDataModuleInterface
330 * Creates a histogram module with undefined bins.
332 * Bin parameters must be defined with init() before data input is
335 AnalysisDataSimpleHistogramModule();
336 //! Creates a histogram module with defined bin parameters.
337 explicit AnalysisDataSimpleHistogramModule(const AnalysisHistogramSettings &settings);
338 virtual ~AnalysisDataSimpleHistogramModule();
341 * (Re)initializes the histogram from settings.
343 void init(const AnalysisHistogramSettings &settings);
346 * Returns the average histogram over all frames.
348 * Can be called already before the histogram is calculated to
349 * customize the way the average histogram is calculated.
351 * \see AbstractAverageHistogram
353 AbstractAverageHistogram &averager();
355 //! Returns bin properties for the histogram.
356 const AnalysisHistogramSettings &settings() const;
358 virtual int flags() const;
360 virtual void dataStarted(AbstractAnalysisData *data);
361 virtual void frameStarted(const AnalysisDataFrameHeader &header);
362 virtual void pointsAdded(const AnalysisDataPointSetRef &points);
363 virtual void frameFinished(const AnalysisDataFrameHeader &header);
364 virtual void dataFinished();
367 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
368 virtual bool requestStorageInternal(int nframes);
370 PrivateImplPointer<internal::BasicHistogramImpl> impl_;
372 // Copy and assign disallowed by base.
377 * Data module for per-frame weighted histograms.
379 * Output data contains the same number of frames as the input data.
380 * Each frame contains the histogram for the points in that frame, interpreted
381 * such that the first column passed to pointsAdded() determines the bin and
382 * the rest give weights to be added to that bin (input data should have at
383 * least two colums, and at least two columns should be added at the same time).
384 * All input columns are averaged into the same histogram.
385 * The number of columns equals the number of bins in the histogram.
388 * \ingroup module_analysisdata
390 class AnalysisDataWeightedHistogramModule : public AbstractAnalysisData,
391 public AnalysisDataModuleInterface
394 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule()
395 AnalysisDataWeightedHistogramModule();
396 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule(const AnalysisHistogramSettings &)
397 explicit AnalysisDataWeightedHistogramModule(const AnalysisHistogramSettings &settings);
398 virtual ~AnalysisDataWeightedHistogramModule();
400 //! \copydoc AnalysisDataSimpleHistogramModule::init()
401 void init(const AnalysisHistogramSettings &settings);
403 //! \copydoc AnalysisDataSimpleHistogramModule::averager()
404 AbstractAverageHistogram &averager();
406 //! \copydoc AnalysisDataSimpleHistogramModule::settings()
407 const AnalysisHistogramSettings &settings() const;
409 virtual int flags() const;
411 virtual void dataStarted(AbstractAnalysisData *data);
412 virtual void frameStarted(const AnalysisDataFrameHeader &header);
413 virtual void pointsAdded(const AnalysisDataPointSetRef &points);
414 virtual void frameFinished(const AnalysisDataFrameHeader &header);
415 virtual void dataFinished();
418 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
419 virtual bool requestStorageInternal(int nframes);
421 PrivateImplPointer<internal::BasicHistogramImpl> impl_;
423 // Copy and assign disallowed by base.
428 * Data module for bin averages.
430 * Output data contains one row for each bin; see AbstractAverageHistogram.
431 * Output data contains three columns: the first is the average over all frames
432 * for that bin, the second is the standard deviation of the values, and the
433 * third is the number of samples in that bin.
434 * The input data is interpreted such that the first column passed to
435 * pointsAdded() determines the bin and the rest give values to be added to
436 * that bin (input data should have at least two colums, and at least two
437 * columns should be added at the same time).
438 * All input columns are averaged into the same histogram.
441 * \ingroup module_analysisdata
443 class AnalysisDataBinAverageModule : public AbstractAnalysisArrayData,
444 public AnalysisDataModuleInterface
447 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule()
448 AnalysisDataBinAverageModule();
449 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule(const AnalysisHistogramSettings &)
450 explicit AnalysisDataBinAverageModule(const AnalysisHistogramSettings &settings);
451 virtual ~AnalysisDataBinAverageModule();
453 //! \copydoc AnalysisDataSimpleHistogramModule::init()
454 void init(const AnalysisHistogramSettings &settings);
456 //! \copydoc AnalysisDataSimpleHistogramModule::settings()
457 const AnalysisHistogramSettings &settings() const { return settings_; }
459 virtual int flags() const;
461 virtual void dataStarted(AbstractAnalysisData *data);
462 virtual void frameStarted(const AnalysisDataFrameHeader &header);
463 virtual void pointsAdded(const AnalysisDataPointSetRef &points);
464 virtual void frameFinished(const AnalysisDataFrameHeader &header);
465 virtual void dataFinished();
468 AnalysisHistogramSettings settings_;
470 // Copy and assign disallowed by base.
473 //! Smart pointer to manage an AnalysisDataSimpleHistogramModule object.
474 typedef boost::shared_ptr<AnalysisDataSimpleHistogramModule>
475 AnalysisDataSimpleHistogramModulePointer;
476 //! Smart pointer to manage an AnalysisDataWeightedHistogramModule object.
477 typedef boost::shared_ptr<AnalysisDataWeightedHistogramModule>
478 AnalysisDataWeightedHistogramModulePointer;
479 //! Smart pointer to manage an AnalysisDataBinAverageModule object.
480 typedef boost::shared_ptr<AnalysisDataBinAverageModule>
481 AnalysisDataBinAverageModulePointer;