source: Sophya/trunk/SophyaPI/PIext/nobjmgr.cc@ 332

Last change on this file since 332 was 332, checked in by ercodmgr, 26 years ago

Probleme avec les const ... Reza 29/6/99

File size: 47.1 KB
RevLine 
[165]1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4
[295]5#include <typeinfo>
[165]6#include <iostream.h>
7#include <string>
8#include <list>
9#include <map>
10#if defined(__KCC__)
11using std::string ;
12#include <list.h>
13#include <map.h>
14#endif
15
16#include "strutil.h"
17#include "datatypes.h"
18
19#include "nobjmgr.h"
20#include "servnobjm.h"
[330]21#include "nomgadapter.h"
[165]22#include "pistdimgapp.h"
23
24#include "matrix.h"
25#include "cvector.h"
[326]26#include "ntuple.h"
[165]27
[293]28// EVOL-PLANCK
29#ifdef SANS_EVOLPLANCK
30#include "fitsimage.h"
31#endif
32
[165]33#include "fct1dfit.h"
34#include "fct2dfit.h"
35
[295]36#include "pisurfdr.h"
[165]37#include "pipodrw.h"
38#include "pihisto.h"
39#include "hisprof.h"
40#include "pihisto2d.h"
41#include "pintuple.h"
42#include "pintup3d.h"
43#include "pigfd1.h"
44#include "pigfd2.h"
45
46
47//++
48// Class NamedObjMgr
49// Lib PI
50// include nobjmgr.h
51//
52// Cette classe fournit les services nécéssaire àla gestion des objets
53// (l'ensemble des objets PPersist de PEIDA++) au sein du programme
54// d'analyse interactive *piapp* . Elle constitue en outre l'interface
55// entre les fonctions utilisateur et l'application graphique.
56//--
57//++
58// Links Voir aussi
59// PIStdImgApp
60// Services2NObjMgr
61// PIACmd
62//--
63
64
65// ..................................................................
66// ...... Gestion des objets nommes, variables globales ............
[331]67struct nobj_diritem {
68 int id;
69 int nobj;
70};
71
72typedef map<string, nobj_diritem, less<string> > NObjDirList;
73
[165]74struct nobj_item {
[295]75 AnyDataObj* obj;
76 NObjMgrAdapter* obja;
[165]77 int num;
[331]78 int dirid;
[165]79 list<int> wrsid;
80 bool operator==(nobj_item const& b) const
81 { return (this->obj == b.obj); }
82};
83
84typedef map<string, nobj_item, less<string> > NObjList;
85
[331]86static NObjDirList* myDirs = NULL;
87static NObjList* myObjs = NULL;
[165]88static int fgOInit = 0;
89static int myNObj = 0;
[331]90static int myDirId = 0;
91static string* currDir;
[165]92
93static PIStdImgApp* myImgApp=NULL;
94static Services2NObjMgr* servnobjm=NULL;
95
96static string* TmpDir; // Repertoire pour les compilations / link dynamique
97
98//++
99// Titre Constructeurs
100//--
101//++
102// NamedObjMgr()
103// Constructeur. Les différents instantiation de la classe "NamedObjMgr"
104// dans une même application créent des objets qui travaillent sur la même
105// liste d'objets. Les objets de cette classe ne possedent en effet pas
106// de variables membres.
107//--
108
109/* --Methode-- */
110NamedObjMgr::NamedObjMgr()
111{
112if (fgOInit == 0) {
[331]113 myDirs = new NObjDirList;
[165]114 myObjs = new NObjList;
[331]115 currDir = new string("home");
[165]116 char* varenv;
117 TmpDir = new string("");
118 if ( (varenv=getenv("PEIDA_TMP")) != NULL ) (*TmpDir) = varenv;
119 else if ( (varenv=getenv("TMPDIR")) != NULL ) (*TmpDir) = varenv;
120 int l = (*TmpDir).length();
121 if ( (l>0) && ((*TmpDir)[l-1] != '/') ) (*TmpDir) += '/';
122 servnobjm = new Services2NObjMgr(NULL, (*TmpDir));
123 }
124fgOInit++;
125}
126
127
128/* --Methode-- */
129NamedObjMgr::~NamedObjMgr()
130{
131fgOInit--;
132if (fgOInit == 0) delete myObjs;
133}
134
135//++
136// Titre Méthodes
137//--
138//++
139// void SetImgApp(PIStdImgApp* app)
140// Spécifie l'objet "PIStdImgApp" associé.
141// PIStdImgApp* GetImgApp()
142// Accès à l'objet "PIStdImgApp" associé.
143//--
144
145/* --Methode-- */
146void NamedObjMgr::SetImgApp(PIStdImgApp* app)
147{
148myImgApp = app;
149servnobjm->SetImgApp(app);
150}
151
152/* --Methode-- */
153PIStdImgApp* NamedObjMgr::GetImgApp()
154{
155return(myImgApp);
156}
157
158/* --Methode-- */
159Services2NObjMgr* NamedObjMgr::GetServiceObj()
160{
161return(servnobjm);
162}
163
[331]164/* --Methode-- */
165void NamedObjMgr::CreateDir(string const& dirname)
166{
167}
168
169/* --Methode-- */
170void NamedObjMgr::DeleteDir(string const& dirname)
171{
172}
173
174/* --Methode-- */
175void NamedObjMgr::SetCurrentDir(string const& dirname)
176{
177}
178
179/* --Methode-- */
180void NamedObjMgr::CleanTmpDir()
181{
182}
183
184/* --Methode-- */
185void NamedObjMgr::CleanOldDir()
186{
187}
188
[165]189//++
190// Titre Gestion de la liste des objets
191//--
192//++
[295]193// void AddObj(AnyDataObj* obj, string& nom)
[165]194// Ajoute l'objet "obj" à la liste, identifié par "nom".
195// Si un objet de même nom existe, l'ancien objet est renommé en concaténant
196// un numéro à son nom.
197// void DelObj(string const& nom, bool fgd=true)
198// Supprime l'objet "nom" de la liste. L'objet est détruit si "fgd==true" ("delete obj")
199// void DelObjects(string const& patt, bool fgd=true)
200// Supprime l'ensemble des objets dont le nom correspond au patron "patt".
201// Le patron peut contenir les caractères "*" et "?" . Les objets sont détruits si "fgd==true"
[295]202// AnyDataObj* GetObj(string const& nom)
[165]203// Retourne l'objet identifié par "nom" dans la liste. Retourne "NULL" si "nom" n'est
204// pas dans la liste.
205// void RenameObj(string const& nom, string& nomnew)
206// Change le nom d'un objet dans la liste.
207//--
208
209
210/* --Methode-- */
[331]211void NamedObjMgr::AddObj(AnyDataObj* obj, string & nom, bool)
[165]212{
213
214if (obj == NULL) return;
215// on supprime les blancs de debut et de fin
216size_t p = nom.find_first_not_of(" ");
217if(p>nom.length()) {
218 nom = "";
219} else {
220 nom = nom.substr(p);
221 p = nom.find_first_of(" ");
222 if(p>nom.length()) p=nom.length();
223 nom = nom.substr(0, p);
224}
225
226myNObj++;
[295]227if (nom.length() < 1) nom = typeid(*obj).name();
[165]228NObjList::iterator it = myObjs->find(nom);
229if (it != myObjs->end()) { // l'objet existe deja
230 char strg[16];
231 sprintf(strg, "%d", myNObj);
232 string nomnew = nom + strg;
233 RenameObj(nom, nomnew);
234 }
235
236nobj_item no;
237no.obj = obj;
[295]238no.obja = servnobjm->GetAdapter(obj); // L'adaptateur
[165]239no.num = myNObj;
[331]240(*myObjs)[nom] = no;
[165]241if (myImgApp) {
[331]242 string str = nom + " (T= " + typeid(*obj).name() + ")" ;
[165]243 (myImgApp->ObjMgrW())->AddObj(str.c_str(), myNObj+1000);
244}
245
[331]246cout << "NamedObjMgr::AddObj() Object " << nom << " ( "
[295]247 << typeid(*obj).name() << " ) added (Total= " << myObjs->size() << ")" << endl;
[165]248return;
249}
250
251/* --Methode-- */
[331]252void NamedObjMgr::RenameObj(string & nom, string& nomnew)
[165]253{
[331]254AnyDataObj* obj = GetObj(nom);
255if (obj == NULL) return;
256DelObj(nom, false);
257AddObj(obj, nomnew);
258return;
259}
260
261/* --Methode-- */
262void NamedObjMgr::DelObj(string & nom, bool fgd)
263{
[165]264NObjList::iterator it = myObjs->find(nom);
265if (it == myObjs->end()) return;
266list<int>::iterator ii;
267if (myImgApp) {
268//DBG cerr << " *DBG* NamedObjMgr::DelObj Sz= " << (*it).second.wrsid.size() << endl;
269 for(ii=(*it).second.wrsid.begin(); ii != (*it).second.wrsid.end(); ii++)
270 myImgApp->DelWRsId((*ii));
271 (myImgApp->ObjMgrW())->DelObj((*it).second.num+1000);
272}
[314]273delete (*it).second.obja; // destruction de l'adaptateur
[165]274if (fgd) delete (*it).second.obj;
275myObjs->erase(it);
276if (fgd) cout << "NamedObjMgr::DelObj() Object " << nom << " deleted (Total= " << myObjs->size() << ")" << endl;
277else cout << "NamedObjMgr::DelObj() Object " << nom << " removed (Total= " << myObjs->size() << ")" << endl;
278return;
279}
280
281/* --Methode-- */
[331]282void NamedObjMgr::DelObj_Id(int oid)
[165]283{
[331]284}
285
286/* --Methode-- */
287void NamedObjMgr::DelObjects(string & patt, bool fgd)
288{
[165]289NObjList::iterator it;
290list<string> odel;
291string cn;
292for(it = myObjs->begin(); it != myObjs->end(); it++) {
293 cn = (*it).first;
294 if (csh_parse(cn.c_str(), patt.c_str()) != 0) odel.push_back(cn);
295 }
296list<string>::iterator ii;
297for(ii=odel.begin(); ii != odel.end(); ii++) DelObj(*ii, fgd);
298}
299
300/* --Methode-- */
[331]301AnyDataObj* NamedObjMgr::GetObj(string & nom)
[165]302{
303NObjList::iterator it = myObjs->find(nom);
304if (it == myObjs->end()) return(NULL);
305return((*it).second.obj);
306}
307
308/* --Methode-- */
[331]309NObjMgrAdapter* NamedObjMgr::GetObjAdapter(string & nom)
[295]310{
311NObjList::iterator it = myObjs->find(nom);
312if (it == myObjs->end()) return(NULL);
313return((*it).second.obja);
314}
315
316/* --Methode-- */
[331]317void NamedObjMgr::ListObjs(string & patt)
[165]318{
[331]319int k;
320AnyDataObj* obj=NULL;
321string ctyp;
322char strg[256];
323
324cout << "NamedObjMgr::ListObjs() NObjs= " << myObjs->size() << "\n" ;
325NObjList::iterator it; k = 0;
326for(it = myObjs->begin(); it != myObjs->end(); it++) {
327 obj = (*it).second.obj;
328
329 ctyp = typeid(*obj).name();
330 sprintf(strg, "%2d/ %16s : %s", k, typeid(*obj).name(), ((*it).first).c_str());
331 ctyp = strg;
332 cout << ctyp << "\n" ;
333 k++;
334}
335cout << endl;
[165]336return;
337}
338
339/* --Methode-- */
[331]340void NamedObjMgr::GetObjList(string & patt, vector<string> &)
[165]341{
342}
343
344//++
345// Titre Entrées-Sorties (I/O) sur les objets
346//--
347//++
348// void ReadObj(PInPersist& s, int num=-1)
349// Lit l'objet à partir avec le tag numéro "num" dans le flot "PInPersist s"
350// et l'ajoute à la liste. Si "num" est négatif, tous les objets présents
351// sur le flot "s" sont créés et ajoutés à la liste.
352// void ReadObj(string const & nomppf, string nobj="")
353// Lit le premier objet à partir du fichier PPF "nomppf". L'objet est ajouté
354// à la liste avec le nom "nobj". Si "nobj" est une chaîne vide, un nom est
355// composé à partir du nom de fichier.
356//--
357
358/* --Methode-- */
[331]359void NamedObjMgr::ReadObj(string const & flnm, string & nobj)
[165]360{
361PPersist* obj=NULL;
362bool ok = true;
363
364TRY{
365 PInPersist pis(flnm);
366 obj = PPersistMgr::ReadObject(pis);
367 if (obj == NULL) ok = false;
368} CATCH(merr)
369 { printf("NamedObjMgr::ReadObj()/Error Exception= %ld (%s) \n",
370 (long)merr, PeidaExc(merr)); ok = false; } ENDTRY;
371
372if (!ok) return;
373if (nobj.length()<1) nobj = servnobjm->FileName2Name(flnm);
[295]374AddObj(obj->DataObj(), nobj);
[165]375return;
376}
377
378/* --Methode-- */
379void NamedObjMgr::ReadObj(PInPersist& s, int num)
380{
381int i, cid, key, ln;
382int n0, n1;
383bool ok = true;
384PPersist* obj=NULL;
385string nom;
386
387if ( (s.NbTags() < 1) || (num >= s.NbTags()) ) {
388 if (num >= 0) {
389 printf("NamedObjMgr::ReadObj(PInPersist, %d) Error! NbTags = %d \n", num, s.NbTags());
390 return;
391 }
392 TRY {
393 obj = PPersistMgr::ReadObject(s);
394 if (obj == NULL) ok = false;
395 } CATCH(merr) {
396 printf("NamedObjMgr::ReadObj()/Error Exception= %ld (%s) \n", (long)merr, PeidaExc(merr));
397 ok = false;
398 } ENDTRY;
399 if (!ok) return;
400 nom = "";
[295]401 AddObj(obj->DataObj(), nom);
[165]402}
403
404if (num < 0) { n0 = 0; n1 = s.NbTags(); }
405else { n0 = num; n1 = num+1; }
406for(i=n0; i<n1; i++) {
407 key = s.TagKey(i, cid, ln);
408 if (ln <= 0) nom = "";
409 else nom = s.TagName(i);
410 s.GotoTag(i);
411 TRY {
412 obj = PPersistMgr::ReadObject(s);
413 if (obj == NULL) ok = false;
414 } CATCH(merr) {
415 printf("NamedObjMgr::ReadObj()/Error Exception= %ld (%s) \n", (long)merr, PeidaExc(merr));
416 ok = false;
417 } ENDTRY;
[295]418 if (ok) AddObj(obj->DataObj(), nom);
[165]419}
420
421return;
422}
423/* --Methode-- */
424void NamedObjMgr::ReadAll(string const & flnm)
425{
426bool ok = true;
427PPersist* obj=NULL;
428
429PInPersist* ppin;
430TRY{
431 ppin = new PInPersist(flnm);
432 if (ppin->NbTags() < 1) obj = PPersistMgr::ReadObject((*ppin));
433 else obj = NULL;
434} CATCH(merr)
435 { printf("NamedObjMgr::ReadAll()/Error Exception= %ld (%s) \n",
436 (long)merr, PeidaExc(merr)); ok = false; } ENDTRY;
437
438if (!ok) return;
439if (obj) {
440 string nom = servnobjm->FileName2Name(flnm);
[295]441 AddObj(obj->DataObj(), nom);
[165]442 }
443else ReadObj((*ppin), -1);
444delete ppin;
445return;
446}
447
448/* --Methode-- */
[331]449void NamedObjMgr::ReadFits(string const & flnm, string & nobj)
[165]450{
451bool ok = true;
452RzImage* obj;
453
454TRY{
455// obj = RzReadFits((char*)flnm.c_str(), ImgOffX, ImgOffY, ImgSizX, ImgSizY, ImgBitSgn);
[293]456#ifdef SANS_EVOLPLANCK
[165]457 obj = RzReadFits((char*)flnm.c_str());
[293]458#else
459 printf("NamedObjMgr::ReadFITS( NON-Disponible EVOL-PLANCK) \n");
460 obj = NULL;
461#endif
[165]462 if (obj == NULL) ok = false;
463} CATCH(merr) {
464 printf("NamedObjMgr::ReadFITS(_Error Exception= %ld (%s) \n", (long)merr, PeidaExc(merr));
465 ok = false;
466} ENDTRY;
467if (ok) {
468 if (nobj.length()<1) nobj = servnobjm->FileName2Name(flnm);
[295]469 AddObj((AnyDataObj*)obj, nobj);
[165]470}
471return;
472}
473
474
475static int key_for_write = 5000;
476/* --Methode-- */
[331]477void NamedObjMgr::SaveObj(string & nom, POutPersist& s)
[165]478{
[295]479NObjMgrAdapter* obja=NULL;
480obja = GetObjAdapter(nom);
481if (obja == NULL) return;
[165]482printf("NamedObjMgr::SaveObj(%s, ) (Type=%s) \n",
[295]483 nom.c_str(), typeid(*(obja->GetDataObj())).name());
484obja->SavePPF(s, nom);
[165]485return;
486}
487
488/* --Methode-- */
489void NamedObjMgr::SaveAll(string const& flnm)
490{
491bool ok = true;
492
493POutPersist* pout;
494TRY{
495 pout = new POutPersist(flnm);
496} CATCH(merr)
497 { printf("NamedObjMgr::SaveAll()/Error Exception= %ld (%s) \n",
498 (long)merr, PeidaExc(merr)); ok = false; } ENDTRY;
499if (!ok) return;
[332]500NObjList::iterator it;
501string no;
502for(it = myObjs->begin(); it != myObjs->end(); it++) {
503 no = (*it).first;
504 SaveObj(no, (*pout));
505 }
[165]506delete pout;
507return;
508}
509
510/* --Methode-- */
[331]511void NamedObjMgr::SaveFits(string& nom, string const & flnm)
[165]512{
[295]513NObjMgrAdapter* obja=NULL;
514obja = GetObjAdapter(nom);
515if (obja == NULL) return;
516obja->SaveFits(flnm);
[293]517return;
[165]518}
519
520
521
522/* --Methode-- */
[331]523void NamedObjMgr::PrintObj(string& nom)
[165]524{
[295]525NObjMgrAdapter* obja=NULL;
526obja = GetObjAdapter(nom);
527if (obja == NULL) return;
[165]528
[295]529string ctyp = typeid(*obja->GetDataObj()).name();
530cout << "NamedObjMgr::PrintObj(" << nom << ") Type: " << ctyp << endl;
531obja->Print(cout);
[165]532
533return;
534}
535
536/* --Methode-- */
[331]537void NamedObjMgr::DisplayObj(string& nom, string dopt)
[165]538{
[295]539NObjMgrAdapter* obja=NULL;
540obja = GetObjAdapter(nom);
541if (obja == NULL) {
[165]542 cout << "NamedObjMgr::DisplayObj() Error , Pas d'objet de nom " << nom << endl;
543 return;
544}
545if (!myImgApp) return;
546
[295]547PIDrawer * dr = NULL;
548P2DArrayAdapter* arr = NULL;
549dr = obja->GetDrawer(dopt);
550if (!dr) arr = obja->Get2DArray(dopt);
[165]551
[295]552if (!dr && !arr) {
553 string ctyp = typeid(*(obja->GetDataObj())).name();
554 cout << "NamedObjMgr::DisplayObj() Error , Pas de display pour " << ctyp << endl;
555 return;
556 }
557
[165]558int wrsid = 0;
559bool fgsr = true;
560int opt = servnobjm->DecodeDispOption(dopt, fgsr);
561
[295]562if (dr) {
563 PIDrawer3D * dr3 = dynamic_cast<PIDrawer3D *>(dr);
564 if(dr3) wrsid = myImgApp->Disp3DDrawer(dr3, nom, opt);
565 else wrsid = myImgApp->DispScDrawer( dr, nom, opt);
566 }
567else if (arr) wrsid = myImgApp->DispImage(arr, nom, opt);
[165]568
[295]569if(wrsid != 0) {
570 NObjList::iterator it = myObjs->find(nom);
571 if (it == myObjs->end()) return;
572 (*it).second.wrsid.push_back(wrsid);
573 }
574if (fgsr) myImgApp->RestoreGraphicAtt();
575return;
576}
[165]577
[295]578/* --Methode-- */
[331]579void NamedObjMgr::DisplayImage(string& nom, string dopt)
[295]580{
581NObjMgrAdapter* obja=NULL;
582obja = GetObjAdapter(nom);
583if (obja == NULL) {
584 cout << "NamedObjMgr::DisplayImage() Error , Pas d'objet de nom " << nom << endl;
585 return;
586}
587if (!myImgApp) return;
588
589P2DArrayAdapter* arr = obja->Get2DArray(dopt);
[165]590
[295]591if (!arr) {
592 string ctyp = typeid(*(obja->GetDataObj())).name();
593 cout << "NamedObjMgr::DisplayImage() Error , Non supporte pour " << ctyp << endl;
594 return;
595 }
[165]596
[295]597int wrsid = 0;
598bool fgsr = true;
599int opt = servnobjm->DecodeDispOption(dopt, fgsr);
600wrsid = myImgApp->DispImage(arr, nom, opt);
[165]601
[295]602if(wrsid != 0) {
603 NObjList::iterator it = myObjs->find(nom);
604 if (it == myObjs->end()) return;
605 (*it).second.wrsid.push_back(wrsid);
606 }
607if (fgsr) myImgApp->RestoreGraphicAtt();
608return;
609}
610/* --Methode-- */
[331]611void NamedObjMgr::DisplaySurf3D(string& nom, string dopt)
[295]612{
613NObjMgrAdapter* obja=NULL;
614obja = GetObjAdapter(nom);
615if (obja == NULL) {
616 cout << "NamedObjMgr::DisplayImage() Error , Pas d'objet de nom " << nom << endl;
617 return;
618}
619if (!myImgApp) return;
620
621P2DArrayAdapter* arr = obja->Get2DArray(dopt);
[165]622
[295]623if (!arr) {
624 string ctyp = typeid(*(obja->GetDataObj())).name();
625 cout << "NamedObjMgr::DisplaySurf3D() Error , Non supporte pour " << ctyp << endl;
626 return;
627 }
[165]628
[295]629if ((arr->XSize() > 250) || (arr->YSize() > 250)) {
630 cout << "NamedObjMgr::DisplaySurf3D() Error , 2D-Array(" << arr->XSize()
631 << "x" << arr->YSize() << ") trop grand (max=250x250)" << endl;
632 delete arr;
633 return;
[165]634 }
635
[295]636int wrsid = 0;
637bool fgsr = true;
638int opt = servnobjm->DecodeDispOption(dopt, fgsr);
639PISurfaceDrawer* sdr = new PISurfaceDrawer(arr, true, true, true);
640wrsid = myImgApp->Disp3DDrawer(sdr, nom, opt);
641if(wrsid >= 0) {
[165]642 NObjList::iterator it = myObjs->find(nom);
643 if (it == myObjs->end()) return;
644 (*it).second.wrsid.push_back(wrsid);
645 }
[295]646
647if (fgsr) myImgApp->RestoreGraphicAtt();
648return;
[165]649}
650
651/* --Methode-- */
[331]652void NamedObjMgr::DisplayNT(string& nom, string& nmx, string& nmy, string& nmz,
[165]653 string& erx, string& ery, string& erz, string dopt)
654{
[295]655AnyDataObj* obj=GetObj(nom);
[165]656if (obj == NULL) {
657 cout << "NamedObjMgr::DisplayNT() Error , Pas d'objet de nom " << nom << endl;
658 return;
659}
660if (!myImgApp) return;
661
[326]662NTupleInterface * nt = dynamic_cast<NTupleInterface *>(obj);
663if (nt == NULL) {
664// if (typeid(*obj) != typeid(NTupleInterface)) {
[295]665 string ctyp = typeid(*obj).name();
[165]666 cout << "NamedObjMgr::DisplayNT() Error , Objet n'est pas un NTuple " << ctyp << endl;
667 return;
668 }
669
670int wrsid = 0;
671bool fgsr = true;
[326]672dopt = "defline," + dopt;
[165]673int opt = servnobjm->DecodeDispOption(dopt, fgsr);
674
675if (nmz.length()>0) { // Display 3D
[326]676 PINTuple3D* pin = new PINTuple3D(nt, false);
[165]677 pin->SelectXYZ(nmx.c_str(), nmy.c_str(), nmz.c_str());
678 pin->SelectErrBar(erx.c_str(), ery.c_str(), erz.c_str());
679 string titre = nmz + "%" + nmy + "%" + nmz;
680 wrsid = myImgApp->Disp3DDrawer(pin, nom, opt, titre);
681}
682else {
[326]683 PINTuple* pin = new PINTuple(nt, false);
[165]684 pin->SelectXY(nmx.c_str(), nmy.c_str());
685 pin->SelectErrBar(erx.c_str(), ery.c_str());
686 string titre = nmy + "%" + nmz;
687 wrsid = myImgApp->DispScDrawer( (PIDrawer*)pin, nom, opt, titre);
688 }
689
690if(wrsid >= 0) {
691 NObjList::iterator it = myObjs->find(nom);
692 if (it == myObjs->end()) return;
693 (*it).second.wrsid.push_back(wrsid);
694 }
695
696if (fgsr) myImgApp->RestoreGraphicAtt();
697return;
698}
699
700/* --Methode-- cmv 13/10/98 */
[331]701void NamedObjMgr::DisplayGFD(string& nom, string& numvarx, string& numvary, string& err, string dopt)
[165]702// Pour le display 2D ou 3D d'un ``GeneralFitData''.
703//| nom = nom de l'objet GeneralFitData a representer.
704//| numvarx = numero (nombre entier) de la 1ere variable d'abscisse.
705//| numvary = numero (nombre entier) de la 2sd variable d'abscisse (3D).
706//| Pour le display 2D, numvary="" string vide.
707//| err = qu'elles erreurs faut il representer ?
708//| - 2D : x y xy (display y=f(x))
709//| - 3D : x y z xy xz yz xzy (display z=f(x,y))
710//| Ceci n'est suivi que si on a PI_NotDefLineAtt, sinon toutes
711//| les barres d'erreurs sont representees.
712//| opt = options generales pour le display.
713{
[295]714AnyDataObj* obj=GetObj(nom);
[165]715if(obj == NULL)
716 {cout << "NamedObjMgr::DisplayGFD() Error , Pas d'objet de nom " << nom << endl;
717 return;}
718if(!myImgApp) return;
[295]719if(typeid(*obj) != typeid(GeneralFitData))
720 {string ctyp = typeid(*obj).name();
[165]721 cout<<"NamedObjMgr::DisplayGFD() Error , Objet n'est pas un GeneralFitData "<<ctyp<<endl;
722 return;}
723
724// Decodage des options classiques
725bool fgsr = true;
726int opt = servnobjm->DecodeDispOption(dopt, fgsr);
727// Decodage des erreurs a representer
728bool errx=false, erry=false, errz=false;
729if(err.length()>0) {
730 for(int i=0;i<err.length();i++)
731 if (err[i]=='x' || err[i]=='X') errx = true;
732 else if(err[i]=='y' || err[i]=='Y') erry = true;
733 else if(err[i]=='z' || err[i]=='Z') errz = true;
734}
735// Decodage des numeros de variables en abscisse
736int numvx=-1, numvy=-1;
737if(numvarx.length()>0) numvx = atoi(numvarx.c_str());
738if(numvary.length()>0) numvy = atoi(numvary.c_str());
739
740int wrsid = 0;
741if(numvy>=0) { // Display 3D
742 PIGenFitDat3D* pigfd = new PIGenFitDat3D(((GeneralFitData*)obj),false);
743 pigfd->SelectXY(numvx,numvy);
744 pigfd->SelectErrBar(errx,erry,errz);
745 wrsid = myImgApp->Disp3DDrawer(pigfd,nom,opt);
746} else { // Display 2D
747 PIGenFitDat* pigfd = new PIGenFitDat(((GeneralFitData*)obj),false);
748 pigfd->SelectX(numvx);
749 pigfd->SelectErrBar(errx,erry);
750 wrsid = myImgApp->DispScDrawer((PIDrawer*)pigfd,nom,opt);
751}
752
753if(wrsid >= 0) {
754 NObjList::iterator it = myObjs->find(nom);
755 if (it == myObjs->end()) return;
756 (*it).second.wrsid.push_back(wrsid);
757}
758if (fgsr) myImgApp->RestoreGraphicAtt();
759return;
760}
761
762/* --Methode--
[331]763void NamedObjMgr::DisplayImage(string& nom, string dopt)
[165]764{
765 cout << "NamedObjMgr::DisplayImage() a faire ! " << endl;
766}
767*/
768
769
770
771
772/* --Methode-- */
773void NamedObjMgr::SetGraphicAttributes(string gratt)
774{
775bool fg = false;
776servnobjm->DecodeDispOption(gratt, fg);
777}
778/* --Methode-- */
779void NamedObjMgr::SetGraphicWinZone(int nzx, int nzy, bool fcr)
780{
781if (!myImgApp) return;
782if (fcr) myImgApp->CreateGraphWin(nzx, nzy);
783else myImgApp->SetZone(nzx, nzy);
784}
785
786/* --Methode-- */
[331]787void NamedObjMgr::DisplayPoints2D(string& nom, string& expx, string& expy,
[165]788 string& experrx, string& experry,
789 string& expcut, string dopt)
790{
[295]791NObjMgrAdapter* obja=NULL;
792obja = GetObjAdapter(nom);
793if (obja == NULL) {
[165]794 cout << "NamedObjMgr::DisplayPoints2D() Error , Pas d'objet de nom " << nom << endl;
795 return;
[295]796 }
[165]797if (!myImgApp) return;
798
799// Creation NTuple
[179]800char* ntn[4] = {"expx","expy","expex","expey",};
[165]801NTuple* nt = NULL;
802bool haserr = false;
803
[321]804if ( (experrx.length() > 0 ) && (experry.length() > 0 ) ) { haserr = true; nt = new NTuple(4, ntn); }
[165]805else { haserr = false; experrx = experry = "0."; nt = new NTuple(2, ntn); }
806
[295]807servnobjm->Nobj_ComputeExpressions(obja, expx, expy, experrx, experry, expcut, nt, NULL, NULL);
[165]808
809if (nt->NEntry() < 1) {
810 cout << "NamedObjMgr::DisplayPoints2D() Warning Zero points satisfy cut !" << endl;
811 delete nt;
812 return;
813 }
814
815// nt->Show();
816// nt->Print(0,10);
817PINTuple* pin = new PINTuple(nt, true);
818pin->SelectXY(ntn[0], ntn[1]);
819if ( haserr ) pin->SelectErrBar(ntn[2], ntn[3]);
820
821bool fgsr = true;
[326]822dopt = "defline," + dopt;
[165]823int opt = servnobjm->DecodeDispOption(dopt, fgsr);
824string titre = nom + ":" + expy + "%" + expx;
825myImgApp->DispScDrawer( (PIDrawer*)pin, titre, opt);
826if (fgsr) myImgApp->RestoreGraphicAtt();
827return;
828}
829
830/* --Methode-- */
[331]831void NamedObjMgr::DisplayPoints3D(string& nom, string& expx, string& expy, string& expz,
[165]832 string& expcut, string dopt)
833{
[295]834NObjMgrAdapter* obja=NULL;
835obja = GetObjAdapter(nom);
836if (obja == NULL) {
[165]837 cout << "NamedObjMgr::DisplayPoints3D() Error , Pas d'objet de nom " << nom << endl;
838 return;
839 }
840if (!myImgApp) return;
841
[179]842char* ntn[3] = {"expx","expy","expz"};
[165]843NTuple* nt = new NTuple(3,ntn); // Creation NTuple
844
845string expwt = "1.";
[295]846servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expz, expwt, expcut, nt, NULL, NULL);
[165]847
848if (nt->NEntry() < 1) {
849 cout << "NamedObjMgr::DisplayPoints3D() Warning Zero points satisfy cut !" << endl;
850 delete nt;
851 return;
852 }
853nt->Show();
854nt->Print(0,10);
855PINTuple3D* pin = new PINTuple3D(nt, true);
856pin->SelectXYZ(ntn[0], ntn[1], ntn[2]);
857bool fgsr = true;
[326]858dopt = "defline," + dopt;
[165]859int opt = servnobjm->DecodeDispOption(dopt, fgsr);
860
861// Pour plot a partir de DispScDrawer
862// string nomdisp = "_NT3D_";
863// myImgApp->DispScDrawer( (PIDrawer*)pin, nomdisp, opt);
864// Pour plot a partir de Disp3DDrawer
865string titre = nom + ":" + expy + "%" + expx;
866myImgApp->Disp3DDrawer(pin, titre, opt);
867
868if (fgsr) myImgApp->RestoreGraphicAtt();
869return;
870}
871
872/* --Methode-- */
[331]873void NamedObjMgr::ProjectH1(string& nom, string& expx, string& expwt, string& expcut, string& nomh1, string dopt)
[165]874{
[295]875NObjMgrAdapter* obja=NULL;
876obja = GetObjAdapter(nom);
877if (obja == NULL) {
[165]878 cout << "NamedObjMgr::ProjectH1() Error , Pas d'objet de nom " << nom << endl;
879 return;
880 }
881if (!myImgApp) return;
882
883Histo* h1 = NULL;
884NTuple* nt = NULL;
[295]885AnyDataObj* oh = NULL;
[165]886if (nomh1.length() > 0) oh=GetObj(nomh1);
887else nomh1 = "H1Proj";
[295]888if ( (oh != NULL) && (typeid(*oh) == typeid(Histo)) ) h1 = (Histo*)oh; // Pas de remise a zero ! h1->Zero();
[165]889else {
[179]890 char* ntn[2]= {"hxval", "hwt"};
[165]891 nt = new NTuple(2,ntn); // Creation NTuple
892 }
893string expz = "0.";
[295]894servnobjm->Nobj_ComputeExpressions(obja, expx, expwt, expz, expwt, expcut, nt, h1, NULL);
[165]895
896if ((!h1) && (!nt)) return;
897if (!h1) {
898 if (nt->NEntry() < 1) {
899 cout << "NamedObjMgr::ProjectH1() Warning Zero points satisfy cut !" << endl;
900 delete nt;
901 return;
902 }
[326]903 double xmin, xmax;
[165]904 nt->GetMinMax(0, xmin, xmax);
905 h1 = new Histo(xmin, xmax, 100);
906 int k;
907 float* xn;
908 for(k=0; k<nt->NEntry(); k++) {
909 xn = nt->GetVec(k);
910 h1->Add(xn[0], xn[1]);
911 }
912 delete nt;
913 AddObj(h1, nomh1);
914 }
915
916DisplayObj(nomh1, dopt);
917return;
918}
919
920/* --Methode-- */
[331]921void NamedObjMgr::ProjectH2(string& nom, string& expx, string& expy, string& expwt, string& expcut,
[165]922 string& nomh2, string dopt)
923{
[295]924NObjMgrAdapter* obja=NULL;
925obja = GetObjAdapter(nom);
926if (obja == NULL) {
[165]927 cout << "NamedObjMgr::ProjectH2() Error , Pas d'objet de nom " << nom << endl;
928 return;
929 }
930if (!myImgApp) return;
931
932Histo2D* h2 = NULL;
933NTuple* nt = NULL;
[295]934AnyDataObj* oh = NULL;
[165]935if (nomh2.length() > 0) oh=GetObj(nomh2);
936else nomh2 = "H2Proj";
[295]937if ( (oh != NULL) && (typeid(*oh) == typeid(Histo2D)) ) h2 = (Histo2D*)oh; // Pas de remise a zero ! h2->Zero();
[165]938else {
[179]939 char* ntn[3]= {"hxval", "hyval", "hwt"};
[165]940 nt = new NTuple(3,ntn); // Creation NTuple
941 }
942string expz = "0.";
[295]943servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, nt, NULL, h2);
[165]944
945if ((!h2) && (!nt)) return;
946if (!h2) {
947 if (nt->NEntry() < 1) {
948 cout << "NamedObjMgr::ProjectH2() Warning Zero points satisfy cut !" << endl;
949 delete nt;
950 return;
951 }
[326]952 double xmin, xmax, ymin, ymax;
[165]953 nt->GetMinMax(0, xmin, xmax);
954 nt->GetMinMax(0, ymin, ymax);
955 h2 = new Histo2D(xmin, xmax, 50, ymin, ymax, 50);
956 int k;
957 float* xn;
958 for(k=0; k<nt->NEntry(); k++) {
959 xn = nt->GetVec(k);
960 h2->Add(xn[0], xn[1], xn[2]);
961 }
962 delete nt;
963 AddObj(h2, nomh2);
964 }
965
966DisplayObj(nomh2, dopt);
967return;
968
969}
970
971/* --Methode-- cmv 13/10/98 */
[331]972void NamedObjMgr::ProjectHProf(string& nom, string& expx, string& expy, string& expwt, string& expcut,
[165]973 string& nomprof, string dopt)
974// Pour remplir un ``GeneralFitData'' a partir de divers objets:
975//| nom = nom de l'objet a projeter dans un HProf.
976//| expx = expression X de definition du bin.
977//| expy = expression Y a additionner dans le bin.
978//| expwt = expression W du poids a additionner.
979//| expcut = expression du test de selection.
980//| nomprof = nom du HProf engendre (optionnel). Si l'objet n'existe pas
981//| les limites Xmin,Xmax sont calculees automatiquement.
982//| sinon ce sont celles de l'objet preexistant.
983//| opt = options generales pour le display.
984{
[295]985NObjMgrAdapter* obja=NULL;
986obja = GetObjAdapter(nom);
987if (obja == NULL) {
[165]988 cout << "NamedObjMgr::ProjectHProf() Error , Pas d'objet de nom " << nom << endl;
989 return;
990 }
991if (!myImgApp) return;
992
993HProf* hprof = NULL;
994NTuple* nt = NULL;
[295]995AnyDataObj* oh = NULL;
[165]996if (nomprof.length() > 0) oh=GetObj(nomprof);
997else nomprof = "ProfProj";
[295]998if( (oh!=NULL) && (typeid(*oh) == typeid(HProf)) ) hprof = (HProf*)oh;
[165]999else {
[179]1000 char* ntn[3]= {"hxval", "hyval", "hwt"};
[165]1001 nt = new NTuple(3,ntn); // Creation NTuple
1002}
1003string expz = "0.";
[295]1004servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, nt, NULL, NULL, hprof);
[165]1005
1006if((!hprof) && (!nt)) return;
1007if(!hprof) {
1008 if (nt->NEntry() < 1) {
1009 cout << "NamedObjMgr::ProjectHProf() Warning Zero points satisfy cut !" << endl;
1010 delete nt;
1011 return;
1012 }
[326]1013 double xmin, xmax;
[165]1014 nt->GetMinMax(0, xmin, xmax);
1015 hprof = new HProf(xmin, xmax, 100);
1016 int k;
1017 float* xn;
1018 for(k=0; k<nt->NEntry(); k++) {
1019 xn = nt->GetVec(k);
1020 hprof->Add(xn[0], xn[1], xn[2]);
1021 }
1022 delete nt;
1023 AddObj(hprof, nomprof);
1024 }
1025hprof->UpdateHisto();
1026
1027DisplayObj(nomprof, dopt);
1028return;
1029}
1030
1031/* --Methode-- */
[331]1032void NamedObjMgr::FillVect(string& nom, string& expx, string& expcut, string& nomvec, string dopt)
[165]1033{
[295]1034NObjMgrAdapter* obja=NULL;
1035obja = GetObjAdapter(nom);
1036if (obja == NULL) {
[165]1037 cout << "NamedObjMgr::FillVect() Error , Pas d'objet de nom " << nom << endl;
1038 return;
1039 }
1040if (!myImgApp) return;
1041
1042NTuple* nt = NULL;
1043if (nomvec.length() < 1) nomvec = "VecFill";
1044
[179]1045char* ntn[2]= {"vecval", "vecwt"};
[165]1046nt = new NTuple(1,ntn); // Creation NTuple
1047
1048string expwt = "1.";
1049string expz = "0.";
[295]1050servnobjm->Nobj_ComputeExpressions(obja, expx, expz, expz, expwt, expcut, nt, NULL, NULL);
[165]1051
1052if (!nt) return;
1053if (nt->NEntry() < 1) {
1054 cout << "NamedObjMgr::FillVect() Warning Zero points satisfy cut !" << endl;
1055 delete nt;
1056 return;
1057 }
1058
1059Vector* vec = new Vector(nt->NEntry());
1060int k;
1061float* xn;
1062for(k=0; k<nt->NEntry(); k++) {
1063 xn = nt->GetVec(k);
1064 (*vec)(k) = xn[0];
1065 }
1066delete nt;
1067AddObj(vec, nomvec);
1068DisplayObj(nomvec, dopt);
1069return;
1070}
1071
1072/* --Methode-- */
[331]1073void NamedObjMgr::FillNT(string& nom, string& expx, string& expy, string& expz, string& expt,
[165]1074 string& expcut, string& nomnt)
1075{
[295]1076NObjMgrAdapter* obja=NULL;
1077obja = GetObjAdapter(nom);
1078if (obja == NULL) {
[165]1079 cout << "NamedObjMgr::FillNT() Error , Pas d'objet de nom " << nom << endl;
1080 return;
1081 }
1082if (!myImgApp) return;
1083
1084bool fgnnt = false;
1085NTuple* nt = NULL;
[295]1086AnyDataObj* oh = NULL;
[165]1087if (nomnt.length() > 0) oh=GetObj(nomnt);
1088else nomnt = "NTFill";
[295]1089if ( (oh != NULL) && (typeid(*oh) == typeid(HProf)) ) {
[165]1090 nt = (NTuple*)oh;
1091 if (nt->NVar() > 10) {
1092 cout << "NamedObjMgr::FillNT() Warning , Max 10 var ds NTuple -> new NTuple" << endl;
1093 nt = NULL;
1094 }
1095 }
1096if (nt == NULL) {
[179]1097 char* ntn[4]= {"x", "y","z","t"};
[165]1098 nt = new NTuple(4,ntn); // Creation NTuple
1099 fgnnt = true;
1100 }
1101
[295]1102servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expz, expt, expcut, nt, NULL, NULL);
[165]1103
1104if (fgnnt) AddObj(nt, nomnt);
1105return;
1106
1107}
1108
1109/* --Methode-- cmv 13/10/98 */
[331]1110void NamedObjMgr::FillGFD(string& nom, string& expx, string& expy, string& expz,
[165]1111 string& experr, string& expcut, string& nomgfd)
1112// Pour remplir un ``GeneralFitData'' a partir de divers objets:
1113//| nom = nom de l'objet a transcrire selon 1D: Z=f(X) ou 2D: Z=f(X,Y) .
1114//| Vector,Matrix,Histo,HProf,Histo2D,Image<T>,StarList,NTuple,GeneralFitData
1115//| expx = expression X du GeneralFitData (1er abscisse)
1116//| expy = expression Y du GeneralFitData (2sd abscisse si non "", Z=f(X,Y))
1117//| expz = expression Z du GeneralFitData (valeur de l'ordonnee)
1118//| experr = expression de l'erreur sur l'ordonnee Z
1119//| expcut = expression du test de selection
1120//| nomgfd = nom du GeneralFitData engendre (optionnel)
1121{
[295]1122NObjMgrAdapter* obja=NULL;
1123obja = GetObjAdapter(nom);
1124if (obja == NULL) {
1125 cout << "NamedObjMgr::FillGFD() Error , Pas d'objet de nom "<<nom<<endl;
1126 return;
1127 }
[165]1128if(!myImgApp) return;
1129
1130// 2D ou 3D?
1131int nvar = 2;
1132if(expy.length()<=0) {nvar = 1; expy = "0.";}
1133
[295]1134// $CHECK$ - cmv calculait le nombre d'entree ndata
1135// en fonction de chaque objet - Je l'ai vire Reza 11/05/99
[165]1136
1137// Creation NTuple Buffer
[179]1138char* ntn[4]= {"x","y","f","e"};
[165]1139NTuple*nt = new NTuple(4,ntn);
1140
1141// Remplissage NTuple buffer
[295]1142servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expz, experr, expcut, nt, NULL, NULL);
[165]1143if(nt->NEntry() < 1)
1144 {cout<<"NamedObjMgr::FillGFD() Warning Zero points satisfy cut !"<<endl;
1145 delete nt; return;}
1146
1147//Remplissage de la structure GeneraFitData
[295]1148if (nt->NEntry() <= 0) {
1149 cout<<"NamedObjMgr::FillGFD() Warning - NData= " << nt->NEntry() << endl;
1150 delete nt;
1151 return;
1152 }
1153
1154GeneralFitData* gfd = new GeneralFitData(nvar,nt->NEntry(),0);
[165]1155int k;
1156float* xn;
1157for(k=0; k<nt->NEntry(); k++) {
1158 xn = nt->GetVec(k);
1159 gfd->AddData(xn,xn[2],xn[3]);
1160}
1161
1162// Menage et table d'objets
1163delete nt;
1164AddObj(gfd, nomgfd);
1165return;
1166}
1167
1168
1169///////////////////// Fit 1D et 2D //////////////////////////
1170/* --Function static propre aux routines de fit 1D et 2D-- cmv 13/10/98 */
1171struct DFOptions {
1172 int okres, okfun;
1173 int polcx,polcy; double xc,yc;
1174 double err_e, err_E;
1175 double stc2;
1176 int lp,lpg;
1177 int i1,i2,j1,j2;
1178};
1179typedef struct DFOptions DFOPTIONS;
1180static void DecodeFitsOptions(string par,string step,string min,string max,string opt
1181 ,Vector& Par,Vector& Step,Vector& Min,Vector& Max,DFOPTIONS& O);
1182void DecodeFitsOptions(string par,string step,string min,string max,string opt
1183 ,Vector& Par,Vector& Step,Vector& Min,Vector& Max,DFOPTIONS& O)
1184//| Pour decoder les "string" et remplir les vecteurs du fit (cf commentaires dans Fit1D)
1185{
1186// set des vecteurs et decodage des string correspondantes
1187int NParMax = 100;
1188Par.Realloc(NParMax); Step.Realloc(NParMax);
1189Min.Realloc(NParMax); Max.Realloc(NParMax);
1190{
1191 Vector* v; string* s;
1192 {for(int i=0;i<NParMax;i++) {Par(i)=0.; Step(i)=1.; Min(i)=1.; Max(i)=-1.;}}
1193 for(int j=0;j<4;j++) {
1194 if(j==0) {v=&Par; s=&par;}
1195 else if(j==1) {v=&Step; s=&step;}
1196 else if(j==2) {v=&Min; s=&min;}
1197 else if(j==3) {v=&Max; s=&max;}
1198 if(s->length()>0) *s += ",";
1199 for(int i=0;i<NParMax;i++) {
1200 if(s->length()<=0) break;
1201 sscanf(s->c_str(),"%lf",&(*v)(i));
1202 size_t p = s->find_first_of(',') + 1;
1203 if(p>=s->length()) *s = ""; else *s = s->substr(p);
1204 }
1205 }
1206}
1207
1208// Decodage de options de opt
1209O.okres = O.okfun = 0;
1210O.polcx = O.polcy = 0;
1211O.xc = O.yc = 0.;
1212O.stc2 = 1.e-3;
1213O.err_e = O.err_E = -1.;
1214O.lp = 1; O.lpg = 0;
1215O.i1 = O.j1 = O.i2 = O.j2 = -1;
1216
1217if(opt.length()<=0) return;
1218opt = "," + opt + ",";
1219
1220if(strstr(opt.c_str(),",r,")) O.okres = 1; // residus
1221if(strstr(opt.c_str(),",f,")) O.okfun = 1; // fonction fittee
1222if(strstr(opt.c_str(),",x")) { // Demande de centrage (fit X=x-xc)
1223 O.polcx = 2; // Le centrage est calcule automatiquement
1224 size_t p = opt.find(",x");
1225 size_t q = opt.find_first_of(',',p+1);
1226 string dum = opt.substr(p,q-p);
1227 if(dum.length()>2) {
1228 sscanf(dum.c_str(),",x%lf",&O.xc);
1229 O.polcx = 1; // Le centrage est fixe par la valeur lue
1230 }
1231}
1232if(strstr(opt.c_str(),",y")) { // Demande de centrage (fit Y=y-yc)
1233 O.polcy = 2; // Le centrage est calcule automatiquement
1234 size_t p = opt.find(",y");
1235 size_t q = opt.find_first_of(',',p+1);
1236 string dum = opt.substr(p,q-p);
1237 if(dum.length()>2) {
1238 sscanf(dum.c_str(),",y%lf",&O.yc);
1239 O.polcy = 1; // Le centrage est fixe par la valeur lue
1240 }
1241}
1242if(strstr(opt.c_str(),",E")) { // Erreurs imposees a "sqrt(val)" ou "aa.b*sqrt(val)"
1243 size_t p = opt.find(",E");
1244 size_t q = opt.find_first_of(',',p+1);
1245 string dum = opt.substr(p,q-p);
1246 if(dum.length()>2) sscanf(dum.c_str(),",E%lf",&O.err_E);
1247 if(O.err_E<=0.) O.err_E = 1.;
1248 O.err_e=-1.;
1249}
1250if(strstr(opt.c_str(),",e")) { // Erreurs imposees a "1" ou "aa.b"
1251 size_t p = opt.find(",e");
1252 size_t q = opt.find_first_of(',',p+1);
1253 string dum = opt.substr(p,q-p);
1254 if(dum.length()>2) sscanf(dum.c_str(),",e%lf",&O.err_e);
1255 if(O.err_e<=0.) O.err_e = 1.;
1256 O.err_E=-1.;
1257}
1258if(strstr(opt.c_str(),",X")) { // Valeur du StopChi2
1259 size_t p = opt.find(",X");
1260 size_t q = opt.find_first_of(',',p+1);
1261 string dum = opt.substr(p,q-p);
1262 if(dum.length()>2) sscanf(dum.c_str(),",X%lf",&O.stc2);
1263 if(O.stc2<=0.) O.stc2 = 1.e-3;
1264}
1265if(strstr(opt.c_str(),",l")) { // niveau de print
1266 size_t p = opt.find(",l");
1267 size_t q = opt.find_first_of(',',p+1);
1268 string dum = opt.substr(p,q-p);
1269 float ab;
1270 if(dum.length()>2) sscanf(dum.c_str(),",l%f",&ab);
1271 if(ab<0) ab = 0.;
1272 O.lp = (int) ab; O.lpg = (int) 10.*(ab-O.lp);
1273}
1274if(strstr(opt.c_str(),",I")) { // intervalle de fit selon X
1275 size_t p = opt.find(",I");
1276 size_t q = opt.find_first_of(',',p+1);
1277 string dum = opt.substr(p,q-p);
1278 if(dum.length()>2) sscanf(dum.c_str(),",I%d/%d",&O.i1,&O.i2);
1279}
1280if(strstr(opt.c_str(),",J")) { // intervalle de fit selon Y
1281 size_t p = opt.find(",J");
1282 size_t q = opt.find_first_of(',',p+1);
1283 string dum = opt.substr(p,q-p);
1284 if(dum.length()>2) sscanf(dum.c_str(),",J%d/%d",&O.j1,&O.j2);
1285}
1286return;
1287}
1288
1289/* --Methode-- cmv 13/10/98 */
[331]1290void NamedObjMgr:: Fit12D(string& nom, string& func,
[165]1291 string par,string step,string min,string max,
1292 string opt)
1293//| --------------- Fit d'objets a 1 et 2 dimensions ---------------
1294//| nom : nom de l'objet qui peut etre:
1295//| fit-1D: Vector,Histo1D,HProf ou GeneraFitData(1D)
1296//| fit-2D: Matrix,Histo2D,Imagexx ou GeneraFitData(2D)
1297//| func : pnn = fit polynome degre nn avec classe Poly (lineaire) 1D ou 2D
1298//| : Pnn = fit polynome degre nn avec GeneralFit (non-lineaire) 1D ou 2D
1299//| : gnn = fit gaussienne (hauteur) + polynome de degre nn 1D
1300//| : g = fit gaussienne (hauteur) 1D
1301//| : enn = fit exponentielle + polynome de degre nn 1D
1302//| : e = fit exponentielle 1D
1303//| : Gnn = fit gaussienne (volume) + polynome de degre nn 1D
1304//| : G = fit gaussienne (volume) 1D
1305//| : = fit gaussienne+fond (volume) 2D
1306//| : Gi = fit gaussienne+fond integree (volume) 2D
1307//| : d = fit DL de gaussienne+fond (volume) 2D
1308//| : di = fit DL de gaussienne+fond integree (volume) 2D
1309//| : D = fit DL de gaussienne+fond avec coeff variable p6 (volume) 2D
1310//| : Di = fit DL de gaussienne+fond integree avec coeff variable p6 (volume) 2D
1311//| : M = fit Moffat+fond (expos=p6) (volume) 2D
1312//| : Mi = fit Moffat+fond integree (expos=p6) (volume) 2D
1313//| par : p1,...,pn valeur d'initialisation des parametres (def=0)
1314//| step : s1,...,sn valeur des steps de depart (def=1)
1315//| min : m1,...,mn valeur des minima (def=1)
1316//| max : M1,...,Mn valeur des maxima (def=-1) (max<=min : pas de limite)
1317//| opt : options "Eaa.b,eaa.b,f,r,caa.b,Xaa.b"
1318//| f = generation d'un Objet identique contenant la fonction fittee
1319//| r = generation d'un Objet identique contenant les residus
1320//| Xaa.b = aa.b valeur du DXi2 d'arret (def=1.e-3)
1321//| la.b = niveau "a.b" de print: a=niveau de print Fit1/2D
1322//| b=niveau de debug GeneralFit
1323//| Ii1/i2 numeros des bins X de l'histos utilises pour le fit [i1,i2]
1324//|2D Jj1/j2 numeros des bins Y de l'histos utilises pour le fit [j1,j2]
1325//| - L'erreur est celle associee a l'objet si existe, 1 sinon sauf si
1326//| E = erreur prise comme la racine de la valeur a fitter
1327//| Eaa.b = erreur prise aa.b*sqrt(val)
1328//| e = erreur prise egale a 1 pour toutes les valeurs
1329//| eaa.b = erreur prise egale a aa.b
1330//| xaa.b = demande de centrage: on fit x-aa.b au lieu de x)
1331//| x = demande de centrage: on fit x-xc au lieu de x
1332//| avec xc=abscisse du milieu de l'histogramme
1333//| Actif pour: exp+poly 1D, poly 1D
1334//| gauss+poly 1D (mais xc est le centre de la gaussienne)
1335//|2D yaa.b et y = idem "xaa.b et x" mais pour y
1336{
[295]1337AnyDataObj* obj=GetObj(nom);
[165]1338if (obj == NULL) {
1339 cout<<"NamedObjMgr::Fit12D() Error , Pas d'objet de nom "<<nom<<endl;
1340 return;
1341}
1342if (!myImgApp) return;
1343if(func.length()<=0)
1344 {cout<<"NamedObjMgr::Fit12D() Donnez un nom de fonction a fitter."<<endl;
1345 return;}
[295]1346string ctyp = typeid(*obj).name();
[165]1347
1348int ndim = 0, nbinx=0, nbiny=0, ndata = 0;
1349Vector* v = NULL; Histo* h = NULL;
1350Matrix* m = NULL; Histo2D* h2 = NULL; RzImage* im = NULL;
1351GeneralFitData* g = NULL;
[295]1352
[165]1353 // 1D
[295]1354if (typeid(*obj) == typeid(Vector)) {
1355 ndim = 1;
1356 v = (Vector*) obj; nbinx = v->NElts(); nbiny = 1;
1357 }
1358else if ( (typeid(*obj) == typeid(HProf)) || (typeid(*obj) == typeid(Histo)) ) {
1359 ndim = 1;
1360 h = (Histo*) obj; nbinx = h->NBins(); nbiny = 1;
1361 }
1362else if (typeid(*obj) == typeid(Matrix)) {
1363 ndim = 2;
1364 m = (Matrix*) obj; nbinx = m->NCol(); nbiny = m->NRows();
1365 }
1366else if (typeid(*obj) == typeid(Histo2D)) {
1367 ndim = 2;
1368 h2 = (Histo2D*) obj; nbinx = h2->NBinX(); nbiny = h2->NBinY();
1369 }
1370else if (typeid(*obj) == typeid(GeneralFitData)) {
1371 g = (GeneralFitData*) obj; nbinx = g->NData(); nbiny = 1;
1372 if( g->NVar()==1) ndim = 1;
1373 else if(g->NVar()==2) ndim = 2;
1374 else {
1375 cout<<"GeneralFitData ne peut avoir que 1 ou 2 variables d'abscisse: "
1376 <<((GeneralFitData*) obj)->NVar()<<endl; return; }
1377 }
1378else if (dynamic_cast<RzImage*>(obj)) {
1379 ndim = 2;
1380 im = (RzImage*) obj; nbinx = im->XSize(); nbiny = im->YSize();
1381 }
1382else {
1383 cout<<"NamedObjMgr::Fit12D() Error , Objet n'est pas un "
1384 <<"Histo1D/HProf/Vector/Histo2D/Image/Matrix/GeneralFitData "<<ctyp<<endl;
1385 return;
1386 }
1387
[165]1388ndata = nbinx*nbiny;
1389if(ndata<=0)
1390 {cout<<"L'objet a "<<nbinx<<","<<nbiny<<" bins ("<<ndata<<")"<<endl; return;}
1391
1392// Decodage des options et des parametres, mise en forme
1393Vector Par(1); Vector Step(1); Vector Min(1); Vector Max(1); DFOPTIONS O;
1394DecodeFitsOptions(par,step,min,max,opt,Par,Step,Min,Max,O);
1395O.i1 = (O.i1<0||O.i1>=nbinx)? 0: O.i1;
1396O.i2 = (O.i2<0||O.i2>=nbinx||O.i2<O.i1)? nbinx-1: O.i2;
1397if(ndim>=2) {
1398 O.j1 = (O.j1<0||O.j1>=nbiny)? 0: O.j1;
1399 O.j2 = (O.j2<0||O.j2>=nbiny||O.j2<O.j1)? nbiny-1: O.j2;
1400} else O.j2 = O.j1 = 0;
1401if(O.polcx==2) {
1402 if(v||m) O.xc = (O.i2-O.i1+1)/2.;
1403 else if(h) O.xc = (h->XMin()+h->XMax())/2.;
1404 else if(h2) O.xc = (h2->XMin()+h2->XMax())/2.;
1405 else if(g) {double mini,maxi; g->GetMinMax(2,mini,maxi); O.xc=(mini+maxi)/2.;}
1406 else if(im) {O.xc = im->XOrg() * im->XPxSize()*(O.i2-O.i1+1)/2.;}
1407}
1408if(O.polcy==2 && ndim>=2) {
1409 if(m) O.yc = (O.j2-O.j1+1)/2.;
1410 if(h2) O.yc = (h2->YMin()+h2->YMax())/2.;
1411 if(g) {double mini,maxi; g->GetMinMax(12,mini,maxi); O.yc=(mini+maxi)/2.;}
1412 if(im) {O.yc = im->YOrg() * im->YPxSize()*(O.j2-O.j1+1)/2.;}
1413}
1414if(O.lp>0)
1415 cout<<"Fit["<<nbinx<<","<<nbiny<<"] ("<<ndata<<") dim="<<ndim<<":"
1416 <<" Int=["<<O.i1<<","<<O.i2<<"],["<<O.j1<<","<<O.j2<<"]"<<endl
1417 <<" Cent="<<O.polcx<<","<<O.polcy<<","<<O.xc<<"+x"<<","<<O.yc<<"+y"
1418 <<" TypE="<<O.err_e<<","<<O.err_E
1419 <<" StpX2="<<O.stc2
1420 <<" lp,lpg="<<O.lp<<","<<O.lpg<<endl;
1421
1422///////////////////////////////////
1423// Remplissage de GeneralFitData //
1424///////////////////////////////////
1425GeneralFitData mydata(ndim,ndata,0);
1426{for(int i=O.i1;i<=O.i2;i++) for(int j=O.j1;j<=O.j2;j++) {
1427 double x,y,f,e;
1428
1429 if(v)
1430 {x= (double) i; f=(*v)(i); e=1.;}
1431 else if(h)
1432 {x=h->BinCenter(i); f=(*h)(i); e=(h->HasErrors())?h->Error(i):1.;}
1433 else if(m)
1434 {x=(double) i; y=(double) j; f=(*m)(j,i); e=1.;}
1435 else if(h2)
1436 {float xf,yf; h2->BinCenter(i,j,xf,yf); x=(double)xf; y=(double)yf;
1437 f=(*h2)(i,j); e=(h2->HasErrors())?h2->Error(i,j):1.;}
1438 else if(im)
1439 {x=im->XOrg()+(i+0.5)*im->XPxSize(); y=im->YOrg()+(j+0.5)*im->YPxSize();
1440 f=im->DValue(i,j); e=1.;}
1441 else if(g&&ndim==1) {x= g->X(i); f=g->Val(i); e=g->EVal(i);}
1442 else if(g&&ndim==2) {x= g->X(i); y= g->Y(i); f=g->Val(i); e=g->EVal(i);}
1443 else x=y=f=e=0.;
1444
1445 // Gestion des erreurs a utiliser
1446 if(O.err_e>0.) e=O.err_e;
1447 else if(O.err_E>0.) {e=(y<-1.||y>1.)?O.err_E*sqrt(fabs(y)):O.err_E;}
1448
1449 // Remplissage de generalfit
1450 if(func[0]=='p') {x -= O.xc; if(ndim>=2) y -= O.yc;}
1451 if(ndim==1) mydata.AddData1(x,f,e);
1452 else if(ndim==2) mydata.AddData2(x,y,f,e);
1453}}
1454if(mydata.NData()<=0)
1455 {cout<<"Pas de donnees dans GeneralFitData: "<<mydata.NData()<<endl;
1456 return;}
1457if(O.lpg>1) {
1458 mydata.PrintStatus();
1459 mydata.PrintData(0);
1460 mydata.PrintData(mydata.NData()-1);
1461}
1462
1463////////////////////////////////////////////
1464// Identification de la fonction a fitter //
1465////////////////////////////////////////////
1466GeneralFunction* myfunc = NULL;
1467if(func[0]=='p' && ndim==1) {
1468 // Fit de polynome sans passer par les GeneralFit
1469 int degre = 0;
1470 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1471 cout<<"Fit (lineaire) 1D polynome de degre "<<degre<<endl;
1472 Poly p1(0);
1473 double c2rl = mydata.PolFit(0,p1,degre);
1474 cout<<"C2r_lineaire = "<<c2rl<<endl;
1475 if(O.lp>0) cout<<p1<<endl;
1476 return;
1477
1478} else if(func[0]=='P' && ndim==1) {
1479 // Fit de polynome
1480 int degre = 0;
1481 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1482 cout<<"Fit polynome 1D de degre "<<degre<<endl;
1483 Polyn1D* myf = new Polyn1D(degre,O.xc);
1484 myfunc = myf;
1485
1486} else if(func[0]=='e' && ndim==1) {
1487 // Fit d'exponentielle
1488 int degre =-1;
1489 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1490 cout<<"Fit d'exponentielle+polynome 1D de degre "<<degre<<endl;
1491 Exp1DPol* myf;
1492 if(degre>=0) myf = new Exp1DPol((unsigned int)degre,O.xc);
1493 else myf = new Exp1DPol(O.xc);
1494 myfunc = myf;
1495
1496} else if(func[0]=='g' && ndim==1) {
1497 // Fit de gaussienne en hauteur
1498 int degre =-1;
1499 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1500 cout<<"Fit de Gaussienne_en_hauteur+polynome 1D de degre "<<degre<<endl;
1501 Gauss1DPol* myf;
1502 if(degre>=0) myf = new Gauss1DPol((unsigned int)degre,((O.polcx)?true:false));
[179]1503 else { bool bfg = (O.polcx)?true:false; myf = new Gauss1DPol(bfg); }
[165]1504 myfunc = myf;
1505
1506} else if(func[0]=='G' && ndim==1) {
1507 // Fit de gaussienne en volume
1508 int degre =-1;
1509 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1510 cout<<"Fit de Gaussienne_en_volume+polynome 1D de degre "<<degre<<endl;
1511 GaussN1DPol* myf;
1512 if(degre>=0) myf = new GaussN1DPol((unsigned int)degre,((O.polcx)?true:false));
[179]1513 else { bool bfg = (O.polcx)?true:false; myf = new GaussN1DPol(bfg); }
[165]1514 myfunc = myf;
1515
1516} else if(func[0]=='p' && ndim==2) {
1517 // Fit de polynome 2D sans passer par les GeneralFit
1518 int degre = 0;
1519 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1520 cout<<"Fit (lineaire) polynome 2D de degre "<<degre<<endl;
1521 Poly2 p2(0);
1522 double c2rl = mydata.PolFit(0,1,p2,degre);
1523 cout<<"C2r_lineaire = "<<c2rl<<endl;
1524 if(O.lp>0) cout<<p2<<endl;
1525 return;
1526
1527} else if(func[0]=='P' && ndim==2) {
1528 // Fit de polynome 2D
1529 int degre = 0;
1530 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1531 cout<<"Fit polynome 2D de degre "<<degre<<endl;
1532 Polyn2D* myf = new Polyn2D(degre,O.xc,O.yc);
1533 myfunc = myf;
1534
1535} else if(func[0]=='G' && ndim==2) {
1536 // Fit de gaussienne+fond en volume
1537 int integ = 0;
1538 if(func.length()>1) if(func[1]=='i') integ=1;
1539 cout<<"Fit de Gaussienne+Fond 2D integ="<<integ<<endl;
1540 if(integ) {GauRhInt2D* myf = new GauRhInt2D; myfunc = myf;}
1541 else {GauRho2D* myf = new GauRho2D; myfunc = myf;}
1542
1543} else if(func[0]=='d' && ndim==2) {
1544 // Fit de DL gaussienne+fond en volume
1545 int integ = 0;
1546 if(func.length()>1) if(func[1]=='i') integ=1;
1547 cout<<"Fit de DL de Gaussienne+Fond 2D integ="<<integ<<endl;
1548 if(integ) {GdlRhInt2D* myf = new GdlRhInt2D; myfunc = myf;}
1549 else {GdlRho2D* myf = new GdlRho2D; myfunc = myf;}
1550
1551} else if(func[0]=='D' && ndim==2) {
1552 // Fit de DL gaussienne+fond avec coeff variable p6 en volume
1553 int integ = 0;
1554 if(func.length()>1) if(func[1]=='i') integ=1;
1555 cout<<"Fit de DL de Gaussienne+Fond avec coeff variable (p6) 2D integ="<<integ<<endl;
1556 if(integ) {Gdl1RhInt2D* myf = new Gdl1RhInt2D; myfunc = myf;}
1557 else {Gdl1Rho2D* myf = new Gdl1Rho2D; myfunc = myf;}
1558
1559} else if(func[0]=='M' && ndim==2) {
1560 // Fit de Moffat+fond (volume)
1561 int integ = 0;
1562 if(func.length()>1) if(func[1]=='i') integ=1;
1563 cout<<"Fit de Moffat+Fond (expos=p6) 2D integ="<<integ<<endl;
1564 if(integ) {MofRhInt2D* myf = new MofRhInt2D; myfunc = myf;}
1565 else {MofRho2D* myf = new MofRho2D; myfunc = myf;}
1566
1567} else {
1568 cout<<"Fonction "<<func<<" inconnue pour la dim "<<ndim<<endl;
1569 return;
1570}
1571
1572/////////////////////////
1573// Fit avec generalfit //
1574/////////////////////////
1575if(myfunc->NPar()>Par.NElts())
1576 {cout<<"Trop de parametres: "<<myfunc->NPar()<<">"<<Par.NElts()<<endl;
1577 if(myfunc) delete myfunc; return;}
1578GeneralFit myfit(myfunc);
1579myfit.SetDebug(O.lpg);
1580myfit.SetData(&mydata);
1581myfit.SetStopChi2(O.stc2);
1582{for(int i=0;i<myfunc->NPar();i++) {
1583 char str[10];
1584 sprintf(str,"P%d",i);
1585 myfit.SetParam(i,str,Par(i),Step(i),Min(i),Max(i));
1586}}
1587if(O.lp>1) myfit.PrintFit();
1588double c2r = (double) myfit.Fit();
1589if(O.lp>0) myfit.PrintFit();
1590if(c2r>0.) {
1591 c2r = myfit.GetChi2Red();
1592 cout<<"C2r_Reduit = "<<c2r<<" nstep="<<myfit.GetNStep()<<endl;
1593 Vector ParFit(myfunc->NPar());
1594 for(int i=0;i<myfunc->NPar();i++) ParFit(i)=myfit.GetParm(i);
1595} else {
1596 cout<<"echec Fit, rc = "<<c2r<<" nstep="<<myfit.GetNStep()<<endl;
1597}
1598
1599// Mise a disposition des resultats
1600if(c2r>=0. && myfunc && (O.okres>0||O.okfun>0)) {
1601 string nomres = nom + "res";
1602 string nomfun = nom + "fun";
1603 if(v) {
1604 if(O.okres) {Vector* ob = v->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1605 if(O.okfun) {Vector* ob = v->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1606 } else if(h) {
1607 if(O.okres) {Histo* ob = h->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1608 if(O.okfun) {Histo* ob = h->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1609 } else if(m) {
1610 if(O.okres) {Matrix* ob = m->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1611 if(O.okfun) {Matrix* ob = m->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1612 } else if(h2) {
1613 if(O.okres) {Histo2D* ob = h2->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1614 if(O.okfun) {Histo2D* ob = h2->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1615 } else if(im) {
1616 if(O.okres) {RzImage* ob = im->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1617 if(O.okfun) {RzImage* ob = im->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1618 } else if(g) {
1619 if(O.okres) {GeneralFitData* ob = g->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1620 if(O.okfun) {GeneralFitData* ob = g->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1621 }
1622}
1623
1624// Nettoyage
1625if(myfunc) delete myfunc;
1626return;
1627}
1628
Note: See TracBrowser for help on using the repository browser.