GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RandObj.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef GW_RANDOBJ_H
24 #define GW_RANDOBJ_H
25 
26 #ifndef ROOT_TObjArray
27 #include <TObjArray.h>
28 #endif
29 
30 #ifndef GW_RANDOM_H
31 #include "Random.h"
32 #endif
33 
34 #include <vector>
35 
36 class TH1;
37 
38 namespace Gw {
39 
55 class RandObj : public TObject
56 {
57 private:
58  Random *fRand; // unique instance to Rand
59 
60  TObjArray fObj; // used to store a array of pointers to objects
61 
62  std::vector<Float_t> fCumu; // used to store the cumulative function
63  std::vector<Float_t> fWeight; // used to store the weight distribution
64 
66  void ComputeCumu();
67 
68 public:
69  RandObj() ;
70  virtual ~RandObj() ;
71 
73  virtual TObject * Rand();
74 
76 
80  virtual TObject * Rand(Int_t &);
81 
83 
97  virtual void Add(TObject *, Float_t);
98 
100  virtual void Clear(Option_t *opt = "");
101 
102  virtual Int_t GetSize() const { return fObj.GetEntries(); }
103  virtual TObject * At(Int_t slot) const { return fObj.At(slot); }
104 
106  virtual Float_t WeightAt(Int_t slot) const { if ( 0 <= slot && slot < fObj.GetEntries() ) return fWeight[slot] ; return 0.0; }
107 
109 
113  virtual void FillRandom(TH1 *h, Int_t ntimes = 5000);
114 
115  virtual void ls(Option_t *) const;
116 
118  ClassDef(RandObj,0); // a class to randomly select object in a TObjArray collection
119 };
120 // inline members
121 
122 }
123 
124 #endif
virtual TObject * Rand()
it returns a pointer to a randomly selected object from the collection
Definition: RandObj.cpp:93
virtual ~RandObj()
Definition: RandObj.cpp:47
A class to select randomly an object in a TObjArray of objects.
Definition: RandObj.h:55
virtual void Clear(Option_t *opt="")
clear everything (the objects in the collection are not deleted)
Definition: RandObj.cpp:88
virtual TObject * At(Int_t slot) const
Definition: RandObj.h:103
virtual void Add(TObject *, Float_t)
it adds an object with a weight to this collection.
Definition: RandObj.cpp:49
virtual Int_t GetSize() const
Definition: RandObj.h:102
header file for Random
ClassDef(RandObj, 0)
rootcint dictionary
To change the internal GammaWare random generator.
Definition: Random.h:64
virtual Float_t WeightAt(Int_t slot) const
Intensity with which the object at slot is associated.
Definition: RandObj.h:106
virtual void ls(Option_t *) const
Definition: RandObj.cpp:132
virtual void FillRandom(TH1 *h, Int_t ntimes=5000)
Allocate and fill the histogram randomly.
Definition: RandObj.cpp:139