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();
|
---|
40 | SetError();
|
---|
41 | SetStatPosOffset();
|
---|
42 | SetName("HistoDrw");
|
---|
43 | }
|
---|
44 |
|
---|
45 | PIHisto::~PIHisto()
|
---|
46 | {
|
---|
47 | if(mAdDO) delete mHisto;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void
|
---|
51 | PIHisto::UpdateLimits()
|
---|
52 | {
|
---|
53 | if (!mHisto) return;
|
---|
54 | float hmin = mHisto->VMin();
|
---|
55 | float hmax = mHisto->VMax()+0.2*(mHisto->VMax()-mHisto->VMin());
|
---|
56 | // si HBProf min,max calcules en tenant compte des erreurs
|
---|
57 | if( typeid(*mHisto) == typeid(HProf) ) {
|
---|
58 | float v1,v2;
|
---|
59 | for (int i=1; i<mHisto->NBins(); i++) {
|
---|
60 | v1 = (*mHisto)(i) - mHisto->Error(i);
|
---|
61 | v2 = (*mHisto)(i) + mHisto->Error(i);
|
---|
62 | if(v1<hmin) hmin = v1;
|
---|
63 | if(v2>hmax) hmax = v2;
|
---|
64 | }
|
---|
65 | v1 = 0.1*(hmax-hmin);
|
---|
66 | hmin -= v1; hmax += v1;
|
---|
67 | }
|
---|
68 | if(hmax<=hmin) hmax = hmin+1.;
|
---|
69 | SetLimits(mHisto->XMin(), mHisto->XMax(), hmin, hmax);
|
---|
70 | }
|
---|
71 |
|
---|
72 | void
|
---|
73 | PIHisto::Draw(PIGraphicUC* g, double /*xmin*/, double /*ymin*/, double /*xmax*/, double /*ymax*/)
|
---|
74 | {
|
---|
75 | if (axesFlags != kAxesNone) DrawAxes(g);
|
---|
76 |
|
---|
77 | if (!mHisto) return;
|
---|
78 |
|
---|
79 | if( typeid(*mHisto)==typeid(HProf) ) mHisto->UpdateHisto();
|
---|
80 |
|
---|
81 | bool oktrace=false;
|
---|
82 | // Tracer des marqueurs si demande ou si HProf
|
---|
83 | bool drawmarker=false;
|
---|
84 | if( (GetGraphicAtt().GetMarker() != PI_NotDefMarker) ||
|
---|
85 | (typeid(*mHisto) == typeid(HProf)) ) drawmarker = true;
|
---|
86 | // Tracer des erreurs ?
|
---|
87 | bool drawerr=false;
|
---|
88 | if(error==0) { // Gestion automatique des erreurs
|
---|
89 | // Tracer les erreurs si HProf
|
---|
90 | if( typeid(*mHisto)==typeid(HProf) ) drawerr=true;
|
---|
91 | // Trace les erreurs si marqueurs demandes
|
---|
92 | if(drawmarker) drawerr=true;
|
---|
93 | }
|
---|
94 | else if(error>0) drawerr=true;
|
---|
95 | else if(error<0) drawerr=false;
|
---|
96 |
|
---|
97 | // Trace des marqeurs
|
---|
98 | if(drawmarker) {
|
---|
99 | double x1,y1; oktrace = true;
|
---|
100 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
101 | x1 = mHisto->BinCenter(i);
|
---|
102 | y1 = (*mHisto)(i);
|
---|
103 | g->DrawMarker(x1,y1);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | // Trace des erreurs
|
---|
108 | if(drawerr) {
|
---|
109 | if(GetGraphicAtt().GetLineAtt()==PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
|
---|
110 | double x1,x2,y1,y2; oktrace = true;
|
---|
111 | double bw = mHisto->BinWidth();
|
---|
112 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
113 | if(mHisto->Error(i)>0.) {
|
---|
114 | // barres d'erreur verticales
|
---|
115 | x1 = x2 = mHisto->BinCenter(i);
|
---|
116 | y1 = (*mHisto)(i) - mHisto->Error(i);
|
---|
117 | y2 = (*mHisto)(i) + mHisto->Error(i);
|
---|
118 | g->DrawLine(x1,y1, x1, y2);
|
---|
119 | // limites de barres d'erreurs (horizontales)
|
---|
120 | x1 -= bw/3.; x2 += bw/3.;
|
---|
121 | g->DrawLine(x1,y1, x2, y1);
|
---|
122 | g->DrawLine(x1,y2, x2, y2);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | // Trace de la ligne continue
|
---|
128 | // Si on ne demande pas de ligne (PI_NotDefLineAtt)
|
---|
129 | // et que l'on a rien trace auparavent, on trace un FPolygon
|
---|
130 | if(GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt || !oktrace) {
|
---|
131 | PIGrCoord* x1 = new PIGrCoord[2*mHisto->NBins()+2];
|
---|
132 | PIGrCoord* y1 = new PIGrCoord[2*mHisto->NBins()+2];
|
---|
133 | double dx = mHisto->BinWidth();
|
---|
134 | int npt=0;
|
---|
135 | x1[npt] = mHisto->BinLowEdge(0);
|
---|
136 | y1[npt] = 0;
|
---|
137 | npt++;
|
---|
138 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
139 | x1[npt] = mHisto->BinLowEdge(i);
|
---|
140 | y1[npt] = (*mHisto)(i);
|
---|
141 | npt++;
|
---|
142 | x1[npt] = (double)x1[npt-1] + dx;
|
---|
143 | y1[npt] = y1[npt-1];
|
---|
144 | npt++;
|
---|
145 | }
|
---|
146 | x1[npt] = x1[npt-1];
|
---|
147 | y1[npt] = 0;
|
---|
148 | npt++;
|
---|
149 | if(GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt) {
|
---|
150 | g->DrawPolygon(x1,y1,npt,false);
|
---|
151 | oktrace = true;
|
---|
152 | }
|
---|
153 | if(!oktrace) g->DrawFPolygon(x1,y1,npt,false);
|
---|
154 | delete[] x1; delete[] y1;
|
---|
155 | }
|
---|
156 |
|
---|
157 | // Trace/Ecriture des statistiques
|
---|
158 | // A faire a la fin - DrawStats change l'attribut de ligne
|
---|
159 | if(stats) DrawStats(g);
|
---|
160 | }
|
---|
161 |
|
---|
162 | int
|
---|
163 | PIHisto::DecodeOptionString(vector<string> & opt, bool rmdecopt)
|
---|
164 | {
|
---|
165 | int optsz1 = opt.size();
|
---|
166 | if(optsz1<1) return(0);
|
---|
167 | int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
|
---|
168 | if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
|
---|
169 |
|
---|
170 | vector<string> udopt; // On gardera ici les options non decodees
|
---|
171 | unsigned int k = 0;
|
---|
172 | int ndec = opt.size();
|
---|
173 | for( k=0; k<opt.size(); k++ ) {
|
---|
174 | string opts = opt[k];
|
---|
175 | if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
|
---|
176 | else if( opts=="nsta" || opts=="nstat"
|
---|
177 | || opts=="nostat" || opts=="nostats") SetStats(false);
|
---|
178 | else if(opts=="err") SetError(1);
|
---|
179 | else if(opts=="noerr") SetError(-1);
|
---|
180 | else if(opts=="autoerr") SetError(0);
|
---|
181 | else if(opts.substr(0,11) == "statposoff=") {
|
---|
182 | float xo=0., yo=0.;
|
---|
183 | sscanf(opts.substr(11).c_str(),"%g,%g",&xo, &yo);
|
---|
184 | SetStatPosOffset(xo, yo);
|
---|
185 | }
|
---|
186 | else {
|
---|
187 | ndec--;
|
---|
188 | // S'il faut supprimer les options decodees
|
---|
189 | if (rmdecopt) udopt.push_back(opts);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | // S'il faut supprimer les options decodees, on remplace l'argument opt
|
---|
193 | // par le vecteur des options non decodees.
|
---|
194 | if (rmdecopt) opt = udopt;
|
---|
195 | return(ndec+ndec1);
|
---|
196 | }
|
---|
197 |
|
---|
198 | void
|
---|
199 | PIHisto::GetOptionsHelpInfo(string& info)
|
---|
200 | {
|
---|
201 | info += " ---- PIHisto options help info : \n" ;
|
---|
202 | info += " sta,stat,stats: activate statistic display\n";
|
---|
203 | info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
|
---|
204 | info += " err / nerr : draw, do not draw error bars\n";
|
---|
205 | info += " autoerr : draw error bars if Marker drawing requested OR Profile histo\n";
|
---|
206 | info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
|
---|
207 | info += " as a fraction of total size \n";
|
---|
208 | // On recupere ensuite la chaine info de la classe de base
|
---|
209 | PIDrawer::GetOptionsHelpInfo(info);
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | void
|
---|
214 | PIHisto::DrawStats(PIGraphicUC* g)
|
---|
215 | {
|
---|
216 | if (!mHisto) return;
|
---|
217 | // if (mLAtt == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
|
---|
218 | g->SelLine(PI_ThinLine);
|
---|
219 | g->SelFontSz((YMax() - YMin())/30);
|
---|
220 |
|
---|
221 | // La hauteur de la cellule
|
---|
222 | PIGrCoord a, d;
|
---|
223 | double cH = (double)g->GetFontHeight(a,d);
|
---|
224 | double cellHeight = 3.6 * cH;
|
---|
225 |
|
---|
226 | // Les labels et leurs longueurs -> largeur de la cellule
|
---|
227 | char *label, label1[64], label2[64], label3[64];
|
---|
228 | sprintf(label1, "N= %-g",mHisto->NData());
|
---|
229 | sprintf(label2, "m= %-g",mHisto->Mean());
|
---|
230 | sprintf(label3, "s= %-g",mHisto->Sigma());
|
---|
231 | label = label1;
|
---|
232 | if(strlen(label)<strlen(label2)) label = label2;
|
---|
233 | if(strlen(label)<strlen(label3)) label = label3;
|
---|
234 | double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
|
---|
235 |
|
---|
236 | double ofpx = spoX*(XMax()-XMin());
|
---|
237 | double ofpy = spoY*(YMax()-YMin());
|
---|
238 |
|
---|
239 | double xu, yu, cw;
|
---|
240 | // Les limites du cadre
|
---|
241 | xu = g->DeltaUCX(XMax(), -cellWidth);
|
---|
242 | yu = g->DeltaUCY(YMax(), -cellHeight);
|
---|
243 | double recw = XMax()-xu;
|
---|
244 | double rech = YMax()-yu;
|
---|
245 | xu += ofpx; yu += ofpy;
|
---|
246 | g->DrawBox(xu, yu, recw, rech);
|
---|
247 |
|
---|
248 | // L'ecriture des labels (attention aux inversions possibles des axes!)
|
---|
249 | cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
|
---|
250 | xu = g->DeltaUCX(XMax(),cw);
|
---|
251 |
|
---|
252 | cw = (g->isAxeYDirUpDown()) ? -0.15*cH : -1.15*cH;
|
---|
253 | yu = g->DeltaUCY(YMax(),cw);
|
---|
254 | xu += ofpx; yu += ofpy;
|
---|
255 | g->DrawString(xu, yu,label1);
|
---|
256 | cw += -1.15*cH;
|
---|
257 | yu = g->DeltaUCY(YMax(),cw); yu += ofpy;
|
---|
258 | g->DrawString(xu, yu,label2);
|
---|
259 | cw += -1.15*cH;
|
---|
260 | yu = g->DeltaUCY(YMax(),cw); yu += ofpy;
|
---|
261 | g->DrawString(xu, yu,label3);
|
---|
262 |
|
---|
263 | }
|
---|