source: Sophya/trunk/SophyaPI/PIext/pintuple.cc@ 2373

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

Ajout possibilite de trace de marker suivant table de couleur/poids ds PINTuple/PINTuple3D - Reza 25/04/03

File size: 10.9 KB
Line 
1// Peida Interactive - PI R. Ansari 97-99
2// Traceur (Drawer) pour NTupleInterface
3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
4
5#include <stdio.h>
6#include "pintuple.h"
7
8//++
9// Class PINTuple
10// Lib PIext
11// include pintuple.h
12//
13// Classe de traceur 2D (dans un plan) à partir des données
14// d'un objet implémentant l'interface *NTupleInterface*.
15// Les objets "PINTuple" peuvent tracer des signes (markers)
16// éventuellement avec des barres d'erreur et une étiquette
17// pour chaque point. Si un nom de colonne poids est spécifié,
18// la taille et/ou la couleur des signes (markers) sera fonction
19// de la valeur de poids.
20//--
21//++
22// Links Parents
23// PIDrawer
24//--
25//++
26// Links Voir aussi
27// NTupleInterface
28// PINTuple3D
29//--
30
31//++
32// Titre Constructeur
33//--
34//++
35// PINTuple(NTupleInterface* nt, bool ad)
36// Constructeur. Si "ad == true", l'objet "nt" est détruit par
37// le destructeur de l'objet "PINTuple"
38// Note : "nt" doit être créé par new dans ce cas.
39//--
40
41/* --Methode-- */
42PINTuple::PINTuple(NTupleInterface* nt, bool ad)
43: PIDrawer()
44{
45 mNT = nt;
46 mAdDO = ad;
47 SetStats(true);
48 ConnectPoints(false);
49 UseSizeScale(true, 5);
50 UseColorScale(true);
51 SelectXY(NULL, NULL);
52 SelectWt(NULL);
53 SelectErrBar();
54 SelectLabel(NULL);
55 SetName("NTupleDrw");
56}
57
58PINTuple::~PINTuple()
59{
60 if (mAdDO && mNT) delete mNT;
61}
62
63//++
64// Titre Méthodes
65//--
66//++
67// void SelectXY(const char* px, const char* py)
68// Choix des noms de colonnes X,Y définissant les coordonnées des points.
69// Ces deux colonnes doivent être spécifiées pour obtenir un tracé.
70// void SelectErrBar(const char* erbx=NULL, const char* erby=NULL)
71// Choix des noms de colonnes pour le tracé des barres d'erreur.
72// void SelectWt(const char* pw=NULL, int nbins=10)
73// Choix du nom de colonne poids. Dans ce cas, la taille du signe
74// (marker) sera proportionnel à la valeur de cette colonne pour
75// chaque point.
76// void SelectLabel(const char* plabel=NULL)
77// Choix du nom de colonne correspondant à l'étiquette.
78//--
79
80/* --Methode-- */
81void PINTuple::SelectXY(const char* px, const char* py)
82{
83string name;
84if (mNT == NULL) xK = yK = -1;
85if (px == NULL) xK = -1;
86else { name = px; xK = mNT->ColumnIndex(name); }
87if (py == NULL) yK = -1;
88else { name = py; yK = mNT->ColumnIndex(name); }
89}
90
91/* --Methode-- */
92void PINTuple::SelectWt(const char* pw)
93{
94if (pw == NULL) wK = -1;
95else { string name = pw; wK = mNT->ColumnIndex(name); }
96
97if (wK >= 0) mNT->GetMinMax(wK, wMin, wMax);
98else { wMin = 0.; wMax = 1.; }
99}
100
101/* --Methode-- */
102void PINTuple::SelectLabel(const char* plabel)
103{
104if (plabel == NULL) lK = -1;
105else { string name = plabel; lK = mNT->ColumnIndex(name); }
106}
107
108/* --Methode-- */
109void PINTuple::SelectErrBar(const char* erbx, const char* erby)
110{
111string name;
112if (mNT == NULL) xebK = yebK = -1;
113if (erbx == NULL) xebK = -1;
114else { name = erbx; xebK = mNT->ColumnIndex(name); }
115if (erby == NULL) yebK = -1;
116else { name = erby; yebK = mNT->ColumnIndex(name); }
117}
118
119
120/* --Methode-- */
121void PINTuple::UpdateLimits()
122{
123 if (!mNT) return;
124 if (mNT->NbLines() <= 0) return;
125 if ( (xK < 0) || (yK < 0) ) return;
126
127 // Commencer par trouver nos limites
128 double xmin, xmax, ymin, ymax;
129 xmin = ymin = 9.e19;
130 xmax = ymax = -9.e19;
131 mNT->GetMinMax(xK, xmin, xmax);
132 mNT->GetMinMax(yK, ymin, ymax);
133 PIAxes::ReSizeMinMax(isLogScaleX(),xmin,xmax);
134 PIAxes::ReSizeMinMax(isLogScaleY(),ymin,ymax);
135 SetLimits(xmin,xmax,ymin,ymax);
136// SetAxesFlags(kBoxAxes | kExtTicks | kLabels); Ne pas faire - Reza 11/99
137}
138
139
140/* --Methode-- */
141void PINTuple::Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax)
142{
143double xp,yp,xer,yer,wp;
144double xl,yl;
145int nok;
146
147if (!mNT) return;
148if (axesFlags != kAxesNone) DrawAxes(g);
149if ( (xK < 0) || (yK < 0) ) return;
150if (GetGraphicAtt().GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
151
152// Pour tracer des markers avec taille fonction de Wt (poids)
153double dw = (wMax-wMin)/nWbins;
154if (dw < 1.e-19) dw = 1.e-19;
155
156// Pour tracer des markers avec couleur en fonction de Wt (poids)
157PIColorMap * cmap = NULL;
158double dwc = 1.;
159double nwc = 1.;
160bool revcmap;
161CMapId mcmapid = GetGraphicAtt().GetColMapId(revcmap);
162if( colorScale && (wK >= 0) && (mcmapid != CMAP_OTHER) ) {
163 cmap = new PIColorMap(mcmapid);
164 cmap->ReverseColorIndex(revcmap);
165 nwc = cmap->NCol();
166 dwc = (wMax-wMin)/nwc;
167}
168
169int msz,sz;
170
171PIMarker mmrk = GetGraphicAtt().GetMarker();
172PIMarker mrk;
173if (wK >= 0) mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_CircleMarker;
174else mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_DotMarker;
175msz = GetGraphicAtt().GetMarkerSz();
176if (msz < 1) msz = 1;
177g->SelMarker(msz, mrk);
178
179PIGrCoord uxmin, uxmax, uymin, uymax;
180g->GetGrSpace(uxmin, uxmax, uymin, uymax);
181double xmin2 = uxmin;
182double ymin2 = uymin;
183double xmax2 = uxmax;
184double ymax2 = uymax;
185
186nok = 0;
187xp = yp = xl = yl = 0;
188for (long i=0; i<(long)mNT->NbLines(); i++) {
189 xl = xp; yl = yp;
190 xp = mNT->GetCell(i, xK);
191 yp = mNT->GetCell(i, yK);
192 if ( (xp < xmin2) || (xp > xmax2) || (yp < ymin2) || (yp > ymax2) ) continue;
193 nok++;
194 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
195
196// Taille - couleur de marker en fonction du poids
197 if (wK >= 0) wp = mNT->GetCell(i, wK);
198 if (mrkSzScale && (wK >= 0)) { // Changement de taille
199 sz = (int)((wp-wMin)/dw);
200 if (sz < 0) sz = 0;
201 if (sz > nWbins) sz = nWbins;
202 sz += msz;
203 if (sz < 2) g->SelMarker(sz, PI_DotMarker);
204 else g->SelMarker(sz, mrk);
205 }
206// Couleur du marker en fonction du poids
207 if( colorScale && (wK >= 0) && cmap ) {
208 int cid = (int)((wp-wMin)/dwc);
209 if (cid < 0) cid = 0;
210 if (cid >= nwc) cid = nwc-1;
211 g->SelForeground(*cmap, cid);
212 }
213
214 if ( (i > 0) && connectPts )
215 g->DrawLine(xl, yl, xp, yp); // On relie les points ...
216 if ( xebK >= 0 ) {
217 xer = mNT->GetCell(i, xebK);
218 if(xer>0.) g->DrawLine(xp-xer, yp, xp+xer, yp);
219 }
220 if ( yebK >= 0 ) {
221 yer = mNT->GetCell(i, yebK);
222 if(yer>0.) g->DrawLine(xp, yp-yer, xp, yp+yer);
223 }
224 // Trace du marker
225 if ((wK >= 0)||(lK < 0)||(mmrk != PI_NotDefMarker)) g->DrawMarker(xp, yp);
226 // Trace eventuel du label
227 if (lK >= 0) g->DrawString(xp, yp, mNT->GetCelltoString(i, lK).c_str());
228
229}
230
231if (stats) { // Trace de stats
232 g->SelFontSz((YMax() - YMin())/30);
233 // La hauteur de la cellule
234 PIGrCoord a,d;
235 double cH = (double)g->GetFontHeight(a,d);
236 double cellHeight = 1.2 * cH;
237 // Les labels et leurs longueurs -> largeur de la cellule
238 char label[64];
239 sprintf(label, "Nd= %d / Ntot= %d", nok, mNT->NbLines());
240 double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
241 double xu, yu, cw;
242 // Les limites du cadre
243 xu = g->DeltaUCX(XMax(), - cellWidth);
244 yu = g->DeltaUCY(YMax(), - cellHeight);
245 g->DrawLine(xu, YMax(), xu, yu);
246 g->DrawLine(xu, yu, XMax(), yu);
247 // L'ecriture des labels (attention aux inversions possibles des axes!)
248 cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
249 xu = g->DeltaUCX(XMax(),cw);
250 cw = (g->isAxeYDirUpDown()) ? -0.1*cH : -1.1*cH;
251 yu = g->DeltaUCY(YMax(),cw);
252 g->DrawString(xu,yu,label);
253}
254
255if (cmap) delete cmap;
256return;
257}
258
259/* --Methode-- */
260void PINTuple::AppendTextInfo(string& info, double xmin, double ymin, double xmax, double ymax)
261{
262if (!mNT) return;
263if ( (xK < 0) || (yK < 0) ) return;
264
265int ncnt = 0;
266double xp,yp;
267char buff[128];
268sprintf(buff,"PINTuple: NLines= %d NCol= %d \n", mNT->NbLines(), mNT->NbColumns());
269info += buff;
270info += mNT->LineHeaderToString();
271for(long i=0; i<(long)mNT->NbLines(); i++) {
272 xp = mNT->GetCell(i, xK);
273 yp = mNT->GetCell(i, yK);
274 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
275 ncnt++;
276 if (ncnt > 101) continue;
277 info += mNT->LineToString(i);
278 }
279if (ncnt >= 101) info += " .... \n";
280sprintf(buff," %d points inside selected region \n", ncnt);
281info += buff;
282// printf("PINTuple::AppendTextInfo()-DBG %g %g %g %g - %d\n", xmin, ymin, xmax, ymax, ncnt);
283return;
284}
285
286/* La methode DecodeOptionString permet de decoder un ensemble d'options
287 et de parametre d'affichage specifie sous forme d'un vecteur de string.
288 Si rmdecopt == true, les options decodees sont supprimees du vecteur
289 de string fourni en entree - ce qui permet l'enchainement eventuel
290 de plusieurs decodages de string.
291 Les options peuvent etre sous forme de flag : "stat" "nostat"
292 ou plus complexes, par exemple "dynamic=-3,3"
293 Rc: La methode renvoie le nombre d'options decodees
294*/
295
296/* --Methode-- */
297int PINTuple::DecodeOptionString(vector<string> & opt, bool rmdecopt)
298{
299 int optsz1 = opt.size();
300 if(optsz1<1) return(0);
301 // On appelle d'abord le decodage de la classe PIDrawer de laquelle
302 // on herite. (Pas obligatoire) on decode donc ici les attributs de
303 // couleur, fontes ...
304 int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
305 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
306
307 vector<string> udopt; // On gardera ici les options non decodees
308 unsigned int k = 0;
309 int ndec = opt.size();
310 for( k=0; k<opt.size(); k++ ) {
311 string opts = opt[k];
312 if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
313 else if( opts=="nsta" || opts=="nstat"
314 || opts=="nostat" || opts=="nostats") SetStats(false);
315 else if (opts == "connectpoints") ConnectPoints(true);
316 else if (opts == "noconnectpoints") ConnectPoints(false);
317 else if (opts == "colorscale") UseColorScale(true);
318 else if (opts == "nocolorscale") UseColorScale(false);
319 else if (opts == "sizescale") UseSizeScale(true);
320 else if (opts == "nosizescale") UseSizeScale(false);
321 else if (opts.substr(0,10) == "sizescale=") {
322 int nbn = atoi(opts.substr(10).c_str());
323 UseSizeScale(true, nbn);
324 }
325 else {
326 // Si option non decode
327 ndec--;
328 // S'il faut supprimer les options decodees
329 if (rmdecopt) udopt.push_back(opts);
330 }
331 }
332 // S'il faut supprimer les options decodees, on remplace l'argument opt
333 // par le vecteur des options non decodees.
334 if (rmdecopt) opt = udopt;
335 return(ndec+ndec1);
336}
337
338/* La methode GetOptionsHelpInfo(string& info) renvoie une chaine
339 avec la description des options comprises par ce drawer
340 Note: Il est preferable de ne pas initialiser la chaine
341 string info au depart, afin de permettre de mettre bout a
342 bout les aides de differents Drawer */
343
344/* --Methode-- */
345void PINTuple::GetOptionsHelpInfo(string& info)
346{
347// On recupere d'abord la chaine info de la classe de base
348PIDrawer::GetOptionsHelpInfo(info);
349info += " ---- PINTuple options help info : \n" ;
350info += " sta,stat,stats: activate statistic display\n";
351info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
352info += " connectpoints: The points are connected by a line \n";
353info += " noconnectpoints (this is the default) \n";
354info += " colorscale/nocolorscale (Use color scale for weight) \n";
355info += " sizescale/sizescale=nbins/nosizescale (Use marker size for weight) \n";
356info += " and usual color/line/marker/... attribute decoding \n";
357return;
358}
359
360
361
Note: See TracBrowser for help on using the repository browser.