Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TFoamMaxwt.cxx
Go to the documentation of this file.
1 // @(#)root/foam:$Id$
2 // Author: S. Jadach <mailto:Stanislaw.jadach@ifj.edu.pl>, P.Sawicki <mailto:Pawel.Sawicki@ifj.edu.pl>
3 
4 /** \class TFoamMaxwt
5 
6 Small auxiliary class for controlling MC weight.
7 It provides certain measure of the "maximum weight"
8 depending on small user-parameter "epsilon".
9 It creates and uses 2 histograms of the TH1D class.
10 User defines no. of bins nBin, nBin=1000 is recommended
11 wmax defines weight range (1,wmax), it is adjusted "manually"
12 */
13 
14 #include "Riostream.h"
15 #include "TH1.h"
16 #include "TFoamMaxwt.h"
17 
18 ClassImp(TFoamMaxwt);
19 
20 ////////////////////////////////////////////////////////////////////////////////
21 /// Constructor for streamer
22 
23 TFoamMaxwt::TFoamMaxwt()
24 {
25  fNent = 0;
26  fnBin = 0;
27  fWtHst1 = 0;
28  fWtHst2 = 0;
29 }
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Principal user constructor
33 
34 TFoamMaxwt::TFoamMaxwt(Double_t wmax, Int_t nBin)
35 {
36  fNent = 0;
37  fnBin = nBin;
38  fwmax = wmax;
39  fWtHst1 = new TH1D("TFoamMaxwt_hst_Wt1","Histo of weight ",nBin,0.0,wmax);
40  fWtHst2 = new TH1D("TFoamMaxwt_hst_Wt2","Histo of weight**2",nBin,0.0,wmax);
41  fWtHst1->SetDirectory(0);// exclude from diskfile
42  fWtHst2->SetDirectory(0);// and enable deleting
43 }
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Explicit COPY CONSTRUCTOR (unused, so far)
47 
48 TFoamMaxwt::TFoamMaxwt(TFoamMaxwt &From): TObject(From)
49 {
50  fnBin = From.fnBin;
51  fwmax = From.fwmax;
52  fWtHst1 = From.fWtHst1;
53  fWtHst2 = From.fWtHst2;
54  Error("TFoamMaxwt","COPY CONSTRUCTOR NOT TESTED!");
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Destructor
59 
60 TFoamMaxwt::~TFoamMaxwt()
61 {
62  delete fWtHst1; // For this SetDirectory(0) is needed!
63  delete fWtHst2; //
64  fWtHst1=0;
65  fWtHst2=0;
66 }
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Reseting weight analysis
69 
70 void TFoamMaxwt::Reset()
71 {
72  fNent = 0;
73  fWtHst1->Reset();
74  fWtHst2->Reset();
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// substitution =
79 
80 TFoamMaxwt& TFoamMaxwt::operator=(const TFoamMaxwt &From)
81 {
82  if (&From == this) return *this;
83  fnBin = From.fnBin;
84  fwmax = From.fwmax;
85  fWtHst1 = From.fWtHst1;
86  fWtHst2 = From.fWtHst2;
87  return *this;
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Filling analyzed weight
92 
93 void TFoamMaxwt::Fill(Double_t wt)
94 {
95  fNent = fNent+1.0;
96  fWtHst1->Fill(wt,1.0);
97  fWtHst2->Fill(wt,wt);
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Calculates Efficiency= aveWt/wtLim for a given tolerance level epsilon<<1
102 /// To be called at the end of the MC run.
103 
104 void TFoamMaxwt::Make(Double_t eps, Double_t &MCeff)
105 {
106  Double_t wtLim,aveWt;
107  GetMCeff(eps, MCeff, wtLim);
108  aveWt = MCeff*wtLim;
109  std::cout<< "00000000000000000000000000000000000000000000000000000000000000000000000"<<std::endl;
110  std::cout<< "00 -->wtLim: No_evt ="<<fNent<<" <Wt> = "<<aveWt<<" wtLim= "<<wtLim<<std::endl;
111  std::cout<< "00 -->wtLim: For eps = "<<eps <<" EFFICIENCY <Wt>/wtLim= "<<MCeff<<std::endl;
112  std::cout<< "00000000000000000000000000000000000000000000000000000000000000000000000"<<std::endl;
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Calculates Efficiency= aveWt/wtLim for a given tolerance level epsilon<<1
117 /// using information stored in two histograms.
118 /// To be called at the end of the MC run.
119 
120 void TFoamMaxwt::GetMCeff(Double_t eps, Double_t &MCeff, Double_t &wtLim)
121 {
122  Int_t ib,ibX;
123  Double_t lowEdge,bin,bin1;
124  Double_t aveWt, aveWt1;
125 
126  fWtHst1->Print();
127  fWtHst2->Print();
128 
129 // Convention on bin-numbering: nb=1 for 1-st bin, underflow nb=0, overflow nb=Nb+1
130  Double_t sum = 0.0;
131  Double_t sumWt = 0.0;
132  for(ib=0;ib<=fnBin+1;ib++) {
133  sum += fWtHst1->GetBinContent(ib);
134  sumWt += fWtHst2->GetBinContent(ib);
135  }
136  if( (sum == 0.0) || (sumWt == 0.0) ) {
137  std::cout<<"TFoamMaxwt::Make: zero content of histogram !!!,sum,sumWt ="<<sum<<sumWt<<std::endl;
138  }
139  aveWt = sumWt/sum;
140  /////////////////////////////////////////////////////////////////////////////
141 
142  for( ibX=fnBin+1; ibX>0; ibX--) {
143  lowEdge = (ibX-1.0)*fwmax/fnBin;
144  sum = 0.0;
145  sumWt = 0.0;
146  for( ib=0; ib<=fnBin+1; ib++) {
147  bin = fWtHst1->GetBinContent(ib);
148  bin1 = fWtHst2->GetBinContent(ib);
149  if(ib >= ibX) bin1=lowEdge*bin;
150  sum += bin;
151  sumWt += bin1;
152  }
153  aveWt1 = sumWt/sum;
154  if( TMath::Abs(1.0-aveWt1/aveWt) > eps ) break;
155  }
156  /////////////////////////////////////////////////////////////////////////////
157 
158  if(ibX == (fnBin+1) ) {
159  wtLim = 1.0e200;
160  MCeff = 0.0;
161  std::cout<< "+++++ wtLim undefined. Higher upper limit in histogram"<<std::endl;
162  } else if( ibX == 1) {
163  wtLim = 0.0;
164  MCeff =-1.0;
165  std::cout<< "+++++ wtLim undefined. Lower upper limit or more bins "<<std::endl;
166  } else {
167  wtLim= (ibX)*fwmax/fnBin; // We over-estimate wtLim, under-estimate MCeff
168  MCeff = aveWt/wtLim;
169  }
170 }
171