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

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

MAJ documentation - Reza 20/2/2001

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