source: Sophya/trunk/SophyaPI/PIext/pawexecut.cc@ 2817

Last change on this file since 2817 was 2817, checked in by cmv, 20 years ago

n/assoc/col merge de colonnes de ntuples avec idexation 1->2 cmv 13/07/05

File size: 79.1 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4#include <iostream>
5#include <typeinfo>
6
7#include "sopnamsp.h"
8#include "strutil.h"
9#include "strutilxx.h"
10#include "histos.h"
11#include "histos2.h"
12#include "hisprof.h"
13#include "histerr.h"
14#include "ntuple.h"
15#include "datatable.h"
16
17#include "pawexecut.h"
18#include "nobjmgr.h"
19#include "servnobjm.h"
20#include "nomgadapter.h"
21#include "pistdimgapp.h"
22#include "pihisto.h"
23
24#ifdef SANS_EVOLPLANCK
25#include "cvector.h"
26#include "matrix.h"
27#else
28#include "tmatrix.h"
29#include "tvector.h"
30#endif
31
32/* Reza + cmv 13/10/99 */
33
34inline bool __vector_sort_up_r8(r_8 x1,r_8 x2) {return x1<x2;}
35inline bool __vector_sort_down_r8(r_8 x1,r_8 x2) {return x1>x2;}
36inline bool __vector_sort_up_r4(r_4 x1,r_4 x2) {return x1<x2;}
37inline bool __vector_sort_down_r4(r_4 x1,r_4 x2) {return x1>x2;}
38inline bool __vector_sort_up_i4(int_4 x1,int_4 x2) {return x1<x2;}
39inline bool __vector_sort_down_i4(int_4 x1,int_4 x2) {return x1>x2;}
40inline bool __vector_sort_up_u8(uint_8 x1,uint_8 x2) {return x1<x2;}
41inline bool __vector_sort_down_u8(uint_8 x1,uint_8 x2) {return x1>x2;}
42
43uint_4 PAWExecutor::autoc_counter_ = 0;
44
45/* methode */
46PAWExecutor::PAWExecutor(PIACmd *piac, PIStdImgApp* app)
47: mApp(app)
48{
49string kw, usage;
50string hgrp = "pawCmd";
51
52kw = "reset";
53usage = "Reset histograms vectors or matrix";
54usage += "\n reset nameobj";
55piac->RegisterCommand(kw,usage,this,hgrp);
56
57kw = "n/plot";
58usage = "Plot NTuple variables \"a la paw\" (alias n/pl)";
59usage += "\n n/plot nameobj.x_exp [cut] [w_exp] [loop] [gratt]";
60usage += "\n n/plot nameobj.y_exp%x_exp [cut] [loop] [gratt]";
61usage += "\n n/plot nameobj.z_exp%y_exp%x_exp [cut] [loop] [gratt]";
62usage += "\n for default use ! , loop=i1[:i2[:di]]";
63usage += "\n for 1 dimensional (1D) projection:";
64usage += "\n use graphic option \"keepbin\" to keep previous";
65usage += "\n plot binning for 1D distribution";
66usage += "\n Related commands: plot2dw plot3d";
67piac->RegisterCommand(kw,usage,this,hgrp);
68
69kw = "n/pl";
70usage = "alias to n/plot (see n/plot)";
71piac->RegisterCommand(kw,usage,this,hgrp);
72
73kw = "n/proj";
74usage = "Project NTuple in histogram (1D or 2D) a la paw";
75usage += "\n n/proj nameproj nameobj.x_exp [cut] [w_exp] [loop] [gratt]";
76usage += "\n n/proj nameproj nameobj.y_exp%x_exp [cut] [w_exp] [loop] [gratt]";
77usage += "\n for default use ! , loop=i1[:i2[:di]]";
78usage += "\n for 1 dimensional (1D) projection:";
79usage += "\n no display is performed if \"nameproj\" is an existing histogram";
80usage += "\n unless a graphic option \"gratt\" is given";
81usage += "\n Related commands: projh1d projh2d projprof exptovec";
82piac->RegisterCommand(kw,usage,this,hgrp);
83
84kw = "n/scan";
85usage = "Scan NTuple a la paw";
86usage += "\n n/scan nameobj[.exp1%exp2%exp3] cut loop";
87usage += "\n [-f:filename] [list_of_variables]";
88usage += "\n loop : iev1[:iev2[:diev]] or !";
89usage += "\n cut : cut expression or 1. or !";
90usage += "\n list_of_variables : default is all variables";
91usage += "\n : var1 var2 var3 ... varn";
92usage += "\n : var1 : var2 (from var1 to var2)";
93usage += "\n : : var2 (from first variable to var2)";
94usage += "\n : var1 : (from var1 to last variable)";
95usage += "\n ex: \"v1 : v3 v7 v4 : v6 v2 v9 :\"";
96usage += "\n exp1%exp2%exp3 :";
97usage += "\n if given add exp1,exp2,exp3 to the variable list";
98usage += "\n -f:filename : write into \"filename\", Default is to stdout";
99piac->RegisterCommand(kw,usage,this,hgrp);
100
101kw = "n/read";
102usage = "Read columns in an ASCII file and fill a NTuple or a DataTable";
103usage += "\n n/read nt fascii [options] var_1,c_1 var_2,c_2 ... var_n,c_n ";
104usage += "\n nt : NTuple or DataTable (see options) name";
105usage += "\n var_i,c_i : variable name, associated column in ASCII file [0,n[";
106usage += "\n where [options] are:";
107usage += "\n \"-dt\": create a DataTable of double instead of a NTuple";
108usage += "\n \"=s\": separator character is \'s\' (could be \"\t\")";
109usage += "\n \"-^abcd\": do not read lines beginning with string \"abcd\" ";
110usage += "\n \"+^abcd\": read only lines beginning with string \"abcd\" ";
111usage += "\n \"-abcd\": do not read lines which contain string \"abcd\" ";
112usage += "\n \"+abcd\": read only lines which contain string \"abcd\" ";
113usage += "\n these options may be repeated (ex: \"-^abcd\" \"-^xyz\") ";
114usage += "\n - in case of \"do not read\" options are added with logical AND ";
115usage += "\n - in case of \"read only\" options are added with logical OR ";
116piac->RegisterCommand(kw,usage,this,hgrp);
117
118kw = "n/merge";
119usage = "Merge ntuples (by adding lines)";
120usage += "\n n/merge nt nt_1 nt_2 ... nt_n";
121usage += "\n Merge ntuples nt_i lines into ntuple nt";
122piac->RegisterCommand(kw,usage,this,hgrp);
123
124kw = "n/merge/col";
125usage = "Merge ntuples (by adding columns for each line)";
126usage += "\n n/merge/col nt nt_1[,ext] nt_2[,ext] ... nt_n[,ext]";
127usage += "\n Merge ntuples nt_i columns into ntuple nt";
128usage += "\n ext : character string to add at the end of the ntuple variable names";
129usage += "\n if \"ext\"=\"!\" no extension is added";
130usage += "\n if no \"ext\" is given, automatic extension \"_0\" \"_1\" \"_2\" ...";
131piac->RegisterCommand(kw,usage,this,hgrp);
132
133kw = "n/assoc/col";
134usage = "Merge ntuples columns with an association index";
135usage += "\n n/assoc/col ntass[,u/i] nt_1[,ext] nt_2[,ext] nt_assoc[,icass]";
136usage += "\n Merge ntuples nt_1 and nt_2 columns into ntuple ntass";
137usage += "\n with line association number given by ntuple nt_assoc";
138usage += "\n ---";
139usage += "\n icass: association number given by column \"icass\" of nt_assoc (def=0)";
140usage += "\n ---";
141usage += "\n ext : character string to add at the end of the ntuple variable names";
142usage += "\n if \"ext\"=\"!\" no extension is added";
143usage += "\n if no \"ext\" is given, automatic extension \"_0\" \"_1\" \"_2\" ...";
144usage += "\n ---";
145usage += "\n u : do the union of all the lines of nt_1 and nt_2";
146usage += "\n (all ssociated and un-associated lines of nt_1 and nt_2)";
147usage += "\n i : do the intersection of all the lines of nt_1 and nt_2";
148usage += "\n (only associated lines of nt_1 and nt_2)";
149usage += "\n def : write all lines of nt_1 with possible associated line of nt_2";
150usage += "\n (number of entries of ntass = number of entries of nt_1)";
151piac->RegisterCommand(kw,usage,this,hgrp);
152
153kw = "n/copy";
154usage = "Copy all or some variables of a ntuple into another new ntuple";
155usage += "\n n/merge ntnew nt [vname1 vname2 vname3 ...]";
156piac->RegisterCommand(kw,usage,this,hgrp);
157
158kw = "h/integ";
159usage = "Integrate a 1D histogram";
160usage += "\n h/integ nameh1d [norm]";
161usage += "\n norm<=0 means no normalisation (def norm=1)";
162usage += "\n Related commands: h/deriv v/integ v/deriv";
163piac->RegisterCommand(kw,usage,this,hgrp);
164
165kw = "v/integ";
166usage = "Integrate a TVector / vector";
167usage += "\n v/integ namevec [norm]";
168usage += "\n norm<=0 means no normalisation (def norm=0)";
169usage += "\n Related commands: h/integ h/deriv v/deriv";
170piac->RegisterCommand(kw,usage,this,hgrp);
171
172kw = "v/sort";
173usage = "Sort a vector into itself";
174usage += "\n v/sort namevec [+1/-1]";
175usage += "\n \"+1\" means increasing order, \"-1\" decreasing order";
176piac->RegisterCommand(kw,usage,this,hgrp);
177
178kw = "h/deriv";
179usage = "Derivate a 1D histogram";
180usage += "\n h/deriv nameh1d";
181usage += "\n Related commands: h/integ v/integ v/deriv";
182piac->RegisterCommand(kw,usage,this,hgrp);
183
184kw = "v/deriv";
185usage = "Derivate a TVector / vector";
186usage += "\n v/deriv namevec [deriv_option]";
187usage += "\n deriv_option -1 replace v[i] with v[i]-v[i-1]";
188usage += "\n 0 replace v[i] with (v[i+1]-v[i-1])/2 (default)";
189usage += "\n +1 replace v[i] with v[i+1]-v[i]";
190usage += "\n Related commands: h/integ h/deriv v/integ";
191piac->RegisterCommand(kw,usage,this,hgrp);
192
193kw = "h/rebin";
194usage = "Rebin a 1D histogram or profile";
195usage += "\n h/rebin nameh1d nbin";
196piac->RegisterCommand(kw,usage,this,hgrp);
197
198kw = "h/cadd";
199usage = "Add a constant to an histogram, a vector or a matrix";
200usage += "\n h/cadd namehisto val";
201usage += "\n Related commands: h/cmult h/oper";
202piac->RegisterCommand(kw,usage,this,hgrp);
203
204kw = "h/cmult";
205usage = "Multiply an histogram, a vector or a matrix by a constant";
206usage += "\n h/cmult namehisto val";
207usage += "\n Related commands: h/cadd h/oper";
208piac->RegisterCommand(kw,usage,this,hgrp);
209
210kw = "h/oper";
211usage = "Operation on histograms vectors or matrices";
212usage += "\n h/oper @ h1 h2 hres";
213usage += "\n hres = h1 @ h2 with @ = (+,-,*,/)";
214usage += "\n For vectors and matrices, operations are elements by elements";
215usage += "\n Related commands: h/cadd h/cmult";
216piac->RegisterCommand(kw,usage,this,hgrp);
217
218kw = "h/plot/2d";
219usage = "Specific plot for 2D histogrammes";
220usage += "\n h/plot/2d nameh2d show : infos on 2D histogramme";
221usage += "\n h/plot/2d nameh2d h [dopt] : plot 2D histogramme";
222usage += "\n h/plot/2d nameh2d px [dopt] : plot X projection";
223usage += "\n h/plot/2d nameh2d py [dopt] : plot Y projection";
224usage += "\n h/plot/2d nameh2d bx n [dopt] : plot X band number n";
225usage += "\n h/plot/2d nameh2d by n [dopt] : plot Y band number n";
226usage += "\n h/plot/2d nameh2d sx n [dopt] : plot X slice number n";
227usage += "\n h/plot/2d nameh2d sy n [dopt] : plot Y slice number n";
228usage += "\n n < 0 means Show Info";
229piac->RegisterCommand(kw,usage,this,hgrp);
230
231kw = "h/put_vec";
232usage = "Put content of vector (matrix) into content of histogram 1D or 2D";
233usage += "\n h/put_vec nameh1d namevector [cont,err2]";
234usage += "\n h/put_vec nameh2d namematrix [cont,err2]";
235usage += "\n cont : put into histogramme content";
236usage += "\n err2 : put into histogramme error^2";
237usage += "\n Related commands: h/get_vec";
238piac->RegisterCommand(kw,usage,this,hgrp);
239
240kw = "h/get_vec";
241usage = "Get content of histogram 1D profile or 2D into vector (matrix)";
242usage += "\n h/get_vec nameh1d namevector [cont,err2,absc] [proj]";
243usage += "\n h/get_vec nameh2d namematrix [cont,err2,absc]";
244usage += "\n cont : get histogramme content";
245usage += "\n err2 : get histogramme error^2";
246usage += "\n absc : get histogramme low bin abscissa (1D only)";
247usage += "\n proj :";
248usage += "\n show : show available projections for Histo2D";
249usage += "\n px : get X projection";
250usage += "\n py : get Y projection";
251usage += "\n bx n : get X band number n";
252usage += "\n by n : get Y band number n";
253usage += "\n sx n : get X slice number n";
254usage += "\n sy n : get Y slice number n";
255usage += "\n Related commands: h/put_vec";
256piac->RegisterCommand(kw,usage,this,hgrp);
257
258kw = "h/copy";
259usage = "Copy content of object1 into object2";
260usage += "\n objects are Vector,Matrix,Histo,Histo2D";
261usage += "\n h/copy namefrom nametocopy [i1[:i2]] [j1[:j2]] [ic1[:jc1]]";
262usage += "\n copy obj1Dfrom(i1->i2) into obj1Dto(ic1->)";
263usage += "\n copy obj2Dfrom(i1,j1->i2,j2) into obj2Dto(ic1,jc1->)";
264usage += "\n Warning: elements from i1 to i2 included are copied";
265usage += "\n If obj1Dto does not exist, is is created with size i2-i1+1";
266usage += "\n or obj2Dto with size i2-i1+1,j2-j1+1";
267usage += "\n The adressed content of obj?Dfrom is overwritten";
268usage += "\n The non-adressed content of obj?Dfrom is left unchanged";
269usage += "\n Related commands: copy";
270piac->RegisterCommand(kw,usage,this,hgrp);
271
272kw = "h/set/err";
273usage = "Set Histo,Histo2D errors for range of bins or range of values";
274usage += "\n h/set/err namehisto setvalue i1[:i2] [j1[:j2]]";
275usage += "\n set error to setvalue for bin range i1:i2 j1:j2";
276usage += "\n h/set/err namehisto setvalue v v1:v2";
277usage += "\n set error to setvalue for content values range v1:v2";
278usage += "\n h/set/err namehisto setvalue e e1:e2";
279usage += "\n set error to setvalue for error values range v1:v2";
280usage += "\n Related commands: h/set/cont";
281piac->RegisterCommand(kw,usage,this,hgrp);
282
283kw = "h/set/cont";
284usage = "Set Histo,Histo2D content for range of bins or range of values";
285usage += "\n h/set/cont namehisto setvalue i1[:i2] [j1[:j2]]";
286usage += "\n set content to setvalue for bin range i1:i2 j1:j2";
287usage += "\n h/set/cont namehisto setvalue v v1:v2";
288usage += "\n set content to setvalue for content values range v1:v2";
289usage += "\n h/set/cont namehisto setvalue e e1:e2";
290usage += "\n set content to setvalue for error values range v1:v2";
291usage += "\n Related commands: h/set/err";
292piac->RegisterCommand(kw,usage,this,hgrp);
293
294kw = "h/err";
295usage = "Set Histo,Histo2D error to function of bin content value";
296usage += "\n h/err namehisto expr_func";
297usage += "\n Related commands: h/set/err";
298piac->RegisterCommand(kw,usage,this,hgrp);
299
300}
301
302/* methode */
303PAWExecutor::~PAWExecutor()
304{
305}
306
307/* methode */
308int PAWExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
309{
310if(kw == "reset") {
311 reset(tokens); return(0);
312} else if(kw == "n/plot" || kw == "n/pl") {
313 n_plot(tokens); return(0);
314} else if(kw == "n/proj") {
315 n_proj(tokens); return(0);
316} else if(kw == "n/scan") {
317 n_scan(tokens); return(0);
318} else if(kw == "n/read") {
319 n_read(tokens); return(0);
320} else if(kw == "n/merge") {
321 n_merge(tokens); return(0);
322} else if(kw == "n/merge/col") {
323 n_merge_col(tokens); return(0);
324} else if(kw == "n/assoc/col") {
325 n_assoc_col(tokens); return(0);
326} else if(kw == "n/copy") {
327 n_copy(tokens); return(0);
328} else if(kw == "h/integ") {
329 h_integ(tokens); return(0);
330} else if(kw == "v/integ") {
331 v_integ(tokens); return(0);
332} else if(kw == "v/sort") {
333 v_sort(tokens); return(0);
334} else if(kw == "h/deriv") {
335 h_deriv(tokens); return(0);
336} else if(kw == "v/deriv") {
337 v_deriv(tokens); return(0);
338} else if(kw == "h/rebin") {
339 h_rebin(tokens); return(0);
340} else if(kw == "h/cadd") {
341 h_cadd(tokens); return(0);
342} else if(kw == "h/cmult") {
343 h_cmult(tokens); return(0);
344} else if(kw == "h/oper") {
345 h_oper(tokens); return(0);
346} else if(kw == "h/plot/2d") {
347 h_plot_2d(tokens); return(0);
348} else if(kw == "h/put_vec") {
349 h_put_vec(tokens); return(0);
350} else if(kw == "h/get_vec") {
351 h_get_vec(tokens); return(0);
352} else if(kw == "h/copy") {
353 h_copy(tokens); return(0);
354} else if(kw == "h/set/err") {
355 string dum = "err";
356 h_set(dum,tokens); return(0);
357} else if(kw == "h/set/cont") {
358 string dum = "cont";
359 h_set(dum,tokens); return(0);
360} else if(kw == "h/err") {
361 h_err(tokens); return(0);
362} else return(1);
363}
364
365/* methode */
366bool PAWExecutor::IsThreadable(string const & keyw)
367{
368 if ( (keyw == "n/plot") || (keyw == "n/pl") ||
369 (keyw == "n/proj") ) return true;
370 else return false;
371}
372
373
374/* methode */
375void PAWExecutor::reset(vector<string>& tokens)
376// Reset d'histogrammes, vecteurs et matrices
377{
378if(tokens.size() < 1)
379 {cout<<"Usage: reset nameobj"<<endl; return;}
380NamedObjMgr omg;
381AnyDataObj* mobj = omg.GetObj(tokens[0]);
382if(mobj == NULL)
383 {cout<<"PAWExecutor::reset Error , Pas d'objet de nom "<<tokens[0]<<endl;
384 return;}
385string ctyp = typeid(*mobj).name();
386
387#ifdef SANS_EVOLPLANCK
388if(typeid(*mobj)==typeid(Vector)) {Vector* ob=(Vector*) mobj; ob->Zero();}
389else if(typeid(*mobj)==typeid(Matrix)) {Matrix* ob=(Matrix*) mobj; ob->Zero();}
390#else
391 if(typeid(*mobj)==typeid(Vector)) {Vector* ob=(Vector*) mobj; (*ob) = 0.; }
392// ob->DataBlock().Reset(0.);}
393 else if(typeid(*mobj)==typeid(Matrix)) {Matrix* ob=(Matrix*) mobj; (*ob) = 0.; }
394//ob->DataBlock().Reset(0.);}
395#endif
396else if(typeid(*mobj)==typeid(Histo)) {Histo* ob=(Histo*) mobj; ob->Zero();}
397else if(typeid(*mobj)==typeid(HProf)) {HProf* ob=(HProf*) mobj; ob->Zero();}
398else if(typeid(*mobj)==typeid(HistoErr)) {HistoErr* ob=(HistoErr*) mobj; ob->Zero();}
399else if(typeid(*mobj)==typeid(Histo2D)) {Histo2D* ob=(Histo2D*)mobj; ob->Zero();}
400else {
401 cout<<"PAWExecutor::reset Error , No reset possible on "<<ctyp<<endl;
402 return;
403}
404
405return;
406}
407
408/* methode */
409void PAWExecutor::n_plot(vector<string>& tokens)
410// Equivalent n/plot de paw
411// Plot 1D
412// n/plot nameobj.x_exp [cut] [w_exp] [gratt]
413// Plot 2D (plot2dw)
414// n/plot nameobj.y_exp%x_exp [cut] [w_exp] [gratt]
415// Plot 3D (plot3d)
416// n/plot nameobj.z_exp%y_exp%x_exp [cut] [gratt]
417{
418if(tokens.size() < 1) {
419 cout
420 <<"Usage: n/plot nameobj.x_exp [cut] [w_exp] [loop] [gratt] [nomh1]"<<endl
421 <<" n/plot nameobj.y_exp%x_exp [cut] [loop] [gratt]"<<endl
422 <<" n/plot nameobj.z_exp%y_exp%x_exp [cut] [loop] [gratt]"<<endl
423 <<" for default use ! , loop=i1[:i2[:di]]"<<endl;
424 return;
425}
426string nameobj,expx,expy,expz;
427int_4 nvar = decodepawstring(tokens[0],nameobj,expx,expy,expz);
428string expcut = "1"; string expwt = "1."; string loop = "";
429string dopt = ""; string nameproj="";
430if(tokens.size()>=2) expcut = tokens[1]; if(expcut=="!") expcut="1";
431
432NamedObjMgr omg;
433Services2NObjMgr* srvo = omg.GetServiceObj();
434
435if(nvar<=0) {
436 cout<<"PAWExecutor::n_plot Error: bad coding "<<tokens[0]<<endl;
437} else if(nvar==1) { // c'est un plot 1D
438 if(tokens.size()>=3) expwt = tokens[2]; if(expwt=="!") expwt="1.";
439 if(tokens.size()>=4) loop = tokens[3]; if(loop=="!") loop="";
440 if(tokens.size()>=5) dopt = tokens[4]; if(dopt=="!") dopt="";
441 if(tokens.size()>=6) nameproj = tokens[5];
442 if(nameproj.length()<=0 || nameproj=="!") {
443 nameproj = "/autoc/paw_n_plot1D_";
444 AnyDataObj* mobj = omg.GetObj(nameproj);
445 if(mobj!=NULL) {
446 char buff[16]; autoc_counter_++; sprintf(buff,"%d",autoc_counter_);
447 nameproj += buff;
448 }
449 // --- Si option "keepbin" on projete avec le meme binning que l'histo 1D precedent
450 if(dopt.find("keepbin")<dopt.size()) {
451 PIStdImgApp* piimapp = omg.GetImgApp();
452 if(piimapp) {
453 PIBaseWdg* pibwdg = piimapp->CurrentBaseWdg();
454 if(pibwdg) {
455 int nbdrw = pibwdg->NbDrawers();
456 if(nbdrw>0) {
457 for(int i=nbdrw-1;i>=0;i--) {
458 PIDrawer* pidwr = pibwdg->GetDrawer(i);
459 PIHisto* pih = NULL;
460 if( (pih = dynamic_cast<PIHisto *>(pidwr))==NULL ) continue;
461 Histo* h = pih->Histogram();
462 if(h==NULL) continue;
463 if(h->NBins()<1) continue;
464 Histo* hsame = new Histo(h->XMin(),h->XMax(),h->NBins());
465 omg.AddObj(hsame,nameproj);
466 break;
467 }
468 }
469 }
470 }
471 }
472 // ---
473 }
474 srvo->ProjectH1(nameobj,expx,expwt,expcut,nameproj,dopt,loop);
475} else if(nvar==2) { // c'est un plot 2D
476 if(tokens.size()>=3) loop = tokens[2]; if(loop=="!") loop="";
477 if(tokens.size()>=4) dopt = tokens[3];
478 string err = "";
479 srvo->DisplayPoints2D(nameobj,expx,expy,err,err,expcut,dopt,loop);
480} else { // c'est un plot 3D
481 if(tokens.size()>=3) loop = tokens[2]; if(loop=="!") loop="";
482 if(tokens.size()>=4) dopt = tokens[3];
483 srvo->DisplayPoints3D(nameobj,expx,expy,expz,expcut,dopt,loop);
484}
485
486return;
487}
488
489
490/* methode */
491void PAWExecutor::n_proj(vector<string>& tokens)
492// Equivalent n/proj de paw
493// Project NTuple in histogram a la paw
494// Dans un Histo 1D
495// n/proj nameproj nameobj.x_exp [cut] [w_exp] [gratt]
496// Dans un Histo 2D ou un HProf (dans ce cas nameproj doit etre cree).
497// n/proj nameproj nameobj.y_exp%x_exp [cut] [w_exp] [gratt]
498{
499if(tokens.size()<2)
500 {cout<<"Usage: n/proj nameproj nameobj.[y_exp%]x_exp [cut] [w_exp] [loop] [gratt]"<<endl
501 <<" for default use ! , loop=i1[:i2[:di]]"<<endl; return;}
502string nameproj = tokens[0];
503string nameobj,expx,expy,expz;
504int_4 nvar = decodepawstring(tokens[1],nameobj,expx,expy,expz);
505string expcut = "1"; string expwt = "1."; string loop = ""; string dopt = "";
506if(tokens.size()>=3) expcut = tokens[2]; if(expcut=="!") expcut="1";
507if(tokens.size()>=4) expwt = tokens[3]; if(expwt=="!") expwt="1.";
508if(tokens.size()>=5) loop = tokens[4]; if(loop=="!") loop="";
509if(tokens.size()>=6) dopt = tokens[5];
510
511NamedObjMgr omg;
512Services2NObjMgr* srvo = omg.GetServiceObj();
513
514if(nvar==1) {
515 // c'est une projection dans un histo 1D
516 srvo->ProjectH1(nameobj,expx,expwt,expcut,nameproj,dopt,loop);
517} else if(nvar==2) {
518 // c'est une projection dans un histo2D
519 // OU un HProf si nameproj est un HProf un deja defini
520 AnyDataObj* mobj = omg.GetObj(nameproj);
521 if(mobj==NULL)
522 srvo->ProjectH2(nameobj,expx,expy,expwt,expcut,nameproj,dopt,loop);
523 else if(dynamic_cast<HProf*>(mobj))
524 srvo->ProjectHProf(nameobj,expx,expy,expwt,expcut,nameproj,dopt,loop);
525 else
526 srvo->ProjectH2(nameobj,expx,expy,expwt,expcut,nameproj,dopt,loop);
527} else {
528 cout<<"PAWExecutor::n_proj Error: bad coding "<<tokens[1]<<" nvar="<<nvar<<endl;
529}
530
531return;
532}
533
534/* methode */
535void PAWExecutor::n_scan(vector<string>& tokens)
536{
537if(tokens.size()<3)
538 {cerr<<"Usage: n/scan nameobj[.exp1%exp2%exp3] cut loop\n"
539 <<" [-f:filename] [list_of_variables]"<<endl; return;}
540
541// decodage des premiers arguments
542string nameobj,expr[4];
543int_4 nexpr = decodepawstring(tokens[0],nameobj,expr[0],expr[1],expr[2]);
544if(nexpr<4) {for(int_4 i=nexpr;i<4;i++) expr[i]="1.";}
545string expcut = tokens[1]; if(expcut=="!") expcut="1";
546string loop = tokens[2]; if(loop=="!") loop="";
547FILE* fout = NULL;
548uint_4 ldebvar = 3;
549if(tokens.size()>3) {
550 if(tokens[3].find("-f:") == 0) {
551 string filename = tokens[3].substr(3);
552 if(filename.size()>0) {
553 fout = fopen(filename.c_str(),"w");
554 if(fout==NULL)
555 {cerr<<"PAWExecutor::n_scan Error, file "<<filename
556 <<" not opened"<<endl; return;}
557 }
558 ldebvar++;
559 }
560}
561
562// ntuple adaptateur
563NamedObjMgr omg;
564Services2NObjMgr& srvo = *omg.GetServiceObj();
565NObjMgrAdapter* obja = omg.GetObjAdapter(nameobj); // Ne pas deleter
566if(obja == NULL)
567 {cerr<<"PAWExecutor::n_scan Error, ObjAdapter==NULL for "
568 <<nameobj<<endl; return;}
569bool adel = true;
570NTupleInterface* objnt = obja->GetNTupleInterface(adel);
571if(objnt == NULL)
572 {cerr<<"PAWExecutor::n_scan Error, NTupleInterface==NULL for "
573 <<nameobj<<endl; return;}
574
575// function pour le choix
576string vardec = objnt->VarList_C("_zz6qi_");
577PlotExprFunc f = srvo.LinkExprFunc(vardec,expr[0],expr[1],expr[2],expr[3],expcut);
578if(!f)
579 {cerr<<"PAWExecutor::n_scan Error, Creation PlotExprFunc"<<endl;
580 if(adel) delete objnt; if(fout) fclose(fout); return;}
581
582// variables a imprimer
583// "rien" --> de 0 a ncol-1 (toutes les variables)
584// : v1 --> de 0 a v1
585// v1 : --> de v1 a ncol-1
586// v1 : v2 --> de v1 a v2 (si v2 apres v1)
587// v1 et v2 (si v2 avant v1)
588// v1 v2 v3 v4 --> v1 v2 v3 v4 (ordre indifferent)
589// et toute combinaison : "v1 : v3 v7 v4 : v6 v2 v9 :"
590// --> v1 v2 v3 v7 v4 v5 v6 v2 v9 v10...v(ncol-1)
591int_4 ncol = objnt->NbColumns();
592if(ncol<=0)
593 {cerr<<"PAWExecutor::n_scan Error, no columns for NTuple"<<endl;
594 return;}
595vector<int_4> varnum;
596if(ldebvar>=tokens.size()) { // Toutes les variables
597 for(int_4 i=0;i<ncol;i++) varnum.push_back(i);
598} else { // Choix de certaines variables
599 int_4 k,klast,kk; bool frlast=false;
600 if(tokens[ldebvar]==":") {varnum.push_back(0); frlast=true;}
601 else {k = objnt->ColumnIndex(tokens[ldebvar]); varnum.push_back(k);}
602 ldebvar++;
603 if(ldebvar<tokens.size()) for(uint_4 i=ldebvar;i<tokens.size();i++) {
604 if(tokens[i]!=":") { // pas un separateur
605 k = klast = objnt->ColumnIndex(tokens[i]);
606 if(frlast) klast = varnum[varnum.size()-1] + 1;
607 if(klast>k) klast=k;
608 for(kk=klast;kk<=k;kk++) varnum.push_back(kk);
609 frlast=false;
610 } else if(i==tokens.size()-1) { // separateur a la fin
611 k = ncol-1;
612 klast = varnum[varnum.size()-1] + 1;
613 if(klast>k) klast=k;
614 for(kk=klast;kk<=k;kk++) varnum.push_back(kk);
615 } else frlast=true; // separateur pas a la fin
616 }
617}
618
619vector<string> varname;
620if(varnum.size()>0) for(int_4 i=0;i<(int)varnum.size();i++) {
621 if(varnum[i]<0 || varnum[i]>=ncol)
622 {cerr<<"PAWExecutor::n_scan Error, bad variable name at pos "
623 <<i<<endl; if(adel) delete objnt; if(fout) fclose(fout); return;}
624 string dum = objnt->ColumnName(varnum[i]);
625 varname.push_back(dum);
626}
627
628// evenements a utiliser
629int_8 k1=0, k2=objnt->NbLines(), dk=1;
630srvo.DecodeLoopParameters(loop,k1,k2,dk);
631if (k1<0) k1=0;
632if (k2<0) k2=objnt->NbLines();
633if (k2>(int_8)objnt->NbLines()) k2=objnt->NbLines();
634if (dk<=0) dk=1;
635
636// boucle sur les evenements et print
637try {
638 int_4 i;
639 if(fout) fprintf(fout,"#ev "); else printf("#ev ");
640 for(i=0;i<(int)varname.size();i++)
641 if(fout) fprintf(fout,"%s ",varname[i].c_str());
642 else printf( "%s ",varname[i].c_str());
643 if(nexpr>0) for(i=0;i<nexpr;i++)
644 if(fout) fprintf(fout,"%s ",expr[i].c_str());
645 else printf( "%s ",expr[i].c_str());
646 if(fout) fprintf(fout,"\n"); else printf("\n");
647
648 double xnt[5]={0,0,0,0,0};
649 double* xn;
650 for(int_8 k=k1; k<k2; k += dk) {
651 xn = objnt->GetLineD(k);
652 if(f((int_8_exprf)k,xn,xnt,xnt+1,xnt+2,xnt+3) != 0) {
653 if(fout) fprintf(fout,"%ld ",(long)k); else printf("%ld ",(long)k);
654 for(i=0;i<(int)varnum.size();i++) {
655 if(fout) fprintf(fout,"%g ",*(xn+varnum[i]));
656 else printf( "%g ",*(xn+varnum[i]));
657 }
658 if(nexpr>0) for(i=0;i<nexpr;i++) {
659 if(fout) fprintf(fout,"%g ",*(xnt+i));
660 else printf( "%g ",*(xnt+i));
661 }
662 if(fout) fprintf(fout,"\n"); else printf("\n");
663 }
664 }
665} // fin du try
666#ifdef SANS_EVOLPLANCK
667CATCH(merr) {
668 fflush(stdout); cout<<endl; cerr<<endl;
669 string es = PeidaExc(merr);
670 cerr<<"Services2NObjMgr::ComputeExpressions() Exception :"<<merr<<es;
671} ENDTRY;
672#else
673catch ( PException exc ) {
674 fflush(stdout); cout<<endl; cerr<<endl;
675 cerr<<"Services2NObjMgr::ComputeExpressions() Exception :"<<exc.Msg()<<endl;
676}
677#endif
678
679if(adel) delete objnt;
680if(fout) fclose(fout);
681srvo.CloseDLL(); // Fermeture du fichier .so
682return;
683}
684
685
686/* methode */
687#define __LENLINE_N_READ__ 8192
688void PAWExecutor::n_read(vector<string>& tokens)
689{
690 int lp=1;
691
692 if(tokens.size()<3) {
693 cerr<<"Usage: n/read nt fascii [options] var_1,c_1 ... var_n,c_n"<<endl;
694 return;
695 }
696
697 // decodage des arguments
698 string nament = tokens[0];
699 string nameascii = tokens[1];
700 bool create_datatable = false;
701 vector<string> donotreadbeg;
702 vector<string> donotreadin;
703 vector<string> onlyreadbeg;
704 vector<string> onlyreadin;
705 vector<string> varname;
706 vector<int> colnum;
707 char separator = ' ';
708 int numcolmaxi=-1;
709
710 for(int i=2;i<(int_4)tokens.size();i++) {
711 int lc = tokens[i].size();
712 if(lc<2) continue;
713 if(tokens[i].find("-dt")==0) { // create DataTable instead of NTuple
714 create_datatable = true;
715 continue;
716 }
717 const char *c = tokens[i].c_str();
718 if(c[0]=='=') { // Separator
719 separator = c[1];
720 if(lc==3) if(c[1]=='\\' && c[2]=='t') separator = '\t';
721 continue;
722 }
723 if(c[0]=='+') { // Selection des lignes a lire
724 if(c[1]!='^') onlyreadin.push_back(&c[1]);
725 else if(lc>2) onlyreadbeg.push_back(&c[2]);
726 continue;
727 }
728 if(c[0]=='-') { // Selection des lignes commentaire
729 if(c[1]!='^') donotreadin.push_back(&c[1]);
730 else if(lc>2) donotreadbeg.push_back(&c[2]);
731 continue;
732 }
733 // decodage des noms de variables et des colonnes associees
734 int p = tokens[i].find(',');
735 if(p<1 || p>=lc-1) continue;
736 string vn = tokens[i].substr(0,p);
737 string cn = tokens[i].substr(p+1,lc-p-1);
738 int ic = atoi(cn.c_str());
739 if(ic<0) continue;
740 if( !isalpha(vn[0]) ) continue;
741 if(ic>numcolmaxi) numcolmaxi = ic;
742 varname.push_back(vn);
743 colnum.push_back(ic);
744 }
745
746 int nvar = varname.size();
747 if(nvar<=0) {
748 cerr<<"n_read: no variables to be read"<<endl;
749 return;
750 }
751
752 // Print what has to be done
753 if(lp) {
754 if(onlyreadin.size()>0) {
755 cout<<"n_read Only read line containing ["<<onlyreadin.size()<<"]:";
756 for(int i=0;i<(int_4)onlyreadin.size();i++) cout<<" \'"<<onlyreadin[i]<<"\'";
757 cout<<endl;
758 }
759 if(onlyreadbeg.size()>0) {
760 cout<<"n_read Only read line begining with ["<<onlyreadbeg.size()<<"]:";
761 for(int i=0;i<(int_4)onlyreadbeg.size();i++) cout<<" \'"<<onlyreadbeg[i]<<"\'";
762 cout<<endl;
763 }
764 if(donotreadin.size()>0) {
765 cout<<"n_read Do not read line containing ["<<donotreadin.size()<<"]:";
766 for(int i=0;i<(int_4)donotreadin.size();i++) cout<<" \'"<<donotreadin[i]<<"\'";
767 cout<<endl;
768 }
769 if(donotreadbeg.size()>0) {
770 cout<<"n_read Do not read line begining with ["<<donotreadbeg.size()<<"]:";
771 for(int i=0;i<(int_4)donotreadbeg.size();i++) cout<<" \'"<<donotreadbeg[i]<<"\'";
772 cout<<endl;
773 }
774 if(nvar>0) {
775 cout<<"n_read Number of variables to be read: "<<nvar<<endl;
776 for(int i=0;i<nvar;i++) cout<<" \'"<<varname[i].c_str()<<","<<colnum[i]<<"\'";
777 cout<<endl;
778 }
779 if(separator!=' ') cout<<"n_read Separator is: \'"<<separator<<"\'"<<endl;
780 }
781
782 // Open ASCII file
783 FILE * fascii = fopen(nameascii.c_str(),"r");
784 if(fascii==NULL) {
785 cerr<<"n_read: cannot open file "<<nameascii<<endl;
786 return;
787 }
788
789 // Creation du NTuple ou de la DataTable
790 NTuple *nt = NULL;
791 DataTable *dt = NULL;
792 if(!create_datatable) { //create NTuple
793 char** ntvn = new char*[nvar];
794 for(int i=0;i<nvar;i++) ntvn[i] = const_cast<char *>(varname[i].c_str());
795 nt = new NTuple(nvar,ntvn);
796 delete [] ntvn;
797 } else { //create DataTable
798 dt = new DataTable();
799 for(int i=0;i<nvar;i++) dt->AddDoubleColumn(varname[i]);
800 }
801 r_8 *xnt = new r_8[nvar];
802
803 // Read file
804 char *line = new char[__LENLINE_N_READ__];
805 int nline=0, nlinecom=0;
806 while(fgets(line,__LENLINE_N_READ__,fascii) != NULL ) {
807 nline++;
808 uint_4 lc = strlen(line); if(lc<1) continue;
809 // Pour enlever le \n final
810 if(line[lc-1]=='\n' || line[lc-1]=='\r')
811 {line[lc-1]='\0'; lc = strlen(line); if(lc<1) continue;}
812
813 string const sline(line);
814 //cout<<"\'"<<sline<<"\' lc="<<lc<<endl;
815
816 // Faut t'il lire cette ligne ?
817 bool read_line_1 = true;
818 if(onlyreadin.size()>0 || onlyreadbeg.size()>0) read_line_1 = false;
819 if(onlyreadin.size()>0) {
820 for(int i=0;i<(int_4)onlyreadin.size();i++) {
821 uint_4 p = sline.find(onlyreadin[i].c_str());
822 if(p>=lc) continue;
823 read_line_1 = true;
824 break;
825 }
826 }
827 if(onlyreadbeg.size()>0) {
828 for(int i=0;i<(int_4)onlyreadbeg.size();i++) {
829 uint_4 p = sline.find(onlyreadbeg[i].c_str());
830 if(p!=0) continue;
831 read_line_1 = true;
832 break;
833 }
834 }
835
836 // Faut t'il ne pas lire cette ligne ?
837 bool read_line_2 = true;
838 if(donotreadin.size()>0) {
839 for(int i=0;i<(int_4)donotreadin.size();i++) {
840 uint_4 p = sline.find(donotreadin[i].c_str());
841 if(p>=lc) continue;
842 read_line_2 = false;
843 break;
844 }
845 }
846 if(donotreadbeg.size()>0) {
847 for(int i=0;i<(int_4)donotreadbeg.size();i++) {
848 uint_4 p = sline.find(donotreadbeg[i].c_str());
849 if(p!=0) continue;
850 read_line_2 = false;
851 break;
852 }
853 }
854 if(!read_line_2) nlinecom++;
855
856 if(!read_line_1 || !read_line_2) continue;
857
858 // Decodage de la ligne
859 vector<string> vs;
860 //FillVStringFrString(sline,vs,separator);
861 SplitStringToVString(sline,vs,separator);
862 int lvs = vs.size();
863 //for(int i=0;i<lvs;i++) cout<<"|"<<vs[i]<<"| "; cout<<endl;
864 if(lvs<numcolmaxi) continue; // Pas assez de champs decodes, mauvaise ligne
865
866 // Remplissage du NTuple ou de la DataTable
867 for(int i=0;i<nvar;i++) {
868 xnt[i] = 0.;
869 int ic = colnum[i];
870 if(ic>=lvs) continue;
871 xnt[i] = atof(vs[ic].c_str());
872 }
873 if(nt) nt->Fill(xnt);
874 else if(dt) dt->Fill(xnt);
875 //cout<<"...xnt"; for(int i=0;i<nvar;i++) cout<<" "<<xnt[i]; cout<<endl;
876
877 }
878 int_4 nentries = (nt)? nt->NEntry(): dt->NEntry();
879 cout<<"n_read: "<<nline<<" lines in file, "
880 <<nlinecom<<" commentary, "
881 <<nentries<<" object entries"<<endl;
882
883 // On sauve le NTuple/DataTable si besoin, on ferme et detruit ce qu'il faut
884 NamedObjMgr omg;
885 if(nentries>0) {
886 if(nt) omg.AddObj(nt,nament);
887 if(dt) omg.AddObj(dt,nament);
888 } else {
889 if(nt) delete nt;
890 if(dt) delete dt;
891 }
892 delete [] xnt;
893 delete [] line;
894 fclose(fascii);
895
896 return;
897}
898#undef __LENLINE_N_READ__
899
900/* methode */
901void PAWExecutor::n_merge(vector<string>& tokens)
902{
903 if(tokens.size()<2) {
904 cerr<<"Usage: n/merge nt nt_1 nt_2 ... nt_n"<<endl;
905 return;
906 }
907
908 NamedObjMgr omg;
909
910 // decodage des arguments
911 string nament = tokens[0];
912
913 // boucle sur les ntuples
914 NTuple * nt = NULL;
915 int nvar=0, nfill=0;
916 r_8 *xnt=NULL;
917
918 for(int i=1;i<(int_4)tokens.size();i++) {
919
920 AnyDataObj* mobj = omg.GetObj(tokens[i]);
921 if(mobj==NULL) {
922 cout<<"n_merge Error: unknow object"<<tokens[i]<<endl;
923 continue;
924 }
925 NTuple* nt1 = dynamic_cast<NTuple*>(mobj);
926 if(nt1==NULL) {
927 cout<<"n_merge Error: "<<tokens[i]<<" not a NTuple"<<endl;
928 continue;
929 }
930 if(nt1->NEntry()==0) {
931 cout<<"n_merge Error: "<<tokens[i]<<" is empty"<<endl;
932 continue;
933 }
934 if(nt1->NVar()==0) {
935 cout<<"n_merge Error: "<<tokens[i]<<" has no variable"<<endl;
936 continue;
937 }
938
939 // create receiving ntuple if first pass
940 if(nt==NULL) {
941 nvar = nt1->NVar();
942 vector<string> sntvn;
943 for(int iv=0;iv<nvar;iv++) sntvn.push_back(nt1->ColumnName(iv));
944 char **ntvn = new char*[nvar];
945 for(int iv=0;iv<nvar;iv++) ntvn[iv] = const_cast<char *>(sntvn[iv].c_str());
946 nt = new NTuple(nvar,ntvn);
947 delete [] ntvn;
948 }
949
950 // filling with current ntuple
951 int nvar1 = nt1->NVar();
952 int n = (nvar1>nvar)? nvar1: nvar;
953 r_8 *xnt1 = new r_8[n];
954 for(int iev=0;iev<n;iev++) xnt1[iev]=0.;
955 for(uint_4 iev=0;iev<(uint_4)nt1->NEntry();iev++) {
956 nt1->GetVecD(iev,xnt1);
957 nt->Fill(xnt1);
958 }
959 nfill++;
960 delete [] xnt1;
961
962 }
963
964 if(xnt!=NULL) delete [] xnt;
965 if(nt!=NULL) {
966 cout<<"n_merge: ntuple filled with "<<nfill
967 <<" ntuples, "<<nt->NEntry()<<" entries"<<endl;
968 if(nt->NEntry()>0) omg.AddObj(nt,nament); else delete nt;
969 }
970 return;
971}
972
973/* methode */
974void PAWExecutor::n_merge_col(vector<string>& tokens)
975{
976 if(tokens.size()<2) {
977 cerr<<"Usage: n/merge/col nt nt_1[,ext] nt_2[,ext] ... nt_n[,ext]"<<endl;
978 return;
979 }
980
981 NamedObjMgr omg;
982
983 // decodage des arguments
984 string nament = tokens[0];
985
986 // boucle sur les ntuples
987 uint_4 numnt=0, nentmax=0, ipos=0;
988 vector<string> varname;
989 vector<string> ntname;
990 vector<int> posfirst;
991 for(int i=1;i<(int_4)tokens.size();i++) {
992
993 // Decode NTuple name and extension
994 int_4 lc = tokens[i].size();
995 string ntn = tokens[i];
996 string ext = "";
997 int p = ntn.find(',');
998 if(p>=0 && p<lc) {
999 if(p<lc-1) ext = ntn.substr(p+1,lc-p-1);
1000 if(p>0) ntn = ntn.substr(0,p); else ntn = "";
1001 }
1002 // choose the extentsion: given,automatic,none
1003 if(ext.size()<1) {
1004 char str[8]; sprintf(str,"_%d",numnt);
1005 ext = str;
1006 } else if(ext[0] == '!') {
1007 ext = "";
1008 }
1009
1010 AnyDataObj* mobj = omg.GetObj(ntn);
1011 if(mobj==NULL) {
1012 cout<<"n_merge_col Error: unknow object "<<ntn<<" ("<<tokens[i]<<")"<<endl;
1013 continue;
1014 }
1015 NTuple* nt1 = dynamic_cast<NTuple*>(mobj);
1016 if(nt1==NULL) {
1017 cout<<"n_merge_col Error: "<<ntn<<" not a NTuple"<<endl;
1018 continue;
1019 }
1020 if(nt1->NEntry()==0) {
1021 cout<<"n_merge_col Error: "<<ntn<<" is empty"<<endl;
1022 continue;
1023 }
1024 if(nt1->NVar()==0) {
1025 cout<<"n_merge_col Error: "<<ntn<<" has no variable"<<endl;
1026 continue;
1027 }
1028
1029 ntname.push_back(ntn);
1030 posfirst.push_back(ipos);
1031 if(nt1->NEntry()>(int_4)nentmax) nentmax = nt1->NEntry();
1032 for(int iv=0;iv<nt1->NVar();iv++) {
1033 string str = nt1->ColumnName(iv) + ext;
1034 varname.push_back(str);
1035 }
1036 numnt++;
1037 ipos += nt1->NVar();
1038 }
1039
1040 if(ntname.size()==0) {
1041 cout<<"n_merge_col Error: no ntuple found in argument list"<<endl;
1042 return;
1043 } else cout<<"Number of NTuple found "<<ntname.size()
1044 <<", Number of variables "<<varname.size()
1045 <<", Max_entries "<<nentmax<<endl;
1046
1047 // create receiving ntuple
1048 int nvar = varname.size();
1049 char **ntvn = new char*[nvar];
1050 for(int iv=0;iv<nvar;iv++) ntvn[iv] = const_cast<char *>(varname[iv].c_str());
1051 NTuple * nt = new NTuple(nvar,ntvn);
1052 //for(int i=0;i<(int_4)ntname.size();i++) cout<<ntname[i]<<" , p="<<posfirst[i]<<endl;
1053 delete [] ntvn;
1054
1055 // filling with current ntuple
1056 r_8 *xnt = new r_8[nvar];
1057 for(int iev=0;iev<(int_4)nentmax;iev++) {
1058 for(int i=0;i<nvar;i++) xnt[i]=0.;
1059 for(int i=0;i<(int_4)ntname.size();i++) {
1060 AnyDataObj* mobj = omg.GetObj(ntname[i]);
1061 NTuple* nt1 = dynamic_cast<NTuple*>(mobj);
1062 if(iev < nt1->NEntry()) nt1->GetVecD(iev,&xnt[posfirst[i]]);
1063 }
1064 nt->Fill(xnt);
1065 }
1066
1067 // Ending and saving merging ntuple columns
1068 delete [] xnt;
1069 cout<<"n_merge_col: ntuple filled with "<<ntname.size()
1070 <<" ntuples, "<<nt->NEntry()<<" entries"<<endl;
1071 if(nt->NEntry()>0) omg.AddObj(nt,nament); else delete nt;
1072
1073 return;
1074}
1075
1076/* methode */
1077void PAWExecutor::n_assoc_col(vector<string>& tokens)
1078{
1079 if(tokens.size()<4) {
1080 cerr<<"Usage: n/assoc/col ntass[,u] nt_1[,ext] nt_2[,ext] nt_assoc[,icass]"<<endl;
1081 return;
1082 }
1083
1084 NamedObjMgr omg;
1085
1086 // decodage du nom du ntuple final associe
1087 // do_merge = 0 : on remplit toutes les lignes de nt_1 associees ou non
1088 // lignes associees + non-associees de nt_1
1089 // -> ntass.NEntry == nt_1.NEntry
1090 // = 1 : on remplit uniquement les lignes associees
1091 // lignes associees uniquement
1092 // -> ntass.NEntry <= min(nt_1.NEntry,nt_2.NEntry)
1093 // = 2 : on remplit l'union des lignes
1094 // lignes associees + non-associees de nt_1 + non-associees de nt_2
1095 // -> ntass.NEntry >= max(nt_1.NEntry,nt_2.NEntry)
1096 uint_2 do_merge=0;
1097 string ntmerge = tokens[0];
1098 int lc = tokens[0].size();
1099 int p = tokens[0].find(',');
1100 if(p>=0 && p<lc) {
1101 ntmerge = ntmerge.substr(0,p);
1102 p = tokens[0].find(",u");
1103 if(p>=0 && p<lc) do_merge = 2;
1104 p = tokens[0].find(",i");
1105 if(p>=0 && p<lc) do_merge = 1;
1106 }
1107 cout<<"n_assoc_col: final ntuple "<<ntmerge<<", do_merge="<<do_merge<<endl;
1108
1109 // boucle pour decoder et valider les 3 ntuples
1110 NTuple *nt[3];
1111 string ntname;
1112 vector<string> varname;
1113 int icass = 0;
1114
1115 for(int i=0;i<3;i++) {
1116
1117 // Decode NTuple name and extension
1118 ntname = tokens[i+1];
1119 int_4 lc = ntname.size();
1120 string ext = "";
1121 int p = ntname.find(',');
1122 if(p>=0 && p<lc) {
1123 if(p<lc-1) ext = ntname.substr(p+1,lc-p-1);
1124 if(p>0) ntname = ntname.substr(0,p); else ntname = "";
1125 }
1126 // choose the extension: given,automatic,none
1127 if(i==2) { // numero de colonne avec l'index d'association
1128 if(ext[0] == '!') ext = "";
1129 if(ext.size()>0) icass = atoi(ext.c_str());
1130 } else {
1131 if(ext.size()<1) {
1132 char str[8]; sprintf(str,"_%d",i);
1133 ext = str;
1134 } else if(ext[0] == '!') {
1135 ext = "";
1136 }
1137 }
1138
1139 AnyDataObj* mobj = omg.GetObj(ntname);
1140 if(mobj==NULL) {
1141 cout<<"n_assoc_col Error: unknow object "<<ntname<<" ("<<tokens[i]<<")"<<endl;
1142 return;
1143 }
1144 nt[i] = dynamic_cast<NTuple*>(mobj);
1145 if(nt[i]==NULL) {
1146 cout<<"n_assoc_col Error: "<<ntname<<" not a NTuple"<<endl;
1147 return;
1148 }
1149 if(nt[i]->NEntry()==0) {
1150 cout<<"n_assoc_col Error: "<<ntname<<" is empty"<<endl;
1151 return;
1152 }
1153 if(nt[i]->NVar()==0) {
1154 cout<<"n_assoc_col Error: "<<ntname<<" has no variable"<<endl;
1155 return;
1156 }
1157
1158 if(i<2)
1159 for(int iv=0;iv<nt[i]->NVar();iv++) {
1160 string str = nt[i]->ColumnName(iv) + ext;
1161 varname.push_back(str);
1162 }
1163
1164 }
1165
1166 // Checking size of associated ntuple
1167 if(nt[0]->NEntry() != nt[2]->NEntry()) {
1168 cout<<"n_assoc_col Error: nentries mismatch "
1169 <<" first ntuple"<<"="<<nt[0]->NEntry()
1170 <<", assoc ntuple"<<"="<<nt[2]->NEntry()<<endl;
1171 return;
1172 }
1173 if(icass<0 || icass>=nt[2]->NVar()) {
1174 cout<<"n_assoc_col Error: bad association column number "
1175 <<icass<<" / "<<nt[2]->NVar()<<endl;
1176 return;
1177 }
1178
1179 // create receiving ntuple
1180 int nvar = varname.size();
1181 char **ntvn = new char*[nvar];
1182 for(int iv=0;iv<nvar;iv++) ntvn[iv] = const_cast<char *>(varname[iv].c_str());
1183 NTuple * ntass = new NTuple(nvar,ntvn);
1184 delete [] ntvn;
1185
1186 // Filling with current ntuple
1187 // the first column gives the associated line in nt[1]
1188 int_4 nfill[2]={0,0};
1189 r_8 *xass = new r_8[nt[2]->NVar()];
1190 r_8 *xnt = new r_8[nvar];
1191 uint_2 *use2 = NULL;
1192 if(do_merge==2) {
1193 use2 = new uint_2[nt[1]->NEntry()];
1194 for(int i=0;i<nt[1]->NEntry();i++) use2[i] = 0;
1195 }
1196 for(int iev=0;iev<nt[0]->NEntry();iev++) {
1197 nt[2]->GetVecD(iev,xass);
1198 int iass = (xass[icass]<0.) ? -1: int(xass[icass]+0.5);
1199 bool okass = (iass>=0 && iass<nt[1]->NEntry());
1200 if(do_merge==1 && !okass) continue; // pas d'assoc et intersection demandee
1201 for(int iv=0;iv<nvar;iv++) xnt[iv]=0.;
1202 nt[0]->GetVecD(iev,xnt);
1203 if(okass) {
1204 nt[1]->GetVecD(iass,&xnt[nt[0]->NVar()]);
1205 if(do_merge==2) use2[iass] = 1;
1206 } else nfill[0]++;
1207 ntass->Fill(xnt);
1208 }
1209 // Filling with remaining unassociated in nt[1] if requested
1210 if(do_merge==2) {
1211 for(int iev=0;iev<nt[1]->NEntry();iev++) {
1212 if(use2[iev]!=0) continue; // deja rempli
1213 for(int iv=0;iv<nvar;iv++) xnt[iv]=0.;
1214 nt[1]->GetVecD(iev,&xnt[nt[0]->NVar()]);
1215 ntass->Fill(xnt);
1216 nfill[1]++;
1217 }
1218 }
1219
1220 // Ending and saving merged ntuple
1221 delete [] xnt; delete [] xass;
1222 if(use2) delete [] use2;
1223 cout<<"n_assoc_col: ntuple "<<ntmerge
1224 <<" filled with "<<ntass->NEntry()<<" entries"
1225 <<", unassoc in nt_1="<<nfill[0]
1226 <<", unassoc in nt_2="<<nfill[1]<<endl;
1227 omg.AddObj(ntass,ntmerge);
1228
1229 return;
1230}
1231
1232/* methode */
1233void PAWExecutor::n_copy(vector<string>& tokens)
1234{
1235 if(tokens.size()<2) {
1236 cerr<<"Usage: n/copy ntnew nt [vname1 vname2 vname3 ...]"<<endl;
1237 return;
1238 }
1239
1240 NamedObjMgr omg;
1241
1242 // Le NTuple de depart
1243 AnyDataObj* mobj = omg.GetObj(tokens[1]);
1244 if(mobj==NULL) {
1245 cout<<"n_copy Error: unknow object"<<tokens[1]<<endl;
1246 return;
1247 }
1248 NTuple* nt = dynamic_cast<NTuple*>(mobj);
1249 if(nt==NULL) {
1250 cout<<"n_copy Error: "<<tokens[1]<<" not a NTuple"<<endl;
1251 return;
1252 }
1253 if(nt->NEntry()==0) {
1254 cout<<"n_copy Error: "<<tokens[1]<<" is empty"<<endl;
1255 return;
1256 }
1257 if(nt->NVar()==0) {
1258 cout<<"n_copy Error: "<<tokens[1]<<" has no variable"<<endl;
1259 return;
1260 }
1261
1262 // Les variables a copier
1263 int nvar; vector<string> varname; vector<int> ivar;
1264 if(tokens.size()==2) {
1265 nvar = nt->NVar();
1266 for(int i=0;i<nvar;i++) {
1267 ivar.push_back(i);
1268 varname.push_back(nt->NomIndex(i));
1269 }
1270 } else {
1271 nvar = tokens.size()-2;
1272 for(int i=0;i<nvar;i++) {
1273 int iv = nt->IndexNom(tokens[i+2].c_str());
1274 if(iv<0) {
1275 cout<<"n_copy Error: unkown variable "<<tokens[i+2].c_str()<<endl;
1276 return;
1277 }
1278 ivar.push_back(iv);
1279 varname.push_back(tokens[i+2]);
1280 }
1281 }
1282 cout<<"coping variables: ";
1283 for(int i=0;i<nvar;i++) cout<<" "<<varname[i];
1284 cout<<endl;
1285
1286 // Creation du nouveau NTuple
1287 char **ntvn = new char*[nvar];
1288 for(int i=0;i<nvar;i++) ntvn[i] = const_cast<char *>(varname[i].c_str());
1289 NTuple ntnew(nvar,ntvn);
1290 delete [] ntvn;
1291
1292 // Copie du NTuple
1293 r_8 *xnt = new r_8[nt->NVar()];
1294 r_8 *xntnew = new r_8[nvar];
1295 for(int iev=0;iev<nt->NEntry();iev++) {
1296 nt->GetVecD(iev,xnt);
1297 for(int i=0;i<nvar;i++) xntnew[i] = xnt[ivar[i]];
1298 ntnew.Fill(xntnew);
1299 }
1300 delete [] xnt; delete [] xntnew;
1301
1302 // Adding new NTuple
1303 omg.AddObj(ntnew,tokens[0]);
1304
1305 return;
1306}
1307
1308/* methode */
1309void PAWExecutor::h_integ(vector<string>& tokens)
1310// Pour remplacer le contenu d'un histo 1D par son integrale
1311{
1312if(tokens.size()<1)
1313 {cout<<"Usage: h/integ nameh1d [norm]"<<endl; return;}
1314NamedObjMgr omg;
1315AnyDataObj* mobj = omg.GetObj(tokens[0]);
1316if(mobj==NULL)
1317 {cout<<"PAWExecutor::h_integ Error: unknow object"<<tokens[0]<<endl;
1318 return;}
1319r_8 norm = 1.;
1320if(tokens.size()>=2) norm = atof(tokens[1].c_str());
1321// attention: dynamic_cast<Histo*>(HProf)=Vrai!
1322Histo* h1 = dynamic_cast<Histo*>(mobj);
1323HProf* hp = dynamic_cast<HProf*>(mobj);
1324if(hp || !h1)
1325 {cout<<"PAWExecutor::h_integ Error: "<<tokens[0]<<" not an Histo"<<endl;
1326 return;}
1327h1->HInteg(norm);
1328}
1329
1330/* methode */
1331void PAWExecutor::v_integ(vector<string>& tokens)
1332// Pour remplacer le contenu d'un TVector<r_8> par son integrale
1333// normalisee a norm:
1334// Si norm <= 0 : pas de normalisation, integration seule
1335{
1336if(tokens.size()<1)
1337 {cout<<"Usage: v/integ namevec [norm]"<<endl; return;}
1338NamedObjMgr omg;
1339AnyDataObj* mobj = omg.GetObj(tokens[0]);
1340if(mobj==NULL)
1341 {cout<<"PAWExecutor::v_integ Error: unknow object"<<tokens[0]<<endl;
1342 return;}
1343r_8 norm=-1.;
1344if(tokens.size()>=2) norm = atof(tokens[1].c_str());
1345#ifdef SANS_EVOLPLANCK
1346Vector* v = dynamic_cast<Vector*>(mobj);
1347#else
1348TVector<r_8>* v = dynamic_cast<TVector<r_8>*>(mobj);
1349#endif
1350
1351if(!v)
1352 {cout<<"PAWExecutor::v_integ Error: "<<tokens[0]<<" not a TVector/Vector"<<endl;
1353 return;}
1354
1355#ifdef SANS_EVOLPLANCK
1356uint_4 n = v->NElts();
1357#else
1358uint_4 n = v->Size();
1359#endif
1360
1361if(n==0)
1362 {cout<<"PAWExecutor::v_integ Error: "<<tokens[0]<<" is an empty vector"<<endl;
1363 return;}
1364if(n>1) for(uint_4 i=1;i<n;i++) (*v)(i)+=(*v)(i-1);
1365if(norm<=0. || (*v)(n-1)==0.) return;
1366norm /= (*v)(n-1);
1367for(uint_4 i=0;i<n;i++) (*v)(i) *= norm;
1368}
1369
1370/* methode */
1371void PAWExecutor::v_sort(vector<string>& tokens)
1372// Pour sort in-place d'un vecteur ascendant ou descendant
1373{
1374 if(tokens.size()<1)
1375 {cout<<"Usage: v/sort namevec [+1/-1]"<<endl; return;}
1376 NamedObjMgr omg;
1377 AnyDataObj* mobj = omg.GetObj(tokens[0]);
1378 if(mobj==NULL)
1379 {cout<<"PAWExecutor::v_sort Error: unknow object"<<tokens[0]<<endl;
1380 return;}
1381
1382 bool up = true;
1383 if(tokens.size()>=2) if(atoi(tokens[1].c_str())<0) up=false;
1384
1385 {
1386 TVector<r_8>* v = dynamic_cast<TVector<r_8>*>(mobj);
1387 if(v) {
1388 if(v->Size()==0) return;
1389 if(up) stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_up_r8);
1390 else stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_down_r8);
1391 cout<<"median is: "<<0.5*((*v)(v->Size()/2)+(*v)((v->Size()-1)/2))<<endl;
1392 return;
1393 }
1394 }
1395
1396 {
1397 TVector<r_4>* v = dynamic_cast<TVector<r_4>*>(mobj);
1398 if(v) {
1399 if(v->Size()==0) return;
1400 if(up) stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_up_r4);
1401 else stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_down_r4);
1402 cout<<"median is: "<<0.5*((*v)(v->Size()/2)+(*v)((v->Size()-1)/2))<<endl;
1403 return;
1404 }
1405 }
1406
1407 {
1408 TVector<int_4>* v = dynamic_cast<TVector<int_4>*>(mobj);
1409 if(v) {
1410 if(v->Size()==0) return;
1411 if(up) stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_up_i4);
1412 else stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_down_i4);
1413 cout<<"median is: "<<0.5*((*v)(v->Size()/2)+(*v)((v->Size()-1)/2))<<endl;
1414 return;
1415 }
1416 }
1417
1418 {
1419 TVector<uint_8>* v = dynamic_cast<TVector<uint_8>*>(mobj);
1420 if(v) {
1421 if(v->Size()==0) return;
1422 if(up) stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_up_u8);
1423 else stable_sort(v->Data(), v->Data()+v->Size(), __vector_sort_down_u8);
1424 cout<<"median is: "<<0.5*((*v)(v->Size()/2)+(*v)((v->Size()-1)/2))<<endl;
1425 return;
1426 }
1427 }
1428
1429 cout<<"PAWExecutor::v_sort Error: "<<tokens[0]
1430 <<" not a TVector or not a supported TVector<TYPE>"<<endl;
1431 return;
1432
1433}
1434
1435/* methode */
1436void PAWExecutor::h_deriv(vector<string>& tokens)
1437// Pour remplacer le contenu d'un histo 1D par sa derivee
1438{
1439if(tokens.size()<1)
1440 {cout<<"Usage: h/deriv nameh1d"<<endl; return;}
1441NamedObjMgr omg;
1442AnyDataObj* mobj = omg.GetObj(tokens[0]);
1443if(mobj==NULL)
1444 {cout<<"PAWExecutor::h_deriv Error: unknow object"<<tokens[0]<<endl;
1445 return;}
1446// attention: dynamic_cast<Histo*>(HProf)=Vrai!
1447Histo* h1 = dynamic_cast<Histo*>(mobj);
1448HProf* hp = dynamic_cast<HProf*>(mobj);
1449if(hp || !h1)
1450 {cout<<"PAWExecutor::h_deriv Error: "<<tokens[0]<<" not an Histo"<<endl;
1451 return;}
1452h1->HDeriv();
1453}
1454
1455/* methode */
1456void PAWExecutor::v_deriv(vector<string>& tokens)
1457// Pour remplacer le contenu d'un TVector<r_8> par sa derivee
1458// deriv_option = -1 replace v[i] with v[i]-v[i-1]
1459// = 0 replace v[i] with (v[i+1]-v[i-1])/2 (default)
1460// = +1 replace v[i] with v[i+1]-v[i]
1461{
1462if(tokens.size()<1)
1463 {cout<<"Usage: v/deriv namevec [deriv_option(-1,0,+1)]"<<endl; return;}
1464NamedObjMgr omg;
1465AnyDataObj* mobj = omg.GetObj(tokens[0]);
1466if(mobj==NULL)
1467 {cout<<"PAWExecutor::v_deriv Error: unknow object"<<tokens[0]<<endl;
1468 return;}
1469int_4 deriv_option = 0;
1470if(tokens.size()>=2) deriv_option = atoi(tokens[1].c_str());
1471
1472#ifdef SANS_EVOLPLANCK
1473Vector* v = dynamic_cast<Vector*>(mobj);
1474#else
1475TVector<r_8>* v = dynamic_cast<TVector<r_8>*>(mobj);
1476#endif
1477
1478if(!v)
1479 {cout<<"PAWExecutor::v_deriv Error: "<<tokens[0]<<" not a TVector/Vector"<<endl;
1480 return;}
1481
1482
1483#ifdef SANS_EVOLPLANCK
1484uint_4 n = v->NElts();
1485#else
1486uint_4 n = v->Size();
1487#endif
1488
1489if(n==0)
1490 {cout<<"PAWExecutor::v_deriv Error: "<<tokens[0]<<" is an empty vector"<<endl;
1491 return;}
1492if(n<=1) return;
1493
1494#ifdef SANS_EVOLPLANCK
1495Vector vsave(*v);
1496#else
1497TVector<r_8> vsave(*v,false);
1498#endif
1499
1500if(deriv_option<0) {
1501 for(uint_4 i=1;i<n;i++) (*v)(i) = vsave(i)-vsave(i-1);
1502 (*v)(0) = (*v)(1);
1503} else if(deriv_option>0) {
1504 for(uint_4 i=0;i<n-1;i++) (*v)(i) = vsave(i+1)-vsave(i);
1505 (*v)(n-1) = (*v)(n-2);
1506} else {
1507 for(uint_4 i=1;i<n-1;i++) (*v)(i) = (vsave(i+1)-vsave(i-1))/2.;
1508 (*v)(0) = vsave(1)-vsave(0);
1509 (*v)(n-1) = vsave(n-1)-vsave(n-2);
1510}
1511}
1512
1513
1514/* methode */
1515void PAWExecutor::h_rebin(vector<string>& tokens)
1516// Pour re-binner un histogramme 1D
1517{
1518if(tokens.size()<2)
1519 {cout<<"Usage: h/rebin nameh1d nbin"<<endl; return;}
1520NamedObjMgr omg;
1521AnyDataObj* mobj = omg.GetObj(tokens[0]);
1522if(mobj==NULL)
1523 {cout<<"PAWExecutor::h_rebin Error: unknow object"<<tokens[0]<<endl;
1524 return;}
1525int_4 nbin = atoi(tokens[1].c_str());
1526Histo* h1 = dynamic_cast<Histo*>(mobj);
1527// Ca marche aussi pour les HProf, HRebin a ete passe virtuel
1528//HProf* hp = dynamic_cast<HProf*>(mobj); if(hp || !h1)
1529if(!h1)
1530 {cout<<"PAWExecutor::h_rebin Error: "<<tokens[0]<<" not an Histo/HProf"<<endl;
1531 return;}
1532h1->HRebin(nbin);
1533}
1534
1535/* methode */
1536void PAWExecutor::h_cadd(vector<string>& tokens)
1537// Additionne une constante a un histogramme
1538{
1539if(tokens.size()<2)
1540 {cout<<"Usage: h/cadd nameh1d val"<<endl; return;}
1541NamedObjMgr omg;
1542AnyDataObj* mobj = omg.GetObj(tokens[0]);
1543if(mobj==NULL)
1544 {cout<<"PAWExecutor::h_cadd Error: unknow object"<<tokens[0]<<endl;
1545 return;}
1546r_8 val = atof(tokens[1].c_str());
1547Histo* h1 = dynamic_cast<Histo*>(mobj);
1548HProf* hp = dynamic_cast<HProf*>(mobj);
1549Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
1550Vector* v = dynamic_cast<Vector*>(mobj);
1551Matrix* m = dynamic_cast<Matrix*>(mobj);
1552if(h1 && !hp) *h1 += val;
1553else if(h2) *h2 += val;
1554else if(v) *v += val;
1555else if(m) *m += val;
1556else cout<<"PAWExecutor::h_cadd Error: not implemented for "<<typeid(*mobj).name()<<endl;
1557}
1558
1559/* methode */
1560void PAWExecutor::h_cmult(vector<string>& tokens)
1561// Multiplie un histogramme par une constante
1562{
1563if(tokens.size()<2)
1564 {cout<<"Usage: h/cmult nameh1d val"<<endl; return;}
1565NamedObjMgr omg;
1566AnyDataObj* mobj = omg.GetObj(tokens[0]);
1567if(mobj==NULL)
1568 {cout<<"PAWExecutor::h_cmult Error: unknow object"<<tokens[0]<<endl;
1569 return;}
1570r_8 val = atof(tokens[1].c_str());
1571Histo* h1 = dynamic_cast<Histo*>(mobj);
1572HProf* hp = dynamic_cast<HProf*>(mobj);
1573Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
1574Vector* v = dynamic_cast<Vector*>(mobj);
1575Matrix* m = dynamic_cast<Matrix*>(mobj);
1576if(h1 && !hp) *h1 *= val;
1577else if(h2) *h2 *= val;
1578else if(v) *v += val;
1579else if(m) *m += val;
1580else cout<<"PAWExecutor::h_mult Error: not implemented for "<<typeid(*mobj).name()<<endl;
1581}
1582
1583/* methode */
1584void PAWExecutor::h_oper(vector<string>& tokens)
1585// Equivalent h/oper/add sub,mul,div de paw
1586// Operation entre 2 histogrammes ou 2 vecteurs ou 2 matrices
1587// h/oper @ h1 h2 hres
1588// hres = h1 @ h2 with @ = (+,-,*,/)
1589// Pour les vecteurs et les matrices, il sagit d'operations elements a elements
1590{
1591if(tokens.size()<4)
1592 {cout<<"Usage: n/oper @ h1 h2 hres with @=(+,-,*,/,@)"<<endl;
1593 return;}
1594
1595// Decode arguments
1596const char * oper = tokens[0].c_str();
1597if( oper[0]!='+' && oper[0]!='-' && oper[0]!='*' && oper[0]!='/' )
1598 {cout<<"PAWExecutor::h_oper Error: unknow operation "<<oper<<endl;
1599 return;}
1600string h1name = tokens[1];
1601string h2name = tokens[2];
1602string h3name = tokens[3];
1603
1604// Get objects
1605NamedObjMgr omg;
1606AnyDataObj* mobjh1 = omg.GetObj(h1name);
1607AnyDataObj* mobjh2 = omg.GetObj(h2name);
1608if( mobjh1==NULL || mobjh2==NULL )
1609 {cout<<"PAWExecutor::h_oper Error: unknow object(s) "<<h1name<<" or "<<h2name<<endl;
1610 return;}
1611AnyDataObj* mobjh3 = omg.GetObj(h3name);
1612
1613// Operations on HProf, only + is working
1614if( dynamic_cast<HProf*>(mobjh1) != NULL ) {
1615 if( oper[0]!='+' )
1616 { cout<<"PAWExecutor::h_oper Error: operation "<<oper
1617 <<" not implemented for HProf"<<endl; return;}
1618 if( dynamic_cast<HProf*>(mobjh2) == NULL )
1619 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h2\n"
1620 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh2).name()<<endl;
1621 return;}
1622 HProf* h1 =(HProf*) mobjh1;
1623 HProf* h2 =(HProf*) mobjh2;
1624 if( h1->NBins() != h2->NBins() )
1625 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h2 "
1626 <<h1->NBins()<<" "<<h2->NBins()<<endl; return;}
1627 HProf* h3 = NULL;
1628 if( mobjh3 == NULL ) // l'objet n'existe pas, on le cree
1629 {h3 = new HProf(*h1); h3->Zero(); omg.AddObj(h3,h3name); mobjh3 = omg.GetObj(h3name);}
1630 h3 = dynamic_cast<HProf*>(mobjh3);
1631 if(h3 == NULL) // ce n'est pas un HProf
1632 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h3\n"
1633 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh3).name()<<endl; return;}
1634 if(h1->NBins() != h3->NBins())
1635 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h3 "
1636 <<h1->NBins()<<" "<<h3->NBins()<<endl; return;}
1637 *h3 = *h1 + *h2;
1638 h3->UpdateHisto();
1639
1640// Operations on Histo
1641} else if( dynamic_cast<Histo*>(mobjh1) != NULL ) {
1642 if( dynamic_cast<Histo*>(mobjh2) == NULL )
1643 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h2\n"
1644 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh2).name()<<endl;
1645 return;}
1646 Histo* h1 =(Histo*) mobjh1;
1647 Histo* h2 =(Histo*) mobjh2;
1648 if( h1->NBins() != h2->NBins() )
1649 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h2 "
1650 <<h1->NBins()<<" "<<h2->NBins()<<endl; return;}
1651 Histo* h3 = NULL;
1652 if( mobjh3 == NULL ) // l'objet n'existe pas, on le cree
1653 {h3 = new Histo(*h1); h3->Zero(); omg.AddObj(h3,h3name); mobjh3 = omg.GetObj(h3name);}
1654 h3 = dynamic_cast<Histo*>(mobjh3);
1655 if(h3 == NULL) // ce n'est pas un Histo
1656 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h3\n"
1657 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh3).name()<<endl; return;}
1658 if(h1->NBins() != h3->NBins())
1659 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h3 "
1660 <<h1->NBins()<<" "<<h3->NBins()<<endl; return;}
1661 if( oper[0]=='+') *h3 = *h1 + *h2;
1662 else if( oper[0]=='-') *h3 = *h1 - *h2;
1663 else if( oper[0]=='*') *h3 = *h1 * *h2;
1664 else if( oper[0]=='/') *h3 = *h1 / *h2;
1665
1666// Operations on Histo2D
1667} else if( dynamic_cast<Histo2D*>(mobjh1) != NULL ) {
1668 if( dynamic_cast<Histo2D*>(mobjh2) == NULL )
1669 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h2\n"
1670 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh2).name()<<endl;
1671 return;}
1672 Histo2D* h1 =(Histo2D*) mobjh1;
1673 Histo2D* h2 =(Histo2D*) mobjh2;
1674 if( h1->NBinX() != h2->NBinX() || h1->NBinY() != h2->NBinY() )
1675 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h2 "
1676 <<h1->NBinX()<<","<<h1->NBinY()<<" "
1677 <<h2->NBinX()<<","<<h2->NBinY()<<endl; return;}
1678 Histo2D* h3 = NULL;
1679 if( mobjh3 == NULL ) // l'objet n'existe pas, on le cree
1680 {h3 = new Histo2D(*h1); h3->Zero(); omg.AddObj(h3,h3name); mobjh3 = omg.GetObj(h3name);}
1681 h3 = dynamic_cast<Histo2D*>(mobjh3);
1682 if(h3 == NULL) // ce n'est pas un Histo2D
1683 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h3\n"
1684 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh3).name()<<endl; return;}
1685 if( h1->NBinX() != h3->NBinX() || h1->NBinY() != h3->NBinY() )
1686 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h3 "
1687 <<h1->NBinX()<<","<<h1->NBinY()<<" "
1688 <<h3->NBinX()<<","<<h3->NBinY()<<endl; return;}
1689 if( oper[0]=='+') *h3 = *h1 + *h2;
1690 else if( oper[0]=='-') *h3 = *h1 - *h2;
1691 else if( oper[0]=='*') *h3 = *h1 * *h2;
1692 else if( oper[0]=='/') *h3 = *h1 / *h2;
1693
1694// Operations on Vector
1695} else if( dynamic_cast<Vector*>(mobjh1) != NULL ) {
1696 if( dynamic_cast<Vector*>(mobjh2) == NULL )
1697 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h2\n"
1698 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh2).name()<<endl;
1699 return;}
1700 Vector* h1 =(Vector*) mobjh1;
1701 Vector* h2 =(Vector*) mobjh2;
1702 if( h1->NElts() != h2->NElts() )
1703 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h2 "
1704 <<h1->NElts()<<" "<<h2->NElts()<<endl; return;}
1705 Vector* h3 = NULL;
1706 if( mobjh3 == NULL ) { // l'objet n'existe pas, on le cree
1707#ifdef SANS_EVOLPLANCK
1708 h3 = new Vector(*h1);
1709#else
1710 h3 = new Vector(*h1,false);
1711#endif
1712 *h3 = 0.; omg.AddObj(h3,h3name); mobjh3 = omg.GetObj(h3name);
1713 }
1714 h3 = dynamic_cast<Vector*>(mobjh3);
1715 if(h3 == NULL) // ce n'est pas un Vector
1716 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h3\n"
1717 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh3).name()<<endl; return;}
1718 if(h1->NElts() != h3->NElts())
1719 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h3 "
1720 <<h1->NElts()<<" "<<h3->NElts()<<endl; return;}
1721 if( oper[0]=='+') *h3 = *h1 + *h2;
1722 else if( oper[0]=='-') *h3 = *h1 - *h2;
1723#ifdef SANS_EVOLPLANCK
1724 else if(oper[0]=='*' || oper[0]=='/')
1725 cout<<"PAWExecutor::h_oper Error: operation "<<oper[0]
1726 <<" not implemented for Vector in Peida"<<endl;
1727#else
1728 else if( oper[0]=='*') {h3->Clone(*h1); h3->MulElt(*h2,*h3);}
1729 else if( oper[0]=='/') {h3->Clone(*h1); h3->DivElt(*h2,*h3,false,true);}
1730#endif
1731
1732// Operations on Matrix
1733} else if( dynamic_cast<Matrix*>(mobjh1) != NULL ) {
1734 if( dynamic_cast<Matrix*>(mobjh2) == NULL )
1735 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h2\n"
1736 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh2).name()<<endl;
1737 return;}
1738 Matrix* h1 =(Matrix*) mobjh1;
1739 Matrix* h2 =(Matrix*) mobjh2;
1740 if( h1->NRows() != h2->NRows() || h1->NCol() != h2->NCol() )
1741 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h2 "
1742 <<h1->NRows()<<","<<h1->NCol()<<" "
1743 <<h2->NRows()<<","<<h2->NCol()<<endl; return;}
1744 Matrix* h3 = NULL;
1745 if( mobjh3 == NULL ) { // l'objet n'existe pas, on le cree
1746#ifdef SANS_EVOLPLANCK
1747 h3 = new Matrix(*h1);
1748#else
1749 h3 = new Matrix(*h1,false);
1750#endif
1751 *h3 = 0.; omg.AddObj(h3,h3name); mobjh3 = omg.GetObj(h3name);
1752 }
1753 h3 = dynamic_cast<Matrix*>(mobjh3);
1754 if(h3 == NULL) // ce n'est pas un Matrix
1755 {cout<<"PAWExecutor::h_oper Error: type mismatch between h1 and h3\n"
1756 <<typeid(*mobjh1).name()<<" , "<<typeid(*mobjh3).name()<<endl; return;}
1757 if( h1->NRows() != h3->NRows() || h1->NCol() != h3->NCol() )
1758 {cout<<"PAWExecutor::h_oper Error: size mismatch between h1, h3 "
1759 <<h1->NRows()<<","<<h1->NCol()<<" "
1760 <<h3->NRows()<<","<<h3->NCol()<<endl; return;}
1761 if( oper[0]=='+') *h3 = *h1 + *h2;
1762 else if( oper[0]=='-') *h3 = *h1 - *h2;
1763#ifdef SANS_EVOLPLANCK
1764 else if(oper[0]=='*' || oper[0]=='/')
1765 cout<<"PAWExecutor::h_oper Error: operation "<<oper[0]
1766 <<" not implemented for Vector in Peida"<<endl;
1767#else
1768 else if( oper[0]=='*') {h3->Clone(*h1); h3->MulElt(*h2,*h3);}
1769 else if( oper[0]=='/') {h3->Clone(*h1); h3->DivElt(*h2,*h3,false,true);}
1770#endif
1771
1772// Doesn't work for other objects
1773} else {
1774 cout<<"PAWExecutor::h_oper Error: not implemented for "
1775 <<typeid(*mobjh1).name()<<endl;
1776 return;
1777}
1778
1779return;
1780}
1781
1782/* methode */
1783void PAWExecutor::h_plot_2d(vector<string>& tokens)
1784// plot for 2D histogramme: plot histo, bandx/y, slicex/y or projx/y
1785{
1786if(tokens.size()<1)
1787 {cout<<"Usage: h/plot/2d nameh2d to_plot [n/s] [dopt]"<<endl; return;}
1788string proj = "h"; if(tokens.size()>1) proj = tokens[1];
1789NamedObjMgr omg;
1790AnyDataObj* mobj = omg.GetObj(tokens[0]);
1791if(mobj==NULL)
1792 {cout<<"PAWExecutor::h_plot_2d Error: unknow object"<<tokens[0]<<endl;
1793 return;}
1794Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
1795if(!h2)
1796 {cout<<"PAWExecutor::h_plot_2d Error: "<<tokens[0]<<" not an Histo2D"<<endl;
1797 return;}
1798
1799Histo* h1p = NULL; string nametoplot = "/autoc/h_plot_2d_h1";
1800
1801string dopt = ""; if(tokens.size()>=3) dopt = tokens[2];
1802if(proj == "show") {
1803 h2->ShowProj();
1804 h2->ShowBand(2);
1805 h2->ShowSli(2);
1806 return;
1807} else if(proj == "h") {
1808 nametoplot = tokens[0];
1809} else if(proj == "px") {
1810 if((h1p=h2->HProjX())) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
1811 else {h2->ShowProj(); return;}
1812} else if(proj == "py") {
1813 if((h1p=h2->HProjY())) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
1814 else {h2->ShowProj(); return;}
1815} else {
1816 if(tokens.size()<3)
1817 {cout<<"Usage: h/plot/2d nameh2d bx/by/sx/sy n [dopt]"<<endl; return;}
1818 int_4 n = atoi(tokens[2].c_str());
1819 dopt = ""; if(tokens.size()>=4) dopt = tokens[3];
1820 if(proj == "bx") {
1821 if((h1p=h2->HBandX(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
1822 else {h2->ShowBand(); return;}
1823 } else if(proj == "by") {
1824 if((h1p=h2->HBandY(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
1825 else {h2->ShowBand(); return;}
1826 } else if(proj == "sx") {
1827 if((h1p=h2->HSliX(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
1828 else {h2->ShowSli(); return;}
1829 } else if(proj == "sy") {
1830 if((h1p=h2->HSliY(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
1831 else {h2->ShowSli(); return;}
1832 }
1833}
1834
1835omg.DisplayObj(nametoplot,dopt);
1836}
1837
1838/* methode */
1839int_4 PAWExecutor::decodepawstring(string tokens,string& nameobj
1840 ,string& xexp,string& yexp,string& zexp)
1841// Decodage general de "nameobj.xexp"
1842// "nameobj.yexp%xexp"
1843// "nameobj.zexp%yexp%xexp"
1844// Return: nombre de variables trouvees, -1 si probleme
1845{
1846nameobj = ""; xexp= ""; yexp= ""; zexp= "";
1847
1848int_4 lt = (int) tokens.length();
1849if(lt<=0) return -1;
1850
1851// decodage de la chaine de type PAW.
1852char *str = new char[lt+2];
1853strcpy(str,tokens.c_str()); strip(str,'B',' '); lt = strlen(str);
1854//cout<<"chaine1["<<lt<<"] :"<<str<<":"<<endl;
1855char *c[3] = {NULL,NULL,NULL};
1856int_4 i, np=0; bool namefound = false;
1857for(i=0;i<lt;i++) {
1858 if(!namefound && str[i]=='.') {
1859 str[i]='\0';
1860 namefound=true;
1861 c[np] = str+i+1; np++;
1862 }
1863 if( namefound && str[i]=='%') {
1864 str[i]='\0';
1865 if(np<3) {c[np] = str+i+1; np++;}
1866 }
1867}
1868//cout<<"chaine2 :"; for(i=0;i<lt;i++) cout<<str[i]; cout<<":"<<endl;
1869
1870// Remplissage du nom et des variables
1871nameobj = str;
1872if(np==1) xexp=c[0];
1873if(np==2) {yexp=c[0]; xexp=c[1];}
1874if(np==3) {zexp=c[0]; yexp=c[1]; xexp=c[2];}
1875//cout<<"pawstring str,c[0-2] "<<str<<" "<<c[0]<<" "<<c[1]<<" "<<c[2]<<endl;
1876delete [] str;
1877
1878// Comptage des variables
1879np = -1;
1880if(nameobj.length()>0)
1881 {np = 0; if(xexp.length()>0)
1882 {np++; if(yexp.length()>0)
1883 {np++; if(zexp.length()>0) np++;}}}
1884//cout<<"pawstring["<<np<<"] name="<<nameobj
1885// <<" xexp="<<xexp<<" yexp="<<yexp<<" zexp="<<zexp<<endl;
1886return np;
1887}
1888
1889/* methode */
1890void PAWExecutor::h_put_vec(vector<string>& tokens)
1891// Pour remplir un histo avec le contenu d'un vecteur ou d'une matrice
1892// L'histogramme doit deja exister. Le nombre de bins remplit
1893// depend des tailles respectives des 2 objets.
1894// tokens[2] = "cont" : on remplit les valeurs de l'histogramme (ou "!")
1895// = "err2" : on remplit les erreurs de l'histogramme
1896{
1897if(tokens.size()<2)
1898 {cout<<"Usage: h/put_vec namehisto namevector"<<endl;
1899 return;}
1900string toput = "cont"; if(tokens.size()>2) toput = tokens[2];
1901if(toput=="!") toput = "cont";
1902if(toput!="cont" && toput!="err2")
1903 {cout<<"PAWExecutor::h_put_vec Error: unknow filling "<<toput<<endl;
1904 return;}
1905string hname = tokens[0];
1906string vname = tokens[1];
1907
1908// Get objects
1909NamedObjMgr omg;
1910AnyDataObj* mobjh = omg.GetObj(hname);
1911AnyDataObj* mobjv = omg.GetObj(vname);
1912if( mobjh==NULL || mobjv==NULL )
1913 {cout<<"PAWExecutor::h_put_vec Error: unknow object(s) "<<hname<<" or "<<vname<<endl;
1914 return;}
1915
1916// Fill Histo from Vector
1917if(typeid(*mobjh) == typeid(Histo) && typeid(*mobjv) == typeid(Vector)) {
1918 Histo* h = dynamic_cast<Histo*>(mobjh);
1919 Vector* v = dynamic_cast<Vector*>(mobjv);
1920 if(toput=="cont") h->PutValue(*v);
1921 else if(toput=="err2") h->PutError2(*v);
1922
1923// Fill Histo2D from Matrix
1924} else if(typeid(*mobjh) == typeid(Histo2D) && typeid(*mobjv) == typeid(Matrix)) {
1925 Histo2D* h = dynamic_cast<Histo2D*>(mobjh);
1926 Matrix* v = dynamic_cast<Matrix*>(mobjv);
1927 if(toput=="cont") h->PutValue(*v);
1928 else if(toput=="err2") h->PutError2(*v);
1929
1930} else {
1931 cout<<"PAWExecutor::h_put_vec Error: type mismatch between histogram and vector/matrix\n"
1932 <<typeid(*mobjh).name()<<" , "<<typeid(*mobjv).name()<<endl;
1933 return;
1934}
1935
1936}
1937
1938/* methode */
1939void PAWExecutor::h_get_vec(vector<string>& tokens)
1940// Pour copier un histo dans un vecteur ou une matrice
1941// Si le vecteur (matrice) n'existe pas, il est cree.
1942// Le vecteur ou la matrice est re-dimensionne correctement.
1943// tokens[2] = "cont" : on copie les valeurs de l'histogramme (ou "!")
1944// = "err2" : on copie les erreurs de l'histogramme
1945// = "absc" : on copie les erreurs de l'histogramme (1D only)
1946// tokens[3[,4]] = show : show available projections for Histo2D
1947// px : get X projection
1948// py : get Y projection
1949// bx n : get X band number n
1950// by n : get Y band number n
1951// sx n : get X slice number n
1952// sy n : get Y slice number n
1953{
1954if(tokens.size()<2)
1955 {cout<<"Usage: h/get_vec namehisto namevector"<<endl;
1956 return;}
1957string toget = "cont"; if(tokens.size()>2) toget = tokens[2];
1958if(toget=="!") toget = "cont";
1959if(toget!="cont" && toget!="err2" && toget!="absc")
1960 {cout<<"PAWExecutor::h_get_vec Error: unknow filling "<<toget<<endl;
1961 return;}
1962string proj = "h"; if(tokens.size()>3) proj = tokens[3];
1963int_4 nproj = -1; if(tokens.size()>4) nproj = atoi(tokens[4].c_str());
1964string hname = tokens[0];
1965string vname = tokens[1];
1966
1967// Get objects
1968NamedObjMgr omg;
1969AnyDataObj* mobjh = omg.GetObj(hname);
1970AnyDataObj* mobjv = omg.GetObj(vname);
1971if( mobjh==NULL)
1972 {cout<<"PAWExecutor::h_put_vec Error: unknow object(s) "<<hname<<endl;
1973 return;}
1974
1975// Copy Histo/Histo2D/Projection to Vector/Matrix
1976Histo* h = dynamic_cast<Histo*>(mobjh);
1977Histo2D* h2 = dynamic_cast<Histo2D*>(mobjh);
1978if( h != NULL ) { // Histo ou HProf
1979 h->UpdateHisto(); // pour le cas ou c'est un HProf
1980 Vector* v = NULL;
1981 if(mobjv==NULL) // le vecteur n'existe pas
1982 {v = new Vector(1); omg.AddObj(v,vname); mobjv = omg.GetObj(vname);}
1983 if(typeid(*mobjv) != typeid(Vector))
1984 {cout<<"PAWExecutor::h_get_vec Error: type mismatch between Histo/HProf and vector\n"
1985 <<typeid(*mobjv).name()<<endl; return;}
1986 v = dynamic_cast<Vector*>(mobjv);
1987 if(toget=="cont") h->GetValue(*v);
1988 else if(toget=="err2") h->GetError2(*v);
1989 else if(toget=="absc") h->GetAbsc(*v);
1990
1991} else if( h2 != NULL) { // Histo2D
1992
1993 if(proj == "h") { // On veut les valeurs de l'histogramme 2D
1994 Matrix* v = NULL;
1995 if(mobjv==NULL) // la matrice n'existe pas
1996 {v = new Matrix(1,1); omg.AddObj(v,vname); mobjv = omg.GetObj(vname);}
1997 if(typeid(*mobjv) != typeid(Matrix))
1998 {cout<<"PAWExecutor::h_get_vec Error: type mismatch between Histo2D and matrix\n"
1999 <<typeid(*mobjv).name()<<endl; return;}
2000 v = dynamic_cast<Matrix*>(mobjv);
2001 if(toget=="cont") h2->GetValue(*v);
2002 else if(toget=="err2") h2->GetError2(*v);
2003 else
2004 {cout<<"PAWExecutor::h_get_vec Error: option "<<toget<<" not valid for Histo2D"<<endl;
2005 return;}
2006
2007 } else { // On veut les valeurs d'une projection de l'histo 2D
2008 h = NULL;
2009 if(proj == "show") {h2->ShowProj(); h2->ShowBand(2); h2->ShowSli(2);}
2010 else if(proj == "px") h = h2->HProjX();
2011 else if(proj == "py") h = h2->HProjY();
2012 else if(proj == "bx") h = h2->HBandX(nproj);
2013 else if(proj == "by") h = h2->HBandY(nproj);
2014 else if(proj == "sx") h = h2->HSliX(nproj);
2015 else if(proj == "sy") h = h2->HSliY(nproj);
2016 if(h==NULL)
2017 {cout<<"PAWExecutor::h_get_vec Error: unknown projection "<<proj
2018 <<" number "<<nproj<<endl; return;}
2019 Vector* v = NULL;
2020 if(mobjv==NULL) // le vecteur n'existe pas
2021 {v = new Vector(1); omg.AddObj(v,vname); mobjv = omg.GetObj(vname);}
2022 if(typeid(*mobjv) != typeid(Vector))
2023 {cout<<"PAWExecutor::h_get_vec Error: type mismatch between Histo2D "<<proj
2024 <<" and vector\n"<<typeid(*mobjv).name()<<endl; return;}
2025 v = dynamic_cast<Vector*>(mobjv);
2026 if(toget=="cont") h->GetValue(*v);
2027 else if(toget=="err2") h->GetError2(*v);
2028 else if(toget=="absc") h->GetAbsc(*v);
2029 }
2030
2031} else {
2032 cout<<"PAWExecutor::h_get_vec Error: type mismatch for histogram\n"
2033 <<typeid(*mobjh).name()<<endl;
2034 return;
2035}
2036
2037}
2038
2039/* methode */
2040void PAWExecutor::h_copy(vector<string>& tokens)
2041// Pour copier un object dans un object
2042// objects are Vector,Matrix,Histo,Histo2D
2043// h/copy namefrom nametocopy [i1[:i2]] [j1[:j2]] [ic1[:jc1]]
2044// copy obj1Dfrom(i1->i2) into obj1Dto(ic1->)
2045// copy obj2Dfrom(i1,j1->i2,j2) into obj2Dto(ic1,jc1->)
2046// Attention: elements depuis i1 jusqu'a i2 COMPRIS
2047// Si obj1Dto n'existe pas, il est cree avec une taille i2-i1+1
2048// ou obj2Dto avec une taille i2-i1+1,j2-j1+1
2049// Le contenu de obj?Dfrom adresse est surecrit
2050// Le contenu de obj?Dfrom non adresse reste le meme
2051{
2052int_4 tks = tokens.size();
2053if(tks<2)
2054 {cout<<"Usage: h_copy namefrom nametocopy [i1[:i2]] [j1[:j2]] [ic1[:jc1]]"<<endl;
2055 return;}
2056
2057NamedObjMgr omg;
2058AnyDataObj* mobjv1 = omg.GetObj(tokens[0]);
2059if( mobjv1==NULL)
2060 {cout<<"PAWExecutor::h_copy Error: unknow object "<<tokens[0]<<endl;
2061 return;}
2062AnyDataObj* mobjv2 = omg.GetObj(tokens[1]);
2063
2064int_4 i1=0,i2=0, j1=0,j2=0, ic1=0,jc1=0, i,ii, j,jj, nx1,ny1, nx2,ny2;
2065
2066// Cas d'un Vector
2067if(typeid(*mobjv1) == typeid(Vector)) {
2068 Vector* v1 = dynamic_cast<Vector*>(mobjv1); nx1 = (int_4)v1->NElts();
2069 i2=nx1-1;
2070 if(tks>2) if(tokens[2]!="!") sscanf(tokens[2].c_str(),"%d:%d",&i1,&i2);
2071 if(i1<0) i1=0; if(i2>=nx1) i2=nx1-1;
2072 if(i2<i1)
2073 {cout<<"PAWExecutor::h_copy Error: wrong range ["<<i1<<":"<<i2<<"] for NElts="
2074 <<nx1<<endl; return;}
2075 Vector* v2 = NULL;
2076 if(mobjv2==NULL)
2077 {v2 = new Vector(i2-i1+1); omg.AddObj(v2,tokens[1]); mobjv2 = omg.GetObj(tokens[1]);}
2078 if(typeid(*mobjv2) != typeid(Vector))
2079 {cout<<"PAWExecutor::h_copy Error: type mismatch for Vector "
2080 <<typeid(*mobjv2).name()<<endl; return;}
2081 v2 = dynamic_cast<Vector*>(mobjv2); nx2 = (int_4)v2->NElts();
2082 if(tks>4) if(tokens[4]!="!") sscanf(tokens[4].c_str(),"%d",&ic1);
2083 if(ic1<0) ic1=0;
2084 if(ic1>=nx2)
2085 {cout<<"PAWExecutor::h_copy Error: wrong pointer to copy ["<<ic1<<"] for NElts="
2086 <<nx2<<endl; return;}
2087 cout<<"copy "<<tokens[0]<<"("<<i1<<":"<<i2<<") to "<<tokens[1]<<"("<<ic1<<"->...)"<<endl;
2088 for(i=i1,ii=ic1; i<=i2 && i<nx1 && ii<nx2; i++,ii++) (*v2)(ii) = (*v1)(i);
2089 return;
2090}
2091
2092// Cas d'un Histo
2093if(typeid(*mobjv1) == typeid(Histo)) {
2094 Histo* v1 = dynamic_cast<Histo*>(mobjv1); nx1 = (int_4)v1->NBins();
2095 i2=nx1-1;
2096 if(tks>2) if(tokens[2]!="!") sscanf(tokens[2].c_str(),"%d:%d",&i1,&i2);
2097 if(i1<0) i1=0; if(i2>=nx1) i2=nx1-1;
2098 if(i2<i1)
2099 {cout<<"PAWExecutor::h_copy Error: wrong range ["<<i1<<":"<<i2<<"] for NBins="
2100 <<nx1<<endl; return;}
2101 Histo* v2 = NULL;
2102 if(mobjv2==NULL)
2103 {v2 = new Histo(0.,(r_8)(i2-i1+1),i2-i1+1);
2104 omg.AddObj(v2,tokens[1]); mobjv2 = omg.GetObj(tokens[1]);}
2105 if(typeid(*mobjv2) != typeid(Histo))
2106 {cout<<"PAWExecutor::h_copy Error: type mismatch for Histo "
2107 <<typeid(*mobjv2).name()<<endl; return;}
2108 v2 = dynamic_cast<Histo*>(mobjv2); nx2 = (int_4)v2->NBins();
2109 if(v1->HasErrors() && !(v2->HasErrors())) v2->Errors();
2110 if(tks>4) if(tokens[4]!="!") sscanf(tokens[4].c_str(),"%d",&ic1);
2111 if(ic1<0) ic1=0;
2112 if(ic1>=nx2)
2113 {cout<<"PAWExecutor::h_copy Error: wrong pointer to copy ["<<ic1<<"] for NBins="
2114 <<nx2<<endl; return;}
2115 cout<<"copy "<<tokens[0]<<"("<<i1<<":"<<i2<<") to "<<tokens[1]<<"("<<ic1<<"->...)"<<endl;
2116 for(i=i1,ii=ic1; i<=i2 && i<nx1 && ii<nx2; i++,ii++)
2117 {(*v2)(ii) = (*v1)(i); if(v1->HasErrors()) v2->Error2(ii) = v1->Error2(i);}
2118 return;
2119}
2120
2121// Cas d'une Matrix
2122if(typeid(*mobjv1) == typeid(Matrix)) {
2123 Matrix* v1 = dynamic_cast<Matrix*>(mobjv1); nx1=v1->NRows(); ny1=v1->NCol();
2124 i2=nx1-1; j2=ny1-1;
2125 if(tks>2) if(tokens[2]!="!") sscanf(tokens[2].c_str(),"%d:%d",&i1,&i2);
2126 if(tks>3) if(tokens[3]!="!") sscanf(tokens[3].c_str(),"%d:%d",&j1,&j2);
2127 if(i1<0) i1=0; if(i2>=nx1) i2=nx1-1; if(j1<0) j1=0; if(j2>=ny1) j2=ny1-1;
2128 if(i2<i1 || j2<j1)
2129 {cout<<"PAWExecutor::h_copy Error: wrong range ["<<i1<<":"<<i2
2130 <<"] , ["<<j1<<":"<<j2<<"] for NRows,NCol="
2131 <<nx1<<" , "<<ny1<<endl; return;}
2132 Matrix* v2 = NULL;
2133 if(mobjv2==NULL)
2134 {v2 = new Matrix(i2-i1+1,j2-j1+1);
2135 omg.AddObj(v2,tokens[1]); mobjv2 = omg.GetObj(tokens[1]);}
2136 if(typeid(*mobjv2) != typeid(Matrix))
2137 {cout<<"PAWExecutor::h_copy Error: type mismatch for Matrix "
2138 <<typeid(*mobjv2).name()<<endl; return;}
2139 v2 = dynamic_cast<Matrix*>(mobjv2); nx2=v2->NRows(); ny2=v2->NCol();
2140 if(tks>4) if(tokens[4]!="!") sscanf(tokens[4].c_str(),"%d:%d",&ic1,&jc1);
2141 if(ic1<0) ic1=0; if(jc1<0) jc1=0;
2142 if(ic1>=nx2 || jc1>=ny2)
2143 {cout<<"PAWExecutor::h_copy Error: wrong pointer to copy ["<<ic1<<","<<jc1
2144 <<"] for NRows,NCol="<<nx2<<","<<ny2<<endl; return;}
2145 cout<<"copy "<<tokens[0]<<"("<<i1<<":"<<i2<<","<<j1<<":"<<j2
2146 <<") to "<<tokens[1]<<"("<<ic1<<","<<jc1<<"->...)"<<endl;
2147 for(i=i1,ii=ic1; i<=i2 && i<nx1 && ii<nx2; i++,ii++)
2148 for(j=j1,jj=jc1; j<=j2 && j<ny1 && jj<ny2; j++,jj++) (*v2)(ii,jj) = (*v1)(i,j);
2149 return;
2150}
2151
2152// Cas d'un Histo2D
2153if(typeid(*mobjv1) == typeid(Histo2D)) {
2154 Histo2D* v1 = dynamic_cast<Histo2D*>(mobjv1); nx1=v1->NBinX(); ny1=v1->NBinY();
2155 i2=nx1-1; j2=ny1-1;
2156 if(tks>2) if(tokens[2]!="!") sscanf(tokens[2].c_str(),"%d:%d",&i1,&i2);
2157 if(tks>3) if(tokens[3]!="!") sscanf(tokens[3].c_str(),"%d:%d",&j1,&j2);
2158 if(i1<0) i1=0; if(i2>=nx1) i2=nx1-1; if(j1<0) j1=0; if(j2>=ny1) j2=ny1-1;
2159 if(i2<i1 || j2<j1)
2160 {cout<<"PAWExecutor::h_copy Error: wrong range ["<<i1<<":"<<i2
2161 <<"] , ["<<j1<<":"<<j2<<"] for NBinX,NBinY="
2162 <<nx1<<" , "<<ny1<<endl; return;}
2163 Histo2D* v2 = NULL;
2164 if(mobjv2==NULL)
2165 {v2 = new Histo2D(0.,(r_8)(i2-i1+1),i2-i1+1,0.,(r_8)(j2-j1+1),j2-j1+1);
2166 omg.AddObj(v2,tokens[1]); mobjv2 = omg.GetObj(tokens[1]);}
2167 if(typeid(*mobjv2) != typeid(Histo2D))
2168 {cout<<"PAWExecutor::h_copy Error: type mismatch for Histo2D"
2169 <<typeid(*mobjv2).name()<<endl; return;}
2170 v2 = dynamic_cast<Histo2D*>(mobjv2); nx2=v2->NBinX(); ny2=v2->NBinY();
2171 if(v1->HasErrors() && !(v2->HasErrors())) v2->Errors();
2172 if(tks>4) sscanf(tokens[4].c_str(),"%d:%d",&ic1,&jc1);
2173 if(ic1<0) ic1=0; if(jc1<0) jc1=0;
2174 if(ic1>=nx2 || jc1>=ny2)
2175 {cout<<"PAWExecutor::h_copy Error: wrong pointer to copy ["<<ic1<<","<<jc1
2176 <<"] for NBinX,NBinY="<<nx2<<","<<ny2<<endl; return;}
2177 cout<<"copy "<<tokens[0]<<"("<<i1<<":"<<i2<<","<<j1<<":"<<j2
2178 <<") to "<<tokens[1]<<"("<<ic1<<","<<jc1<<"->...)"<<endl;
2179 for(i=i1,ii=ic1; i<=i2 && i<nx1 && ii<nx2; i++,ii++)
2180 for(j=j1,jj=jc1; j<=j2 && j<ny1 && jj<ny2; j++,jj++)
2181 {(*v2)(ii,jj) = (*v1)(i,j); if(v1->HasErrors()) v2->Error2(ii,jj) = v1->Error2(i,j);}
2182 return;
2183}
2184
2185// Cas non prevu
2186cout<<"PAWExecutor::h_copy Error: type mismatch for Vector/Matrix/Histo/Histo2D\n"
2187 <<typeid(*mobjv1).name()<<endl;
2188return;
2189}
2190
2191/* methode */
2192void PAWExecutor::h_set(string dum,vector<string>& tokens)
2193// Mise de valeurs/erreurs d'un histo 1D ou 2D a une valeur donnee
2194// dum = err : on change les valeurs des erreurs
2195// cont : on change les valeurs du contenu des bins
2196// tokens[0] : nom de l'histogramme
2197// tokens[1] : valeur de remplacement
2198// tokens[2-3] : i1:i2 j1:j2 intervalle des bins qui doivent etre remplace
2199// : v v1:v2 remplacement des bins ayant une valeur dans cet intervalle
2200// : e e1:e2 remplacement des bins ayant une erreur dans cet intervalle
2201{
2202int_4 tks = tokens.size();
2203if(tks<3)
2204 {cout<<"Usage: h/set/[err,cont] namehisto setvalue i1[:i2] [j1[:j2]]\n"
2205 <<" v v1:v2\n"
2206 <<" e e1:e2"
2207 <<endl; return;}
2208
2209// Decodage arguments
2210bool replerr = false; if(dum=="err") replerr = true;
2211r_8 setval = atof(tokens[1].c_str());
2212int_4 testcont=0; if(tokens[2]=="v") testcont=1; if(tokens[2]=="e") testcont=2;
2213int_4 i1=-1, i2=-1, j1=-1, j2=-1;
2214r_8 v1=0., v2=0.;
2215if(testcont==0) {
2216 sscanf(tokens[2].c_str(),"%d:%d",&i1,&i2);
2217 if(tks>3) sscanf(tokens[3].c_str(),"%d:%d",&j1,&j2);
2218} else {
2219 if(tks<=3)
2220 {cout<<"PAWExecutor::h_set Error: h/set/... v v1:v2, please give v1:v2"<<endl;
2221 return;}
2222 sscanf(tokens[3].c_str(),"%lf:%lf",&v1,&v2);
2223}
2224
2225if(replerr) {if(setval<0.) setval = 0.; else setval *= setval;}
2226if(testcont!=0 && v2<v1)
2227 {cout<<"PAWExecutor::h_set Error: bad value range="<<v1<<","<<v2<<endl; return;}
2228
2229// identification objet
2230NamedObjMgr omg;
2231AnyDataObj* mobj = omg.GetObj(tokens[0]);
2232if( mobj==NULL)
2233 {cout<<"PAWExecutor::h_set Error: unknow object "<<tokens[0]<<endl;
2234 return;}
2235
2236// Traitement
2237if(typeid(*mobj) == typeid(Histo)) { // Histo 1D
2238 Histo* h = dynamic_cast<Histo*>(mobj);
2239 if( (replerr || testcont==2) && !(h->HasErrors()) )
2240 {cout<<"PAWExecutor::h_set Error: histogram has no errors"<<endl; return;}
2241 if(testcont!=0) {i1=0; i2=h->NBins()-1;}
2242 if(i1<0 || i1>=h->NBins())
2243 {cout<<"PAWExecutor::h_set Error: bad bin range i1="<<i1<<endl; return;}
2244 if(i2<i1) i2=i1; if(i2>=h->NBins()) i2=h->NBins()-1;
2245 for(int_4 i=i1;i<=i2;i++) {
2246 bool change = true;
2247 if(testcont==1) {if((*h)(i)<v1 || (*h)(i)>v2) change = false;}
2248 else if(testcont==2) {if(h->Error(i)<v1 || h->Error(i)>v2) change = false;}
2249 if(!change) continue;
2250 if(replerr) h->Error2(i) = setval; else (*h)(i) = setval;
2251 }
2252
2253} else if(typeid(*mobj) == typeid(Histo2D)) { // Histo 2D
2254 Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
2255 if( (replerr || testcont==2) && !(h2->HasErrors()) )
2256 {cout<<"PAWExecutor::h_set Error: histogram has no errors"<<endl; return;}
2257 if(testcont!=0) {i1=0; i2=h2->NBinX()-1;j1=0; j2=h2->NBinY()-1;}
2258 if(i1<0 || i1>=h2->NBinX() || j1<0 || j1>=h2->NBinY())
2259 {cout<<"PAWExecutor::h_set Error: bad bin range i1,j1="<<i1<<","<<j1<<endl; return;}
2260 if(i2<i1) i2=i1; if(i2>=h2->NBinX()) i2=h2->NBinX()-1;
2261 if(j2<j1) j2=j1; if(j2>=h2->NBinY()) j2=h2->NBinY()-1;
2262 for(int_4 i=i1;i<=i2;i++) for(int_4 j=j1;j<=j2;j++) {
2263 bool change = true;
2264 if(testcont==1) {if((*h2)(i,j)<v1 || (*h2)(i,j)>v2) change = false;}
2265 else if(testcont==2) {if(h2->Error(i,j)<v1 || h2->Error(i,j)>v2) change = false;}
2266 if(!change) continue;
2267 if(replerr) h2->Error2(i,j) = setval; else (*h2)(i,j) = setval;
2268 }
2269
2270} else {
2271 cout<<"PAWExecutor::h_set Error: type mismatch for Histo/Histo2D\n"
2272 <<typeid(*mobj).name()<<endl;
2273 return;
2274}
2275
2276}
2277
2278/* methode */
2279void PAWExecutor::h_err(vector<string>& tokens)
2280// Mise des erreurs d'un histo 1D ou 2D a une valeur
2281// donnee par une fonction de la valeur du bin
2282// tokens[0] : nom de l'histogramme
2283// tokens[1] : expression de la fonction
2284{
2285if(tokens.size()<2)
2286 {cout<<"Usage: h/err namehisto expr_func"<<endl; return;}
2287
2288// identification objet
2289NamedObjMgr omg;
2290AnyDataObj* mobj = omg.GetObj(tokens[0]);
2291if( mobj==NULL)
2292 {cout<<"PAWExecutor::h_err Error: unknow object "<<tokens[0]<<endl;
2293 return;}
2294
2295// Fonction
2296FILE *fip = NULL;
2297string expfunc = ""; {for(int_4 i=1;i<(int)tokens.size();i++) expfunc += tokens[i];}
2298string fname = "func1or2_pia_dl.c";
2299string func = "func1or2_pia_dl_func";
2300if((fip = fopen(fname.c_str(), "w")) == NULL)
2301 {cout<<"PAWExecutor::h_err Error: open function file "<<fname<<endl;
2302 return;}
2303fprintf(fip,"#include <math.h>\n");
2304fprintf(fip,"double %s(double x) \n{\n",func.c_str());
2305fprintf(fip,"return(%s); \n}\n", expfunc.c_str());
2306fclose(fip);
2307DlFunctionOfX f = (DlFunctionOfX) omg.GetServiceObj()->LinkFunctionFromFile(fname,func);
2308if(!f)
2309 {cout<<"PAWExecutor::h_err Error: bad function "<<func<<","<<fname<<endl;
2310 return;}
2311
2312// Traitement
2313if(typeid(*mobj) == typeid(Histo)) { // Histo 1D
2314 Histo* h = dynamic_cast<Histo*>(mobj);
2315 if(!(h->HasErrors())) h->Errors();
2316 for(int_4 i=0;i<h->NBins();i++) {
2317 r_8 x = (*h)(i);
2318 r_8 e = f(x);
2319 h->SetErr2(i,e*e);
2320 }
2321
2322} else if(typeid(*mobj) == typeid(Histo2D)) { // Histo 2D
2323 Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
2324 if(!(h2->HasErrors())) h2->Errors();
2325 for(int_4 i=0;i<h2->NBinX();i++) for(int_4 j=0;j<h2->NBinY();j++) {
2326 r_8 x = (*h2)(i,j);
2327 r_8 e = f(x);
2328 h2->Error2(i,j) = e*e;
2329 }
2330
2331} else {
2332 cout<<"PAWExecutor::h_err Error: type mismatch for Histo/Histo2D\n"
2333 <<typeid(*mobj).name()<<endl;
2334 return;
2335}
2336
2337}
Note: See TracBrowser for help on using the repository browser.