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

Last change on this file since 2463 was 2388, checked in by ansari, 22 years ago

Amelioration help/decodage options - Reza 20/5/2003

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