ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/csander/ToyMC/toyMC_LVMini.cpp
Revision: 1.1.1.1 (vendor branch)
Committed: Tue Nov 4 13:41:08 2008 UTC (16 years, 6 months ago) by csander
Branch: ToyMc, MAIN
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
program using toyMC to studie gamma jet calibration (using root and LVMini)

File Contents

# User Rev Content
1 csander 1.1 #include "TROOT.h"
2     #include "TSystem.h"
3     #include "TF1.h"
4     #include "TH1.h"
5     #include "TH2.h"
6     #include "TProfile.h"
7     #include "TCanvas.h"
8     #include "TRandom.h"
9     #include "TStyle.h"
10     #include "TObjArray.h"
11     #include "TGraph.h"
12     #include "TMath.h"
13     #include "TSpline.h"
14    
15     #include <iostream>
16     #include <vector>
17     #include <string>
18    
19     #include "external.h"
20    
21     using namespace::std;
22    
23     ////////////////////////////////////////////////////////////
24     class TDataPoint{
25    
26     private:
27     double weight;
28     double weight_orig;
29     double truth;
30     double meas;
31     double error;
32    
33     public:
34     double GetTruth(){return truth;};
35     double GetMeas(){return meas;};
36     double GetError(){return error;};
37     double GetWeight(){return weight;};
38     double GetWeight_orig(){return weight_orig;};
39     void SetWeight(double w){weight=w;};
40     void SetWeight_orig(double w){weight_orig=w;};
41    
42     //constructor
43     TDataPoint(double t, double m, double e, double w){
44     weight = w;
45     weight_orig = w;
46     truth = t;
47     meas = m;
48     error = e;
49     };
50    
51     //destructor
52     ~TDataPoint(){};
53    
54     };
55     ////////////////////////////////////////////////////////////
56    
57     ////////////////////////////////////////////////////////////
58     class TData{
59    
60     private:
61     int Nevts;
62     double minX, maxX;
63     double minY, maxY;
64     double dY;
65     vector<TDataPoint> data;
66     vector<double> sp;
67    
68     //----------------------------------------------------------
69     double chi2(vector<double>* PAR){
70    
71     TGraph cgr(sp.size(), &sp.front(), &PAR->front());
72     TSpline3 *cspl = new TSpline3("cspl",&cgr);
73     double x2 = 0.;
74     for (vector<TDataPoint>::iterator i = data.begin(); i < data.end(); ++i){
75     double mess=i->GetMeas();
76     double truth=i->GetTruth();
77     /*
78     double c = calib(mess,cspl);
79     double dc = dcalib(mess,cspl);
80     double d = c * mess - truth;
81     double e = dc * i->GetError();
82     */
83     /*
84     double c = calib(truth,cspl);
85     double d = c * mess - truth;
86     double e = c * i->GetError();
87     */
88     double cmess = calib(mess,cspl);
89     double cold = 1;
90     double cinv = calibinv(truth,cmess,cold,cspl);
91     double d = mess - truth/cinv;
92     double e = i->GetError();
93     x2 += i->GetWeight()*pow(d/e, 2);
94     }
95     return x2;
96     }
97     //----------------------------------------------------------
98    
99     //----------------------------------------------------------
100     double calib(double &x, TSpline3* calibSpline){
101     return calibSpline->Eval(x);
102     }
103     //----------------------------------------------------------
104    
105     //----------------------------------------------------------
106     //might be usefull if calibration constant is defined as function of truth
107     double calibinv(double& x, double &cmess, double &cold, TSpline3* calibSpline){
108     double cinv = calibSpline->Eval(x/cmess);
109     //cout<<"cold: "<<cold<<" cnew: "<<cinv<<endl;
110     double delta = fabs((cinv-cold)/cold);
111     if (delta<1.e-4){
112     return cinv;
113     } else {
114     return calibSpline->Eval(x/calibinv(x,cmess,cinv,calibSpline));
115     }
116     }
117     //----------------------------------------------------------
118    
119     //----------------------------------------------------------
120     double dcalib(double &mess, TSpline3* calibSpline){
121     double eps=1.;
122     double dc=((mess+eps)*calibSpline->Eval(mess+eps)-
123     (mess-eps)*calibSpline->Eval(mess-eps))/2./eps;
124     return dc;
125     }
126     //----------------------------------------------------------
127    
128     //----------------------------------------------------------
129     //error parametrization, width of measured distribution
130     double error(double x){
131     double error = -1;
132     if ( x >= 0 ) error = 1.25*sqrt(x);
133     return error;
134     }
135     //----------------------------------------------------------
136    
137     //----------------------------------------------------------
138     //reweight events to have flat truth distribution
139     void FlattenSpectra(){
140    
141     TH1F* hist1 = new TH1F("truth non weighted","True distribution of measurement", 50, 0, 1000);
142     TH1F* hist2 = new TH1F("truth weighted","Weighted true distribution of measurement", 50, 0, 1000);
143     TH1F* weights = new TH1F("weights","Weights to flatten distribution", 50, 0, 1000);
144     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
145     hist1->Fill(i->GetTruth(),i->GetWeight());
146     }
147     double Integral1=hist1->Integral();
148     for (int i=0; i<hist1->GetNbinsX(); ++i){
149     weights->SetBinContent(i,1./hist1->GetBinContent(i));
150     }
151     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
152     double w=weights->GetBinContent(hist1->FindBin(i->GetTruth()));
153     hist2->Fill(i->GetTruth(),w);
154     }
155     double Integral2=hist2->Integral();
156     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
157     double w=weights->GetBinContent(hist1->FindBin(i->GetTruth()));
158     w*=i->GetWeight();
159     w*=Integral1/Integral2;
160     i->SetWeight(w);
161     i->SetWeight_orig(w);
162     }
163     }
164     //----------------------------------------------------------
165    
166     //----------------------------------------------------------
167     //reweight spectra to correct for energy dependent resolution
168     void ResolutionReweight(bool logscale = false, bool outlier=false, int sigmamax=3){
169    
170     cout<<"Events at start of ResolutionReweight: "<<data.size()<<endl;
171    
172     int Nbins=49;
173     int minEntries=200;
174     vector<TDataPoint> dataCut;
175     vector<double> mean;
176     vector<double> sigma;
177    
178     double d;
179     if (logscale){
180     d=(log(maxY)-log(minY))/Nbins;
181     } else {
182     d=(maxY-minY)/Nbins;
183     }
184    
185     //book histos
186     TH1F* hist[Nbins];
187     TF1* f[Nbins];
188     for (int n=0; n<Nbins; ++n){
189     char hname[100];
190     sprintf(hname,"hr%i",n);
191     hist[n] = new TH1F(hname,"Truth for MeasBin", 1000, minX, maxX);
192     hist[n]->Sumw2();
193     }
194    
195     //Fill histos
196     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
197     int n;
198     if (logscale){
199     n=(int)(Nbins*(log(i->GetMeas())-log(minY))/(log(maxY)-log(minY)));
200     if (n>=Nbins) n=Nbins-1;
201     } else {
202     n=(int)(Nbins*(i->GetMeas()-minY)/d);
203     if (n>=Nbins) n=Nbins-1;
204     };
205     hist[n]->Fill(i->GetTruth(),i->GetWeight());
206     }
207    
208     //Fit histos
209     for (int n=0; n<Nbins; ++n){
210     cout<<n<<" of "<<Nbins<<endl;
211     if (hist[n]->GetEntries()<minEntries){
212     mean.push_back(0);
213     sigma.push_back(0);
214     continue;
215     }
216     char fname[100];
217     sprintf(fname,"fr%i",n);
218     f[n] = new TF1(fname,"gausn",minX,maxX);
219     //use a guess for starting values
220     f[n]->SetParameters(hist[n]->Integral("width"),hist[n]->GetMean(),hist[n]->GetRMS());
221     hist[n]->Fit(fname,"ILLRQN");
222     double Mean=f[n]->GetParameter(1);
223     double Sigma=f[n]->GetParameter(2);
224     mean.push_back(Mean);
225     sigma.push_back(Sigma);
226     }
227    
228     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
229     int n;
230     if (logscale){
231     n=(int)(Nbins*(log(i->GetMeas())-log(minY))/(log(maxY)-log(minY)));
232     if (n>=Nbins) n=Nbins-1;
233     } else {
234     n=(int)(Nbins*(i->GetMeas()-minY)/d);
235     if (n>=Nbins) n=Nbins-1;
236     };
237     double delta = mean.at(n) - i->GetTruth();
238     //Use only events with truth value in 3 sigma of mean truth... ???
239     if (outlier && fabs(delta/sigma.at(n))>3.) continue;
240     double A, B;
241     double ERR = error(i->GetTruth()+2*delta);
242     if (ERR > 0){
243     A = 1./sqrt(2*TMath::Pi())/i->GetError()*exp(-pow(delta,2)/2./pow(i->GetError(),2));
244     B = 1./sqrt(2*TMath::Pi())/ERR*exp(-pow(delta,2)/2./pow(ERR,2));
245     double newweight = 2*B/(A+B);
246     i->SetWeight(i->GetWeight_orig()*newweight);
247     }
248     dataCut.push_back(*i);
249     }
250     data.clear();
251     data.assign(dataCut.begin(),dataCut.end());
252     cout<<"Events at end of ResolutionReweight: "<<data.size()<<endl;
253    
254     minY=99999.;
255     maxY=0;
256     minX=99999.;
257     maxX=0;
258     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
259     if (i->GetMeas() < minY) minY=i->GetMeas();
260     if (i->GetMeas() > maxY) maxY=i->GetMeas();
261     if (i->GetTruth() < minX) minX=i->GetTruth();
262     if (i->GetTruth() > maxX) maxX=i->GetTruth();
263     }
264    
265     }
266     //----------------------------------------------------------
267    
268     //----------------------------------------------------------
269     //symmetrize truth distribution for one measurement
270     void CorrectCutoff(bool logscale = false){
271    
272     cout<<"Events at start of CorrectCutoff: "<<data.size()<<endl;
273     int Nbins=49;
274     int minEntries=200;
275     vector<TDataPoint> dataCut;
276     vector<double> mean;
277     vector<double> dmean;
278    
279     TCanvas *c1 = new TCanvas("c1","Correct for cut off",0,0,1000,1000);
280     c1->Divide(7,7);
281     double d;
282     if (logscale){
283     d=(log(maxY)-log(minY))/Nbins;
284     } else {
285     d=(maxY-minY)/Nbins;
286     }
287    
288     //book histos
289     TH1F* hist[Nbins];
290     TF1* f[Nbins];
291     for (int n=0; n<Nbins; ++n){
292     char hname[100];
293     sprintf(hname,"hc%i",n);
294     hist[n] = new TH1F(hname,"Truth for MeasBin", 1000, minX, maxX);
295     hist[n]->Sumw2();
296     }
297    
298     //Fill histos
299     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
300     int n;
301     if (logscale){
302     n=(int)(Nbins*(log(i->GetMeas())-log(minY))/(log(maxY)-log(minY)));
303     if (n>=Nbins) n=Nbins-1;
304     } else {
305     n=(int)(Nbins*(i->GetMeas()-minY)/d);
306     if (n>=Nbins) n=Nbins-1;
307     };
308     hist[n]->Fill(i->GetTruth(),i->GetWeight());
309     }
310    
311     //Fit histos
312     for (int n=0; n<Nbins; ++n){
313     cout<<n<<" of "<<Nbins<<endl;
314     if (hist[n]->GetEntries()<minEntries){
315     mean.push_back(0);
316     dmean.push_back(0);
317     continue;
318     }
319     char fname[100];
320     sprintf(fname,"fc%i",n);
321     f[n] = new TF1(fname,"gausn",minX,maxX);
322     //use a guess for starting values
323     f[n]->SetParameters(hist[n]->Integral("width"),hist[n]->GetMean(),hist[n]->GetRMS());
324     c1->cd(n+1);
325     hist[n]->SetLineWidth(0);
326     hist[n]->Draw();
327     f[n]->SetLineWidth(0);
328     f[n]->SetLineColor(2);
329     hist[n]->Fit(fname,"ILLRQ");
330     double Mean=f[n]->GetParameter(1);
331     mean.push_back(Mean);
332     double dMean=0;
333     if (Mean<minX || Mean>maxX){
334     dMean=0;
335     } else {
336     double dMeanMin=fabs(Mean-minX);
337     double dMeanMax=fabs(Mean-maxX);
338     dMeanMin<dMeanMax?dMean=dMeanMin:dMean=dMeanMax;
339     }
340     dmean.push_back(dMean);
341     }
342     c1->SaveAs("cut_LVMini.eps");
343    
344     //symmetrize distributions
345     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
346     int n;
347     if (logscale){
348     n=(int)(Nbins*(log(i->GetMeas())-log(minY))/(log(maxY)-log(minY)));
349     if (n>=Nbins) n=Nbins-1;
350     } else {
351     n=(int)(Nbins*(i->GetMeas()-minY)/d);
352     if (n>=Nbins) n=Nbins-1;
353     };
354     double delta = fabs(mean.at(n) - i->GetTruth());
355     if (delta>dmean.at(n)) continue;
356     dataCut.push_back(*i);
357     }
358    
359     data.clear();
360     data.assign(dataCut.begin(),dataCut.end());
361     cout<<"Events at end of CorrectCutoff: "<<data.size()<<endl;
362    
363     minY=99999.;
364     maxY=0;
365     minX=99999.;
366     maxX=0;
367     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
368     if (i->GetMeas() < minY) minY=i->GetMeas();
369     if (i->GetMeas() > maxY) maxY=i->GetMeas();
370     if (i->GetTruth() < minX) minX=i->GetTruth();
371     if (i->GetTruth() > maxX) maxX=i->GetTruth();
372     }
373    
374     }
375     //----------------------------------------------------------
376    
377     public:
378    
379     vector<TDataPoint>* GetDataRef(){return &data;};
380    
381     //----------------------------------------------------------
382     void DrawTruth(){
383     TH1F* hist = new TH1F("truth","True distribution of measurement", 100, 0, maxX);
384     hist->Sumw2();
385     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
386     hist->Fill(i->GetTruth(),i->GetWeight());
387     }
388     TCanvas *c1 = new TCanvas("c1","Truth",0,0,600,600);
389     c1->Divide(1,1);
390     c1->cd(1);
391     c1->cd(1)->SetLogy();
392     hist->SetXTitle("Truth");
393     hist->Draw();
394     c1->SaveAs("truth_LVMini.eps");
395     };
396     //----------------------------------------------------------
397    
398     //----------------------------------------------------------
399     void DrawMeas(){
400     TH1F* hist = new TH1F("meas","Measured distribution of measurement", 100, 0, maxY);
401     hist->Sumw2();
402     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
403     hist->Fill(i->GetMeas(),i->GetWeight());
404     }
405     TCanvas *c1 = new TCanvas("c2","Measurement",0,0,600,600);
406     c1->Divide(1,1);
407     c1->cd(1);
408     c1->cd(1)->SetLogy();
409     hist->SetXTitle("Measurement");
410     hist->Draw();
411     c1->SaveAs("meas_LVMini.eps");
412     };
413     //----------------------------------------------------------
414    
415     //----------------------------------------------------------
416     void DrawCalibConst(vector<double> p){
417     TGraph cgr(sp.size(), &sp.front(), &p.front());
418     TSpline3 *cspl = new TSpline3("cspl",&cgr);
419     vector<double> vx, vy;
420     int Nbins = 100;
421     for (int i=0; i<=Nbins; ++i){
422     double x=minY+i*(maxY-minY)/Nbins;
423     vx.push_back(double(x));
424     double c=calib(x,cspl);
425     vy.push_back(c);
426     }
427     TGraph* CalibConst = new TGraph(vx.size(), &vx.front(), &vy.front());
428     TCanvas *c1 = new TCanvas("c3","Calibration Constant vs. Measurement",0,0,600,600);
429     c1->Divide(1,1);
430     c1->cd(1);
431     CalibConst->SetLineColor(2);
432     CalibConst->SetLineWidth(4);
433     CalibConst->SetTitle("Calibration Constant");
434     CalibConst->GetXaxis()->SetTitle("Truth");
435     CalibConst->GetYaxis()->SetTitle("C");
436     CalibConst->SetMinimum(0.9);
437     CalibConst->SetMaximum(1.3);
438     CalibConst->Draw("ACP");
439     //CalibConst->Draw("L");
440     c1->SaveAs("calibconst_LVMini.eps");
441     };
442     //----------------------------------------------------------
443    
444     //----------------------------------------------------------
445     void DrawRatio(vector<double> p){
446     TGraph cgr(sp.size(), &sp.front(), &p.front());
447     TSpline3 *cspl = new TSpline3("cspl",&cgr);
448     TH2F* hist1 = new TH2F("r1","Measurement/Truth vs. Truth", 100, 0, 1000, 100, 0.0, 2.0);
449     TH2F* hist2 = new TH2F("r2","Measurement/Truth vs. Measurement", 100, 0, 1000, 100, 0.0, 2.0);
450     TH2F* hist3 = new TH2F("r3","Measurement/Truth vs. CalibMeasurement", 100, 0, 1000, 100, 0.0, 2.0);
451     TH2F* hist4 = new TH2F("r4","CalibMeasurement/Truth vs. Truth", 100, 0, 1000, 100, 0.0, 2.0);
452     TH2F* hist5 = new TH2F("r5","CalibMeasurement/Truth vs. Measurement", 100, 0, 1000, 100, 0.0, 2.0);
453     TH2F* hist6 = new TH2F("r6","CalibMeasurement/Truth vs. CalibMeasurement", 100, 0, 1000, 100, 0.0, 2.0);
454     hist1->Sumw2();
455     hist2->Sumw2();
456     hist3->Sumw2();
457     hist4->Sumw2();
458     hist5->Sumw2();
459     hist6->Sumw2();
460     hist1->SetTitleOffset(1.2,"Y");
461     hist2->SetTitleOffset(1.2,"Y");
462     hist3->SetTitleOffset(1.2,"Y");
463     hist4->SetTitleOffset(1.2,"Y");
464     hist5->SetTitleOffset(1.2,"Y");
465     hist6->SetTitleOffset(1.2,"Y");
466     hist1->SetTitleOffset(1.2,"Y");
467     hist1->SetYTitle("k=Meas/Truth");
468     hist2->SetYTitle("k=Meas/Truth");
469     hist3->SetYTitle("k=Meas/Truth");
470     hist4->SetYTitle("k=calibMeas/Truth");
471     hist5->SetYTitle("k=calibMeas/Truth");
472     hist6->SetYTitle("k=calibMeas/Truth");
473     hist1->SetXTitle("Truth");
474     hist4->SetXTitle("Truth");
475     hist2->SetXTitle("Measurement");
476     hist5->SetXTitle("Measurement");
477     hist3->SetXTitle("calib. Measurement");
478     hist6->SetXTitle("calib. Measurement");
479     for (vector<TDataPoint>::iterator i=data.begin(); i<data.end(); ++i){
480     double mess=i->GetMeas();
481     double truth=i->GetTruth();
482     double c=calib(mess,cspl);
483     hist1->Fill(truth, mess/truth, i->GetWeight());
484     hist2->Fill(mess, mess/truth, i->GetWeight());
485     hist3->Fill(c*mess, mess/truth, i->GetWeight());
486     hist4->Fill(truth, (c*mess)/truth, i->GetWeight());
487     hist5->Fill(mess, (c*mess)/truth, i->GetWeight());
488     hist6->Fill(c*mess, (c*mess)/truth, i->GetWeight());
489     }
490     gROOT->SetStyle("Plain");
491     gStyle->SetPalette(51,0);
492     TCanvas *c1 = new TCanvas("c4","Ration",0,0,900,900);
493     TF1 *f = new TF1("f","gaus",minX,maxX);
494     TObjArray aSlices;
495     c1->Divide(3,3);
496     c1->cd(1);
497     gPad->SetRightMargin(0.15);
498     gPad->SetLeftMargin(0.15);
499     hist1->Draw("COLZ");
500     hist1->FitSlicesY(f, 0, -1, 0, "NQ", &aSlices);
501     TH1* hist1_1 = (TH1*)(aSlices[1]);
502     TH1* hist1_2 = (TH1*)(aSlices[2]);
503     for (int i=0; i<hist1_1->GetNbinsX(); ++i){
504     hist1_1->SetBinError(i,hist1_2->GetBinContent(i));
505     }
506     hist1_1->SetLineColor(2);
507     hist1_1->Draw("same");
508     hist1->SetLineColor(5);
509     hist1->ProfileX()->Draw("same");
510    
511     c1->cd(2);
512     gPad->SetRightMargin(0.12);
513     hist2->Draw("COLZ");
514     hist2->FitSlicesY(f, 0, -1, 0, "NQ", &aSlices);
515     TH1* hist2_1 = (TH1*)(aSlices[1]);
516     TH1* hist2_2 = (TH1*)(aSlices[2]);
517     for (int i=0; i<hist2_1->GetNbinsX(); ++i){
518     hist2_1->SetBinError(i,hist2_2->GetBinContent(i));
519     }
520     hist2_1->SetLineColor(2);
521     hist2_1->Draw("same");
522     hist2->SetLineColor(5);
523     hist2->ProfileX()->Draw("same");
524    
525     c1->cd(3);
526     gPad->SetRightMargin(0.12);
527     hist3->Draw("COLZ");
528     hist3->FitSlicesY(f, 0, -1, 0, "NQ", &aSlices);
529     TH1* hist3_1 = (TH1*)(aSlices[1]);
530     TH1* hist3_2 = (TH1*)(aSlices[2]);
531     for (int i=0; i<hist3_1->GetNbinsX(); ++i){
532     hist3_1->SetBinError(i,hist3_2->GetBinContent(i));
533     }
534     hist3_1->SetLineColor(2);
535     hist3_1->Draw("same");
536     hist3->SetLineColor(5);
537     hist3->ProfileX()->Draw("same");
538    
539     c1->cd(4);
540     gPad->SetRightMargin(0.12);
541     hist4->Draw("COLZ");
542     hist4->FitSlicesY(f, 0, -1, 0, "NQ", &aSlices);
543     TH1* hist4_1 = (TH1*)(aSlices[1]);
544     TH1* hist4_2 = (TH1*)(aSlices[2]);
545     for (int i=0; i<hist4_1->GetNbinsX(); ++i){
546     hist4_1->SetBinError(i,hist4_2->GetBinContent(i));
547     }
548     hist4_1->SetLineColor(2);
549     hist4_1->Draw("same");
550     hist4->SetLineColor(5);
551     hist4->ProfileX()->Draw("same");
552    
553     c1->cd(5);
554     gPad->SetRightMargin(0.12);
555     hist5->Draw("COLZ");
556     hist5->FitSlicesY(f, 0, -1, 0, "NQ", &aSlices);
557     TH1* hist5_1 = (TH1*)(aSlices[1]);
558     TH1* hist5_2 = (TH1*)(aSlices[2]);
559     for (int i=0; i<hist5_1->GetNbinsX(); ++i){
560     hist5_1->SetBinError(i,hist5_2->GetBinContent(i));
561     }
562     hist5_1->SetLineColor(2);
563     hist5_1->Draw("same");
564     hist5->SetLineColor(5);
565     hist5->ProfileX()->Draw("same");
566    
567     c1->cd(6);
568     gPad->SetRightMargin(0.12);
569     hist6->Draw("COLZ");
570     hist6->FitSlicesY(f, 0, -1, 0, "NQ", &aSlices);
571     TH1* hist6_1 = (TH1*)(aSlices[1]);
572     TH1* hist6_2 = (TH1*)(aSlices[2]);
573     for (int i=0; i<hist6_1->GetNbinsX(); ++i){
574     hist6_1->SetBinError(i,hist6_2->GetBinContent(i));
575     }
576     hist6_1->SetLineColor(2);
577     hist6_1->Draw("same");
578     hist6->SetLineColor(5);
579     hist6->ProfileX()->Draw("same");
580    
581     c1->cd(7);
582     hist4_1->SetTitleOffset(1.2,"Y");
583     hist4_1->SetXTitle("Truth");
584     hist4_1->SetYTitle("k");
585     hist4_1->SetLineWidth(0);
586     hist4_1->SetLineColor(2);
587     hist4_1->SetMinimum(0.95);
588     hist4_1->SetMaximum(1.05);
589     hist4_1->Draw();
590    
591     c1->cd(8);
592     hist5_1->SetTitleOffset(1.2,"Y");
593     hist5_1->SetXTitle("Measurement");
594     hist5_1->SetYTitle("k");
595     hist5_1->SetLineWidth(0);
596     hist5_1->SetLineColor(2);
597     hist5_1->SetMinimum(0.95);
598     hist5_1->SetMaximum(1.05);
599     hist5_1->Draw();
600    
601     c1->cd(9);
602     hist6_1->SetTitleOffset(1.2,"Y");
603     hist6_1->SetXTitle("calib. Measurement");
604     hist6_1->SetYTitle("k");
605     hist6_1->SetLineWidth(0);
606     hist6_1->SetLineColor(2);
607     hist6_1->SetMinimum(0.95);
608     hist6_1->SetMaximum(1.05);
609     hist6_1->Draw();
610    
611     c1->SaveAs("ratio_LVMini.eps");
612     };
613     //----------------------------------------------------------
614    
615     //----------------------------------------------------------
616     //constructor
617     TData(int N=100, double min=300., double max=700., int npar=10,
618     bool flat=false,
619     bool cutoff=false, bool cutofflog=false,
620     bool reweight=false, bool reweightlog=false,
621     bool reco=false){
622     Nevts = N;
623     minX=99999;
624     maxX=0;
625     minY=99990;
626     maxY=0;
627     int Nnegative=0;
628     TF1 *recoeff = new TF1("f","pow(x/30.,2)/(1.+pow(x/30.,2))");
629     for (int i=0;i<Nevts;i++) {
630     double x = gRandom->Rndm(i);
631     //double truth = min+(max-min)*x;
632     double truth = gRandom->Exp(150);
633     double width = error(truth);
634     double meas = gRandom->Gaus(0.9*truth,width);
635     double pr = gRandom->Rndm(i);
636     //double pr = -9999.;
637     if (meas<0 && truth>min){
638     ++Nnegative;
639     cout<<"WARNING: negative measurement (truth="<<truth<<",meas="<<meas<<") no. "<<Nnegative<<endl;
640     }
641     if (meas>0 && truth>min && pr<(*recoeff)(truth)){
642     if (meas<minY) minY=meas;
643     if (meas>maxY) maxY=meas;
644     if (truth<minX) minX=truth;
645     if (truth>maxX) maxX=truth;
646     double error = width;
647     double weight=1.;
648     //reco eff correction
649     if (reco) weight = 1./(*recoeff)(truth);
650     TDataPoint d(truth, meas, error, weight);
651     data.push_back(d);
652     }
653     }
654     sp.clear();
655    
656     if (flat) FlattenSpectra();
657     if (reweight){
658     bool outlier=true;
659     ResolutionReweight(reweightlog, outlier, 5);
660     ResolutionReweight(reweightlog, outlier, 4);
661     ResolutionReweight(reweightlog, outlier, 3);
662     }
663     if (cutoff) CorrectCutoff(cutofflog);
664    
665     dY=(maxY-minY)/(npar-3);
666     for (int i=0; i<npar; ++i){
667     sp.push_back(minY+(i-1)*dY);
668     }
669    
670     };
671     //----------------------------------------------------------
672    
673     //----------------------------------------------------------
674     void evalF(int &NPAR, vector<double>* GRAD, double &FSUM, vector<double>* PAR){
675    
676     FSUM = chi2(PAR);
677     // gradients
678     double eps=1.e-5;
679     for (int i=0; i<NPAR; ++i){
680     vector<double> PAR1 = *PAR;
681     vector<double> PAR2 = *PAR;
682     PAR1.at(i) += eps;
683     PAR2.at(i) -= eps;
684     GRAD->at(i) = (chi2(&PAR1)-chi2(&PAR2))/2./eps;
685     PAR1.at(i) += eps;
686     PAR2.at(i) -= eps;
687     GRAD->at(i+NPAR) = (chi2(&PAR1)-2*FSUM+chi2(&PAR2))/4./eps/eps;
688     }
689     }
690     //----------------------------------------------------------
691    
692     //destructor
693     ~TData(){};
694    
695     };
696     ////////////////////////////////////////////////////////////
697    
698     ////////////////////////////////////////////////////////////
699     int main(){
700    
701     int NPAR=11;
702    
703     TROOT simple("simple","Some calibration toyMC tests");
704     bool flat = true;
705     bool cutoff = true;
706     bool cutofflog = true;
707     bool reweight = true;
708     bool reweightlog = true;
709     bool reco = true;
710     TData* toy= new TData(1000000,20,99999,NPAR,flat,cutoff,cutofflog,reweight,reweightlog,reco);
711     toy->DrawTruth();
712     toy->DrawMeas();
713    
714     ////// LVMINI //////
715     int NITER = 500;
716     int MVEC;
717     NPAR<29?MVEC=NPAR:MVEC=29;
718     int NAUX = 10000;
719    
720     vector<double> AUX(NAUX, 0.);
721     vector<double> parameter;
722     double FSUM;
723     double FOPT, FEDM, DUMMY;
724    
725     for (int i=0; i<NPAR; ++i){
726     parameter.push_back(1./0.9);
727     }
728     float eps =float(1.E-3);
729     float wlf1=1.E-4;
730     float wlf2=0.9;
731     //default values
732     //double wlf1=-1.;
733     //double wlf2=-1.;
734    
735     lvmeps_(eps,wlf1,wlf2);
736    
737     int NPARNEG = -fabs(NPAR);
738     if(lvmdim_(NPAR, MVEC) > NAUX)
739     cout<<"Aux field too small: "<<NAUX<<"<"<<lvmdim_(NPAR, MVEC)<<" needed entries"<<endl;
740     lvmini_(NPARNEG, MVEC, NITER, &AUX.front());
741     int IFLAG;
742     int IRET = 0;
743     int ITER = 0;
744     do {
745     ++ITER;
746     toy->evalF(NPAR, &AUX, FSUM, &parameter);
747     lvmfun_(&parameter.front(), FSUM, IRET, &AUX.front());
748     lvmprt_(2, &AUX.front(), 2); //print out
749     /*
750     if (ITER%10==0){
751     toy->DrawRatio(parameter);
752     toy->DrawCalibConst(parameter);
753     }
754     */
755     } while (IRET<0);
756     int error_index=2;
757     error_index = lvmind_(error_index);
758    
759     toy->DrawRatio(parameter);
760     toy->DrawCalibConst(parameter);
761    
762     return 0;
763    
764     }
765     ////////////////////////////////////////////////////////////
766