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

Last change on this file since 2259 was 2232, checked in by cmv, 23 years ago

details cmv 25/10/02

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