Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
HistRef.h
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: L. Moneta
3 /*************************************************************************
4  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #ifndef HISTFACTORY_HISTREF_H
12 #define HISTFACTORY_HISTREF_H
13 
14 #include <memory>
15 
16 class TH1;
17 
18 namespace RooStats{
19 namespace HistFactory {
20 
21 
22 // Internal class wrapping an histogram and managing its content.
23 // conveninet for dealing with histogram pointers in the
24 // HistFactory class
25 class HistRef {
26 
27 public:
28 
29 
30  /// constructor - use gives away ownerhip of the given pointer
31  HistRef(TH1 * h = nullptr) : fHist(h) {}
32 
33  HistRef( const HistRef& other ) :
34  fHist() {
35  if (other.fHist) fHist.reset(CopyObject(other.fHist.get()));
36  }
37 
38  HistRef(HistRef&& other) :
39  fHist(std::move(other.fHist)) {}
40 
41  ~HistRef() {}
42 
43  /// assignment operator (delete previous contained histogram)
44  HistRef & operator= (const HistRef & other) {
45  if (this == &other) return *this;
46 
47  fHist.reset(CopyObject(other.fHist.get()));
48  return *this;
49  }
50 
51  HistRef& operator=(HistRef&& other) {
52  fHist = std::move(other.fHist);
53  return *this;
54  }
55 
56  TH1 * GetObject() const { return fHist.get(); }
57 
58  /// set the object - user gives away the ownerhisp
59  void SetObject(TH1 *h) {
60  fHist.reset(h);
61  }
62 
63  /// operator= passing an object pointer : user gives away its ownerhisp
64  void operator= (TH1 * h) { SetObject(h); }
65 
66  /// Release ownership of object.
67  TH1* ReleaseObject() {
68  return fHist.release();
69  }
70 
71 
72 
73 private:
74  static TH1 * CopyObject(const TH1 * h);
75  std::unique_ptr<TH1> fHist; // pointer to contained histogram
76 };
77 
78 }
79 }
80 
81 #endif