KaliVeda
Toolkit for HIC analysis
Loading...
Searching...
No Matches
KVAutoFit.cpp
1//Created by KVClassFactory on Tue Feb 7 15:17:39 2012
2//Author: bonnet
3
4#include "KVAutoFit.h"
5#include "KVConfig.h"
6#include "Riostream.h"
7#include "TStyle.h"
8#include "TSystem.h"
9#include "TH2.h"
10#include "TObjString.h"
11
12using namespace std;
13
15
16
17
18
24
26{
27 // Default constructor
28 //
29 // le booleen is2D indique si les histo et donc les fonctions
30 // doivent etre a 2D ou 1D
31 // par defaut is2D= kFALSE dans la methode init
32 fBatch = batch;
33 SetName("KVAutoFit");
34 init();
35 Clear();
36 /*
37 is2D = kTRUE;
38 */
39
40}
41
42
43
47
49{
50 // Default constructor
51 //
52
53}
54
55
56
58
60{
61 lfunc = new KVHashList();
64 c1 = 0;
65 lplabel = 0;
66 if (!fBatch) {
67 c1 = new TCanvas(Form("%s_canvas", GetName()), Form("%s_canvas", GetName()), 0, 0, 1200, 600);
68
69 c1->AddExec("interval", "ap->GetInterval()");
70 c1->AddExec("recommence", "ap->Recommence()");
71 c1->AddExec("gather", "ap->Gather()");
72
73 lplabel = new KVHashList();
75 lplabel->Add(new TPaveLabel(0.10, 0.92, 0.25, 0.99, "Annule Tout", "NDC"));
76 lplabel->Add(new TPaveLabel(0.27, 0.92, 0.44, 0.99, "Annule Dernier", "NDC"));
77 lplabel->Add(new TPaveLabel(0.46, 0.92, 0.55, 0.99, "Gather", "NDC"));
78 lplabel->Add(new TPaveLabel(0.57, 0.92, 0.65, 0.99, "Suivant", "NDC"));
79
80 lplabel->Execute("SetTextSize", "0.625");
81 lplabel->Execute("SetFillColor", "10");
82 }
83
84 hfit = 0;
85 hclone = 0;
86
87 f1Dfit = 0;
88 f2Dfit = 0;
89
91
92 is2D = kFALSE;
93
94 lhisto = 0;
95 nhisto = 0;
96
97 koption = "";
98}
99
100
101
102
109
111{
112 // Copy constructor
113 // This ctor is used to make a copy of an existing object (for example
114 // when a method returns an object), and it is always a good idea to
115 // implement it.
116 // If your class allocates memory in its constructor(s) then it is ESSENTIAL :-)
117
118 obj.Copy(*this);
119}
120
121
122
123
131
132void KVAutoFit::Copy(TObject& obj) const
133{
134 // This method copies the current state of 'this' object into 'obj'
135 // You should add here any member variables, for example:
136 // (supposing a member variable KVAutoFit::fToto)
137 // CastedObj.fToto = fToto;
138 // or
139 // CastedObj.SetToto( GetToto() );
140
141 KVBase::Copy(obj);
142 //KVAutoFit& CastedObj = (KVAutoFit&)obj;
143}
144
145
146
150
152{
153 //Efface les fonctions de fit
154 //et le contenu du canvas
155 lfunc->Clear();
156 if (!fBatch) c1->Clear();
157 ClearRange();
158
159}
160
161
164
166{
167 //Efface les bornes min et max pour le prochain fit
168 XminSet = kFALSE;
169 XmaxSet = kFALSE;
170
171 Xmin = Xmax = -1;
172 Ymin = Ymax = -1;
173
174}
175
176
177
180
182{
183 // Destructor
184 Clear();
185
186 delete lfunc;
187 if (!fBatch) {
188 delete c1;
189 delete lplabel;
190 }
191 hfit = 0;
192 if (hclone)
193 delete hclone;
194 hclone = 0;
195 f1Dfit = 0;
196 f2Dfit = 0;
197
198}
199
200
201
203
205{
206 if (fBatch)
207 return;
208
209 if (option == "all") {
210 Info("SetHistos", "On s'occupe de tous les histos de la liste");
211 }
212 else if (option == "relecture") {
213 Info("SetHistos", "On s'occupe de tous les histos de la liste en relisant les infos des histos pour lesquels on en a (des info)");
214 }
215 else if (option == "inconnu") {
216 Info("SetHistos", "On ne s'occupe que des histos pour lesquels on n'a pas d info");
217 }
218 else {
219 Info("SetHistos", "la variable option doit prendre la valeur \"all\", \"relecture\" ou \"inconnu\"");
220 return;
221 }
222
223 koption = option;
224
225 lhisto = lh;
226 nhisto = -1;
227 NextHisto();
228
229}
230
231
232
234
236{
237
238 if (fBatch)
239 return;
240
241 if (!lhisto) return;
242
243 nhisto += 1;
244
245 if (nhisto >= lhisto->GetEntries()) {
246 Info("NextHisto", "Fin de la liste d histos");
247 return;
248 }
249
250 if (koption == "all" || koption == "relecture") {
252 }
253
254 if (koption == "inconnu") {
255 TString snom(lhisto->At(nhisto)->GetName());
256 while (IsKnown(snom.Data())) {
257 nhisto += 1;
258 if (nhisto >= lhisto->GetEntries()) {
259 Info("NextHisto", "Fin de la liste d histos");
260 return;
261 }
262 snom.Form("%s", lhisto->At(nhisto)->GetName());
263 }
265 }
266
267}
268
269
270
272
274{
275 if (hfit)
276 Info("Print", "Liste des fonctions pour l'histogram %s :", hfit->GetName());
277 else
278 Info("Print", "Liste des fonctions :");
279 lfunc->Print();
280
281}
282
283
284
286
288{
289
290 return lfunc;
291
292}
293
294
295
299
301{
302 //Generation de la fonction 1D ou 2D
303 //definie dans la methode NewFunction_1D ou NewFunction_2D
304
305 if (is2D)
306 return NewFunction_2D();
307 else
308 return NewFunction_1D();
309
310}
311
312
314
316{
317
318 Info("NewFunction_1D", "To be defined in child class");
319 return kFALSE;
320}
321
322
324
326{
327 Info("NewFunction_2D", "To be defined in child class");
328 return kFALSE;
329
330}
331
332
333
337
339{
340 //Generation de la fonction 1D ou 2D
341 //definie dans la methode NewFunction_1D ou NewFunction_2D
342
343 if (is2D)
344 return ReloadFunction_2D(name, np);
345 else
346 return ReloadFunction_1D(name, np);
347
348}
349
350
352
354{
355
356 Info("ReloadFunction_1D", "To be defined in child class");
357 return 0;
358}
359
360
362
364{
365 Info("ReloadFunction_2D", "To be defined in child class");
366 return 0;
367
368}
369
370
371
374
376{
377 //userdefined = kTRUE;
378 return 0;
379
380}
381
382
383
386
388{
389
390 //userdefined = kTRUE;
391 return 0;
392
393}
394
395
396
398
400{
401
402 if (fBatch)
403 return;
404
405 Clear();
406
407 Bool_t ok = kFALSE;
408 if (hh->InheritsFrom("TH2")) {
409 if (is2D)
410 ok = kTRUE;
411 }
412 else if (hh->InheritsFrom("TH1")) {
413 if (!is2D)
414 ok = kTRUE;
415 }
416
417 if (!ok) {
418 Warning("SetHisto", "La dimension de lhisto n'est pas compatible avec celle definie ds cette classe");
419 return;
420 }
421
422 if (hclone)
423 delete hclone;
424 hclone = (TH1*)hh->Clone("ap_clone");
425
426 c1->cd();
427
428 hfit = hh;
429 if (is2D) hfit->Draw("colz");
430 else hfit->Draw();
431
432 lplabel->Execute("Draw", "");
433
434 if (koption == "relecture")
435 if (IsKnown(hfit->GetName()))
437
438 c1->Update();
439
440}
441
442
443
445
447{
448
449 if (fBatch)
450 return;
451
452 Int_t event = gPad->GetEvent();
453 TObject* select = gPad->GetSelected();
454
455// cout << event << endl;
456 if (event == 24) HandleKey(gPad->GetEventY());
457
458 if (!select) {}
459 else {
460 if (select->InheritsFrom("TPaveLabel"))
461 return;
462 }
463
464 if (event == kButton1Down) {
465
466 Int_t xx = gPad->GetEventX();
467 Int_t yy = gPad->GetEventY();
468
469 Xmin = gPad->AbsPixeltoX(xx);
470 Ymin = gPad->AbsPixeltoY(yy);
471
472 XminSet = kTRUE;
473 }
474 else if (event == kButton2Up) {
475
476 Int_t xx = gPad->GetEventX();
477 Int_t yy = gPad->GetEventY();
478
479 Xmax = gPad->AbsPixeltoX(xx);
480 Ymax = gPad->AbsPixeltoY(yy);
481
482 if (Xmax > Xmin) {
483 XmaxSet = kTRUE;
484 if (Ymin > Ymax) {
485 Double_t temp = Ymax;
486 Ymax = Ymin;
487 Ymin = temp;
488 }
489 }
490 }
491 else if (event == kButton1Double) {
492 Save();
493 NextHisto();
494 }
495
496 if (XminSet && XmaxSet) {
497
498 printf("\tX : %lf %lf\n", Xmin, Xmax);
499 printf("\tY : %lf %lf\n", Ymin, Ymax);
500
501
502 //-------------
503 //fit a 1D ou 2D
504 if (NewFunction()) {
505 if (!is2D) {
506 hfit->Fit(f1Dfit, "0N", "", Xmin, Xmax);
507 f1Dfit->Draw("same");
508 ExtraDrawing();
509 }
510 else {
511 hfit->Fit(f2Dfit, "0N", "", Xmin, Xmax);
512 f2Dfit->Draw("same,cont2");
513 ExtraDrawing();
514 }
515 }
516 //-------------
517
518 ClearRange();
519 c1->Update();
520 }
521
522}
523
524
525
527
529{
530
531 if (fBatch)
532 return;
533
534 Int_t event = gPad->GetEvent();
535 TObject* select = gPad->GetSelected();
536 if (!select) return;
537//else printf("Cliquage sur %s %s\n",select->GetName(),select->ClassName());
538
539 if (event == kButton1Down) {
540 if (!strcmp("Annule Tout", select->GetTitle())) {
541 Info("", "On efface les fonctions...");
542 lfunc->Clear();
543 ClearRange();
544 }
545
546 if (!strcmp("Annule Dernier", select->GetTitle())) {
547 Info("", "On retire la derniere fonction ...");
548 Int_t nf = lfunc->GetEntries();
549 if (nf > 0)
550 delete lfunc->RemoveAt(nf - 1);
551 ClearRange();
552 }
553 if (!strcmp("Suivant", select->GetTitle())) {
554 NextHisto();
555 }
556
557 }
558
559}
560
561
563
565{
566
567 if (fBatch)
568 return;
569
570 Int_t event = gPad->GetEvent();
571 TObject* select = gPad->GetSelected();
572 if (!select) return;
573 if (event == kButton1Down) {
574 if (!strcmp("Gather", select->GetTitle())) {
575 Info("Gather", "Do nothing");
576 }
577 }
578
579}
580
581
582
584
586{
587
588 if (lfunc->GetEntries() == 0) return;
589
590 TF1* f1;
591 ofstream fout(Form("%s", hfit->GetName()));
592 TIter it(lfunc);
593 fout << "// Functions generated by class=" << GetName() << endl;
594#ifdef WITH_BZR_INFOS
595 fout << "// From bzr revision=" << KVBase::bzrRevisionNumber() << endl;
596#endif
597#ifdef WITH_GIT_INFOS
598 fout << "// From " << KVBase::gitBranch() << "@" << KVBase::gitCommit() << endl;
599#endif
600 fout << lfunc->GetEntries() << endl;
601 while ((f1 = (TF1*)it.Next())) {
602 fout << f1->GetName() << endl;
603 fout << userdefined << endl;
604 if (!userdefined)
605 fout << f1->GetExpFormula() << endl;
606 else
607 fout << f1->GetNpar() << endl;
608 Double_t x1, x2, y1, y2;
609 if (!is2D) {
610 f1->GetRange(x1, x2);
611 fout << x1 << " " << x2 << endl;
612 }
613 else {
614 f1->GetRange(x1, y1, x2, y2);
615 fout << x1 << " " << x2 << " " << y1 << " " << y2 << endl;
616 }
617 for (Int_t ii = 0; ii < f1->GetNpar(); ii += 1) {
618 fout << ii << " " << f1->GetParameter(ii) << " " << f1->GetParError(ii) << endl;
619 }
620
621 }
622 fout.close();
623
624}
625
626
627
629
631{
632
633 Info("Relecture", "%s, Reading existing data", name);
634 TObjArray* toks;
635 ifstream fin(name);
637
638 if (!is2D) {
639 TF1* freload;
640 TString f1d;
641 f1d.Form("%s::f1D", GetName());
642 while (fin.good()) {
643 line.ReadLine(fin);
644 while (line.BeginsWith("//")) line.ReadLine(fin);
645
646 Int_t nf = line.Atoi(); //Lecture du nombre de fonctions enregistrees
647 for (Int_t ii = 0; ii < nf; ii += 1) {
648
649 line.ReadLine(fin); //lecture du nom de la fonction
650 TString snom = line;
651
652 line.ReadLine(fin); //lecture de l'expression ou de la methode definissant la fonction
653 TString sfor = line;
654
655 if (sfor.Atoi() == 0) {
656 //Creation de la fonction a partir d une formule
657 line.ReadLine(fin);
658 freload = new TF1(snom.Data(), line.Data());
659 }
660 else {
661 //lecture de l'expression ou de la methode definissant la fonction
662 //lecture du nombre de parametres
663 line.ReadLine(fin);
664 Int_t np = line.Atoi();
665 freload = ReloadFunction(snom.Data(), np);
666 }
667
668 line.ReadLine(fin); //Lecture de l intervalle de definition
669 toks = line.Tokenize(" ");
670 freload->SetRange(
671 ((TObjString*)toks->At(0))->GetString().Atof(),
672 ((TObjString*)toks->At(1))->GetString().Atof()
673 );
674 delete toks;
675
676 for (Int_t jj = 0; jj < freload->GetNpar(); jj += 1) { //Lecture des parametres
677 line.ReadLine(fin);
678 toks = line.Tokenize(" ");
679 freload->SetParameter(jj, ((TObjString*)toks->At(1))->GetString().Atof());
680 freload->SetParError(jj, ((TObjString*)toks->At(2))->GetString().Atof());
681 delete toks;
682 }
683
684 lfunc->Add(freload);
685 if (!fBatch)
686 freload->Draw("same");
687
688 }
689 }
690 }
691 else {
692 TF2* freload;
693 TString f2d;
694 f2d.Form("%s::f2D", GetName());
695 while (fin.good()) {
696 line.ReadLine(fin);
697 while (line.BeginsWith("//")) line.ReadLine(fin);
698
699 Int_t nf = line.Atoi(); //Lecture du nombre de fonctions enregistrees
700 for (Int_t ii = 0; ii < nf; ii += 1) {
701
702 line.ReadLine(fin); //lecture du nom de la fonction
703 TString snom = line;
704
705 line.ReadLine(fin); //lecture de l'expression ou de la methode definissant la fonction
706 TString sfor = line;
707
708 if (sfor.Atoi() == 0) {
709 //Creation de la fonction a partir d une formule
710 line.ReadLine(fin);
711 freload = new TF2(snom.Data(), line.Data());
712 }
713 else {
714 //lecture de l'expression ou de la methode definissant la fonction
715 //lecture du nombre de parametres
716 line.ReadLine(fin);
717 Int_t np = line.Atoi();
718 freload = (TF2*)ReloadFunction(snom.Data(), np);
719 }
720
721 line.ReadLine(fin); //Lecture de l intervalle de definition
722 toks = line.Tokenize(" ");
723 freload->SetRange(
724 ((TObjString*)toks->At(0))->GetString().Atof(),
725 ((TObjString*)toks->At(2))->GetString().Atof(),
726 ((TObjString*)toks->At(1))->GetString().Atof(),
727 ((TObjString*)toks->At(3))->GetString().Atof()
728 );
729 delete toks;
730
731 for (Int_t jj = 0; jj < freload->GetNpar(); jj += 1) { //Lecture des parametres
732 line.ReadLine(fin);
733 toks = line.Tokenize(" ");
734 freload->SetParameter(jj, ((TObjString*)toks->At(1))->GetString().Atof());
735 freload->SetParError(jj, ((TObjString*)toks->At(2))->GetString().Atof());
736 delete toks;
737 }
738
739 lfunc->Add(freload);
740 if (!fBatch)
741 freload->Draw("cont2,same");
742
743 }
744 }
745 }
746
747
748 fin.close();
749 Info("Relecture", "done");
750
751
752}
753
754
755
756
758
760{
761
763
764}
765
766
kButton1Double
kButton2Up
kButton1Down
int Int_t
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
const char Option_t
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
char name[80]
char * Form(const char *fmt,...)
R__EXTERN TStyle * gStyle
R__EXTERN TSystem * gSystem
#define gPad
Manage SemiAutomatic Fits.
Definition KVAutoFit.h:26
virtual Double_t f1D(Double_t *xx, Double_t *para)
userdefined = kTRUE;
KVHashList * lfunc
liste des fonctions de fit
Definition KVAutoFit.h:29
Bool_t XminSet
Definition KVAutoFit.h:39
Bool_t is2D
histogramme analyse herite de TH1 ou TH2
Definition KVAutoFit.h:74
virtual void SetHisto(TH1 *hh)
virtual Double_t f2D(Double_t *xx, Double_t *para)
userdefined = kTRUE;
Bool_t XmaxSet
Indique que l intervalle du fit est defini.
Definition KVAutoFit.h:39
Bool_t userdefined
Definition KVAutoFit.h:51
Double_t Xmax
Borne en X du fit.
Definition KVAutoFit.h:40
virtual void Gather()
TF2 * f2Dfit
fonction de fit deux dimensions
Definition KVAutoFit.h:32
Double_t Xmin
Definition KVAutoFit.h:40
TF1 * ReloadFunction(const Char_t *, Int_t)
TString koption
Definition KVAutoFit.h:52
void NextHisto()
virtual void SetHistos(KVHashList *lh, TString option="")
virtual void ExtraDrawing()
Definition KVAutoFit.h:95
Double_t Ymax
Borne en Y du fit.
Definition KVAutoFit.h:41
virtual void Save()
virtual ~KVAutoFit()
Destructor.
virtual TF1 * ReloadFunction_1D(const Char_t *, Int_t)
TH1 * hfit
histogramme analyse
Definition KVAutoFit.h:34
Bool_t NewFunction()
void Clear(Option_t *opt="")
void Copy(TObject &) const
KVHashList * GetFunctions() const
void Print(Option_t *opt="") const
KVHashList * lplabel
contient la liste des TPaveLabel constituant le menu contextuel
Definition KVAutoFit.h:43
Int_t nhisto
nombre d'histo traites
Definition KVAutoFit.h:46
TCanvas * c1
Canvas ou est trace l histo a analyse.
Definition KVAutoFit.h:36
virtual Bool_t NewFunction_2D()
virtual void init()
Definition KVAutoFit.cpp:59
virtual void Relecture(const Char_t *name)
virtual void HandleKey(Int_t)
Definition KVAutoFit.h:96
TH1 * hclone
clone de histogramme analyse
Definition KVAutoFit.h:35
virtual Bool_t NewFunction_1D()
Bool_t IsKnown(const Char_t *)
void Recommence()
Bool_t fBatch
set if graphical objects are used or not
Definition KVAutoFit.h:37
KVHashList * lhisto
liste des histos initialise via SetHistos method
Definition KVAutoFit.h:45
TF1 * f1Dfit
fonction de fit une dimension
Definition KVAutoFit.h:31
virtual TF1 * ReloadFunction_2D(const Char_t *, Int_t)
void GetInterval()
Double_t Ymin
Definition KVAutoFit.h:41
void ClearRange(void)
Efface les bornes min et max pour le prochain fit.
Base class for KaliVeda framework.
Definition KVBase.h:142
static const Char_t * gitCommit()
Returns last git commit of sources.
Definition KVBase.cpp:976
static const Char_t * gitBranch()
Returns git branch of sources.
Definition KVBase.cpp:964
virtual void Copy(TObject &) const
Make a copy of this object.
Definition KVBase.cpp:394
Extended version of ROOT THashList.
Definition KVHashList.h:29
virtual void SetOwner(Bool_t enable=kTRUE)
virtual void Clear(Option_t *option="")
virtual void Execute(const char *method, const char *params, Int_t *error=0)
virtual TObject * At(Int_t idx) const
virtual void Add(TObject *obj)
void Clear(Option_t *option="") override
TVirtualPad * cd(Int_t subpadnumber=0) override
void Update() override
virtual void Print(Option_t *option, const char *wildcard, Int_t recurse=1) const
virtual Int_t GetEntries() const
virtual void SetParError(Int_t ipar, Double_t error)
virtual Double_t GetParameter(const TString &name) const
virtual Double_t GetParError(Int_t ipar) const
virtual void SetRange(Double_t xmin, Double_t xmax)
virtual Int_t GetNpar() const
virtual TString GetExpFormula(Option_t *option="") const
void Draw(Option_t *option="") override
virtual void GetRange(Double_t &xmin, Double_t &xmax) const
virtual void SetParameter(const TString &name, Double_t value)
void Draw(Option_t *option="") override
void SetRange(Double_t xmin, Double_t xmax) override
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
void Draw(Option_t *option="") override
TObject * Clone(const char *newname="") const override
TObject * Next()
const char * GetName() const override
TObject * At(Int_t idx) const override
virtual const char * GetName() const
virtual void Warning(const char *method, const char *msgfmt,...) const
virtual Bool_t InheritsFrom(const char *classname) const
virtual const char * GetTitle() const
virtual void Info(const char *method, const char *msgfmt,...) const
void AddExec(const char *name, const char *command) override
virtual TObject * RemoveAt(Int_t idx)
Int_t Atoi() const
const char * Data() const
void Form(const char *fmt,...)
void SetOptTitle(Int_t tit=1)
virtual Bool_t IsFileInIncludePath(const char *name, char **fullpath=nullptr)
TLine * line
gr SetName("gr")
TF1 * f1
void init()
ClassImp(TPyArg)