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