| 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 attribut de ligne, autre que
 | 
|---|
| 18 | //      "PI_NotDefLineAtt" est spécifié, les points sont connectés
 | 
|---|
| 19 | //      par une ligne.
 | 
|---|
| 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
 | 
|---|
| 39 | //--
 | 
|---|
| 40 |  
 | 
|---|
| 41 | /* --Methode-- */
 | 
|---|
| 42 | PINTuple::PINTuple(NTupleInterface* nt, bool ad)
 | 
|---|
| 43 | : PIDrawer()
 | 
|---|
| 44 | {
 | 
|---|
| 45 |   mNT = nt;
 | 
|---|
| 46 |   mAdDO = ad; 
 | 
|---|
| 47 |   SetStats(true);
 | 
|---|
| 48 |   SelectXY(NULL, NULL);
 | 
|---|
| 49 |   SelectWt(NULL, 1);
 | 
|---|
| 50 |   SelectErrBar();
 | 
|---|
| 51 |   SelectLabel(NULL);
 | 
|---|
| 52 |   SetName("NTupleDrw");
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | PINTuple::~PINTuple()
 | 
|---|
| 56 | {
 | 
|---|
| 57 |   if (mAdDO && mNT)  delete mNT;
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | //++
 | 
|---|
| 61 | // Titre        Méthodes
 | 
|---|
| 62 | //--
 | 
|---|
| 63 | //++
 | 
|---|
| 64 | // void  SelectXY(const char* px, const char* py)
 | 
|---|
| 65 | //      Choix des noms de colonnes X,Y définissant les coordonnées des points. 
 | 
|---|
| 66 | //      Ces deux colonnes doivent être spécifiées pour obtenir un tracé.
 | 
|---|
| 67 | // void  SelectErrBar(const char* erbx=NULL, const char* erby=NULL)
 | 
|---|
| 68 | //      Choix des noms de colonnes pour le tracé des barres d'erreur.
 | 
|---|
| 69 | // void  SelectWt(const char* pw=NULL, int nbins=10)
 | 
|---|
| 70 | //      Choix du nom de colonne poids. Dans ce cas, la taille du signe
 | 
|---|
| 71 | //      (marker) sera proportionnel à la valeur de cette colonne pour 
 | 
|---|
| 72 | //      chaque point.
 | 
|---|
| 73 | // void  SelectLabel(const char* plabel=NULL)
 | 
|---|
| 74 | //      Choix du nom de colonne correspondant à l'étiquette.
 | 
|---|
| 75 | //--
 | 
|---|
| 76 | 
 | 
|---|
| 77 | /* --Methode-- */
 | 
|---|
| 78 | void  PINTuple::SelectXY(const char* px, const char* py)
 | 
|---|
| 79 | {
 | 
|---|
| 80 | string name;
 | 
|---|
| 81 | if (mNT == NULL)  xK = yK = -1;
 | 
|---|
| 82 | if (px == NULL) xK = -1;
 | 
|---|
| 83 | else { name = px; xK = mNT->ColumnIndex(name); }
 | 
|---|
| 84 | if (py == NULL) yK = -1;
 | 
|---|
| 85 | else { name = py; yK = mNT->ColumnIndex(name); }
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | /* --Methode-- */
 | 
|---|
| 89 | void  PINTuple::SelectWt(const char* pw, int nbins)
 | 
|---|
| 90 | {
 | 
|---|
| 91 | nWbins = (nbins > 0) ? nbins : 10;
 | 
|---|
| 92 | if (pw == NULL) wK = -1;  
 | 
|---|
| 93 | else { string name = pw;   wK = mNT->ColumnIndex(name);  }
 | 
|---|
| 94 |  
 | 
|---|
| 95 | if (wK >= 0) mNT->GetMinMax(wK, wMin, wMax);
 | 
|---|
| 96 | else  { wMin = 0.; wMax = 1.; }
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | /* --Methode-- */
 | 
|---|
| 100 | void  PINTuple::SelectLabel(const char* plabel)
 | 
|---|
| 101 | {
 | 
|---|
| 102 | if (plabel == NULL) lK = -1;
 | 
|---|
| 103 | else {  string name = plabel;  lK = mNT->ColumnIndex(name);  }
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | /* --Methode-- */
 | 
|---|
| 107 | void  PINTuple::SelectErrBar(const char* erbx, const char* erby)
 | 
|---|
| 108 | {
 | 
|---|
| 109 | string name;
 | 
|---|
| 110 | if (mNT == NULL)  xebK = yebK = -1;
 | 
|---|
| 111 | if (erbx == NULL) xebK = -1;
 | 
|---|
| 112 | else { name = erbx;  xebK = mNT->ColumnIndex(name); }
 | 
|---|
| 113 | if (erby == NULL) yebK = -1;
 | 
|---|
| 114 | else { name = erby;  yebK = mNT->ColumnIndex(name); }
 | 
|---|
| 115 | }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /* --Methode-- */
 | 
|---|
| 119 | void PINTuple::UpdateLimits()
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   if (!mNT) return;
 | 
|---|
| 122 |   if (mNT->NbLines() <= 0)  return;
 | 
|---|
| 123 |   if ( (xK < 0) || (yK < 0) )   return;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   // Commencer par trouver nos limites
 | 
|---|
| 126 |   double dx, dy;
 | 
|---|
| 127 |   double xmin, xmax, ymin, ymax;
 | 
|---|
| 128 |   xmin = ymin = 9.e19;
 | 
|---|
| 129 |   xmax = ymax = -9.e19;
 | 
|---|
| 130 |   mNT->GetMinMax(xK, xmin, xmax);
 | 
|---|
| 131 |   mNT->GetMinMax(yK, ymin, ymax);
 | 
|---|
| 132 |     
 | 
|---|
| 133 |   dx = 0.02*(xmax-xmin);
 | 
|---|
| 134 |   dy = 0.02*(ymax-ymin);
 | 
|---|
| 135 |   
 | 
|---|
| 136 |   SetLimits(xmin-dx, xmax+dx, ymin-dy, ymax+dy);
 | 
|---|
| 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.e19;
 | 
|---|
| 156 | int msz,sz;
 | 
|---|
| 157 | 
 | 
|---|
| 158 | PIMarker mmrk = GetGraphicAtt().GetMarker();
 | 
|---|
| 159 | PIMarker mrk;
 | 
|---|
| 160 | if (wK >= 0)  mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_CircleMarker;
 | 
|---|
| 161 | else   mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_DotMarker;
 | 
|---|
| 162 |  msz = GetGraphicAtt().GetMarkerSz(); 
 | 
|---|
| 163 | if (msz < 1) msz = 1;
 | 
|---|
| 164 | g->SelMarker(msz, mrk);
 | 
|---|
| 165 | 
 | 
|---|
| 166 | PIGrCoord uxmin, uxmax, uymin, uymax;
 | 
|---|
| 167 | g->GetGrSpace(uxmin, uxmax, uymin, uymax);
 | 
|---|
| 168 | double xmin2 = uxmin;
 | 
|---|
| 169 | double ymin2 = uymin;
 | 
|---|
| 170 | double xmax2 = uxmax;
 | 
|---|
| 171 | double ymax2 = uymax;
 | 
|---|
| 172 | 
 | 
|---|
| 173 | nok = 0;  
 | 
|---|
| 174 | xp = yp = xl = yl = 0;
 | 
|---|
| 175 | for (int i=0; i<mNT->NbLines(); i++) {
 | 
|---|
| 176 |   xl = xp;  yl = yp; 
 | 
|---|
| 177 |   xp = mNT->GetCell(i, xK);
 | 
|---|
| 178 |   yp = mNT->GetCell(i, yK);
 | 
|---|
| 179 |   if ( (xp < xmin2) || (xp > xmax2) || (yp < ymin2) || (yp > ymax2) )  continue;
 | 
|---|
| 180 |   nok++;
 | 
|---|
| 181 |   if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) )  continue;
 | 
|---|
| 182 |   if ( (i > 0) && (GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt) )   
 | 
|---|
| 183 |     g->DrawLine(xl, yl, xp, yp);  // On relie les points ...
 | 
|---|
| 184 |   if ( xebK >= 0 ) {
 | 
|---|
| 185 |     xer = mNT->GetCell(i, xebK);
 | 
|---|
| 186 |     if(xer>0.) g->DrawLine(xp-xer, yp, xp+xer, yp);
 | 
|---|
| 187 |   }
 | 
|---|
| 188 |   if ( yebK >= 0 ) {
 | 
|---|
| 189 |     yer = mNT->GetCell(i, yebK);
 | 
|---|
| 190 |     if(yer>0.) g->DrawLine(xp, yp-yer, xp, yp+yer);
 | 
|---|
| 191 |   }
 | 
|---|
| 192 |   if (wK >= 0) { // Taille de marker en fonction du poids
 | 
|---|
| 193 |     wp = mNT->GetCell(i, wK);
 | 
|---|
| 194 |     sz = (int)((wp-wMin)/dw);
 | 
|---|
| 195 |     if (sz < 0) sz = 0;
 | 
|---|
| 196 |     if (sz > nWbins)  sz = nWbins;
 | 
|---|
| 197 |     sz += msz;
 | 
|---|
| 198 |     if (sz < 2)  g->SelMarker(sz, PI_DotMarker);
 | 
|---|
| 199 |     else g->SelMarker(sz, mrk);
 | 
|---|
| 200 |   }
 | 
|---|
| 201 |   // Trace du marker
 | 
|---|
| 202 |   if ((wK >= 0)||(lK < 0)||(mmrk != PI_NotDefMarker))  g->DrawMarker(xp, yp); 
 | 
|---|
| 203 |   // Trace eventuel du label
 | 
|---|
| 204 |   if (lK >= 0) g->DrawString(xp, yp, mNT->GetCelltoString(i, lK).c_str());
 | 
|---|
| 205 | 
 | 
|---|
| 206 | }
 | 
|---|
| 207 | 
 | 
|---|
| 208 | if (stats) { // Trace de stats 
 | 
|---|
| 209 |   g->SelFontSz((YMax() - YMin())/30);  
 | 
|---|
| 210 |   // La hauteur de la cellule
 | 
|---|
| 211 |   PIGrCoord a,d;
 | 
|---|
| 212 |   double cH = (double)g->GetFontHeight(a,d);
 | 
|---|
| 213 |   double cellHeight = 1.2 * cH;
 | 
|---|
| 214 |   // Les labels et leurs longueurs -> largeur de la cellule
 | 
|---|
| 215 |   char label[64];
 | 
|---|
| 216 |   sprintf(label, "Nd= %d / Ntot= %d", nok, mNT->NbLines());
 | 
|---|
| 217 |   double cellWidth =   1.1 * (double)g->CalcStringWidth(label);
 | 
|---|
| 218 |   double xu, yu, cw;
 | 
|---|
| 219 |   // Les limites du cadre
 | 
|---|
| 220 |   xu = g->DeltaUCX(XMax(), - cellWidth);
 | 
|---|
| 221 |   yu = g->DeltaUCY(YMax(), - cellHeight);
 | 
|---|
| 222 |   g->DrawLine(xu, YMax(), xu, yu);
 | 
|---|
| 223 |   g->DrawLine(xu, yu, XMax(), yu);
 | 
|---|
| 224 |   // L'ecriture des labels (attention aux inversions possibles des axes!)
 | 
|---|
| 225 |   cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
 | 
|---|
| 226 |   xu = g->DeltaUCX(XMax(),cw);
 | 
|---|
| 227 |   cw = (g->isAxeYDirUpDown()) ? -0.1*cH : -1.1*cH;
 | 
|---|
| 228 |   yu = g->DeltaUCY(YMax(),cw);
 | 
|---|
| 229 |   g->DrawString(xu,yu,label);
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | return;
 | 
|---|
| 233 | }
 | 
|---|
| 234 | 
 | 
|---|
| 235 | /* --Methode-- */
 | 
|---|
| 236 | void PINTuple::AppendTextInfo(string& info, double xmin, double ymin, double xmax, double ymax)
 | 
|---|
| 237 | {
 | 
|---|
| 238 | if (!mNT) return;
 | 
|---|
| 239 | if ( (xK < 0) || (yK < 0) )  return;
 | 
|---|
| 240 | 
 | 
|---|
| 241 | int ncnt = 0;
 | 
|---|
| 242 | double xp,yp;
 | 
|---|
| 243 | char buff[128];
 | 
|---|
| 244 | sprintf(buff,"PINTuple: NLines= %d  NCol= %d \n", mNT->NbLines(),  mNT->NbColumns());
 | 
|---|
| 245 | info += buff;
 | 
|---|
| 246 | info += mNT->LineHeaderToString();
 | 
|---|
| 247 | for (int i=0; i<mNT->NbLines(); i++) {
 | 
|---|
| 248 |   xp = mNT->GetCell(i, xK);
 | 
|---|
| 249 |   yp = mNT->GetCell(i, yK);
 | 
|---|
| 250 |   if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) )  continue;
 | 
|---|
| 251 |   ncnt++;
 | 
|---|
| 252 |   if (ncnt > 101) continue;
 | 
|---|
| 253 |   info += mNT->LineToString(i);
 | 
|---|
| 254 |   }
 | 
|---|
| 255 | if (ncnt >= 101) info += " .... \n";
 | 
|---|
| 256 | sprintf(buff," %d points inside selected region \n", ncnt);
 | 
|---|
| 257 | info += buff;
 | 
|---|
| 258 | // printf("PINTuple::AppendTextInfo()-DBG %g %g %g %g - %d\n", xmin, ymin, xmax, ymax, ncnt); 
 | 
|---|
| 259 | return;
 | 
|---|
| 260 | }
 | 
|---|
| 261 | 
 | 
|---|
| 262 | /*  La methode DecodeOptionString permet de decoder un ensemble d'options 
 | 
|---|
| 263 |     et de parametre d'affichage specifie sous forme d'un vecteur de string.
 | 
|---|
| 264 |     Si rmdecopt == true, les options decodees sont supprimees du vecteur 
 | 
|---|
| 265 |     de string fourni en entree - ce qui permet l'enchainement eventuel 
 | 
|---|
| 266 |     de plusieurs decodages de string.
 | 
|---|
| 267 |     Les options peuvent etre sous forme de flag : "stat" "nostat" 
 | 
|---|
| 268 |     ou plus complexes, par exemple "dynamic=-3,3"
 | 
|---|
| 269 |     Rc: La methode renvoie le nombre d'options decodees 
 | 
|---|
| 270 | */
 | 
|---|
| 271 | 
 | 
|---|
| 272 | /* --Methode-- */
 | 
|---|
| 273 | int PINTuple::DecodeOptionString(vector<string> & opt, bool rmdecopt)
 | 
|---|
| 274 | {
 | 
|---|
| 275 |   if (opt.size() < 1)  return(0);  
 | 
|---|
| 276 |   // On appelle d'abord le decodage de la classe PIDrawer de laquelle
 | 
|---|
| 277 |   // on herite. (Pas obligatoire) on decode donc ici les attributs de
 | 
|---|
| 278 |   // couleur, fontes ...
 | 
|---|
| 279 |   int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
 | 
|---|
| 280 |   if ((opt.size() - ndec1) < 1) return(ndec1);  // si tout a ete decode
 | 
|---|
| 281 | 
 | 
|---|
| 282 |   vector<string> udopt;  // On gardera ici les options non decodees
 | 
|---|
| 283 |   unsigned int k = 0;
 | 
|---|
| 284 |   int ndec = opt.size();
 | 
|---|
| 285 |   for( k=0; k<opt.size(); k++ ) {
 | 
|---|
| 286 |     string opts = opt[k];
 | 
|---|
| 287 |     if (opts == "stat")  SetStats(true);
 | 
|---|
| 288 |     else if (opts == "nostat")  SetStats(false);
 | 
|---|
| 289 |     else {
 | 
|---|
| 290 |       // Si option non decode
 | 
|---|
| 291 |       ndec--;
 | 
|---|
| 292 |       // S'il faut supprimer les options decodees
 | 
|---|
| 293 |       if (rmdecopt)  udopt.push_back(opts);
 | 
|---|
| 294 |     }
 | 
|---|
| 295 |   }  
 | 
|---|
| 296 |   // S'il faut supprimer les options decodees, on remplace l'argument opt
 | 
|---|
| 297 |   // par le vecteur des options non decodees.
 | 
|---|
| 298 |   if (rmdecopt)  opt = udopt;
 | 
|---|
| 299 |   return(ndec+ndec1);  
 | 
|---|
| 300 | }
 | 
|---|
| 301 | 
 | 
|---|
| 302 | /* La methode GetOptionsHelpInfo(string& info) renvoie une chaine 
 | 
|---|
| 303 |    avec la description des options comprises par ce drawer  
 | 
|---|
| 304 |    Note: Il est preferable de ne pas initialiser la chaine 
 | 
|---|
| 305 |    string info au depart, afin de permettre de mettre bout a 
 | 
|---|
| 306 |    bout les aides de differents Drawer */
 | 
|---|
| 307 | 
 | 
|---|
| 308 | /* --Methode-- */
 | 
|---|
| 309 | void PINTuple::GetOptionsHelpInfo(string& info)
 | 
|---|
| 310 | {
 | 
|---|
| 311 |   // On recupere d'abord la chaine info de la classe de base
 | 
|---|
| 312 |   PIDrawer::GetOptionsHelpInfo(info);
 | 
|---|
| 313 |   info += " ---- PINTuple options help info : \n" ;
 | 
|---|
| 314 |   info += "    stat / nostat : activate/deactivate statistic information display \n";
 | 
|---|
| 315 |   return;
 | 
|---|
| 316 | }
 | 
|---|