source: Sophya/trunk/SophyaPI/PIext/servnobjm.cc@ 3370

Last change on this file since 3370 was 3370, checked in by ansari, 18 years ago

Adaptation pour redirection stdout/err a travers un fichier ds PIATmpxxx/, Reza 31/10/2007

File size: 42.7 KB
RevLine 
[165]1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4
[295]5#include <typeinfo>
[2322]6#include <iostream>
[165]7#include <string>
8#include <list>
9#include <map>
10
[2615]11#include "sopnamsp.h"
[165]12#include "strutil.h"
13
14#include "nobjmgr.h"
15#include "servnobjm.h"
[330]16#include "nomgadapter.h"
[165]17#include "pistdimgapp.h"
18
[333]19#include "fct1dfit.h"
20#include "fct2dfit.h"
21
[544]22#ifdef SANS_EVOLPLANCK
[333]23#include "matrix.h"
24#include "cvector.h"
[544]25#else
26#include "tmatrix.h"
27#include "tvector.h"
[2922]28#include "matharr.h"
[584]29#include "pitvmaad.h"
[544]30#endif
31
[333]32#include "ntuple.h"
33#include "cimage.h"
34
[165]35#include "histos.h"
36#include "histos2.h"
[2792]37#include "hisprof.h"
[165]38#include "ntuple.h"
[2792]39#include "datatable.h"
[165]40
[1905]41#include "piyfxdrw.h"
[326]42#include "pisurfdr.h"
[333]43
44#include "pintuple.h"
45#include "pintup3d.h"
46
[326]47#include "pipodrw.h"
[165]48
49
[326]50
[165]51/* --Methode-- */
[2491]52Services2NObjMgr::Services2NObjMgr(string& tmpdir)
[165]53{
[1276]54SetTmpDir(tmpdir);
[333]55mImgapp = NULL;
[2491]56mOmg = NULL;
[171]57dynlink = NULL;
[165]58}
59
60/* --Methode-- */
61Services2NObjMgr::~Services2NObjMgr()
62{
63CloseDLL();
[2491]64if (mOmg) delete mOmg;
[165]65}
66
67/* --Methode-- */
[295]68void Services2NObjMgr::RegisterClass(AnyDataObj* o, NObjMgrAdapter* oa)
[165]69{
[295]70ObjAdaptList::iterator it;
71for(it = objadaplist.begin(); it != objadaplist.end(); it++)
[495]72#ifdef SANS_EVOLPLANCK
[295]73 if (typeid(*o) == typeid(*((*it).obj))) THROW(dupIdErr);
[495]74#else
75 if (typeid(*o) == typeid(*((*it).obj)))
76 throw(DuplicateIdExc("Services2NObjMgr::RegisterClass() - Duplicate class"));
77#endif
[295]78dataobj_adapter oba;
79oba.obj = o;
80oba.obja = oa;
81objadaplist.push_back(oba);
82}
[165]83
[295]84/* --Methode-- */
85NObjMgrAdapter* Services2NObjMgr::GetAdapter(AnyDataObj* o)
86{
87ObjAdaptList::iterator it;
88for(it = objadaplist.begin(); it != objadaplist.end(); it++)
89 if (typeid(*o) == typeid(*((*it).obj))) return((*it).obja->Clone(o));
90return(new NObjMgrAdapter(o));
91}
[165]92
[295]93/* --Methode-- */
[1276]94void Services2NObjMgr::SetTmpDir(string const & tmpdir)
95{
96TmpDir = tmpdir;
97PDynLinkMgr::SetTmpDir(tmpdir);
98return;
99}
100
101/* --Methode-- */
[333]102void Services2NObjMgr::PlotFunc(string const & expfunc, string & nom, double xmin, double xmax, int np, string dopt)
[295]103{
[165]104FILE *fip;
[326]105string fname = TmpDir + "func1_pia_dl.c";
106string cmd;
107int rc;
108
109if (!mImgapp) return;
110
[2755]111// Pour synchronisation d'execution simultanee
[2762]112ZSync zs(mutx_dynlink); zs.NOp();
[2755]113
[326]114cmd = "rm -f " + fname;
115rc = system(cmd.c_str());
116// printf("PlotFunc_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
117
118if ((fip = fopen(fname.c_str(), "w")) == NULL) {
119 string sn = fname;
[449]120 cout << "Services2NObjMgr/PlotFunc_Error: fopen( " << sn << endl;
[326]121 return;
122 }
123
124// constitution du fichier a compiler
125fputs("#include <math.h> \n", fip);
126fputs("double func1_pia_dl_func(double x) \n{\n", fip);
127fprintf(fip,"return(%s); \n}\n", expfunc.c_str());
128fclose(fip);
129
130string func = "func1_pia_dl_func";
131DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname, func);
132if (!f) return;
[333]133PlotFunc(f, nom, xmin, xmax, np, dopt);
[326]134CloseDLL();
135return;
136}
137
138/* --Methode-- */
[333]139void Services2NObjMgr::PlotFunc2D(string const & expfunc, string & nom, double xmin, double xmax,
140 double ymin, double ymax, int npx, int npy, string dopt)
[326]141{
142FILE *fip;
143string fname = TmpDir + "func2_pia_dl.c";
144string cmd;
145int rc;
146
147if (!mImgapp) return;
[2755]148// Pour synchronisation d'execution simultanee
[2762]149ZSync zs(mutx_dynlink); zs.NOp();
[326]150
151cmd = "rm " + fname;
152rc = system(cmd.c_str());
153// printf("PlotFunc2D_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
154
155if ((fip = fopen(fname.c_str(), "w")) == NULL) {
156 string sn = fname;
[449]157 cout << "Services2NObjMgr/PlotFunc2D_Error: fopen( " << sn << endl;
[326]158 return;
159 }
160
161// constitution du fichier a compiler
162fputs("#include <math.h> \n", fip);
163fputs("double func2_pia_dl_func(double x, double y) \n{\n", fip);
164fprintf(fip,"return(%s); \n}\n", expfunc.c_str());
165fclose(fip);
166
167string func = "func2_pia_dl_func";
168DlFunctionOfXY f = (DlFunctionOfXY) LinkFunctionFromFile(fname, func);
169if (!f) return;
[333]170PlotFunc2D(f, nom, xmin, xmax, ymin, ymax, npx, npy, dopt);
[326]171CloseDLL();
172return;
173}
174
175/* --Methode-- */
[333]176void Services2NObjMgr::PlotFuncFrCFile(string const & fname, string const & func, string & nom,
177 double xmin, double xmax, int np, string dopt)
[326]178{
[2755]179// Pour synchronisation d'execution simultanee
[2762]180ZSync zs(mutx_dynlink); zs.NOp();
[326]181DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname, func);
182if (!f) return;
[333]183PlotFunc(f, nom, xmin, xmax, np, dopt);
[326]184CloseDLL();
185return;
186}
187
188/* --Methode-- */
[333]189void Services2NObjMgr::PlotFunc2DFrCFile(string const & fname, string const & func, string & nom,
190 double xmin, double xmax, double ymin, double ymax, int npx, int npy, string dopt)
[326]191{
[2755]192// Pour synchronisation d'execution simultanee
[2762]193ZSync zs(mutx_dynlink); zs.NOp();
[326]194DlFunctionOfXY f = (DlFunctionOfXY) LinkFunctionFromFile(fname, func);
195if (!f) return;
[333]196PlotFunc2D(f, nom, xmin, xmax, ymin, ymax, npx, npy, dopt);
[326]197CloseDLL();
198return;
199}
200
201/* --Methode-- */
[333]202void Services2NObjMgr::PlotFunc(DlFunctionOfX f, string & nom, double xmin, double xmax, int np, string dopt)
[326]203{
204if (!mImgapp) return;
205
206int k;
207if (np < 1) np = 1;
208if (xmax <= xmin) xmax = xmin+1.;
209Vector* vpy = new Vector(np);
210
211double xx;
212double dx = (xmax-xmin)/np;
[500]213
214try {
215 for(k=0; k<np; k++) { xx = xmin+dx*k; (*vpy)(k) = f(xx); }
216}
[495]217#ifdef SANS_EVOLPLANCK
[500]218CATCH(merr) {
[326]219 fflush(stdout);
220 cout << endl;
221 cerr << endl;
222 string es = PeidaExc(merr);
223 cerr << "Services2NObjMgr::PlotFunc() Exception :" << merr << es;
224 delete vpy;
225 vpy = NULL;
226 } ENDTRY;
[495]227#else
228catch ( PException exc ) {
229 fflush(stdout);
230 cout << endl;
231 cerr << endl;
232 cerr << "Services2NObjMgr::PlotFunc() Exception :" << exc.Msg() << endl;
233 delete vpy;
234 vpy = NULL;
235}
236#endif
[326]237
238if (vpy) {
[344]239 string titre;
240 if (nom.length() < 1) {
241 titre = "Function f(x)";
[466]242 nom = "/autoc/f_x";
[344]243 }
244 else titre = nom;
[2924]245
246 MyObjMgr()->AddObj(vpy, nom);
247
248 if ( (dopt == "nodisp") || (dopt == "nodisplay") ) return;
249
[344]250 P1DArrayAdapter* vya = new POVectorAdapter(vpy, false);
[326]251 vya->DefineXCoordinate(xmin, (xmax-xmin)/np);
252 PIYfXDrawer* dr = new PIYfXDrawer(vya, NULL, true) ;
[1971]253 dopt = "thinline " + dopt;
254 int rsid = mImgapp->DispScDrawer(dr, titre, dopt);
[2924]255 MyObjMgr()->AddWRsId(nom, rsid);
256}
[326]257return;
258}
259
260/* --Methode-- */
[333]261void Services2NObjMgr::PlotFunc2D(DlFunctionOfXY f, string & nom, double xmin, double xmax, double ymin, double ymax,
[326]262 int npx, int npy, string dopt)
263{
264if (!mImgapp) return;
265
[2781]266if (npx < 2) npx = 2;
267if (npy < 2) npy = 2;
268//if (npx > 250) npx = 250;
269//if (npy > 250) npy = 250;
[326]270if (xmax <= xmin) xmax = xmin+1.;
271if (ymax <= ymin) ymax = ymin+1.;
272
273Matrix* mtx = new Matrix(npy, npx);
274
275int i,j;
276double xx, yy;
277double dx = (xmax-xmin)/npx;
278double dy = (ymax-ymin)/npy;
279// printf(" -- DBG -- %d %d , %g %g , %g %g \n", npx, npy, xmin, xmax, ymin, ymax);
[500]280try {
[326]281 for(j=0; j<npy; j++) {
282 yy = ymin+dy*j;
283 for(i=0; i<npx; i++) {
284 xx = xmin+dx*i;
285 (*mtx)(j, i) = f(xx, yy);
286 }
287 }
[500]288}
289#ifdef SANS_EVOLPLANCK
290CATCH(merr) {
[326]291 fflush(stdout);
292 cout << endl;
293 cerr << endl;
294 string es = PeidaExc(merr);
295 cerr << "Services2NObjMgr::PlotFunc2D() Exception :" << merr << es;
296 delete mtx; mtx = NULL;
297 } ENDTRY;
[495]298#else
299catch ( PException exc ) {
300 fflush(stdout);
301 cout << endl;
302 cerr << endl;
303 cerr << "Services2NObjMgr::PlotFunc2D() Exception :" << exc.Msg() << endl;
304 delete mtx; mtx = NULL;
305}
306#endif
[326]307
308if (mtx) {
[344]309 string titre;
310 if (nom.length() < 1) {
311 titre = "Function f(x,y)";
[466]312 nom = "/autoc/f2d_xy";
[344]313 }
314 else titre = nom;
[2781]315 int rsid = 0;
[344]316 P2DArrayAdapter* arr = new POMatrixAdapter(mtx, false);
[326]317 arr->DefineXYCoordinates(xmin, ymin, dx, dy);
[2781]318 if ( (npx <= 200) && (npy <= 200) ) {
319 PISurfaceDrawer* sdr = new PISurfaceDrawer(arr, true, true, true);
320 rsid = mImgapp->Disp3DDrawer(sdr, titre, dopt);
321 }
322 else rsid = mImgapp->DispImage(arr, titre, dopt);
323
[333]324 if (nom.length() > 0) {
[2491]325 MyObjMgr()->AddObj(mtx, nom);
326 MyObjMgr()->AddWRsId(nom, rsid);
[333]327 }
[326]328 }
329
330return;
331}
332
333/* --Methode-- */
[2180]334void Services2NObjMgr::ExpVal(string expval,string resultvarname)
335{
336 // Fill C-file to be executed
337 FILE *fip;
338 string func = "eval_pia_dl_func";
339 string fname = TmpDir + func; fname += ".c";
340 string cmd = "rm -f " + fname; system(cmd.c_str());
341 if((fip=fopen(fname.c_str(), "w"))==NULL) {
342 cout << "Services2NObjMgr/EvalExp_Error: fopen("<<fname<<")"<<endl;
343 return;
344 }
345 fprintf(fip,"#include <math.h>\n");
346 fprintf(fip,"double %s(double ___dummy_variable___) \n{\n",func.c_str());
347 // Add all variables already declared
[2491]348 DVList& varlist = MyObjMgr()->GetVarList();
[2180]349 DVList::ValList::const_iterator it;
350 for(it = varlist.Begin(); it != varlist.End(); it++) {
351#ifdef SANS_EVOLPLANCK
352 MuTyV mtv = (*it).second;
353 double value = (double)(mtv);
354#else
355 double value = (double)((*it).second.elval);
356#endif
357 string name_var = (*it).first;
358 fprintf(fip,"double %s = %.17f;\n",name_var.c_str(),value);
359 }
360 fprintf(fip,"return %s;\n}\n",expval.c_str());
361 fclose(fip);
362
363 // Dynamically link function
364 DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname,func);
365 if(!f) {
366 cout<<"Services2NObjMgr/EvalExp_Error: linking DlFunctionOfX"<<endl;
367 cout<<"...expval = "<<expval<<endl;
368 return;
369 }
370
371 // Evaluate function and close dynamic link
372 double result;
373 try {
374 result = f(0.);
375 CloseDLL();
376 } catch ( ... ) {
377 cout<<"Services2NObjMgr/EvalExp_Error: Arithmetic exception"<<endl;
378 CloseDLL();
379 return;
380 }
381
382 // Eventually store the result into variable or just print it
383 if(resultvarname.size()>0) {
[2491]384 if(MyObjMgr()->HasVar(resultvarname)) MyObjMgr()->DeleteVar(resultvarname);
[2180]385 char str[512];
386 if(result==0.) sprintf(str,"%f",result);
387 else {
388 double lr = log10(fabs(result));
389 if(lr>-17. && lr<17.) sprintf(str,"%17f",result);
390 else sprintf(str,"%.17e",result);
391 }
[2491]392 MyObjMgr()->SetVar(resultvarname,(string)str);
[2180]393 } else cout<<result<<" = "<<expval<<endl;
394
395}
396
397/* --Methode-- */
[333]398void Services2NObjMgr::DisplayPoints2D(string& nom, string& expx, string& expy,
399 string& experrx, string& experry,
[357]400 string& expcut, string dopt, string loop)
[333]401{
402NObjMgrAdapter* obja=NULL;
[2491]403obja = MyObjMgr()->GetObjAdapter(nom);
[333]404if (obja == NULL) {
[449]405 cout << "Services2NObjMgr::DisplayPoints2D() Error , No such object " << nom << endl;
[333]406 return;
407 }
408if (!mImgapp) return;
409
410// Creation NTuple
411char* ntn[4] = {"expx","expy","expex","expey",};
412NTuple* nt = NULL;
413bool haserr = false;
414
415if ( (experrx.length() > 0 ) && (experry.length() > 0 ) ) { haserr = true; nt = new NTuple(4, ntn); }
416else { haserr = false; experrx = experry = "0."; nt = new NTuple(2, ntn); }
417
[357]418ComputeExpressions(obja, expx, expy, experrx, experry, expcut, loop, nt, NULL, NULL);
[333]419
420if (nt->NEntry() < 1) {
421 cout << "Services2NObjMgr::DisplayPoints2D() Warning Zero points satisfy cut !" << endl;
422 delete nt;
423 return;
424 }
425
426// nt->Show();
427// nt->Print(0,10);
428PINTuple* pin = new PINTuple(nt, true);
429pin->SelectXY(ntn[0], ntn[1]);
430if ( haserr ) pin->SelectErrBar(ntn[2], ntn[3]);
431
[1971]432dopt = "defline " + dopt;
[333]433string titre = nom + ":" + expy + "%" + expx;
[1971]434mImgapp->DispScDrawer( (PIDrawer*)pin, titre, dopt);
[333]435return;
436}
437
438/* --Methode-- */
439void Services2NObjMgr::DisplayPoints3D(string& nom, string& expx, string& expy,
[357]440 string& expz, string& expcut, string dopt, string loop)
[333]441{
442NObjMgrAdapter* obja=NULL;
[2491]443obja = MyObjMgr()->GetObjAdapter(nom);
[333]444if (obja == NULL) {
[449]445 cout << "Services2NObjMgr::DisplayPoints3D() Error , No such object " << nom << endl;
[333]446 return;
447 }
448if (!mImgapp) return;
449
450char* ntn[3] = {"expx","expy","expz"};
451NTuple* nt = new NTuple(3,ntn); // Creation NTuple
452
453string expwt = "1.";
[357]454ComputeExpressions(obja, expx, expy, expz, expwt, expcut, loop, nt, NULL, NULL);
[333]455
456if (nt->NEntry() < 1) {
457 cout << "Services2NObjMgr::DisplayPoints3D() Warning Zero points satisfy cut !" << endl;
458 delete nt;
459 return;
460 }
[2494]461//DBG nt->Show();
462//DBG nt->Print(0,10);
[333]463PINTuple3D* pin = new PINTuple3D(nt, true);
464pin->SelectXYZ(ntn[0], ntn[1], ntn[2]);
[1971]465dopt = "defline " + dopt;
[333]466
467// Pour plot a partir de DispScDrawer
468// string nomdisp = "_NT3D_";
469// mImgapp->DispScDrawer( (PIDrawer*)pin, nomdisp, opt);
470// Pour plot a partir de Disp3DDrawer
471string titre = nom + ":" + expy + "%" + expx;
[1971]472mImgapp->Disp3DDrawer(pin, titre, dopt);
[333]473
474return;
475}
476
477/* --Methode-- */
478void Services2NObjMgr::DisplayPoints2DW(string& nom, string& expx, string& expy,
[3279]479 string& expwt, string& expcut, bool fgcolidx, string dopt, string loop)
[333]480{
481NObjMgrAdapter* obja=NULL;
[2491]482obja = MyObjMgr()->GetObjAdapter(nom);
[333]483if (obja == NULL) {
[449]484 cout << "Services2NObjMgr::DisplayPoints2DW() Error , No such object " << nom << endl;
[333]485 return;
486 }
487if (!mImgapp) return;
488
489char* ntn[3] = {"expx","expy","expw"};
490NTuple* nt = new NTuple(3,ntn); // Creation NTuple
491
492string exp = "1.";
[357]493ComputeExpressions(obja, expx, expy, expwt, exp, expcut, loop, nt, NULL, NULL);
[333]494
495if (nt->NEntry() < 1) {
496 cout << "Services2NObjMgr::DisplayPoints2DW() Warning Zero points satisfy cut !" << endl;
497 delete nt;
498 return;
499 }
500
501PINTuple* pin = new PINTuple(nt, true);
502pin->SelectXY(ntn[0], ntn[1]);
[3279]503if (fgcolidx) pin->SelectColorByIndex(ntn[2]); // Use the weight expression as a color index
504else pin->SelectWt(ntn[2]);
[333]505
506string titre = nom + ":" + expwt + "_" + expy + "%" + expx ;
[1971]507mImgapp->DispScDrawer( (PIDrawer*)pin, titre, dopt);
[333]508return;
509}
510
511/* --Methode-- */
[2999]512void Services2NObjMgr::DisplayPoints3DW(string& nom, string& expx, string& expy, string& expz,
513 string& expwt, string& expcut, string dopt, string loop)
514{
515NObjMgrAdapter* obja=NULL;
516obja = MyObjMgr()->GetObjAdapter(nom);
517if (obja == NULL) {
518 cout << "Services2NObjMgr::DisplayPoints3D() Error , No such object " << nom << endl;
519 return;
520 }
521if (!mImgapp) return;
522
523char* ntn[4] = {"expx","expy","expz","expw"};
524NTuple* nt = new NTuple(4,ntn); // Creation NTuple
525
526ComputeExpressions(obja, expx, expy, expz, expwt, expcut, loop, nt, NULL, NULL);
527
528if (nt->NEntry() < 1) {
529 cout << "Services2NObjMgr::DisplayPoints3DW() Warning Zero points satisfy cut !" << endl;
530 delete nt;
531 return;
532 }
533//DBG nt->Show();
534//DBG nt->Print(0,10);
535PINTuple3D* pin = new PINTuple3D(nt, true);
536pin->SelectXYZ(ntn[0], ntn[1], ntn[2]);
537pin->SelectWt(ntn[3]);
538pin->UseColorScale(true);
539dopt = "defline " + dopt;
540
541// Pour plot a partir de DispScDrawer
542// string nomdisp = "_NT3D_";
543// mImgapp->DispScDrawer( (PIDrawer*)pin, nomdisp, opt);
544// Pour plot a partir de Disp3DDrawer
545string titre = nom + ":" + expy + "%" + expx + " W=" + expwt;
546mImgapp->Disp3DDrawer(pin, titre, dopt);
547
548return;
549}
550
551/* --Methode-- */
[333]552void Services2NObjMgr::ProjectH1(string& nom, string& expx, string& expwt,
[357]553 string& expcut, string& nomh1, string dopt, string loop)
[333]554{
555NObjMgrAdapter* obja=NULL;
[2491]556obja = MyObjMgr()->GetObjAdapter(nom);
[333]557if (obja == NULL) {
[449]558 cout << "Services2NObjMgr::ProjectH1() Error , No such object " << nom << endl;
[333]559 return;
[2681]560}
[333]561if (!mImgapp) return;
562
563Histo* h1 = NULL;
564NTuple* nt = NULL;
565AnyDataObj* oh = NULL;
[2679]566bool h1_already_exist = false;
[2491]567if (nomh1.length() > 0) oh=MyObjMgr()->GetObj(nomh1);
[333]568else nomh1 = "/tmp/projh1d";
[2679]569if ( (oh != NULL) && (typeid(*oh) == typeid(Histo)) ) {
570 h1 = (Histo*)oh;
571 h1_already_exist = true;
572 // Pas de remise a zero ! h1->Zero();
573} else {
[333]574 char* ntn[2]= {"hxval", "hwt"};
575 nt = new NTuple(2,ntn); // Creation NTuple
[2681]576}
[333]577string expz = "0.";
[357]578ComputeExpressions(obja, expx, expwt, expz, expwt, expcut, loop, nt, h1, NULL);
[333]579
580if ((!h1) && (!nt)) return;
581if (!h1) {
582 if (nt->NEntry() < 1) {
583 cout << "Services2NObjMgr::ProjectH1() Warning Zero points satisfy cut !" << endl;
584 delete nt;
585 return;
[2681]586 }
[333]587 double xmin, xmax;
588 nt->GetMinMax(0, xmin, xmax);
589 h1 = new Histo(xmin, xmax, 100);
590 int k;
591 float* xn;
592 for(k=0; k<nt->NEntry(); k++) {
593 xn = nt->GetVec(k);
594 h1->Add(xn[0], xn[1]);
[2681]595 }
[333]596 delete nt;
[2491]597 MyObjMgr()->AddObj(h1, nomh1);
[2681]598}
[333]599
[2681]600if(!h1_already_exist || dopt.size()>0) MyObjMgr()->DisplayObj(nomh1, dopt);
[333]601return;
602}
603
604/* --Methode-- */
605void Services2NObjMgr::ProjectH2(string& nom, string& expx, string& expy, string& expwt,
[357]606 string& expcut, string& nomh2, string dopt, string loop)
[333]607{
608NObjMgrAdapter* obja=NULL;
[2491]609obja = MyObjMgr()->GetObjAdapter(nom);
[333]610if (obja == NULL) {
[449]611 cout << "Services2NObjMgr::ProjectH2() Error , No such object " << nom << endl;
[333]612 return;
[2681]613}
[333]614if (!mImgapp) return;
615
616Histo2D* h2 = NULL;
617NTuple* nt = NULL;
618AnyDataObj* oh = NULL;
[2679]619bool h2_already_exist = false;
[2491]620if (nomh2.length() > 0) oh=MyObjMgr()->GetObj(nomh2);
[333]621else nomh2 = "/tmp/projh2d";
[2679]622if ( (oh != NULL) && (typeid(*oh) == typeid(Histo2D)) ) {
623 h2 = (Histo2D*)oh;
624 h2_already_exist = true;
625 // Pas de remise a zero ! h2->Zero();
626} else {
[333]627 char* ntn[3]= {"hxval", "hyval", "hwt"};
628 nt = new NTuple(3,ntn); // Creation NTuple
[2681]629}
[333]630string expz = "0.";
[357]631ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, loop, nt, NULL, h2);
[333]632
633if ((!h2) && (!nt)) return;
634if (!h2) {
635 if (nt->NEntry() < 1) {
636 cout << "Services2NObjMgr::ProjectH2() Warning Zero points satisfy cut !" << endl;
637 delete nt;
638 return;
[2681]639 }
[333]640 double xmin, xmax, ymin, ymax;
641 nt->GetMinMax(0, xmin, xmax);
[466]642 nt->GetMinMax(1, ymin, ymax);
[333]643 h2 = new Histo2D(xmin, xmax, 50, ymin, ymax, 50);
644 int k;
645 float* xn;
646 for(k=0; k<nt->NEntry(); k++) {
647 xn = nt->GetVec(k);
648 h2->Add(xn[0], xn[1], xn[2]);
[2681]649 }
[333]650 delete nt;
[2491]651 MyObjMgr()->AddObj(h2, nomh2);
[2681]652}
[333]653
[2681]654if(!h2_already_exist || dopt.size()>0) MyObjMgr()->DisplayObj(nomh2, dopt);
[333]655return;
656
657}
658
659/* --Methode-- cmv 13/10/98 */
660void Services2NObjMgr::ProjectHProf(string& nom, string& expx, string& expy, string& expwt,
[357]661 string& expcut, string& nomprof, string dopt, string loop)
662// Pour remplir un ``GeneralFitData'' a partir de divers objets:
[333]663//| nom = nom de l'objet a projeter dans un HProf.
664//| expx = expression X de definition du bin.
665//| expy = expression Y a additionner dans le bin.
666//| expwt = expression W du poids a additionner.
667//| expcut = expression du test de selection.
668//| nomprof = nom du HProf engendre (optionnel). Si l'objet n'existe pas
669//| les limites Xmin,Xmax sont calculees automatiquement.
670//| sinon ce sont celles de l'objet preexistant.
671//| opt = options generales pour le display.
672{
673NObjMgrAdapter* obja=NULL;
[2491]674obja = MyObjMgr()->GetObjAdapter(nom);
[333]675if (obja == NULL) {
[449]676 cout << "Services2NObjMgr::ProjectHProf() Error , No such object " << nom << endl;
[333]677 return;
[2681]678}
[333]679if (!mImgapp) return;
680
681HProf* hprof = NULL;
682NTuple* nt = NULL;
683AnyDataObj* oh = NULL;
[2679]684bool hp_already_exist = false;
[2491]685if (nomprof.length() > 0) oh=MyObjMgr()->GetObj(nomprof);
[333]686else nomprof = "/tmp/projprof";
[2679]687if( (oh!=NULL) && (typeid(*oh) == typeid(HProf)) ) {
688 hprof = (HProf*)oh;
689 hp_already_exist = true;
690} else {
[333]691 char* ntn[3]= {"hxval", "hyval", "hwt"};
692 nt = new NTuple(3,ntn); // Creation NTuple
693}
694string expz = "0.";
[357]695ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, loop, nt, NULL, NULL, hprof);
[333]696
697if((!hprof) && (!nt)) return;
698if(!hprof) {
699 if (nt->NEntry() < 1) {
700 cout << "Services2NObjMgr::ProjectHProf() Warning Zero points satisfy cut !" << endl;
701 delete nt;
702 return;
703 }
[1091]704 r_8 xmin, xmax;
[333]705 nt->GetMinMax(0, xmin, xmax);
706 hprof = new HProf(xmin, xmax, 100);
707 int k;
708 float* xn;
709 for(k=0; k<nt->NEntry(); k++) {
710 xn = nt->GetVec(k);
711 hprof->Add(xn[0], xn[1], xn[2]);
[2681]712 }
[333]713 delete nt;
[2491]714 MyObjMgr()->AddObj(hprof, nomprof);
[2681]715}
[1090]716hprof->UpdateHisto();
[333]717
[2681]718if(!hp_already_exist || dopt.size()>0) MyObjMgr()->DisplayObj(nomprof, dopt);
[333]719return;
720}
721
[357]722
[333]723/* --Methode-- */
[357]724void Services2NObjMgr::FillVect(string& nom, string& expx, string& expv,
725 string& expcut, string& nomvec, string dopt, string loop)
[333]726{
727NObjMgrAdapter* obja=NULL;
[2491]728obja = MyObjMgr()->GetObjAdapter(nom);
[333]729if (obja == NULL) {
[357]730 cout << "Services2NObjMgr::FillVect() Error , No such object: " << nom << endl;
[333]731 return;
732 }
733if (!mImgapp) return;
734
[357]735Vector* v1 = NULL;
736AnyDataObj* ov = NULL;
[2491]737ov=MyObjMgr()->GetObj(nomvec);
[357]738if (ov != NULL) v1 = dynamic_cast<Vector *>(ov);
739if (v1 == NULL) {
740 cout << "Services2NObjMgr::FillVect() Error , No such object or not a vector: " << nomvec << endl;
741 return;
742 }
743
744char* ntn[2]= {"vi", "vv"};
745NTuple* nt = new NTuple(2,ntn); // Creation NTuple
746
747string expz = "0.";
748ComputeExpressions(obja, expx, expv, expz, expz, expcut, loop, nt);
749
750if (!nt) return;
751if (nt->NEntry() < 1) {
752 cout << "Services2NObjMgr::FillVect() Warning Zero points satisfy cut !" << endl;
753 delete nt;
754 return;
755 }
756
757 int i,k;
758 double* xn;
759 for(k=0; k<nt->NEntry(); k++) {
760 xn = nt->GetLineD(k);
[2180]761 i = int(xn[0]+0.5);
[357]762 if ( (i < 0) || i >= v1->NElts() ) continue;
763 (*v1)(i) = xn[1];
764 }
765 delete nt;
766
767
[2491]768MyObjMgr()->DisplayObj(nomvec, dopt);
[357]769return;
770}
771
772/* --Methode-- */
773void Services2NObjMgr::FillMatx(string& nom, string& expx, string& expy, string& expv,
774 string& expcut, string& nommtx, string dopt, string loop)
775{
776NObjMgrAdapter* obja=NULL;
[2491]777obja = MyObjMgr()->GetObjAdapter(nom);
[357]778if (obja == NULL) {
779 cout << "Services2NObjMgr::FillMatx() Error , No such objet " << nom << endl;
780 return;
781 }
782if (!mImgapp) return;
783
784Matrix* mtx = NULL;
785AnyDataObj* om = NULL;
[2491]786om=MyObjMgr()->GetObj(nommtx);
[357]787if (om != NULL) mtx = dynamic_cast<Matrix *>(om);
788if (mtx == NULL) {
789 cout << "Services2NObjMgr::FillMatx() Error , No such object or not a matrix " << nommtx << endl;
790 return;
791 }
792
793char* ntn[3]= {"mi", "mj", "mv"};
794NTuple* nt = new NTuple(3,ntn); // Creation NTuple
795
796string expz = "0.";
797ComputeExpressions(obja, expx, expy, expv, expz, expcut, loop, nt);
798
799if (!nt) return;
800if (nt->NEntry() < 1) {
801 cout << "Services2NObjMgr::FillMatx() Warning Zero points satisfy cut !" << endl;
802 delete nt;
803 return;
804 }
805
806 int ic, jl, k;
807 double* xn;
808 for(k=0; k<nt->NEntry(); k++) {
809 xn = nt->GetLineD(k);
[2180]810 ic = int(xn[0]+0.5);
811 jl = int(xn[1]+0.5);
[357]812 if ( (ic < 0) || ic >= mtx->NCol() ) continue;
813 if ( (jl < 0) || jl >= mtx->NRows() ) continue;
814 (*mtx)(jl, ic) = xn[2];
815 }
816 delete nt;
817
818
[2491]819MyObjMgr()->DisplayObj(nommtx, dopt);
[357]820return;
821
822}
823
824/* --Methode-- */
825void Services2NObjMgr::ExpressionToVector(string& nom, string& expx, string& expcut,
826 string& nomvec, string dopt, string loop)
827{
828NObjMgrAdapter* obja=NULL;
[2491]829obja = MyObjMgr()->GetObjAdapter(nom);
[357]830if (obja == NULL) {
[449]831 cout << "Services2NObjMgr::ExpressionToVector() Error , No such object " << nom << endl;
[357]832 return;
833 }
834if (!mImgapp) return;
835
[333]836NTuple* nt = NULL;
[2924]837// if (nomvec.length() < 1) nomvec = "/tmp/expvec";
[333]838
839char* ntn[2]= {"vecval", "vecwt"};
840nt = new NTuple(1,ntn); // Creation NTuple
841
842string expwt = "1.";
[1486]843string expz = "0.";
844string dumexpcut = expcut; if(dumexpcut.size()<=0) dumexpcut = "1.";
845ComputeExpressions(obja,expx,expz,expz,expwt,dumexpcut,loop,nt,NULL,NULL);
[333]846
847if (!nt) return;
848if (nt->NEntry() < 1) {
[357]849 cout << "Services2NObjMgr::ExpressionToVector() Warning Zero points satisfy cut !" << endl;
[333]850 delete nt;
851 return;
852 }
853
854Vector* vec = new Vector(nt->NEntry());
855int k;
856float* xn;
857for(k=0; k<nt->NEntry(); k++) {
858 xn = nt->GetVec(k);
859 (*vec)(k) = xn[0];
860 }
861delete nt;
[2922]862if (nomvec.size() > 0) {
863 MyObjMgr()->AddObj(vec, nomvec);
864 MyObjMgr()->DisplayObj(nomvec, dopt);
865}
866else {
867// On calcule et on affiche mean/sigma + min/max
868 double min, max, mean, sigma;
869 vec->MinMax(min, max);
870 MathArray<r_8> ma;
871 ma.MeanSigma(*vec, mean, sigma);
[2924]872 cout << "Size= " << vec->Size() << " Mean= " << mean << " Sigma= " << sigma
[2922]873 << " Min= " << min << " Max= " << max << endl;
874 delete vec;
875}
[333]876return;
877}
878
879/* --Methode-- */
[447]880void Services2NObjMgr::NtFromASCIIFile(string& nom,string& filename,double def_val)
[2792]881// Pour remplir un NTuple/DataTable "nom" existant a partir du fichier
[447]882// ASCII table "filename". Si il y a plus de variables dans le
883// ntuple que dans le fichier "filename",
[2792]884// les sur-numeraires numeriques sont mises a "def_val" par defaut.
[447]885{
[2491]886AnyDataObj* mobj = MyObjMgr()->GetObj(nom);
[2792]887if(mobj == NULL) {
888 cout<<"NtFromASCIIFile() Error, object "<<nom<<" not existing"<<endl;
889 return;
890}
891
892NTuple* nt = NULL;
893DataTable* dt = NULL;
894if(typeid(*mobj) == typeid(NTuple)) nt = (NTuple*) mobj;
895else if(typeid(*mobj) == typeid(DataTable)) dt = (DataTable*) mobj;
896
897if(nt==NULL && dt==NULL) {
898 cout<<"NtFromASCIIFile() Error, object "<<nom<<" not an NTuple nor a DataTable"<<endl;
899 return;
900}
[447]901if (!mImgapp) return;
902
[2792]903if(nt) nt->FillFromASCIIFile(filename, def_val);
[2833]904else if(dt) {
905 ifstream is(filename.c_str());
906 dt->FillFromASCIIFile(is);
907}
[447]908return;
909}
910
911/* --Methode-- */
[333]912void Services2NObjMgr::FillNT(string& nom, string& expx, string& expy, string& expz,
[357]913 string& expt, string& expcut, string& nomnt, string loop)
[333]914{
915NObjMgrAdapter* obja=NULL;
[2491]916obja = MyObjMgr()->GetObjAdapter(nom);
[333]917if (obja == NULL) {
[449]918 cout << "Services2NObjMgr::FillNT() Error , No such object " << nom << endl;
[333]919 return;
920 }
921if (!mImgapp) return;
922
923bool fgnnt = false;
924NTuple* nt = NULL;
925AnyDataObj* oh = NULL;
[2491]926if (nomnt.length() > 0) oh=MyObjMgr()->GetObj(nomnt);
[333]927else nomnt = "/tmp/fillnt";
928if ( (oh != NULL) && (typeid(*oh) == typeid(NTuple)) ) {
929 nt = (NTuple*)oh;
930 if (nt->NVar() > 10) {
[449]931 cout << "Services2NObjMgr::FillNT() Warning , Max 10 var in NTuple -> new NTuple" << endl;
[333]932 nt = NULL;
933 }
934 }
935if (nt == NULL) {
936 char* ntn[4]= {"x", "y","z","t"};
937 nt = new NTuple(4,ntn); // Creation NTuple
938 fgnnt = true;
939 }
940
[357]941ComputeExpressions(obja, expx, expy, expz, expt, expcut, loop, nt, NULL, NULL);
[333]942
[2491]943if (fgnnt) MyObjMgr()->AddObj(nt, nomnt);
[333]944return;
945
946}
947
948/* --Methode-- */
949void Services2NObjMgr::FillNTFrCFile(string & nom, string const & fname,
[357]950 string const & funcname, string & nomnt, string loop)
[333]951{
952if (!mImgapp) return;
953
954NObjMgrAdapter* obja=NULL;
[2491]955obja = MyObjMgr()->GetObjAdapter(nom);
[333]956if (obja == NULL) {
957 cout << "Services2NObjMgr::FillNTFrCFile( " << nom << "...) No such object" <<endl;
958 return;
959 }
[344]960bool adel = true;
961NTupleInterface* objnt = obja->GetNTupleInterface(adel);
[333]962if (objnt == NULL) {
[449]963 cout << "Services2NObjMgr::FillNTFrCFile( " << nom << "...) Not an NTupleInterface !" <<endl;
[333]964 return;
965 }
966
[2755]967// Pour synchronisation d'execution simultanee
[2762]968ZSync zs(mutx_dynlink); zs.NOp();
[2755]969
[333]970NTLoopExprFunc f = (NTLoopExprFunc)LinkFunctionFromFile(fname, funcname);
971if (!f) {
972 cerr << "Services2NObjMgr::FillNTFrCFile Error Creation NTLoopExprFunc" << endl;
[344]973 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]974 return;
975 }
976
977bool fgnnt = false;
978NTuple* nt = NULL;
979if (nomnt.length() > 0) {
980 AnyDataObj* oh = NULL;
[2491]981 oh=MyObjMgr()->GetObj(nomnt);
[333]982 if ( (oh != NULL) && (typeid(*oh) == typeid(NTuple)) ) {
983 nt = (NTuple*)oh;
984 if (nt->NVar() > 10) {
[449]985 cout << "Services2NObjMgr::FillNTFrCFile() Warning , Max 10 var in NTuple -> new NTuple" << endl;
[333]986 nt = NULL;
987 }
988 }
989 if (nt == NULL) {
990 char* ntn[4]= {"x", "y","z","t"};
991 nt = new NTuple(4,ntn); // Creation NTuple
992 fgnnt = true;
993 }
994 }
995
996double xnt[10];
997
998int i,k;
[2668]999for(i=0; i<10; i++) xnt[i] = 0.;
[333]1000
[2419]1001int_8 k1,k2,dk;
[357]1002k1 = 0; k2 = objnt->NbLines(); dk = 1;
1003DecodeLoopParameters(loop, k1, k2, dk);
1004if (k1 < 0) k1 = 0;
1005if (k2 < 0) k2 = objnt->NbLines();
[2419]1006if (k2 > (int_8)objnt->NbLines()) k2 = objnt->NbLines();
[357]1007if (dk <= 0) dk = 1;
1008
[500]1009try {
[333]1010 double* xn;
[2419]1011 int_8 kstart = k1, kend = k2;
1012 for(k=kstart; k<kend; k+=dk) {
[333]1013 xn = objnt->GetLineD(k);
[2419]1014 if (f((int_8_exprf)k, xn, xnt, xnt+1, xnt+2, xnt+3, (int_8_exprf)kstart,(int_8_exprf) kend) != 0) {
[2668]1015 if (nt) nt->Fill(xnt);
[333]1016 }
1017 }
1018 }
[500]1019#ifdef SANS_EVOLPLANCK
[333]1020CATCH(merr) {
1021 fflush(stdout);
1022 cout << endl;
1023 cerr << endl;
1024 string es = PeidaExc(merr);
1025 cerr << "Services2NObjMgr::FillNTFrCFile() Exception :" << merr << es;
1026 } ENDTRY;
[500]1027#else
1028catch ( PException exc ) {
1029 fflush(stdout);
1030 cout << endl;
1031 cerr << endl;
1032 cerr << "Services2NObjMgr::FillNTFrCFile() Exception :" << exc.Msg() << endl;
1033}
1034#endif
[344]1035
1036if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1037CloseDLL();
1038
[2491]1039if (fgnnt) MyObjMgr()->AddObj(nt, nomnt);
[333]1040return;
1041}
1042
1043/* --Methode-- */
1044void Services2NObjMgr::PrepareNTExpressionCFile(string & nom, string const & fname,
1045 string const & funcname)
1046{
1047NObjMgrAdapter* obja=NULL;
[2491]1048obja = MyObjMgr()->GetObjAdapter(nom);
[333]1049if (obja == NULL) {
1050 cout << "Services2NObjMgr::PrepareNTExpressionCFile( " << nom << "...) No such object" <<endl;
1051 return;
1052 }
[344]1053bool adel = true;
1054NTupleInterface* objnt = obja->GetNTupleInterface(adel);
[333]1055if (objnt == NULL) {
1056 cout << "Services2NObjMgr::PrepareNTExpressionCFile( " << nom
1057 << "...) No NTupleInterface !" <<endl;
1058 return;
1059 }
1060string vardec = objnt->VarList_C("_xnti_");
1061
1062FILE *fip;
1063if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1064 cout << "Services2NObjMgr::PrepareNTExpressionCFile()_Error: fopen " << fname << endl;
[344]1065 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1066 return;
1067 }
[344]1068
[333]1069// constitution du fichier des decalarations des variables de l'interface NTuple
1070fputs("#include <stdlib.h> \n", fip);
1071fputs("#include <stdio.h> \n", fip);
1072fputs("#include <math.h> \n\n", fip);
[344]1073
[1469]1074fputs("/* ------ Compare bits on double --------- */ \n", fip);
[2419]1075fputs("typedef long long int_8_exprf;\n", fip);
1076fputs("int_8_exprf BitCmp64(double v,int_8_exprf flg)\n", fip);
1077fputs("{return ((int_8_exprf)((v<0.) ? v-0.1 : v+0.1))&flg;}\n", fip);
[344]1078fputs("/* ------ Some random number generators --------- */ \n", fip);
[1931]1079fputs("#if defined(__ppc__) && defined(__MACH__) \n",fip);
1080fputs("#include <limits.h> \n", fip);
1081fputs("#define drand48() ((double)(random())/LONG_MAX) \n",fip);
1082fputs("#endif \n",fip);
[344]1083fputs("#define frand01() ( (float) drand48() ) \n", fip);
1084fputs("#define drand01() drand48() \n", fip);
1085fputs("#define rand01() drand48() \n", fip);
1086fputs("#define frandpm1() ( 2. * frand01() - 1.) \n", fip);
1087fputs("#define drandpm1() ( 2. * drand01() - 1.) \n", fip);
1088fputs("#define randpm1() ( 2. * drand01() - 1.) \n", fip);
1089fputs("double NorRand(void) \n", fip);
1090fputs(" { \n double x,A,B; \n LAB10: \n A = drand01(); \n", fip);
1091fputs(" if ( A == 0. ) goto LAB10; \n B = drand01(); \n", fip);
[345]1092fputs(" x = sqrt(-2.*log(A))*cos(2.*M_PI*B); \n", fip);
[344]1093fputs(" return(x); \n } \n", fip);
1094fputs("#define GauRand() NorRand() \n", fip);
1095fputs("#define gaurand() NorRand() \n\n", fip);
1096
[2419]1097fputs("/* NTupleInterface Variable declaration - Generated by piapp */\n", fip);
1098fputs("/* -- Services2NObjMgr::PrepareNTExpressionCFile() -- */ \n", fip);
1099fputs("/* _nl line number or sequential index : _nstart <= _nl < _nend */ \n\n", fip);
1100fprintf(fip,"int %s(int_8_exprf _nl, double* _xnti_, double* _rx_, double* _ry_, double* _rz_, \n",
[333]1101 funcname.c_str());
[2419]1102fprintf(fip," double* _rt_, int_8_exprf _nstart, int_8_exprf _nend) \n");
[333]1103fprintf(fip, "{ \n %s \n", vardec.c_str());
1104fputs(" if (!1) { /* Cut Expression failed */ \n", fip);
1105fputs(" *_rx_ = *_ry_ = *_rz_ = *_rt_ = 0.; return(0);", fip);
1106fputs(" } \n /* Cut expression satisfied */ \n", fip);
1107fputs(" *_rx_ = 1.; \n *_ry_ = 1.; \n *_rz_ = 1.; \n *_rt_ = 1.; \n", fip);
1108fputs(" return(1); \n} \n", fip);
1109
1110fclose(fip);
[344]1111
1112if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1113return;
1114}
1115
1116/* --Methode-- cmv 13/10/98 */
1117void Services2NObjMgr::FillGFD(string& nom, string& expx, string& expy, string& expz,
[357]1118 string& experr, string& expcut, string& nomgfd, string loop)
[333]1119// Pour remplir un ``GeneralFitData'' a partir de divers objets:
1120//| nom = nom de l'objet a transcrire selon 1D: Z=f(X) ou 2D: Z=f(X,Y) .
1121//| Vector,Matrix,Histo,HProf,Histo2D,Image<T>,StarList,NTuple,GeneralFitData
1122//| expx = expression X du GeneralFitData (1er abscisse)
1123//| expy = expression Y du GeneralFitData (2sd abscisse si non "", Z=f(X,Y))
1124//| expz = expression Z du GeneralFitData (valeur de l'ordonnee)
1125//| experr = expression de l'erreur sur l'ordonnee Z
1126//| expcut = expression du test de selection
1127//| nomgfd = nom du GeneralFitData engendre (optionnel)
1128{
1129NObjMgrAdapter* obja=NULL;
[2491]1130obja = MyObjMgr()->GetObjAdapter(nom);
[333]1131if (obja == NULL) {
[449]1132 cout << "Services2NObjMgr::FillGFD() Error , No such object "<<nom<<endl;
[333]1133 return;
1134 }
1135if(!mImgapp) return;
1136
1137// 2D ou 3D?
1138int nvar = 2;
1139if(expy.length()<=0) {nvar = 1; expy = "0.";}
1140
1141// Creation NTuple Buffer
1142char* ntn[4]= {"x","y","f","e"};
1143NTuple*nt = new NTuple(4,ntn);
1144
1145// Remplissage NTuple buffer
[357]1146ComputeExpressions(obja, expx, expy, expz, experr, expcut, loop, nt, NULL, NULL);
[333]1147if(nt->NEntry() < 1)
1148 {cout<<"Services2NObjMgr::FillGFD() Warning Zero points satisfy cut !"<<endl;
1149 delete nt; return;}
1150
1151//Remplissage de la structure GeneraFitData
1152if (nt->NEntry() <= 0) {
1153 cout<<"Services2NObjMgr::FillGFD() Warning - NData= " << nt->NEntry() << endl;
1154 delete nt;
1155 return;
1156 }
1157
1158GeneralFitData* gfd = new GeneralFitData(nvar,nt->NEntry(),0);
1159int k;
1160float* xn;
1161for(k=0; k<nt->NEntry(); k++) {
1162 xn = nt->GetVec(k);
1163 gfd->AddData(xn,xn[2],xn[3]);
1164}
1165
1166// Menage et table d'objets
1167delete nt;
[2491]1168MyObjMgr()->AddObj(gfd, nomgfd);
[333]1169return;
1170}
1171
[1067]1172/* --Methode-- cmv 12/07/00 */
1173void Services2NObjMgr::FillGFDfrVec(string nomgfd,string namx,string namy,string namz,string name)
1174// Pour remplir un ``GeneralFitData'' a partir de vecteurs
1175//| gdfrvec nomgd X Y ! !
1176//| gdfrvec nomgd X Y ! EY
1177//| gdfrvec nomgd X Y Z !
1178//| gdfrvec nomgd X Y Z EZ
1179//| - nomgfd = nom du generaldata a remplir
1180//| - namx = nom du vecteur contenant les valeurs X
1181//| - namy = nom du vecteur contenant les valeurs Y
1182//| - namz = nom du vecteur contenant les valeurs Z (ou "!")
1183//| - name = nom du vecteur contenant les valeurs des erreurs EY ou EZ
1184{
1185// Decodage des noms des vecteurs pour le remplissage du generaldata
1186if(nomgfd=="!" || nomgfd.length()<1)
1187 {cout<<"FillGFDfrVec_Error: bad GenaralData name "<<nomgfd<<endl; return;}
1188if(namx=="!" || namx.length()<1)
1189 {cout<<"FillGFDfrVec_Error: bad X vector name "<<namx<<endl; return;}
1190if(namy=="!" || namy.length()<1)
1191 {cout<<"FillGFDfrVec_Error: bad Y vector name "<<namy<<endl; return;}
1192if(namz.length()<1) namz = "!";
1193if(name.length()<1) name = "!";
1194int nvar = 0;
1195if(namz=="!") nvar = 1; else nvar = 2;
[333]1196
[1067]1197// Identify data
1198NamedObjMgr omg;
1199AnyDataObj* mobj = NULL;
1200Vector* v;
1201int nel = 0;
1202r_8 *x=NULL, *y=NULL, *z=NULL,* ez=NULL;
1203
1204if( (mobj=omg.GetObj(namx)) == NULL) {
1205 cout<<"FillGFDfrVec_Error: unknown X object "<<namx<<endl; return;
1206} else {
1207 v = (Vector*) mobj; x = v->Data(); nel=v->NElts();
1208}
1209
1210if( (mobj=omg.GetObj(namy)) == NULL) {
1211 cout<<"FillGFDfrVec_Error: unknown Y object "<<namy<<endl; return;
1212} else {
1213 v = (Vector*) mobj; y = z = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1214}
1215
1216if( nvar==2 && (mobj=omg.GetObj(namz)) == NULL) {
1217 cout<<"FillGFDfrVec_Error: unknown Z object "<<namz<<endl; return;
1218} else {
1219 v = (Vector*) mobj; z = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1220}
1221
1222if(name!="!") {
1223 if( (mobj=omg.GetObj(name)) == NULL) {
1224 cout<<"FillGFDfrVec_Error: unknown EZ object "<<name<<endl; return;
1225 } else {
1226 v = (Vector*) mobj; ez = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1227 }
1228}
1229
1230if(nel<=0)
1231 {cout<<"FillGFDfrVec_Error: bad number of elements "<<nel<<endl; return;}
1232
1233// Create GeneralData and fill it with vectors
1234GeneralFitData* gfd = new GeneralFitData(nvar,nel+5,0);
1235if(nvar==1) gfd->SetData1(nel,x,z,ez); // On remplit Y=f(X)
1236else gfd->SetData2(nel,x,y,z,ez); // On remplit Z=f(X,y)
1237
1238// Menage et table d'objets
1239if( omg.GetObj(nomgfd) != NULL ) omg.DelObj(nomgfd);
[2491]1240MyObjMgr()->AddObj(gfd,nomgfd);
[1067]1241return;
1242}
1243
[333]1244/* --Methode-- */
1245void Services2NObjMgr::ComputeExpressions(NObjMgrAdapter* obja, string& expx,
[357]1246 string& expy, string& expz, string& expt, string& expcut, string& loop,
[333]1247 NTuple* nt, Histo* h1, Histo2D* h2, HProf* hp)
1248{
1249if (obja == NULL) return;
[344]1250bool adel = true;
1251NTupleInterface* objnt = obja->GetNTupleInterface(adel);
[333]1252if (objnt == NULL) return;
1253string vardec = objnt->VarList_C("_zz6qi_");
1254
[2755]1255// Pour synchronisation d'execution simultanee
[2762]1256ZSync zs(mutx_dynlink); zs.NOp();
[2755]1257
[333]1258PlotExprFunc f = LinkExprFunc(vardec, expx, expy, expz, expt, expcut);
1259if (!f) {
1260 cerr << "Services2NObjMgr::::ComputeExpressions() Error Creation PlotExprFunc " << endl;
[344]1261 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1262 return;
1263 }
1264
1265double xnt[10];
1266
[2419]1267int_8 k;
1268for(k=0; k<10; k++) xnt[k] = 0.;
1269int_8 k1,k2,dk;
[357]1270k1 = 0; k2 = objnt->NbLines(); dk = 1;
1271DecodeLoopParameters(loop, k1, k2, dk);
1272if (k1 < 0) k1 = 0;
1273if (k2 < 0) k2 = objnt->NbLines();
[2419]1274if (k2 > (int_8)objnt->NbLines()) k2 = objnt->NbLines();
[357]1275if (dk <= 0) dk = 1;
[500]1276
1277try {
[333]1278 double* xn;
[357]1279 for(k=k1; k<k2; k += dk) {
[333]1280 xn = objnt->GetLineD(k);
[2419]1281 if (f((int_8_exprf)k,xn, xnt, xnt+1, xnt+2, xnt+3) != 0) {
[2668]1282 if (nt) nt->Fill(xnt);
[333]1283 if (h1) h1->Add(xnt[0], xnt[3]);
1284 if (h2) h2->Add(xnt[0], xnt[1], xnt[3]);
1285 if (hp) hp->Add(xnt[0], xnt[1], xnt[3]);
1286 }
1287 }
1288 }
[500]1289#ifdef SANS_EVOLPLANCK
[333]1290CATCH(merr) {
1291 fflush(stdout);
1292 cout << endl;
1293 cerr << endl;
1294 string es = PeidaExc(merr);
1295 cerr << "Services2NObjMgr::ComputeExpressions() Exception :" << merr << es;
1296 } ENDTRY;
[500]1297#else
1298catch ( PException exc ) {
1299 fflush(stdout);
1300 cout << endl;
1301 cerr << endl;
1302 cerr << "Services2NObjMgr::ComputeExpressions() Exception :" << exc.Msg() << endl;
1303}
1304#endif
[333]1305
[344]1306if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1307// Fermeture du fichier .so
1308CloseDLL();
1309return;
1310}
1311
1312
1313/* --Methode-- */
1314PlotExprFunc Services2NObjMgr::LinkExprFunc(string& vardec, string& expx, string& expy,
1315 string& expz, string& expt, string& cut)
1316{
1317FILE *fip;
1318string fname = TmpDir + "expf_pia_dl.c";
1319string cmd;
1320int rc;
1321
1322cmd = "rm -f " + fname;
1323rc = system(cmd.c_str());
1324//DBG printf("LinkExprFunc_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
1325
1326if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1327 string sn = fname;
[449]1328 cout << "Services2NObjMgr/LinkExprFunc_Error: fopen( " << sn << endl;
[333]1329 return(NULL);
1330 }
1331
1332// constitution du fichier a compiler
1333fputs("#include <stdlib.h> \n", fip);
1334fputs("#include <math.h> \n", fip);
[344]1335
[1469]1336fputs("/* ------ Compare bits on double --------- */ \n", fip);
[2419]1337fputs("typedef long long int_8_exprf;\n", fip);
1338fputs("int_8_exprf BitCmp64(double v,int_8_exprf flg)\n", fip);
1339fputs("{return ((int_8_exprf)((v<0.) ? v-0.1 : v+0.1))&flg;}\n", fip);
[344]1340fputs("/* ------ Some random number generators --------- */ \n", fip);
[1931]1341fputs("#if defined(__ppc__) && defined(__MACH__) \n",fip);
1342fputs("#include <limits.h> \n", fip);
1343fputs("#define drand48() ((double)(random())/LONG_MAX) \n",fip);
1344fputs("#endif \n",fip);
[344]1345fputs("#define frand01() ( (float) drand48() ) \n", fip);
1346fputs("#define drand01() drand48() \n", fip);
1347fputs("#define rand01() drand48() \n", fip);
1348fputs("#define frandpm1() ( 2. * frand01() - 1.) \n", fip);
1349fputs("#define drandpm1() ( 2. * drand01() - 1.) \n", fip);
1350fputs("#define randpm1() ( 2. * drand01() - 1.) \n", fip);
1351fputs("double NorRand(void) \n", fip);
1352fputs(" { \n double x,A,B; \n LAB10: \n A = drand01(); \n", fip);
1353fputs(" if ( A == 0. ) goto LAB10; \n B = drand01(); \n", fip);
[345]1354fputs(" x = sqrt(-2.*log(A))*cos(2.*M_PI*B); \n", fip);
[344]1355fputs(" return(x); \n } \n", fip);
1356fputs("#define GauRand() NorRand() \n", fip);
1357fputs("#define gaurand() NorRand() \n\n", fip);
1358
[2419]1359fputs("int expf_pia_dl_func(int_8_exprf _nl, double* _zz6qi_, double* _rx_6q_, double* _ry_6q_, double* _rz_6q_, double* _rt_6q_) \n{\n", fip);
[333]1360fprintf(fip,"%s \n", vardec.c_str());
1361fprintf(fip, "if (!(%s)) { *_rx_6q_ = *_ry_6q_ = *_rz_6q_ = *_rt_6q_ = 0.; return(0); } \n", cut.c_str());
1362fprintf(fip, "*_rx_6q_ = %s ; \n", expx.c_str());
1363fprintf(fip, "*_ry_6q_ = %s ; \n", expy.c_str());
1364fprintf(fip, "*_rz_6q_ = %s ; \n", expz.c_str());
1365fprintf(fip, "*_rt_6q_ = %s ; \n", expt.c_str());
1366fputs("return(1); \n} \n", fip);
1367fclose(fip);
1368string func = "expf_pia_dl_func";
1369return((PlotExprFunc)LinkFunctionFromFile(fname, func));
1370}
1371
1372
1373/* --Methode-- */
1374DlFunction Services2NObjMgr::LinkFunctionFromFile(string const & fname, string const & funcname)
1375{
1376// Le link dynamique
1377CloseDLL();
1378dynlink = PDynLinkMgr::BuildFromCFile(fname);
1379if (dynlink == NULL) {
1380 cerr << "Services2NObjMgr/LinkFunctionFromFile_Erreur: Erreur creation/Ouverture SO " << endl;
1381 return(NULL);
1382 }
1383
1384DlFunction retfunc = dynlink->GetFunction(funcname);
1385if (retfunc == NULL) {
1386 string sn = funcname;
1387 cerr << "Services2NObjMgr/LinkExprFunc_Erreur: Erreur linking " << sn << endl;
1388 CloseDLL();
1389 return(NULL);
1390 }
1391else return(retfunc);
1392}
1393
1394/* --Methode-- */
1395void Services2NObjMgr::CloseDLL()
1396{
1397if (dynlink) delete dynlink; dynlink = NULL;
1398}
1399
[1319]1400/* --Methode-- */
1401int Services2NObjMgr::ExecuteCommand(string line)
1402{
1403 if (mImgapp == NULL) return(99);
1404 return(mImgapp->CmdInterpreter()->Interpret(line));
1405}
1406
[357]1407// Fonction static
1408/* --Methode-- */
[2419]1409void Services2NObjMgr::DecodeLoopParameters(string& loop, int_8& i1, int_8& i2, int_8& di)
[357]1410{
1411// Decode des paramatres de boucle for(int i=i1; i<i2; i+=di) specifies
1412// sous forme i1[:i2[:di]]
1413// cout << "LoopParam() " << loop << " I1=" << i1 << " I2=" << i2 << " DI=" << di;
1414size_t l = loop.length();
1415if (l < 1) return;
1416size_t p = loop.find(':');
[2419]1417if (p >= l) { i1 = atol(loop.c_str()); return; }
1418i1 = atol(loop.substr(0, p).c_str());
[357]1419string aa = loop.substr(p+1);
1420p = aa.find(':');
1421if (p < aa.length() ) {
[2419]1422 i2 = atol(aa.substr(0,p).c_str());
1423 di = atol(aa.substr(p+1).c_str());
[357]1424 }
[2419]1425else i2 = atol(aa.c_str());
[357]1426// cout << "-> I1= " << i1 << " I2= " << i2 << " DI= " << di << endl;
1427return;
1428}
[333]1429
1430/* --Methode-- */
[165]1431string Services2NObjMgr::FileName2Name(string const & fn)
1432{
1433
1434char fsep[2] = {FILESEP, '\0'};
1435char tsep[2] = {'.', '\0'};
1436size_t p = fn.find_last_of(fsep);
[194]1437size_t l = fn.length();
1438if (p >= l) p = 0;
1439else p++;
1440size_t q = fn.find_first_of(tsep,p);
1441if (q < p) q = l;
[165]1442return(fn.substr(p,q-p));
1443}
1444
1445
[546]1446
[165]1447
[333]1448// SANS_EVOLPLANCK Attention !
[1105]1449#ifdef SANS_EVOLPLANCK
[333]1450#include "pclassids.h"
1451
1452/* --Methode-- */
1453char* Services2NObjMgr::PClassIdToClassName(int cid)
1454{
1455switch (cid) {
1456 case ClassId_Poly1 :
1457 return("Poly1");
1458 case ClassId_Poly2 :
1459 return("Poly2");
1460 case ClassId_Matrix :
1461 return("Matrix");
1462 case ClassId_Vector :
1463 return("Vector");
1464
1465 case ClassId_DVList :
1466 return("DVList");
1467
1468 case ClassId_Histo1D :
1469 return("Histo1D");
1470 case ClassId_Histo2D :
1471 return("Histo2D");
1472 case ClassId_HProf :
1473 return("HProf");
[2605]1474 case ClassId_HistoErr :
1475 return("HistoErr");
[333]1476 case ClassId_NTuple :
1477 return("NTuple");
[361]1478 case ClassId_XNTuple :
1479 return("XNTuple");
[333]1480 case ClassId_GeneralFitData :
1481 return("GeneralFitData");
1482
1483 case ClassId_Image :
1484 return("RzImage");
1485 case ClassId_Image + kuint_1 :
1486 return("ImageU1");
1487 case ClassId_Image + kint_1 :
1488 return("ImageI1");
1489 case ClassId_Image + kuint_2 :
1490 return("ImageU2");
1491 case ClassId_Image + kint_2 :
1492 return("ImageI2");
1493 case ClassId_Image + kuint_4 :
1494 return("ImageU4");
1495 case ClassId_Image + kint_4 :
1496 return("ImageI4");
1497 case ClassId_Image + kr_4 :
1498 return("ImageR4");
1499 case ClassId_Image + kr_8 :
1500 return("ImageR8");
1501 case ClassId_ZFidu :
1502 return("ZFidu");
1503
1504 case ClassId_StarList :
1505 return("StarList");
1506 case ClassId_Transfo :
1507 return("Transfo");
1508 case ClassId_PSF :
1509 return("PSF");
1510
1511
1512// - Ajout objet PPF
1513 default:
1514 return("AnyDataObj");
1515 }
1516}
1517
[1105]1518#else
1519char* Services2NObjMgr::PClassIdToClassName(int cid)
1520{
1521 return("AnyDataObj");
1522}
1523#endif
Note: See TracBrowser for help on using the repository browser.