1 | // Classe traceur d histogramme 96-99
|
---|
2 | // CEA-DAPNIA LAL-IN2P3/CNRS
|
---|
3 |
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <typeinfo>
|
---|
6 |
|
---|
7 | #include "pihisto.h"
|
---|
8 | #include "hisprof.h"
|
---|
9 |
|
---|
10 | //++
|
---|
11 | // Class PIHisto
|
---|
12 | // Lib PIext
|
---|
13 | // include pihisto.h
|
---|
14 | //
|
---|
15 | // Classe traceur d'objet histogramme (classe *Histo*)
|
---|
16 | //--
|
---|
17 | //++
|
---|
18 | // Links Parents
|
---|
19 | // PIDrawer
|
---|
20 | //--
|
---|
21 | //++
|
---|
22 | // Titre Constructeur, méthodes
|
---|
23 | //--
|
---|
24 | //++
|
---|
25 | // PIHisto(Histo* histo, bool ad=false)
|
---|
26 | // Constructeur. Si "ad == true", l'objet "histo" est détruit par
|
---|
27 | // le destructeur de l'objet "PIHisto"
|
---|
28 | // Note : "histo" doit être créé par new
|
---|
29 | //
|
---|
30 | // void SetStats(bool fg=true)
|
---|
31 | // Active/ désactive l'indication des statistiques d'histogramme
|
---|
32 | //--
|
---|
33 |
|
---|
34 |
|
---|
35 | PIHisto::PIHisto(Histo* histo, bool ad)
|
---|
36 | : PIDrawer(), mHisto(histo)
|
---|
37 | {
|
---|
38 | mAdDO = ad; // Flag pour suppression automatique de mHisto
|
---|
39 | SetStats(true);
|
---|
40 | SetName("HistoDrw");
|
---|
41 | }
|
---|
42 |
|
---|
43 | PIHisto::~PIHisto()
|
---|
44 | {
|
---|
45 | if (mAdDO) delete mHisto;
|
---|
46 | }
|
---|
47 |
|
---|
48 | void
|
---|
49 | PIHisto::UpdateLimits()
|
---|
50 | {
|
---|
51 | if (!mHisto) return;
|
---|
52 | float hmin = mHisto->VMin();
|
---|
53 | float hmax = mHisto->VMax()+0.2*(mHisto->VMax()-mHisto->VMin());
|
---|
54 | // si HBProf min,max calcules en tenant compte des erreurs
|
---|
55 | if( typeid(*mHisto) == typeid(HProf) ) {
|
---|
56 | float v1,v2;
|
---|
57 | for (int i=1; i<mHisto->NBins(); i++) {
|
---|
58 | v1 = (*mHisto)(i) - mHisto->Error(i);
|
---|
59 | v2 = (*mHisto)(i) + mHisto->Error(i);
|
---|
60 | if(v1<hmin) hmin = v1;
|
---|
61 | if(v2>hmax) hmax = v2;
|
---|
62 | }
|
---|
63 | v1 = 0.1*(hmax-hmin);
|
---|
64 | hmin -= v1; hmax += v1;
|
---|
65 | }
|
---|
66 | if(hmax<=hmin) hmax += 1.;
|
---|
67 | SetLimits(mHisto->XMin(), mHisto->XMax(), hmin, hmax);
|
---|
68 | // SetAxesFlags(kBoxAxes | kExtTicks | kLabels); NE PAS faire Reza 11/99
|
---|
69 | }
|
---|
70 |
|
---|
71 | void
|
---|
72 | PIHisto::Draw(PIGraphicUC* g, double /*xmin*/, double /*ymin*/, double /*xmax*/, double /*ymax*/)
|
---|
73 | {
|
---|
74 | if (axesFlags != kAxesNone) DrawAxes(g);
|
---|
75 |
|
---|
76 | bool oktrace = false;
|
---|
77 | if (!mHisto) return;
|
---|
78 |
|
---|
79 | if((GetGraphicAtt().GetMarker() != PI_NotDefMarker) ||
|
---|
80 | (typeid(*mHisto) == typeid(HProf)) ) {
|
---|
81 | // Marqeurs definis OU HProf => marqueurs+Erreurs si il y en a
|
---|
82 | mHisto->UpdateHisto(); // pour le cas ou c'est un HProf
|
---|
83 | if(GetGraphicAtt().GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
|
---|
84 | double x1,y1, x2,y2;
|
---|
85 | double bw = mHisto->BinWidth();
|
---|
86 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
87 | if(mHisto->Error(i)>0.) {
|
---|
88 | // barres d'erreur verticales
|
---|
89 | x1 = x2 = mHisto->BinCenter(i);
|
---|
90 | y1 = (*mHisto)(i) - mHisto->Error(i);
|
---|
91 | y2 = (*mHisto)(i) + mHisto->Error(i);
|
---|
92 | g->DrawLine(x1,y1, x1, y2);
|
---|
93 | // limites de barres d'erreurs (horizontales)
|
---|
94 | x1 -= bw/3.; x2 += bw/3.;
|
---|
95 | g->DrawLine(x1,y1, x2, y1);
|
---|
96 | g->DrawLine(x1,y2, x2, y2);
|
---|
97 | }
|
---|
98 | // le marqueur
|
---|
99 | x1 = mHisto->BinCenter(i);
|
---|
100 | y1 = (*mHisto)(i);
|
---|
101 | g->DrawMarker(x1,y1);
|
---|
102 | }
|
---|
103 | oktrace = true;
|
---|
104 | }
|
---|
105 | if(GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt) {
|
---|
106 | double x1,y1, x2, y2;
|
---|
107 | x1 = mHisto->BinLowEdge(0);
|
---|
108 | y1 = 0;
|
---|
109 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
110 | x2 = x1;
|
---|
111 | y2 = (*mHisto)(i);
|
---|
112 | g->DrawLine(x1,y1, x2, y2);
|
---|
113 | y1 = y2;
|
---|
114 | x2 = mHisto->BinHighEdge(i);
|
---|
115 | g->DrawLine(x1,y1, x2, y2);
|
---|
116 | x1 = x2;
|
---|
117 | }
|
---|
118 | y2 = 0; g->DrawLine(x1,y1, x2, y2);
|
---|
119 | oktrace = true;
|
---|
120 | }
|
---|
121 | if( !oktrace ) {
|
---|
122 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
123 | double left = mHisto->BinLowEdge(i);
|
---|
124 | double width = mHisto->BinWidth();
|
---|
125 | double bottom = 0;
|
---|
126 | double height = (*mHisto)(i);
|
---|
127 | g->DrawFBox(left,bottom,width,height);
|
---|
128 | g->DrawLine(left, bottom, left, bottom + height); // Au moins une ligne...
|
---|
129 | }
|
---|
130 | }
|
---|
131 | // A faire a la fin - DrawStats change l'attribut de ligne
|
---|
132 | if(stats) DrawStats(g);
|
---|
133 | }
|
---|
134 |
|
---|
135 | int
|
---|
136 | PIHisto::DecodeOptionString(vector<string> & opt, bool rmdecopt)
|
---|
137 | {
|
---|
138 | if (opt.size() < 1) return(0);
|
---|
139 | int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
|
---|
140 | if ((opt.size() - ndec1) < 1) return(ndec1); // si tout a ete decode
|
---|
141 |
|
---|
142 | vector<string> udopt; // On gardera ici les options non decodees
|
---|
143 | unsigned int k = 0;
|
---|
144 | int ndec = opt.size();
|
---|
145 | for( k=0; k<opt.size(); k++ ) {
|
---|
146 | string opts = opt[k];
|
---|
147 | if (opts == "stat") SetStats(true);
|
---|
148 | else if (opts == "nostat") SetStats(false);
|
---|
149 | else {
|
---|
150 | ndec--;
|
---|
151 | // S'il faut supprimer les options decodees
|
---|
152 | if (rmdecopt) udopt.push_back(opts);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | // S'il faut supprimer les options decodees, on remplace l'argument opt
|
---|
156 | // par le vecteur des options non decodees.
|
---|
157 | if (rmdecopt) opt = udopt;
|
---|
158 | return(ndec+ndec1);
|
---|
159 | }
|
---|
160 |
|
---|
161 | void
|
---|
162 | PIHisto::DrawStats(PIGraphicUC* g)
|
---|
163 | {
|
---|
164 | if (!mHisto) return;
|
---|
165 | // if (mLAtt == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
|
---|
166 | g->SelLine(PI_ThinLine);
|
---|
167 | g->SelFontSz((YMax() - YMin())/30);
|
---|
168 |
|
---|
169 | // La hauteur de la cellule
|
---|
170 | PIGrCoord a, d;
|
---|
171 | double cH = (double)g->GetFontHeight(a,d);
|
---|
172 | double cellHeight = 3.6 * cH;
|
---|
173 |
|
---|
174 | // Les labels et leurs longueurs -> largeur de la cellule
|
---|
175 | char *label, label1[64], label2[64], label3[64];
|
---|
176 | sprintf(label1, "N= %-g",mHisto->NData());
|
---|
177 | sprintf(label2, "m= %-g",mHisto->Mean());
|
---|
178 | sprintf(label3, "s= %-g",mHisto->Sigma());
|
---|
179 | label = label1;
|
---|
180 | if(strlen(label)<strlen(label2)) label = label2;
|
---|
181 | if(strlen(label)<strlen(label3)) label = label3;
|
---|
182 | double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
|
---|
183 |
|
---|
184 | double xu, yu, cw;
|
---|
185 | // Les limites du cadre
|
---|
186 | xu = g->DeltaUCX(XMax(), -cellWidth);
|
---|
187 | yu = g->DeltaUCY(YMax(), -cellHeight);
|
---|
188 | g->DrawLine(xu,YMax(),xu,yu);
|
---|
189 | g->DrawLine(xu,yu,XMax(),yu);
|
---|
190 |
|
---|
191 | // L'ecriture des labels (attention aux inversions possibles des axes!)
|
---|
192 | cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
|
---|
193 | xu = g->DeltaUCX(XMax(),cw);
|
---|
194 |
|
---|
195 | cw = (g->isAxeYDirUpDown()) ? -0.15*cH : -1.15*cH;
|
---|
196 | yu = g->DeltaUCY(YMax(),cw);
|
---|
197 | g->DrawString(xu, yu,label1);
|
---|
198 | cw += -1.15*cH;
|
---|
199 | yu = g->DeltaUCY(YMax(),cw);
|
---|
200 | g->DrawString(xu, yu,label2);
|
---|
201 | cw += -1.15*cH;
|
---|
202 | yu = g->DeltaUCY(YMax(),cw);
|
---|
203 | g->DrawString(xu, yu,label3);
|
---|
204 |
|
---|
205 | }
|
---|