source: Sophya/trunk/SophyaPI/PIext/pihisto.cc@ 3089

Last change on this file since 3089 was 3055, checked in by cmv, 19 years ago

Protection draw pour histo vides cmv 13/8/2006

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