1 | // Classe traceur d histogramme 96-99
|
---|
2 | // C. Magneville , R. Ansari 2000-2007
|
---|
3 | // (C) CEA-DAPNIA LAL-IN2P3/CNRS
|
---|
4 |
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <stdlib.h>
|
---|
7 | #include <iostream>
|
---|
8 | #include <math.h>
|
---|
9 | #include <typeinfo>
|
---|
10 |
|
---|
11 | //RZDEL #include "sopnamsp.h"
|
---|
12 | #include "pihisto.h"
|
---|
13 |
|
---|
14 | //++
|
---|
15 | // Class PIHisto
|
---|
16 | // Lib PIext
|
---|
17 | // include pihisto.h
|
---|
18 | //
|
---|
19 | // Classe traceur d'objet histogramme (classe *P1DHistoWrapper*)
|
---|
20 | //--
|
---|
21 | //++
|
---|
22 | // Links Parents
|
---|
23 | // PIDrawer
|
---|
24 | //--
|
---|
25 | //++
|
---|
26 | // Titre Constructeur, méthodes
|
---|
27 | //--
|
---|
28 | //++
|
---|
29 | // PIHisto(P1DHistoWrapper* histo, bool ad=false)
|
---|
30 | // Constructeur. Si "ad == true", l'objet "histo" est détruit par
|
---|
31 | // le destructeur de l'objet "PIHisto"
|
---|
32 | // Note : "histo" doit être créé par new
|
---|
33 | //
|
---|
34 | // void SetStats(bool fg=true)
|
---|
35 | // Active/ désactive l'indication des statistiques d'histogramme
|
---|
36 | //--
|
---|
37 |
|
---|
38 |
|
---|
39 | PIHisto::PIHisto(P1DHistoWrapper* histo, bool ad)
|
---|
40 | : PIDrawer(), mHisto(histo)
|
---|
41 | {
|
---|
42 | mAdDO = ad; // Flag pour suppression automatique de mHisto
|
---|
43 | SetStats();
|
---|
44 | SetError();
|
---|
45 | SetFilled();
|
---|
46 | SetStatPosOffset();
|
---|
47 | SetName("HistoDrw");
|
---|
48 | }
|
---|
49 |
|
---|
50 | PIHisto::~PIHisto()
|
---|
51 | {
|
---|
52 | if(mAdDO) delete mHisto;
|
---|
53 | }
|
---|
54 |
|
---|
55 | void
|
---|
56 | PIHisto::UpdateLimits()
|
---|
57 | {
|
---|
58 | if (!mHisto) return;
|
---|
59 | double v1,v2,hmin,hmax;
|
---|
60 | hmin = 9.e39;
|
---|
61 | hmax = -9.e39;
|
---|
62 | for (int i=1; i<mHisto->NBins(); i++) {
|
---|
63 | v1 = (*mHisto)(i); //DEL - mHisto->Error(i);
|
---|
64 | v2 = (*mHisto)(i); //DEL + mHisto->Error(i);
|
---|
65 | if(v1<hmin) hmin = v1;
|
---|
66 | if(v2>hmax) hmax = v2;
|
---|
67 | }
|
---|
68 | // REZA$CHECK : Modifier pour tenir compte si axe (Y) en log
|
---|
69 | v1 = 0.1*(hmax-hmin);
|
---|
70 | hmin -= v1; hmax += v1;
|
---|
71 |
|
---|
72 | if(hmax<=hmin) hmax = hmin+1.;
|
---|
73 | SetLimits(mHisto->XMin(), mHisto->XMax(), hmin, hmax);
|
---|
74 | }
|
---|
75 |
|
---|
76 | void
|
---|
77 | PIHisto::Draw(PIGraphicUC* g, double /*xmin*/, double /*ymin*/, double /*xmax*/, double /*ymax*/)
|
---|
78 | {
|
---|
79 | if (axesFlags != kAxesNone) DrawAxes(g);
|
---|
80 |
|
---|
81 | if (!mHisto) return;
|
---|
82 | if (mHisto->NBins()<=0) return;
|
---|
83 |
|
---|
84 | mHisto->Update();
|
---|
85 |
|
---|
86 | bool oktrace=false;
|
---|
87 | // Tracer d'une polyline si demandee
|
---|
88 | bool drawpline=false;
|
---|
89 | if(GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt) drawpline=true;
|
---|
90 | // Tracer des marqueurs si demande ou si HProf
|
---|
91 | bool drawmarker=false;
|
---|
92 | if (GetGraphicAtt().GetMarker() != PI_NotDefMarker) drawmarker = true;
|
---|
93 | //RZDEL || (typeid(*mHisto) == typeid(HProf)) ) drawmarker = true;
|
---|
94 | // Tracer des erreurs ?
|
---|
95 | bool drawerr=false;
|
---|
96 | if(error==0) { // Gestion automatique des erreurs
|
---|
97 | //RZDEL Tracer les erreurs si HProf
|
---|
98 | //RZDEL if( typeid(*mHisto)==typeid(HProf) ) drawerr=true;
|
---|
99 | // Trace les erreurs si marqueurs demandes
|
---|
100 | if(drawmarker) drawerr=true;
|
---|
101 | }
|
---|
102 | else if(error>0) drawerr=true;
|
---|
103 | else if(error<0) drawerr=false;
|
---|
104 | // Fill de l'histo ?
|
---|
105 | bool drawfill=false;
|
---|
106 | if(filled) drawfill=true; else drawfill=false;
|
---|
107 | // Et aussi si on ne demande ni ligne ni marqueur ?
|
---|
108 | if( !drawmarker && !drawpline && !drawerr ) drawfill=true;
|
---|
109 |
|
---|
110 | // Remplissage des bins avec la couleur courante (trace en premier)
|
---|
111 | if(drawfill) {
|
---|
112 | oktrace = true;
|
---|
113 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
114 | double left = mHisto->BinLowEdge(i);
|
---|
115 | double width = mHisto->BinWidth();
|
---|
116 | double bottom = 0;
|
---|
117 | double height = (*mHisto)(i);
|
---|
118 | g->DrawFBox(left,bottom,width,height);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | // Trace des marqeurs
|
---|
123 | if(drawmarker) {
|
---|
124 | double x1,y1; oktrace = true;
|
---|
125 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
126 | x1 = mHisto->BinCenter(i);
|
---|
127 | y1 = (*mHisto)(i);
|
---|
128 | g->DrawMarker(x1,y1);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | // Trace des erreurs
|
---|
133 | if(drawerr) {
|
---|
134 | if(GetGraphicAtt().GetLineAtt()==PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
|
---|
135 | double x1,x2,y1,y2; oktrace = true;
|
---|
136 | double bw = mHisto->BinWidth();
|
---|
137 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
138 | if(mHisto->Error(i)>0.) {
|
---|
139 | // barres d'erreur verticales
|
---|
140 | x1 = x2 = mHisto->BinCenter(i);
|
---|
141 | y1 = (*mHisto)(i) - mHisto->Error(i);
|
---|
142 | y2 = (*mHisto)(i) + mHisto->Error(i);
|
---|
143 | g->DrawLine(x1,y1, x1, y2);
|
---|
144 | // limites de barres d'erreurs (horizontales)
|
---|
145 | x1 -= bw/3.; x2 += bw/3.;
|
---|
146 | g->DrawLine(x1,y1, x2, y1);
|
---|
147 | g->DrawLine(x1,y2, x2, y2);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | // Trace de la ligne continue si demandee
|
---|
153 | if(drawpline) {
|
---|
154 | PIGrCoord* x1 = new PIGrCoord[2*mHisto->NBins()+2];
|
---|
155 | PIGrCoord* y1 = new PIGrCoord[2*mHisto->NBins()+2];
|
---|
156 | double dx = mHisto->BinWidth();
|
---|
157 | int npt=0;
|
---|
158 | x1[npt] = mHisto->BinLowEdge(0);
|
---|
159 | y1[npt] = 0;
|
---|
160 | npt++;
|
---|
161 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
162 | x1[npt] = mHisto->BinLowEdge(i);
|
---|
163 | y1[npt] = (*mHisto)(i);
|
---|
164 | npt++;
|
---|
165 | x1[npt] = (double)x1[npt-1] + dx;
|
---|
166 | y1[npt] = y1[npt-1];
|
---|
167 | npt++;
|
---|
168 | }
|
---|
169 | x1[npt] = x1[npt-1];
|
---|
170 | y1[npt] = 0;
|
---|
171 | npt++;
|
---|
172 | g->DrawPolygon(x1,y1,npt,false);
|
---|
173 | delete [] x1; delete [] y1;
|
---|
174 | oktrace = true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // Trace/Ecriture des statistiques
|
---|
178 | // A faire a la fin - DrawStats change l'attribut de ligne
|
---|
179 | if(stats) DrawStats(g);
|
---|
180 | }
|
---|
181 |
|
---|
182 | int
|
---|
183 | PIHisto::DecodeOptionString(vector<string> & opt, bool rmdecopt)
|
---|
184 | {
|
---|
185 | int optsz1 = opt.size();
|
---|
186 | if(optsz1<1) return(0);
|
---|
187 | int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
|
---|
188 | if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
|
---|
189 |
|
---|
190 | vector<string> udopt; // On gardera ici les options non decodees
|
---|
191 | unsigned int k = 0;
|
---|
192 | int ndec = opt.size();
|
---|
193 | for( k=0; k<opt.size(); k++ ) {
|
---|
194 | string opts = opt[k];
|
---|
195 | if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
|
---|
196 | else if( opts=="nsta" || opts=="nstat"
|
---|
197 | || opts=="nostat" || opts=="nostats") SetStats(false);
|
---|
198 | else if(opts=="err") SetError(1);
|
---|
199 | else if(opts=="noerr" || opts=="nerr") SetError(-1);
|
---|
200 | else if(opts=="autoerr") SetError(0);
|
---|
201 | else if(opts=="fill") SetFilled(true);
|
---|
202 | else if(opts=="nofill" || opts=="nfill") SetFilled(false);
|
---|
203 | else if(opts.substr(0,11) == "statposoff=") {
|
---|
204 | float xo=0., yo=0.;
|
---|
205 | sscanf(opts.substr(11).c_str(),"%g,%g",&xo, &yo);
|
---|
206 | SetStatPosOffset(xo, yo);
|
---|
207 | }
|
---|
208 | else {
|
---|
209 | ndec--;
|
---|
210 | // S'il faut supprimer les options decodees
|
---|
211 | if (rmdecopt) udopt.push_back(opts);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | // S'il faut supprimer les options decodees, on remplace l'argument opt
|
---|
215 | // par le vecteur des options non decodees.
|
---|
216 | if (rmdecopt) opt = udopt;
|
---|
217 | return(ndec+ndec1);
|
---|
218 | }
|
---|
219 |
|
---|
220 | int
|
---|
221 | PIHisto::OptionToString(vector<string> & opt) const
|
---|
222 | {
|
---|
223 | PIDrawer::OptionToString(opt);
|
---|
224 |
|
---|
225 | if(stats) opt.push_back("stat"); else opt.push_back("nstat");
|
---|
226 | if(error==-1) opt.push_back("noerr");
|
---|
227 | else if(error==0) opt.push_back("autoerr");
|
---|
228 | else if(error==1) opt.push_back("err");
|
---|
229 | if(filled) opt.push_back("fill"); else opt.push_back("nofill");
|
---|
230 |
|
---|
231 | char str[256]; sprintf(str,"statposoff=%g,%g",spoX,spoY);
|
---|
232 | opt.push_back(str);
|
---|
233 |
|
---|
234 | return 1;
|
---|
235 | }
|
---|
236 |
|
---|
237 | void
|
---|
238 | PIHisto::GetOptionsHelpInfo(string& info)
|
---|
239 | {
|
---|
240 | info += " ---- PIHisto options help info : \n" ;
|
---|
241 | info += " sta,stat,stats: activate statistic display\n";
|
---|
242 | info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
|
---|
243 | info += " err / noerr,nerr : draw, do not draw error bars\n";
|
---|
244 | info += " autoerr : draw error bars if Marker drawing requested OR Profile histo\n";
|
---|
245 | info += " fill / nofill,nfill : fill, do not fill bars with selected color\n";
|
---|
246 | info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
|
---|
247 | info += " as a fraction of total size \n";
|
---|
248 | // On recupere ensuite la chaine info de la classe de base
|
---|
249 | PIDrawer::GetOptionsHelpInfo(info);
|
---|
250 | return;
|
---|
251 | }
|
---|
252 |
|
---|
253 | void
|
---|
254 | PIHisto::DrawStats(PIGraphicUC* g)
|
---|
255 | {
|
---|
256 | if (!mHisto) return;
|
---|
257 | // if (mLAtt == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
|
---|
258 | g->SelLine(PI_ThinLine);
|
---|
259 | g->SelFontSz((YMax() - YMin())/30);
|
---|
260 |
|
---|
261 | // La hauteur de la cellule
|
---|
262 | PIGrCoord a, d;
|
---|
263 | double cH = (double)g->GetFontHeight(a,d);
|
---|
264 | vector<string> lines;
|
---|
265 | int nlig = mHisto->GetStatInfoAsText(lines);
|
---|
266 | if (nlig < 1) return;
|
---|
267 |
|
---|
268 | double cellHeight = (nlig+0.6) * cH;
|
---|
269 |
|
---|
270 | int idxll = 0;
|
---|
271 |
|
---|
272 | int kl;
|
---|
273 | // on recherche la ligne la plus longue
|
---|
274 | for(kl=1; kl<nlig; kl++)
|
---|
275 | if ( lines[kl].length() > lines[idxll].length() ) idxll = kl;
|
---|
276 |
|
---|
277 | double cellWidth = 1.1 * (double)g->CalcStringWidth(lines[idxll].c_str());
|
---|
278 |
|
---|
279 | double ofpx = spoX*(XMax()-XMin());
|
---|
280 | double ofpy = spoY*(YMax()-YMin());
|
---|
281 |
|
---|
282 | double xu, yu, cw;
|
---|
283 | // Les limites du cadre
|
---|
284 | xu = g->DeltaUCX(XMax(), -cellWidth);
|
---|
285 | yu = g->DeltaUCY(YMax(), -cellHeight);
|
---|
286 | double recw = XMax()-xu;
|
---|
287 | double rech = YMax()-yu;
|
---|
288 | xu += ofpx; yu += ofpy;
|
---|
289 | g->DrawBox(xu, yu, recw, rech);
|
---|
290 |
|
---|
291 | // L'ecriture des labels (attention aux inversions possibles des axes!)
|
---|
292 | cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
|
---|
293 | xu = g->DeltaUCX(XMax(),cw);
|
---|
294 |
|
---|
295 | cw = (g->isAxeYDirUpDown()) ? -0.15*cH : -1.15*cH;
|
---|
296 | yu = g->DeltaUCY(YMax(),cw);
|
---|
297 | xu += ofpx; yu += ofpy;
|
---|
298 |
|
---|
299 | for(kl=0; kl<nlig; kl++) {
|
---|
300 | g->DrawString(xu, yu, lines[kl].c_str() );
|
---|
301 | cw += -1.15*cH;
|
---|
302 | yu = g->DeltaUCY(YMax(),cw); yu += ofpy;
|
---|
303 | }
|
---|
304 |
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 |
|
---|
309 | /* --Methode-- */
|
---|
310 | double PIHisto::GetDistanceToPoint(double x, double y)
|
---|
311 | {
|
---|
312 | if (!mHisto) return 1.e+9;
|
---|
313 |
|
---|
314 | double dist = -1.e+18;
|
---|
315 | for(int i=0; i<mHisto->NBins(); i++) {
|
---|
316 | double xp=mHisto->BinCenter(i);
|
---|
317 | double yp=(*mHisto)(i);
|
---|
318 | xp = (xp-x)/(XMax()-XMin())/0.5;
|
---|
319 | yp = (yp-y)/(YMax()-YMin())/0.5;
|
---|
320 | xp = xp*xp+yp*yp;
|
---|
321 | if(dist<0. || xp<dist) dist = xp;
|
---|
322 | }
|
---|
323 | dist=sqrt(fabs(dist));
|
---|
324 | //cout<<dist<<"PIHisto: xlim="<<XMin()<<","<<XMax()<<" ylim="<<YMin()<<","<<YMax()
|
---|
325 | // <<" NBins="<<mHisto->NBins()<<endl;
|
---|
326 | //cout<<"....d="<<dist<<" x="<<x<<" y="<<y<<endl;
|
---|
327 |
|
---|
328 | return dist;
|
---|
329 | }
|
---|