source: Sophya/trunk/SophyaLib/Manual/sophya.tex@ 1434

Last change on this file since 1434 was 1434, checked in by ansari, 25 years ago

Rajout de documentation - modif fichier .sty - Reza 6/3/2001

File size: 42.2 KB
Line 
1\documentclass[twoside,11pt]{article}
2% Package standard : Utilisation de caracteres accentues, mode francais et graphique
3\usepackage{url}
4\usepackage[latin1]{inputenc}
5\usepackage[T1]{fontenc}
6\usepackage[english]{babel}
7\usepackage{graphicx}
8% package a mettre pour faire du pdf
9\usepackage{palatino}
10
11% Extension de symboles mathematiques
12\usepackage{amssymb}
13
14% Definition pour Docs Sophya
15\usepackage{defsophya}
16
17% Constitution d'index
18\usepackage{makeidx}
19\makeindex
20
21\begin{document}
22
23\begin{titlepage}
24% The title page - top of the page with the title of the paper
25\titrehp{Sophya \\ An overview }
26% Authors list
27\auteurs{
28R. Ansari & ansari@lal.in2p3.fr \\
29E. Aubourg & aubourg@hep.saclay.cea.fr \\
30G. Le Meur & lemeur@lal.in2p3.fr \\
31C. Magneville & cmv@hep.saclay.cea.fr \\
32S. Henrot-Versille & versille@in2p3.fr
33}
34% \auteursall
35% The title page - bottom of the page with the paper number
36\vspace{1cm}
37\begin{center}
38{\bf \Large Document being updated !}
39\end{center}
40\titrebp{1}
41\end{titlepage}
42
43\tableofcontents
44
45\newpage
46
47\section{Introduction}
48
49{\bf SOPHYA} ({\bf SO}ftware for {\bf PHY}sics {\bf A}nalysis)
50is a collection of C++ classes designed for numerical and
51physics analysis software development. Our goal is to provide
52easy to use, yet powerful classes which can be used by scientists.
53We have decided to use as much as possible available
54numerical analysis libraries, encapsulating them whenever
55possible. Although some the modules in SOPHYA have been
56designed with the specific goal of providing the general framework for
57the Planck-HFI data processing software, most of the
58packages presented here have a more general scope than the
59CMB analysis and Planck mission problem. This documents
60presents only a brief overview of the class library,
61mainly from the user's point of view. A more complete description
62can be found in the reference manual, available from our
63web site: \\
64{\bf http://www.sophya.org}.
65
66The source directory tree
67\footnote{ CVS: cvsserver.lal.in2p3.fr:/exp/eros/CVSPlanck}
68is organised into a number of modules.
69
70\begin{itemize}
71\item[] {\bf Mgr/} Scripts for code management,
72makefile generation and software installation
73\item[] {\bf SysTools/} General architecture support classes such
74as {\tt PPersist, NDataBlock<T>}, and few utility classes
75({\tt DataCard, DVList} \ldots).
76\item[] {\bf TArray/} template numerical arrays, vectors and matrices \\
77({\tt PixelMap<T> SphericalMap<T>} \ldots)
78\item[] {\bf NTools/} Some standard numerical analysis tools
79(linear, and non linear parameter fitting, FFT, \ldots)
80\item[] {\bf HiStats/} Histogram-ming and data set handling classes \\
81({\tt Histo Histo2D NTuple XNTuple} \ldots)
82\end{itemize}
83
84The modules listed below are more tightly related to the
85CMB (Cosmic Microwave Background) data analysis problem:
86\begin{itemize}
87\item[] {\bf SkyMap/} Local and full sky maps, and few geometry
88handling utility classes. \\
89({\tt PixelMap<T>, LocalMap<T>, SphericalMap<T>, \ldots})
90\item[] {\bf SkyT/}
91classes for spectral emission and detector frequency response modelling \\
92({\tt SpectralResponse, RadSpectra, BlackBody} \ldots)
93\item[] {\bf Samba/} Spherical harmonic analysis.
94\end{itemize}
95
96The following modules contain the interface classes with
97external libraries:
98\begin{itemize}
99\item[] {\bf FitsIOServer/} Classes for handling file input-output
100in FITS format using the cfitsio library.
101\item[] {\bf LinAlg/} Interface with Lapack linear algebra package
102\item[] {\bf IFFTW/} Interface with FFTW package (libfftw.a)
103\end{itemize}
104
105Other modules:
106\begin{itemize}
107\item[] {\bf Tests/} Simple test programs
108\item[] {\bf PrgUtil/} Various utility programs (runcxx, scanppf, scanfits, \ldots)
109\item[] {\bf PMixer/} skymixer and related programs
110\item[] {\bf ProgPI/} interactive analysis tool - It should be noted that
111this module uses the SOPHYA class library and is based on {\bf PI}
112which is a C++ library defining a complete GUI program
113architecture. An additional module (PIext) define the interactive
114analysis program framework and the interfaces with the objects
115in SOPHYA. The {\bf PI/} \footnote{the PI package documentation
116is available from {\bf http://www.lal.in2p3.fr/recherche/eros/PeidaDoc/} }
117and {\bf PIext/} modules are not currently part
118of the SOPHYA CVS structure.
119\end{itemize}
120
121Modules containing examples and demo programs:
122\begin{itemize}
123\item[] {\bf Examples/} Sample SOPHYA codes and example programs and
124makefiles (auto\_makefile and ex\_makefile).
125\item[] {\bf DemoPIApp/} Sample exripts and programs for (s)piapp
126interactive analysis tools.
127\end{itemize}
128\newpage
129
130\section{Using Sophya}
131Basic usage of Sophya classes are described in in the following sections.
132Complete Sophya documentation can be found at our web site: \\
133{\bf http://www.sophya.org}.
134
135\subsection{Environment variables}
136Two environment variables {\bf DPCBASEREP} and {\bf EROSCXX} are used
137to define the path where the Sophya libraries and executable are installed.
138{\bf DPCBASEREP} defines the base directory path and {\bf EROSCXX} the
139name of the C++ compiler. The complete path is built using {\bf DPCBASEREP},
140the operating system name (as obtained by the {\tt uname} command), and
141the compiler name. In the example below, we show the complete path
142for a {\tt Linux} system, using the GNU g++ compiler:
143
144\begin{itemize}
145\item \$DPCBASEREP/Include : Include (.h) files
146\item \$DPCBASEREP/Linux-g++/Libs : Path for the archive libraries (.a)
147\item \$DPCBASEREP/Linux-g++/ShLibs : Shared library path (.so)
148\item \$DPCBASEREP/Linux-g++/Exec : Executable file path
149\end{itemize}
150
151In order to use the shared libraries, the {\bf LD\_LIBRARY\_PATH} variable
152should contain the Sophya shared library path
153({\tt \$DPCBASEREP/Linux-g++/ShLibs } when using g++ compiler on Linux)
154
155For modules using external libraries, the {\bf EXTLIBDIR}
156environment variable should contain the path to these libraries
157and corresponding include files.
158C-FitsIO anf FFTW include files should be accessible through: \\
159{\tt \$EXTLIBDIR/Include/FitsIO } \\
160{\tt \$EXTLIBDIR/Include/FFTW } \\
161The corresponding libraries are expected to be found in: \\
162{\tt \$EXTLIBDIR/Linux-g++/Libs} \\
163
164\subsection{User makefiles}
165The file {\tt \$DPCBASEREP/Include/MakefileUser.h} defines the compilation
166flags and the list of Sophya libraries. It should be included in the
167user's makefile. The default compilation rules assumes that the object (.o)
168and executable files would be put in the following diretories: \\
169{\tt \$HOME/`uname`-\$EROSCXX/Objs} \\
170{\tt \$HOME/`uname`-\$EROSCXX/Exec}.
171In the case of a {\tt Linux} system and using {\tt g++} as the C++ compiler,
172these two directories would be translated to \\
173{\tt \$HOME/Linux-g++/Objs} and {\tt \$HOME/Linux-g++/Exec}.
174The GNU make program should be used.
175\par
176The file {\tt \$DPCBASEREP/Include/makefile\_auto} defines the rules to compile
177a given source program, and link it against the Sophya libraries to produce
178an executable. The example below shows the steps to compile a program named
179{\tt trivial.cc }.
180\begin{verbatim}
181csh> cp Examples/auto_makefile makefile
182csh> cp Examples/ex1.cc trivial.cc
183csh> make trivial
184\end{verbatim}
185This command should compile the {\tt trivial.cc} file,
186and link it against the sophya libraries.
187The file {\tt \$DPCBASEREP/Include/ex\_makefile} provides another
188example makefile.
189
190\subsection{the runcxx program}
191\index{runcxx}
192{\bf runcxx} is a simple program which can be used to compile, link
193and run simple C++ programs. It handles the creation of a
194complete program file, containing the basic set C++ include files,
195the necessary include files for SOPHYA SysTools, TArray, HiStats
196and NTools modules, and the main program with exception handling.
197Other Sophya modules can be included using the {\tt -import} flag.
198\begin{verbatim}
199csh> runcxx -h
200SOPHYA Version 1.1 Revision 0 (V_Fev2001) -- Feb 28 2001 11:19:17 cxx
201 runcxx : compiling and running of a piece of C++ code
202 Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions]
203 [-tmpdir TmpDirectory] [-f C++CodeFileName]
204 [-inc includefile] [-inc includefile ...]
205 [-import modulename] [-import modulename ...]
206 [-uarg UserArg1 UserArg2 ...]
207 if no file name is specified, read from standard input
208 modulenames: SkyMap, Samba, SkyT, FitsIOServer, LinAlg, IFFTW
209\end{verbatim}
210Most examples in this manual can be tested using runcxx. The
211example below shows how to compile, link and run a sample
212code.
213\begin{verbatim}
214// File example.icc
215Matrix a(3,3);
216a = IdentityMatrix(1.);
217cout << a ;
218// Executing this sample code
219csh> runcxx -f example.icc
220\end{verbatim}
221
222
223\section{Copy constructor and assignment operator}
224In C++, objects can be copied by assignment or by initialization.
225Copying by initialization corresponds to creating an object and
226initializing its value through the copy constructor.
227The copy constructor has its first argument as a reference, or
228const reference to the object's class type. It can have
229more arguments, if default values are provided.
230Copying by assignment applies to an existing object and
231is performed through the assignment operator (=).
232The copy constructor implements this for identical type objects:
233\begin{verbatim}
234class MyObject {
235public:
236 MyObject(); // Default constructor
237 MyObject(MyObject const & a); // Copy constructor
238 MyObject & operator = (MyObject const & a) // Assignment operator
239}
240\end{verbatim}
241The copy constructors play an important role, as they are
242called when class objects are passed by value,
243returned by value, or thrown as an exception.
244\begin{verbatim}
245// A function declaration with an argument of type MyObject,
246// passed by value, and returning a MyObject
247MyObject f(MyObject x)
248{
249 MyObject r;
250 ...
251 return(r); // Copy constructor is called here
252}
253// Calling the function :
254MyObject a;
255f(a); // Copy constructor called for a
256\end{verbatim}
257It should be noted that the C++ syntax is ambiguous for the
258assignment operator. {\tt MyObject x; x=y; } and
259{\tt MyObject x=y;} have different meaning.
260\begin{verbatim}
261MyObject a; // default constructor call
262MyObject b(a); // copy constructor call
263MyObject bb = a; // identical to bb(a) : copy constructor call
264MyObject c; // default constructor call
265c = a; // assignment operator call
266\end{verbatim}
267
268As a general rule in SOPHYA, objects which implements
269reference sharing on their data members have a copy constructor
270which shares the data, while the assignment operator copies or
271duplicate the data.
272
273\section{Module SysTools}
274
275{\bf SysTools} contains utility classes such as {\tt DataCards} or
276{\tt DVlist}, an hierarchy of exception classes for Sophya, a template
277class {\tcls{NDataBlock}} for handling reference counting on numerical
278arrays, as well as classes providing the services for implementing simple
279serialization.
280\vspace*{5mm}
281
282\subsection{SOPHYA persistence}
283\index{PPersist} \index{PInPersist} \index{POutPersist}
284\begin{figure}[hbt]
285\dclsa{PPersist}
286\dclsbb{PIOPersist}{PInPersist}
287\dclsb{POutPersist}
288\caption{partial class diagram for classes handling persistence in Sophya}
289\end{figure}
290A simple persistence mechanism is defined in SOPHYA. Its main
291features are:
292\begin{itemize}
293\item[] Portable file format, containing the description of the data structures
294and object hierarchy. \\
295{\bf PPF} {\bf P}ortable {\bf P}ersistence file {\bf F}ormat.
296\item[] Handling of read/write for mutiply referenced objects.
297\item[] All write operations are carried using sequential access only. This
298holds also for read operations, unless positional tags are used.
299SOPHYA persistence services can thus be used to transfer objects
300through network links.
301\item[] The serialisation (reading/writing) for objects for a given class
302is implemented through a delegate object. The delegate class inherits
303from {\tt PPersist} class.
304\end{itemize}
305A complete description of SOPHYA persistence mechanism and guidelines
306for writing delegate classes for handling object persistence is beyond
307the scope of this document. The example in the next paragraph shows
308simple use of SOPHYA persistence.
309
310\subsection{\tcls{NDataBlock}}
311\index{\tcls{NDataBlock}}
312\begin{figure}[hbt]
313\dclsbb{AnyDataObj}{\tcls{NDataBlock}}
314\dclsbb{PPersist}{\tcls{FIO\_NDataBlock}}
315\end{figure}
316The {\bf \tcls{NDataBlock}} is designed to handle reference counting
317and sharing of memory blocs (contiguous arrays) for numerical data
318types. Initialisation, resizing, basic arithmetic operations, as
319well as persistence handling services are provided.
320The persistence handler class ({\tt \tcls{FIO\_NDataBlock}}) insures
321that a single copy of data is written for multiply referenced objects,
322and the data is shared among objects when reading.
323\par
324The example below shows writing of NDataBlock objects through the
325use of overloaded operator $ << $ :
326\begin{verbatim}
327#include "fiondblock.h"
328// ...
329POutPersist pos("aa.ppf");
330NDataBlock<r_4> rdb(40);
331rdb = 567.89;
332pos << rdb;
333// We can also use the PutObject method
334NDataBlock<int_4> idb(20);
335idb = 123;
336pos.PutObject(idb);
337\end{verbatim}
338The following sample programs show the reading of the created PPF file :
339\begin{verbatim}
340PInPersist pis("aa.ppf");
341NDataBlock<r_4> rdb;
342pis >> rdb;
343cout << rdb;
344NDataBlock<int_4> idb;
345cout << idb;
346\end{verbatim}
347
348\subsection{Using DVList}
349\index{DVList} \index{MuTyV}
350\begin{figure}[hbt]
351\dclsbb{AnyDataObj}{DVList}
352\dclsbb{PPersist}{\tclsc{ObjFileIO}{DVList}}
353\end{figure}
354The {\bf DVList} class objects can be used to create and manage list
355of values, associated with names. A list of pairs of (MuTyV, name(string))
356is maintained by DVList objects. {\bf MuTyV} is a simple class
357capable of holding string, integer, float or complex values,
358providing easy conversion methods between these objects.
359\begin{verbatim}
360// Using MuTyV objects
361MuTyV s("hello"); // string type value
362MuTyV x;
363x = "3.14159626"; // string type value, ascii representation for Pi
364double d = x; // x converted to double = 3.141596
365x = 314; // x contains the integer value = 314
366// Using DVList
367DVList dvl;
368dvl("Pi") = 3.14159626; // float value, named Pi
369dvl("Log2") = 0.30102999; // float value, named Log2
370dvl("FileName") = "myfile.fits"; // string value, named myfile.fits
371// Printing DVList object
372cout << dvl;
373\end{verbatim}
374
375\subsection{Using DataCards}
376\index{DataCards}
377The {\bf DataCards} class can be used to read parameters from a file.
378Each line in the file starting with \@ defines a set of values
379associated with a keyword. In the example below, we read the
380parameters corresponding with the keyword {\tt SIZE} from the
381file {\tt ex.d}. We suppose that {\tt ex.d} contains the line: \\
382{\tt @SIZE 400 250} \\
383\begin{verbatim}
384#include "datacards.h"
385// ...
386// Initializing DataCards object dc from file ex.d
387DataCards dc( "ex.d" );
388// Getting the first and second parameters for keyword size
389// We define a default value 100
390int size_x = dc.IParam("SIZE", 0, 100);
391int size_y = dc.IParam("SIZE", 1, 100);
392cout << " size_x= " << size_x << " size_y= " << size_y << endl;
393\end{verbatim}
394
395\subsection{Dynamic linker}
396\index{PDynLinkMgr}
397The class {\bf PDynLinkMgr} can be used for managing shared libraries
398at run time. The example below shows the run time linking of a function:\\
399{\tt extern "C" { void myfunc(); } } \\
400\begin{verbatim}
401#include "pdlmgr.h"
402// ...
403string soname = "mylib.so";
404string funcname = "myfunc";
405PDynLinkMgr dyl(soname);
406DlFunction f = dyl.GetFunction(funcname);
407if (f != NULL) {
408// Calling the function
409 f();
410}
411\end{verbatim}
412
413\subsection{CxxCompilerLinker class}
414\index{CxxCompilerLinker}
415This class provides the services to compile C++ code and building
416shared libraries, using the same compiler and options which have
417been used to create the SOPHYA shared library.
418The sample program below illustrates using this class to build
419the shared library (myfunc.so) from the source file myfunc.cc :
420\begin{verbatim}
421#include "cxxcmplnk.h"
422// ...
423string flnm = "myfunc.cc";
424string oname, soname;
425int rc;
426CxxCompilerLinker cxx;
427// The Compile method provides a default object file name
428rc = cxx.Compile(flnm, oname);
429if (rc != 0 ) { // Error when compiling ... }
430// The BuildSO method provides a default shared object file name
431rc = cxx.BuildSO(oname, soname);
432if (rc != 0 ) { // Error when creating shared object ... }
433\end{verbatim}
434
435\section{Module TArray}
436\index{\tcls{TArray}}
437{\bf TArray} module contains template classes for handling standard
438operations on numerical arrays. Using the class {\tt \tcls{TArray} },
439it is possible to create and manipulate up to 5-dimension numerical
440arrays {\tt (int, float, double, complex, \ldots)}. The include
441file {\tt array.h} declares all the classes and definitions
442in module TArray. {\bf Array} is a typdef for arrays
443with double precision floating value elements. \\
444{\tt typedef TArray$<$r\_8$>$ Array ; }
445
446\begin{figure}[hbt]
447\dclsccc{AnyDataObj}{BaseArray}{\tcls{TArray}}
448\dclsbb{PPersist}{\tcls{FIO\_TArray}}
449\end{figure}
450
451
452\subsection{Using arrays}
453\index{Sequence} \index{RandomSequence} \index{RegularSequence}
454\index{EnumeratedSequence}
455The example below shows basic usage of arrays, creation, initialization
456and arithmetic operations. Different kind of {\bf Sequence} objects
457can be used for initializing arrays.
458
459\begin{figure}[hbt]
460\dclsbb{Sequence}{RandomSequence}
461\dclsb{RegularSequence}
462\dclsb{EnumeratedSequence}
463\end{figure}
464
465The example below shows basic usage of arrays:
466\index{\tcls{TArray}}
467\begin{verbatim}
468// Creating and initializing a 1-D array of integers
469TArray<int> ia(5);
470EnumeratedSequence es;
471es = 24, 35, 46, 57, 68;
472ia = es;
473cout << "Array<int> ia = " << ia;
474// 2-D array of floats
475TArray<r_4> b(6,4), c(6,4);
476// Initializing b with a constant
477b = 2.71828;
478// Filling c with random numbers
479c = RandomSequence();
480// Arithmetic operations
481TArray<r_4> d = b+0.3f*c;
482cout << "Array<float> d = " << d;
483\end{verbatim}
484
485The copy constructor shares the array data, while the assignment operator
486copies the array elements, as illustrated in the following example:
487\begin{verbatim}
488TArray<int> a1(4,3);
489a1 = RegularSequence(0,2);
490// Array a2 and a1 shares their data
491TArray<int> a2(a1);
492// a3 and a1 have the same size and identical elements
493TArray<int> a3;
494a3 = a1;
495// Changing one of the a2 elements
496a2(1,1,0) = 555;
497// a1(1,1) is also changed to 555, but not a3(1,1)
498cout << "Array<int> a1 = " << a1;
499cout << "Array<int> a3 = " << a3;
500\end{verbatim}
501
502\subsection{Matrices and vectors}
503\index{\tcls{TMatrix}} \index{\tcls{TVector}}
504\begin{figure}[hbt]
505\dclsccc{\tcls{TArray}}{\tcls{TMatrix}}{\tcls{TVector}}
506\end{figure}
507Vectors and matrices are 2 dimensional arrays. The array size
508along one dimension is equal 1 for vectors. Column vectors
509have {\tt NCols() = 1} and row vectors have {\tt NRows() = 1}.
510Mathematical expressions involving matrices and vectors can easily
511be translated into C++ code using {\tt TMatrix} and
512{\tt TVector} objects. {\bf Matrix} and {\bf Vector} are
513typedefs for double precision float matrices and vectors.
514The operator {\bf *} beteween matrices is redefined to
515perform matrix multiplication. One can then write: \\
516\begin{verbatim}
517 // We create a row vector
518 Vector v(1000, BaseArray::RowVector);
519 // Initialize values with a random sequence
520 v = RandomSequence();
521 // Compute the vector length (norm)
522 double norm = (v*v.Transpose()).toScalar();
523 cout << "Norm(v) = " << norm << endl;
524\end{verbatim}
525
526This module contains basic array and matrix operations
527such as the Gauss matrix inversion algorithm
528which can be used to solve linear systems, as illustrated by the
529example below:
530\begin{verbatim}
531#include "sopemtx.h"
532// ...
533// Creation of a random 5x5 matrix
534Matrix A(5,5);
535A = RandomSequence(RandomSequence::Flat);
536Vector X0(5);
537X0 = RandomSequence(RandomSequence::Gaussian);
538// Computing B = A*X0
539Vector B = A*X0;
540// Solving the system A*X = B
541Vector X;
542LinSolve(A, B, X);
543// Checking the result
544Vector diff = X-X0;
545cout << "X-X0= " << diff ;
546double min,max;
547diff.MinMax(min, max);
548cout << " Min(X-X0) = " << min << " Max(X-X0) = " << max << endl;
549\end{verbatim}
550
551\subsection{Working with sub-arrays and Ranges}
552\index{Range}
553A powerful mechanism is included in array classes for working with
554sub-arrays. The class {\bf Range} can be used to specify range of array
555indexes in any of the array dimensions. Any regularly spaced index
556range can be specified, using the {\tt start} and {\tt end} index
557and an optional step (or stride). It is also possible to specify
558the {\tt start} index and the number of elements.
559In the following example, a simple low-pas filter, on a one
560dimensional stream (Vector) has been written using sub-arrays:
561
562\begin{verbatim}
563// Input Vector containing a noisy periodic signal
564 Vector in(1024), out(1024);
565 in = RandomSequence(RandomSequence::Gaussian, 0., 1.);
566 for(int kk=0; kk<in.Size(); kk++)
567 in(kk) += 2*sin(kk*0.05);
568// Compute the output vector by a simple low pass filter
569 Vector out(1024);
570 int w = 2;
571 for(int k=w; k<in.Size()-w; k++)
572 out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.);
573\end{verbatim}
574
575\newpage
576\subsection{Memory organisation}
577{\tt \tcls{TArray} } can handle numerical arrays with various memory
578organisation, as long as the spacing (steps) along each axis is
579regular. The five axis are labeled X,Y,Z,T,U. The examples below
580illustrates the memory location for a 2-dimensional, $N_x=4 \times N_y=3$.
581The first index is along the X axis and the second index along the Y axis.
582\begin{verbatim}
583 | (0,0) (0,1) (0,2) (0,3) |
584 | (1,0) (1,1) (1,2) (1,3) |
585 | (2,0) (2,1) (2,2) (2,3) |
586\end{verbatim}
587In the first case, the array is completely packed
588($Step_X=1, Step_Y=N_X=4$), with zero offset,
589while in the second case, $Step_X=2, Step_Y=10, Offset=10$:
590\begin{verbatim}
591 | 0 1 2 3 | | 10 12 14 16 |
592Ex1 | 4 5 6 7 | Ex2 | 20 22 24 26 |
593 | 8 9 10 11 | | 30 32 34 36 |
594\end{verbatim}
595
596For matrices and vectors, an optional argument ({\tt MemoryMapping})
597can be used to select the memory mapping, where two basic schemes
598are available: \\
599{\tt CMemoryMapping} and {\tt FortranMemoryMapping}. \\
600In the case where {\tt CMemoryMapping} is used, a given matrix line
601is packed in memory, while the columns are packed when
602{\tt FortranMemoryMapping} is used. The first index when addressing
603the matrix elements (line number index) runs along
604the Y-axis if {\tt CMemoryMapping} is used, and along the X-axis
605in the case of {\tt FortranMemoryMapping}.
606Arithmetic operations between matrices
607with different memory organisation is allowed as long as
608the two matrices have the same sizes (Number of rows and columns).
609The following code example and the corresponding output illustrates
610these two memory mappings. The {\tt \tcls{TMatrix}::TransposeSelf() }
611method changes effectively the matrix memory mapping, which is also
612the case of {\tt \tcls{TMatrix}::Transpose() } method without argument.
613
614\begin{verbatim}
615TArray<r_4> X(4,2);
616X = RegularSequence(1,1);
617cout << "Array X= " << X << endl;
618TMatrix<r_4> X_C(X, true, BaseArray::CMemoryMapping);
619cout << "Matrix X_C (CMemoryMapping) = " << X_C << endl;
620TMatrix<r_4> X_F(X, true, BaseArray::FortranMemoryMapping);
621cout << "Matrix X_F (FortranMemoryMapping) = " << X_F << endl;
622\end{verbatim}
623\newpage
624This code would produce the following output (X\_F = Transpose(X\_C)) :
625\begin{verbatim}
626Array X=
627--- TArray<f> ND=2 SizeX*Y*...= 4x2 ---
6281, 2, 3, 4
6295, 6, 7, 8
630
631Matrix X_C (CMemoryMapping) =
632--- TMatrix<f>(NRows=2, NCols=4) ND=2 SizeX*Y*...= 4x2 ---
6331, 2, 3, 4
6345, 6, 7, 8
635
636Matrix X_F (FortranMemoryMapping) =
637--- TMatrix<f>(NRows=4, NCols=2) ND=2 SizeX*Y*...= 4x2 ---
6381, 5
6392, 6
6403, 7
6414, 8
642\end{verbatim}
643
644\newpage
645
646\section{Module HiStats}
647\begin{figure}[hbt]
648\dclsccc{AnyDataObj}{Histo}{HProf}
649\dclsbb{AnyDataObj}{Histo2D}
650\dclsbb{AnyDataObj}{Ntuple}
651\dclsb{XNtuple}
652\caption{partial class diagram for histograms and ntuples}
653\end{figure}
654
655{\bf HiStats} contains classes for creating, filling, printing and
656doing various operations on one or two dimensional histograms
657{\tt Histo} and {\tt Histo2D} as well as profile histograms {\tt HProf}. \\
658This module also contains {\tt NTuple} and {\tt XNTuple} which are
659more or less the same that the binary FITS tables.
660
661\subsection{1D Histograms}
662\index{Histo}
663For 1D histograms, various numerical methods are provided such as
664computing means and sigmas, finding maxima, fitting, rebinning,
665integrating \dots \\
666The example below shows creating and filling a one dimensionnal histogram
667of 100 bins from $-5.$ to $+5.$ to create a gaussian normal distribution
668with errors~:
669\begin{verbatim}
670#include "histos.h"
671// ...
672Histo H(-0.5,0.5,100);
673H.Errors();
674for(int i=0;i<25000;i++) {
675 double x = NorRand();
676 H.Add(x);
677}
678H.Print(80);
679\end{verbatim}
680
681\subsection{2D Histograms}
682\index{Histo2D}
683Much of these operations are also valid for 2D histograms. 1D projection
684or slices can be set~:
685\begin{verbatim}
686#include "histos2.h"
687// ...
688Histo2D H2(-1.,1.,100,0.,60.,50);
689H2.SetProjX(); // create the 1D histo for X projection
690H2.SetBandX(25.,35.); // create 1D histo projection for 25.<y<35.
691H2.SetBandX(35.,45.); // create 1D histo projection for 35.<y<45.
692H2.SetBandX(40.,55.); // create 1D histo projection for 40.<y<55.
693//... fill H2 with what ever you want
694H2.Print();
695Histo *hx = H2.HProjX();
696 hx->Print(80);
697Histo *hbx2 = HBandX(1); // Get the second X band (35.<y<45.)
698 hbx2->Print(80);
699\end{verbatim}
700
701\subsection{Profile Histograms}
702\index{HProf}
703Profiles histograms {\bf HProf} contains the mean and the
704sigma of the distribution
705of the values filled in each bin. The sigma can be changed to
706the error on the mean. When filled, the profile histogram looks
707like a 1D histogram and much of the operations that can be done on 1D histo
708may be applied onto profile histograms.
709
710\subsection{NTuples}
711\index{NTuple}
712NTuple are memory resident tables of 32 bits floating values (float).
713They are arranged in columns. Each line is often called an event.
714These objects are frequently used to analyze data.
715Graphicals tools (spiapp) can plot a column against an other one
716with respect to various selection cuts. \\
717Here is an example of creation and filling~:
718\begin{verbatim}
719#include "ntuple.h"
720#include "srandgen.h"
721// ...
722char* nament[4] = {"i","x","y","ey"};
723r_4 xnt[4];
724NTuple NT(4,nament);
725for(i=0;i<5000;i++) {
726 xnt[0] = i+1;
727 xnt[1] = 5.*drandpm1(); // a random value between -5 and +5
728 xnt[2] = 100.*exp(-0.5*xnt[1]*xnt[1]) + 1.;
729 xnt[3] = sqrt(xnt[2]);
730 xnt[2] += xnt[3] * NorRand(); // add a random gaussian error
731 NT.Fill(xnt);
732}
733\end{verbatim}
734
735XNTuple are sophisticated NTuple : they accept various types
736of column values (double,float,int,string,...) and can handle
737very large data sets, through swap space on disk.
738\index{XNTuple}
739In the sample code below we show how to create a XNTuple
740object with four columns (double, double, int, string).
741Several entries (lines) are then appended to the table,
742which is saved to a PPF file.
743\begin{verbatim}
744#include "xntuple.h"
745// ...
746char * names[4] = {"X", "X2", "XInt","XStr"};
747// XNTuple (Table) creation with 4 columns, of integer,
748// double(2) and string type
749XNTuple xnt(2,0,1,1, names);
750// Filling the NTuple
751r_8 xd[2];
752int_4 xi[2];
753char xss[2][32];
754char * xs[2] = {xss[0], xss[1]} ;
755for(int i=0; i<50; i++) {
756 xi[0] = i; xd[0] = i+0.5; xd[1] = xd[0]*xd[0];
757 sprintf(xs[0],"X=%g", xd[0]);
758 xnt.Fill(xd, NULL, xi, xs);
759}
760// Printing table info
761cout << xnt ;
762// Saving object into a PPF file
763POutPersist po("xnt.ppf");
764po << xnt ;
765\end{verbatim}
766
767\subsection{Writing, viewing \dots }
768
769All these objects have been design to be written to or read from a persistant file.
770The following example shows how to write the previously created objects
771into such a file~:
772\begin{verbatim}
773//-- Writing
774{
775char *fileout = "myfile.ppf";
776string tag;
777POutPersist outppf(fileout);
778tag = "H"; outppf.PutObject(H,tag);
779tag = "H2"; outppf.PutObject(H2,tag);
780tag = "NT"; outppf.PutObject(NT,tag);
781} // closing ``}'' destroy ``outppf'' and automatically close the file !
782\end{verbatim}
783
784Sophya graphical tools (spiapp) can automatically display and operate
785all these objects.
786
787\subsection{Fourier transform (FFT)}
788\index{FFT} \index{FFTServer}
789
790\newpage
791\section{Module SkyMap}
792\begin{figure}[hbt]
793\dclsbb{AnyDataObj}{PixelMap}
794\dclsccc{PixelMap}{Sphericalmap}{SphereHEALPix}
795\dclsc{SphereThetaPhi}
796\dclsb{LocalMap}
797\caption{partial class diagram for pixelization classes in Sophya}
798\end{figure}
799The {\bf SkyMap} module provides classes for creating, filling, reading pixelized spherical and 2D-maps. The types of values stored in pixels can be int, float, double , complex etc. according to the specialization of the template type.
800\subsection {Spherical maps}
801There are two kinds of spherical maps according pixelization algorithms. SphereHEALPix represents spheres pixelized following the HEALPIix algorithm (E. Yvon, K. Gorski), SphereThetaPhi represents spheres pixelized following an algorithm developed at LAL-ORSAY. The example below shows creating and filling of a SphereHEALPix with nside = 8 (it will be 12*8*8= 768 pixels) :
802\index{\tcls{SphereHEALPix}}
803
804\begin{verbatim}
805#include ``spherehealpix.h''
806// ...
807SphereHEALPix<double> sph(8);
808for (int k=0; k< sph.NbPixels(); k++) sph(k) = (double)(10*k);
809\end{verbatim}
810
811SphereThetaPhi is used in a similar way with an argument representing number of slices in theta (Euler angle) for an hemisphere.
812\index{\tcls{SphereThetaPhi}}
813
814\subsection {Local maps}
815\index{\tcls{LocalMap}}
816A local map is a 2 dimensional array, with i as column index and j as row index. The map is supposed to lie on a plan tangent to the celestial sphere in a point whose coordinates are (x0,y0) on the local map and (theta0, phi0) on the sphere. The range of the map is defined by two values of angles covered respectively by all the pixels in x direction and all the pixels in y direction (SetSize()). Default value of (x0, y0) is middle of the map, center of pixel(nx/2, ny/2).
817
818Internally, a map is first defined within this reference plane and tranported until the point (theta0, phi0) in such a way that both axes are kept parallel to meridian and parallel lines of the sphere. The user can define its own map with axes rotated with respect to reference axes (this rotation is characterized by angle between the local parallel line and the wanted x-axis-- method SetOrigin(...))
819
820The example below shows creating and filling of a LocalMap with 4 columns and 5 rows. The origin is set to default. The map covers a sphere portion defined by two angles of 30. degrees (methods \textit{SetOrigin()} and \textit{SetSize()} must be called in order to completely define the map).
821\begin{verbatim}
822#include "localmap.h"
823//..............
824 LocalMap<r_4> locmap(4,5);
825 for (int k=0; k<locmap.NbPixels();k++) locmap(k)=10.*k;
826 locmap.SetOrigin();
827 locmap.SetSize(30.,30.);
828\end{verbatim}
829
830\subsection{Writing, viewing \dots }
831
832All these objects have been design to be written to or read from a persistant file.
833The following example shows how to write the previously created objects
834into such a file~:
835\begin{verbatim}
836//-- Writing
837
838#include "fiospherehealpix.h"
839//................
840
841char *fileout = "myfile.ppf";
842POutPersist outppf(fileout);
843FIO_SphereHEALPix<r_8> outsph(sph);
844outsph.Write(outppf);
845FIO_LocalMap<r_8> outloc(locmap);
846outloc.Write(outppf);
847
848\end{verbatim}
849
850Sophya graphical tools (spiapp) can automatically display and operate
851all these objects.
852
853
854\section{Module NTools}
855
856This module provides elementary numerical tools for numerical integration,
857fitting, sorting and ODE solving. FFTs are also provided (Mayer,FFTPack).
858
859\subsection{Fitting}
860\index{Fitting} \index{Minimization}
861Fitting is done with two classes {\tt GeneralFit} and {\tt GeneralFitData}
862and is based on the Levenberg-Marquardt method.
863\index{GeneralFit} \index{GeneralFitData}
864GeneralFitData is a class which provide a description of the data
865to be fitted. GeneralFit is the fitter class. Parametrized functions
866can be given as classes which inherit {\tt GeneralFunction}
867or as simple C functions. Classes of pre-defined functions are provided
868(see files fct1dfit.h and fct2dfit.h). The user interface is very close
869from that of the CERN {\tt Minuit} fitter.
870Number of objects (Histo, HProf \dots ) are interfaced with GeneralFit
871and can be easily fitted. \\
872Here is a very simple example for fitting the previously created NTuple
873with a gaussian~:
874\begin{verbatim}
875#include "fct1dfit.h"
876// ...
877
878// Read from ppf file
879NTuple nt;
880{
881PInPersist pis("myfile.ppf");
882string tag = "NT"; pis.GetObject(nt,tag);
883}
884
885// Fill GeneralData
886GeneralData mGdata(nt.NEntry());
887for(int i=0; i<nt.NEntry(); i++)
888 mGdata.AddData1(xnt[1],xnt[2],xnt[3]); // Fill x, y and error on y
889mGData.PrintStatus();
890
891// Function for fitting : y = f(x) + noise
892Gauss1DPol mFunction; // gaussian + constant
893
894// Prepare for fit
895GeneralFit mFit(&mFunction); // create a fitter for the choosen function
896mFit.SetData(&mGData); // connect data to the fitter
897
898// Set and initialize the parameters (that's non-linear fitting!)
899// (num par, name, guess start, step, [limits min and max])
900mFit.SetParam(0,"high",90.,1..);
901mFit.SetParam(1,"xcenter",0.05,0.01);
902mFit.SetParam(2,"sigma",sig,0.05,0.01,10.);
903 // Give limits to avoid division by zero
904mFit.SetParam(3,"constant",0.,1.);
905
906// Fit and print result
907int rcfit = mFit.Fit();
908mFit.PrintFit();
909if(rcfit>0) {)
910 cout<<"Reduce_Chisquare = "<<mFit.GetChi2Red()
911 <<" nstep="<<mFit.GetNStep()<<" rc="<<rcfit<<endl;
912} else {
913 cout<<"Fit_Error, rc = "<<rcfit<<" nstep="<<mFit.GetNStep()<<endl;
914 mFit.PrintFitErr(rcfit);
915}
916
917// Get the result for further use
918TVector<r_8> ParResult = mFit.GetParm();
919cout<<ParResult;
920\end{verbatim}
921
922Much more usefull possibilities and detailed informations might be found
923in the HTML pages of the Sophya manual.
924
925\subsection{Polynomial}
926\index{Polynomial} \index{Poly} \index{Poly2}
927Polynomials of 1 or 2 variables are supported ({\tt Poly} and {\tt Poly2}).
928Various operations are supported~:
929\begin{itemize}
930\item elementary operations between polynomials $(+,-,*,/) $
931\item setting or getting coefficients
932\item computing the value of the polynomial for a given value
933 of the variable(s),
934\item derivating
935\item computing roots (degre 1 or 2)
936\item fitting the polynomial to vectors of data.
937\end{itemize}
938Here is an example of polynomial fitting~:
939\begin{verbatim}
940#include "poly.h"
941// ...
942Poly pol(2);
943pol[0] = 100.; pol[1] = 0.; pol[2] = 0.01; // Setting coefficients
944TVector<r_8> x(100);
945TVector<r_8> y(100);
946TVector<r_8> ey(100);
947for(int i=0;i<100;i++) {
948 x(i) = i;
949 ey(i) = 10.;
950 y(i) = pol((double) i) + ey(i)*NorRand();
951 ey(i) *= ey(i)
952}
953
954TVector<r_8> errcoef;
955Poly polfit;
956polfit.Fit(x,y,ey,2,errcoef);
957
958cout<<"Fit Result"<<polfit<<endl;
959cout<<"Errors :"<<errcoef;
960\end{verbatim}
961
962Similar operations can be done on polynomials with 2 variables.
963
964\section{Module Samba}
965\index{Spherical Harmonics}
966\index{SphericalTransformServer}
967The module provides several classes for spherical harmonic analysis. The main class is \textit{SphericalTranformServer}. It contains methods for analysis and synthesis of spherical maps. The following example fills a vector of Cl's, generate a spherical map from these Cl's. This map is analyzed back to Cl's...
968\begin{verbatim}
969#include "skymap.h"
970#include "samba.h"
971....................
972
973// Generate input spectra a + b* l + c * gaussienne(l, 50, 20)
974int lmax = 92;
975Vector clin(lmax);
976for(int l=0; l<lmax; l++) {
977 double xx = (l-50.)/10.;
978 clin(l) = 1.e-2 -1.e-4*l + 0.1*exp(-xx*xx);
979}
980
981// Compute map from spectra
982SphericalTransformServer<r_8> ylmserver;
983int m = 128; // HealPix pixelisation parameter
984SphereHEALPix<r_8> map(m);
985ylmserver.GenerateFromCl(map, m, clin, 0.);
986// Compute power spectrum from map
987Vector clout = ylmserver.DecomposeToCl(map, lmax, 0.);
988\end{verbatim}
989
990\section{Module SkyT}
991
992\section{Module FitsIOServer}
993\begin{figure}[hbt]
994\dclsbb{FitsFile}{FitsInFile}
995\dclsb{FitsOutFile}
996\end{figure}
997\index{FITS}
998This module provides classes for handling file input-output in FITS format using the cfitsio library. It works like the SOPHYA persistence (see Module SysTools), using delegate objects, but its design is simpler. The following example writes a matrix (see module TArray) and a spherical map (see module SkyMap) on a FITS file and reads back from FITS file and creates new objects :
999\begin{verbatim}
1000#include "spherehealpix.h"
1001#include "fitsspherehealpix.h"
1002#include "fitstarray.h"
1003#include "tmatrix.h"
1004//...........................
1005
1006int m=...;
1007SphereHEALPix<r_8> sph(m);
1008................
1009int dim1=...;
1010int dim2=...;
1011TMatrix<r_8> mat(dim1,dim2);
1012............
1013
1014FITS_SphereHEALPix<r_8> sph_temp(sph);
1015FITS_TArray<r_8> mat_temp(mat);
1016// writing
1017
1018FitsOutFile os("myfile.fits");
1019sph_temp.Write(os);
1020mat_temp.Write(os);
1021
1022// reading
1023FitsInFile is("myfile.fits");
1024sph_temp.Read(is);
1025mat_temp.Read(is);
1026SphereHEALPix<r_8> new_sph=(SphereHEALPix<r_8>)sph_temp;
1027TMatrix<r_8> new_mat=(TMatrix<r_8>)mat_temp;
1028................
1029
1030\end{verbatim}
1031
1032
1033
1034\newpage
1035\section{Building and installing Sophya}
1036Presently, the Sophya library compilation has been tested with the following
1037compiler/platform pairs:
1038
1039\begin{center}
1040\begin{tabular}{ll}
1041Compaq/DEC OSF1 & cxx (6.0 , 6.2) \\
1042Linux & g++ (2.91 , 2.95) \\
1043Linux & KCC (3.4) \\
1044Solaris & g++ (2.95) \\
1045SGI IRIX64 & CC \\
1046\end{tabular}
1047\end{center}
1048
1049Some of the modules in the Sophya package uses external libraries. The
1050{\bf FitsIOServer} is the example of such a module, where the {\tt libcfitsio.a}
1051is used.
1052The build procedure expects to find the include files and the libraries in: \\
1053{\tt \$EXTLIBDIR/Include/FitsIO } \\
1054{\tt \$EXTLIBDIR/`uname`-\$EROSCXX/Libs} \\
1055
1056The object files from a given Sophya module are grouped in an archive library
1057with the module's name ({\tt libmodulename.a}). All Sophya modules
1058 are grouped in a single shared library ({\tt libsophya.so}), while the
1059modules with reference to external libraries are grouped in
1060({\tt libextsophya.so}). The {\bf PI} and {\bf PIext} modules are
1061grouped in ({\tt libPI.so}).
1062
1063The environment variables {\bf DPCDEVREP}, {\bf EXTLIBDIR} and {\bf EROSCXX}
1064must be defined in order to install the Sophya package.
1065In the example below, we assume that we want to install Sophya from a
1066released (tagged) version in the source directory {\tt \$SRC} in the
1067{\tt /usr/local/Sophya} diretory, using {\tt g++}. We assume that
1068the external libraries directory tree has been set up in
1069{\tt /usr/local/ExtLibs/}. \\[3mm]
1070\centerline{
1071 \rule{20mm}{0.5mm}
1072 {\bf \large the use of GNU make is mandatory}
1073 \rule{20mm}{0.5mm} }
1074
1075\vspace*{3mm}
1076\begin{verbatim}
1077# We select our C++ compiler
1078csh> setenv EROSCXX g++
1079# Setup the build directory
1080csh> mkdir /usr/local/Sophya/
1081csh> setenv DPCDEVREP /usr/local/Sophya/
1082csh> setenv EXTLIBDIR /usr/local/ExtLibs/
1083# Use the top level makefile in Mgr/
1084csh> cd \$SRC
1085csh> cp Mgr/Makefile Makefile
1086# Step 1: Create the directory tree and copy the include files (.h)
1087csh> make depend
1088# Step 2: Compile the modules without external library reference
1089csh> make libs
1090# Step 3: Compile the modules WITH external library reference (optional)
1091csh> make extlibs
1092# Step 4: Build libsophya.so
1093csh> make slb
1094# Step 5: Build libextsophya.so (optional)
1095csh> make slbext
1096# Step 6: Compile the PI and PIext modules (optional)
1097csh> make PI
1098# Step 7: Build the corresponding shared library libPI.so (optional)
1099csh> make slbpi
1100\end{verbatim}
1101
1102To compile all modules and build the shared libraries, it is possible
1103to use:
1104\begin{verbatim}
1105# Step 2,3,6
1106csh> make all
1107# Step 4,5,7
1108csh> make slball
1109\end{verbatim}
1110
1111At this step, all libraries sould have been made. Programs using
1112Sophya libraries can now be built:
1113\begin{verbatim}
1114# To compile test programs
1115csh> cd Tests
1116csh> make arrt ...
1117csh> cd ..
1118# To compile other programs, for example from the PMixer module
1119csh> cd PMixer
1120csh> make
1121csh> cd ..
1122# To build (s)piapp (libPI.so is needed)
1123csh> cd ProgPI
1124csh> make
1125csh> cd ..
1126\end{verbatim}
1127
1128\subsection{Mgr module}
1129This module contains scripts which can be used for generating the
1130makefiles for each module.
1131\begin{itemize}
1132\item {\bf Makefile} Top level Makefile for builiding the libraries.
1133\item {\bf Makefile.h} contains the defintion of compilation flags for the
1134different compilers and systems. This file is used for building the
1135library and generating {\bf MakefileUser.h} (to be included in makefiles).
1136\item {\bf Makefile.slb} contains the rules for building shared libraries
1137for the different compilers and systems. (to be included in makefiles)
1138\item {\bf crerep\_sophya} c-shell script for creating the directory tree
1139under {\tt \$DPCBASEREP} and {\tt \$DPCDEVREP}
1140\item {\bf install\_sophya} c-shell script for installing the Sophya package.
1141Usually from {\tt \$DPCDEVREP} to {\tt \$DPCBASEREP}
1142\item {\bf mkmflien} c-shell script for making symbolic links or copying
1143include files to {\tt \$DPCDEVREP/Include} or {\tt \$DPCBASEREP/Include}
1144\item {\bf mkmf} c-shell script for generating module makefiles and the
1145top level makefile (named GNUmakefile)
1146\item {\bf mkmflib} c-shell script for generating each library module
1147makefile (named GNUmakefile)
1148\item {\bf mkmfprog} c-shell script for generating makefile for a module
1149containing the source for executable programs (named GNUmakefile).
1150\item {\bf mkmfPI} c-shell script for generating makefile for PI and PIext
1151modules (named GNUmakefile)
1152\item {\bf libdirs} List of Sophya modules without reference to external
1153libraries.
1154\item {\bf extlibdirs} List of Sophya modules with reference to external
1155libraries.
1156
1157\end{itemize}
1158
1159\newpage
1160\appendix
1161\section{SOPHYA Exceptions}
1162\index{Exception classes} \index{PThrowable} \index{PError} \index{PException}
1163SOPHYA library defines a set of exceptions which are used
1164for signaling error conditions. The figure below shows a partial
1165class diagram for exception classes in SOPHYA.
1166\begin{figure}[hbt]
1167\dclsbb{PThrowable}{PError}
1168\dclscc{PError}{AllocationError}
1169\dclscc{PError}{NullPtrError}
1170\dclscc{PError}{ForbiddenError}
1171\dclscc{PError}{AssertionFailedError}
1172\dclsbb{PThrowable}{PException}
1173\dclscc{PException}{IOExc}
1174\dclscc{PException}{SzMismatchError}
1175\dclscc{PException}{RangeCheckError}
1176\dclscc{PException}{ParmError}
1177\dclscc{PException}{TypeMismatchExc}
1178\dclscc{PException}{MathExc}
1179\dclscc{PException}{CaughtSignalExc}
1180\caption{partial class diagram for exception handling in Sophya}
1181\end{figure}
1182
1183For simple programs, it is a good practice to handle
1184the exceptions at least at high level, in the {\tt main()} function.
1185The example below shows the exception handling and the usage
1186of Sophya persistence.
1187
1188\input{ex1.inc}
1189
1190
1191\newpage
1192\addcontentsline{toc}{section}{Index}
1193\printindex
1194\end{document}
1195
Note: See TracBrowser for help on using the repository browser.