ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/auterman/SusyScan/PlotScript/PlotTools.cc
Revision: 1.10
Committed: Thu Mar 10 20:59:52 2011 UTC (14 years, 2 months ago) by auterman
Content type: text/plain
Branch: MAIN
Changes since 1.9: +116 -4 lines
Log Message:
commiting style changes

File Contents

# User Rev Content
1 auterman 1.1 #include "PlotTools.h"
2     #include "SusyScan.h"
3     #include "GeneratorMasses.h"
4    
5     #include <cmath>
6     #include <algorithm>
7 auterman 1.5 #include <iostream>
8 auterman 1.1
9     #include "TGraph.h"
10 auterman 1.8 #include "TF1.h"
11 auterman 1.1 #include "TH2.h"
12     #include "TH2F.h"
13     #include "TObjArray.h"
14     #include "TPad.h"
15 auterman 1.4 #include "TCanvas.h"
16 auterman 1.1 #include "TRint.h"
17     #include "TROOT.h"
18    
19     template<class T>
20     TGraph * PlotTools<T>::Line( double(*x)(const T*), double(*y)(const T*),
21 auterman 1.2 double(*func)(const T*), const double mass, const double diff )
22 auterman 1.1 {
23     TGraph * result = new TGraph(1);
24     std::sort(_scan->begin(),_scan->end(),sort_by(x));
25     int i=0;
26     for (typename std::vector<T*>::const_iterator it=_scan->begin();it!=_scan->end();++it){
27     if ( fabs(func( *it)-mass)<diff )
28     result->SetPoint(i++, x(*it), y(*it));
29     }
30 auterman 1.9 result->SetLineWidth(0.5);
31     result->SetLineColor(kGray);
32 auterman 1.1 return result;
33     }
34    
35    
36     template<class T>
37     void PlotTools<T>::Area( TH2*h, double(*x)(const T*), double(*y)(const T*),
38 auterman 1.2 double(*f)(const T*) )
39 auterman 1.1 {
40 auterman 1.6 //std::cout<< _scan->size() <<", &scan="<<_scan<<std::endl;
41 auterman 1.1 for (typename std::vector<T*>::const_iterator it=_scan->begin();it!=_scan->end();++it){
42     h->SetBinContent( h->GetXaxis()->FindBin(x(*it)),
43     h->GetYaxis()->FindBin(y(*it)), f(*it) );
44     }
45     }
46    
47     template<class T>
48 auterman 1.5 void PlotTools<T>::Graph( TGraph*g, double(*x)(const T*), double(*y)(const T*),double ymin)
49     {
50     unsigned i = g->GetN();
51     std::sort(_scan->begin(),_scan->end(),sort_by(x));
52     for (typename std::vector<T*>::const_iterator it=_scan->begin();it!=_scan->end();++it){
53     if (y(*it)>=ymin) g->SetPoint(i++, x(*it), y(*it));
54     //std::cout << i << ": x="<<x(*it)<< ", y="<<y(*it)<< std::endl;
55     }
56     }
57    
58     template<class T>
59 auterman 1.10 std::vector<TGraph*> PlotTools<T>::GetContours005(TH2*h, int ncont)
60     {
61     double conts[1];
62     conts[0] = 0.05;
63     if (!h) return std::vector<TGraph*>();
64     TH2 * plot = (TH2*)h->Clone();
65     plot->SetContour(1, conts);
66     plot->SetFillColor(1);
67     plot->Draw("CONT Z List");
68     gPad->Update();
69     TObjArray *contours = (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");
70     int ncontours = contours->GetSize();
71     std::vector<TGraph*> result;
72     for (int i=0;i<ncontours;++i){
73     TList *list = (TList*)contours->At(i);
74     TGraph* curv = (TGraph*)list->First();
75     if (curv) result.push_back( curv );
76     for(int j = 0; j < list->GetSize(); j++){
77     curv = (TGraph*)list->After(curv); // Get Next graph
78     if (curv) result.push_back( curv );
79     }
80     }
81     delete plot;
82     std::sort(result.begin(),result.end(),sort_TGraph());
83     return result;
84     }
85    
86     template<class T>
87     TGraph * PlotTools<T>::GetContour005(TH2*h, int ncont, int flag)
88     {
89     return (TGraph*)GetContours005(h, ncont).at(flag)->Clone();
90     }
91    
92     template<class T>
93 auterman 1.3 std::vector<TGraph*> PlotTools<T>::GetContours(TH2*h, int ncont)
94 auterman 1.1 {
95 auterman 1.3 if (!h) return std::vector<TGraph*>();
96     TH2 * plot = (TH2*)h->Clone();
97     plot->SetContour(ncont);
98 auterman 1.1 plot->SetFillColor(1);
99 auterman 1.4 plot->Draw("CONT Z List");
100 auterman 1.1 gPad->Update();
101     TObjArray *contours = (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");
102     int ncontours = contours->GetSize();
103 auterman 1.3 std::vector<TGraph*> result;
104     for (int i=0;i<ncontours;++i){
105     TList *list = (TList*)contours->At(i);
106 auterman 1.4 TGraph* curv = (TGraph*)list->First();
107     if (curv) result.push_back( curv );
108     for(int j = 0; j < list->GetSize(); j++){
109     curv = (TGraph*)list->After(curv); // Get Next graph
110     if (curv) result.push_back( curv );
111     }
112 auterman 1.3 }
113     delete plot;
114 auterman 1.5 std::sort(result.begin(),result.end(),sort_TGraph());
115 auterman 1.3 return result;
116     }
117    
118     template<class T>
119     TGraph * PlotTools<T>::GetContour(TH2*h, int ncont, int flag)
120     {
121 auterman 1.4 return (TGraph*)GetContours(h, ncont).at(flag)->Clone();
122     }
123    
124     template<class T>
125 auterman 1.6 TGraph * PlotTools<T>::GetContour(TH2*h,double(*x)(const T*), double(*y)(const T*),
126     double(*func)(const T*), int ncont, int flag,
127     int color, int style){
128     TH2*hist = (TH2*)h->Clone();
129     Area(hist, x, y, func);
130     TGraph * graph = GetContour(hist, ncont, flag);
131     graph->SetLineColor(color);
132     graph->SetLineStyle(style);
133     return graph;
134     }
135 auterman 1.7
136     template<class T>
137 auterman 1.10 TGraph * PlotTools<T>::GetContour005(TH2*h,double(*x)(const T*), double(*y)(const T*),
138     double(*func)(const T*), int ncont, int flag,
139     int color, int style){
140     TH2*hist = (TH2*)h->Clone();
141     Area(hist, x, y, func);
142     TGraph * graph = GetContour005(hist, ncont, flag);
143     graph->SetLineColor(color);
144     graph->SetLineStyle(style);
145     return graph;
146     }
147    
148     template<class T>
149 auterman 1.7 void PlotTools<T>::Print(double(*f)(const T*), double(*x)(const T*), double(*y)(const T*), TGraph*g, double p)
150     {
151     for (typename std::vector<T*>::const_iterator it=_scan->begin();it!=_scan->end();++it){
152     for (int j=0; j<g->GetN(); ++j) {
153     double gx, gy;
154     g->GetPoint(j, gx, gy);
155     if ( (x(*it)-gx)*(x(*it)-gx) + (y(*it)-gy)*(y(*it)-gy) < p*p)
156     std::cout << x(*it) << ", " << y(*it) << " :: " << f(*it) << std::endl;
157     }
158     }
159    
160     }
161    
162 auterman 1.9 template<class T>
163     TGraph * PlotTools<T>::ChooseBest(TGraph*r1,TGraph*r2,TGraph*g1,TGraph*g2,double x,double y)
164     {
165     TGraph * res = new TGraph(0);
166     for (int i=0; i<r1->GetN(); ++i) {
167     double rx, r1y;
168     r1->GetPoint(i,rx,r1y);
169     double r2y = r2->Eval(rx);
170     double g1y=g1->Eval(rx);
171     double resy = (r1y>=r2y?g1y:g2->Eval(rx));
172     if (rx>x&&r1y>y)
173     res->SetPoint(i,rx,resy);
174     else
175     res->SetPoint(i,rx,g1y);
176     }
177     res->SetLineColor(g1->GetLineColor());
178     res->SetLineWidth(g1->GetLineWidth());
179     res->SetLineStyle(g1->GetLineStyle());
180     res->SetFillColor(g1->GetFillColor());
181     res->SetFillStyle(g1->GetFillStyle());
182     return res;
183     }
184    
185     template<class T>
186     TH2 * PlotTools<T>::BinWiseOr(TH2*h1, TH2*h2)
187     {
188     TH2 * res = (TH2*)h1->Clone();
189     for (int x=0; x<=res->GetXaxis()->GetNbins(); ++x)
190     for (int y=0; y<=res->GetYaxis()->GetNbins(); ++y)
191     if (h2->GetBinContent(x,y)>0.5)
192     res->SetBinContent(x,y,h2->GetBinContent(x,y));
193     return res;
194     }
195 auterman 1.7
196 auterman 1.6
197     template<class T>
198 auterman 1.5 bool PlotTools<T>::sort_TGraph::operator()(const TGraph*g1, const TGraph*g2)
199     {
200     return g1->GetN() > g2->GetN();
201 auterman 1.1 }
202 auterman 1.5
203 auterman 1.6 TGraph * MakeBand(TGraph *g1, TGraph *g2, bool b){
204     TGraph * res = new TGraph(g1->GetN()+g2->GetN());
205     int p=0;
206     for (int i=0; i<g1->GetN(); ++i) {
207     double x, y;
208     g1->GetPoint(i, x, y);
209     res->SetPoint(p++, x, y);
210     }
211     for (int i=g2->GetN()-1; i>=0; --i) {
212     double x, y;
213     g2->GetPoint(i, x, y);
214     res->SetPoint(p++, x, y);
215     }
216     res->SetLineColor( g1->GetLineColor() );
217     res->SetFillColor( g2->GetLineColor() );
218 auterman 1.9 res->SetFillStyle(4050);
219 auterman 1.6 return res;
220     }
221    
222 auterman 1.8 void Smooth(TGraph * g, int N)
223     {
224     TGraph * old = (TGraph*)g->Clone();
225     //int N = (n%2==0?n+1:n);
226     if (N>2*g->GetN()) N=2*g->GetN()-1;
227    
228    
229     double gauss[N];
230     double sigma = (double)N/4.;
231     double sum=0;
232     double lim=(double)N/2.;
233     TF1 *fb = new TF1("fb","gaus(0)",-lim,lim);
234     fb->SetParameter(0, 1./(sqrt(2*3.1415)*sigma) );
235     fb->SetParameter(1, 0);
236     fb->SetParameter(2, sigma);
237     for (int i=0; i<N; ++i){
238     gauss[i]=fb->Integral(-lim+i,-lim+i+1);
239     sum+=gauss[i];
240     }
241     for (int i=0; i<N; ++i)
242     gauss[i] /= sum;
243    
244     for (int i=0; i<g->GetN(); ++i){
245 auterman 1.10 double avy=0., avx=0., x, x0, y;
246 auterman 1.8 int points=0;
247     for (int j=i-N/2; j<=i+N/2; ++j){
248     if (j<0) old->GetPoint(0, x, y);
249     else if (j>=g->GetN()) old->GetPoint(old->GetN()-1, x, y);
250     else old->GetPoint(j, x, y);
251     if (i==j) x0=x;
252 auterman 1.10 avy += y * gauss[points];
253     avx += x * gauss[points];
254 auterman 1.8 ++points;
255     }
256 auterman 1.10 if (i-N/2<0 || i+N/2>=g->GetN()) g->SetPoint(i, x0, avy);
257     else g->SetPoint(i, avx, avy);
258 auterman 1.8 }
259     delete old;
260     }
261    
262 auterman 1.1
263 auterman 1.10 void Smooth2D(TGraph * g, int N)
264     {
265     //TGraph * old = (TGraph*)g->Clone();
266     TGraph * old = Close2D(g);
267     //int N = (n%2==0?n+1:n);
268     if (N>2*g->GetN()) N=2*g->GetN()-1;
269    
270    
271     double gauss[N];
272     double sigma = (double)N/4.;
273     double sum=0;
274     double lim=(double)N/2.;
275     TF1 *fb = new TF1("fb","gaus(0)",-lim,lim);
276     fb->SetParameter(0, 1./(sqrt(2*3.1415)*sigma) );
277     fb->SetParameter(1, 0);
278     fb->SetParameter(2, sigma);
279     for (int i=0; i<N; ++i){
280     gauss[i]=fb->Integral(-lim+i,-lim+i+1);
281     sum+=gauss[i];
282     }
283     for (int i=0; i<N; ++i)
284     gauss[i] /= sum;
285    
286     for (int i=0; i<g->GetN(); ++i){
287     double avy=0., avx=0, x, x0, y;
288     int points=0;
289     for (int j=i-N/2; j<=i+N/2; ++j){
290     //if (j<0) old->GetPoint(old->GetN()+j, x, y);
291     //else if (j>=g->GetN()) old->GetPoint(j-old->GetN(), x, y);
292     if (j<0) old->GetPoint(0, x, y);
293     else if (j>=g->GetN()) old->GetPoint(old->GetN()-1, x, y);
294     else old->GetPoint(j, x, y);
295     if (i==j) x0=x;
296     avy += y * gauss[points];
297     avx += x * gauss[points];
298     ++points;
299     }
300     g->SetPoint(i, avx, avy);
301     }
302     delete old;
303     }
304    
305     TGraph * Close2D(TGraph * g)
306     {
307     double x, y;
308     g->GetPoint(0,x,y);
309     g->SetPoint(g->GetN(),x,y);
310    
311     int i=0;
312     for (;i<g->GetN();++i){
313     g->GetPoint(i,x,y);
314     if (x<450&&y<450) break;
315     }
316     int p=0;
317     TGraph * f = new TGraph(0);
318     for (int j=i+1;j!=i;++j){
319     if (j==g->GetN()) j=0;
320     g->GetPoint(j,x,y);
321     if (y<110+(x-120)*390/442||(x<330&&y<1000)||(x<500&&y<600)) continue;
322     f->SetPoint(p++,x,y);
323     }
324     return f;
325     }
326    
327    
328 auterman 1.1 template class PlotTools<SusyScan>;
329     template class PlotTools<GeneratorMasses>;