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

Last change on this file since 1940 was 1648, checked in by ansari, 24 years ago

modifications - MAJ de sophya.tex (overview) pour la version 1.2 , Reza 11/9/2001

File size: 55.8 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 Sophya Version: 1.2 (V\_Jul2001) }
39% Document revision 1.0 }
40\end{center}
41\titrebp{1}
42\end{titlepage}
43
44\tableofcontents
45
46\newpage
47
48\section{Introduction}
49
50{\bf SOPHYA} ({\bf SO}ftware for {\bf PHY}sics {\bf A}nalysis)
51is a collection of C++ classes designed for numerical and
52physics analysis software development. Our goal is to provide
53easy to use, yet powerful classes which can be used by scientists.
54We have decided to use as much as possible available
55numerical analysis libraries, encapsulating them whenever
56possible. Although some the modules in SOPHYA have been
57designed with the specific goal of providing some of the tools for
58the Planck-HFI data processing software, most of the
59packages presented here have a more general scope than the
60CMB analysis and Planck mission problem.
61\par
62\vspace*{2mm}
63This documents
64presents only a brief overview of the class library,
65mainly from the user's point of view. A more complete description
66can be found in the reference manual, available from our
67web site: {\bf http://www.sophya.org}.
68\par
69\vspace*{2mm}
70The source directory tree
71\footnote{ CVS: cvsserver.lal.in2p3.fr:/exp/eros/CVSPlanck}
72is organised into a number of modules.
73
74\begin{itemize}
75\item[] {\bf Mgr/} Scripts for code management,
76makefile generation and software installation
77\item[] {\bf SysTools/} General architecture support classes such
78as {\tt PPersist, NDataBlock<T>}, and few utility classes
79({\tt DataCard, DVList} \ldots). Presently, this module contains
80also classes implementing interfaces to OS specific services, such
81as shared object and dynamic link handling. This module will be
82separated in three modules, one for the general architecture
83support, one for the utility classes, and one for the OS specific
84services.
85\item[] {\bf TArray/} template numerical arrays, vectors and matrices \\
86({\tt TArray<T> TMatrix<T> TVector<T> } \ldots)
87\item[] {\bf NTools/} Some standard numerical analysis tools
88(linear, and non linear parameter fitting, FFT, \ldots)
89\item[] {\bf HiStats/} Histogram-ming and data set handling classes (tuples) \\
90({\tt Histo Histo2D NTuple XNTuple} \ldots)
91\item[] {\bf SkyMap/} Local and full sky maps, and few geometry
92handling utility classes. \\
93({\tt PixelMap<T>, LocalMap<T>, SphericalMap<T>, \ldots})
94\end{itemize}
95
96The modules listed below are more tightly related to the
97CMB (Cosmic Microwave Background) data analysis problem:
98\begin{itemize}
99\item[] {\bf SkyT/}
100classes for spectral emission and detector frequency response modelling \\
101({\tt SpectralResponse, RadSpectra, BlackBody} \ldots)
102\item[] {\bf Samba/} Spherical harmonic analysis, noise generators \ldots
103\end{itemize}
104
105The following modules contain the interface classes with
106external libraries:
107\begin{itemize}
108\item[] {\bf FitsIOServer/} Classes for handling file input-output
109in FITS format using the cfitsio library.
110\item[] {\bf LinAlg/} Interface with Lapack linear algebra package
111\item[] {\bf IFFTW/} Interface with FFTW package (libfftw.a)
112\item[] {\bf XAstroPack/} Interface to some common astronomical
113computation libraries. Presently, this module uses an external library
114extracted from the {\bf Xephem } source code. The corresponding source
115code is also available from SOPHYA cvs repository, module {\bf XephemAstroLib}.
116\end{itemize}
117
118Other modules:
119\begin{itemize}
120\item[] {\bf Tests/} Simple test programs
121\item[] {\bf PrgUtil/} Various utility programs (runcxx, scanppf, scanfits, \ldots)
122\item[] {\bf PMixer/} skymixer and related programs
123\item[] {\bf ProgPI/} interactive analysis tool - It should be noted that
124this module uses the SOPHYA class library and is based on {\bf PI}
125which is a C++ library defining a complete GUI program
126architecture. An additional module (PIext) define the interactive
127analysis program framework and the interfaces with the objects
128in SOPHYA. The {\bf PI/} \footnote{the PI package documentation
129is available from {\bf http://www.lal.in2p3.fr/recherche/eros/PeidaDoc/} }
130and {\bf PIext/} modules are not currently part
131of the SOPHYA CVS structure.
132\end{itemize}
133
134Modules containing examples and demo programs:
135\begin{itemize}
136\item[] {\bf Examples/} Sample SOPHYA codes and example programs and
137makefiles (auto\_makefile and ex\_makefile).
138\item[] {\bf DemoPIApp/} Sample exripts and programs for (s)piapp
139interactive analysis tools.
140\end{itemize}
141\newpage
142
143\section{Using Sophya}
144Basic usage of Sophya classes are described in in the following sections.
145Complete Sophya documentation can be found at our web site: \\
146{\bf http://www.sophya.org}.
147
148\subsection{Environment variables}
149Two environment variables {\bf DPCBASEREP} and {\bf EROSCXX} are used
150to define the path where the Sophya libraries and executable are installed.
151{\bf DPCBASEREP} defines the base directory path and {\bf EROSCXX} the
152name of the C++ compiler. The complete path is built using {\bf DPCBASEREP},
153the operating system name (as obtained by the {\tt uname} command), and
154the compiler name. In the example below, we show the complete path
155for a {\tt Linux} system, using the GNU g++ compiler:
156
157\begin{itemize}
158\item \$DPCBASEREP/Include : Include (.h) files
159\item \$DPCBASEREP/Linux-g++/Libs : Path for the archive libraries (.a)
160\item \$DPCBASEREP/Linux-g++/ShLibs : Shared library path (.so)
161\item \$DPCBASEREP/Linux-g++/Exec : Executable file path
162\end{itemize}
163
164In order to use the shared libraries, the {\bf LD\_LIBRARY\_PATH} variable
165should contain the Sophya shared library path
166({\tt \$DPCBASEREP/Linux-g++/ShLibs } when using g++ compiler on Linux)
167
168For modules using external libraries, the {\bf EXTLIBDIR}
169environment variable should contain the path to these libraries
170and corresponding include files.
171C-FitsIO anf FFTW include files should be accessible through: \\
172{\tt \$EXTLIBDIR/Include/FitsIO } \\
173{\tt \$EXTLIBDIR/Include/FFTW } \\
174The corresponding libraries are expected to be found in: \\
175{\tt \$EXTLIBDIR/Linux-g++/Libs} \\
176
177\subsection{User makefiles}
178The file {\tt \$DPCBASEREP/Include/MakefileUser.h} defines the compilation
179flags and the list of Sophya libraries. It should be included in the
180user's makefile. The default compilation rules assumes that the object (.o)
181and executable files would be put in the following directories: \\
182{\tt \$HOME/`uname`-\$EROSCXX/Objs} \\
183{\tt \$HOME/`uname`-\$EROSCXX/Exec}.
184In the case of a {\tt Linux} system and using {\tt g++} as the C++ compiler,
185these two directories would be translated to \\
186{\tt \$HOME/Linux-g++/Objs} and {\tt \$HOME/Linux-g++/Exec}.
187The GNU make program should be used.
188\par
189The file {\tt Examples/auto\_makefile} defines the rules to compile
190a given source program, and link it against the Sophya libraries to produce
191an executable. The example below shows the steps to compile a program named
192{\tt trivial.cc }.
193\begin{verbatim}
194csh> cp Examples/auto_makefile makefile
195csh> cp Examples/ex1.cc trivial.cc
196csh> make trivial
197\end{verbatim}
198This command should compile the {\tt trivial.cc} file,
199and link it against the sophya libraries.
200The file {\tt Examples/ex\_makefile} provides another
201example makefile.
202
203\subsection{the runcxx program}
204\index{runcxx}
205{\bf runcxx} is a simple program which can be used to compile, link
206and run simple C++ programs. It handles the creation of a
207complete program file, containing the basic set C++ include files,
208the necessary include files for SOPHYA SysTools, TArray, HiStats
209and NTools modules, and the main program with exception handling.
210Other Sophya modules can be included using the {\tt -import} flag.
211Use of additional include files can be specified using the
212{\tt -inc} flag.
213\begin{verbatim}
214csh> runcxx -h
215SOPHYA Version 1.1 Revision 0 (V_Fev2001) -- Feb 28 2001 11:19:17 cxx
216 runcxx : compiling and running of a piece of C++ code
217 Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions]
218 [-tmpdir TmpDirectory] [-f C++CodeFileName]
219 [-inc includefile] [-inc includefile ...]
220 [-import modulename] [-import modulename ...]
221 [-uarg UserArg1 UserArg2 ...]
222 if no file name is specified, read from standard input
223 modulenames: SkyMap, Samba, SkyT, FitsIOServer, LinAlg, IFFTW
224\end{verbatim}
225Most examples in this manual can be tested using runcxx. The
226example below shows how to compile, link and run a sample
227code.
228\begin{verbatim}
229// File example.icc
230Matrix a(3,3);
231a = IdentityMatrix(1.);
232cout << a ;
233// Executing this sample code
234csh> runcxx -f example.icc
235\end{verbatim}
236
237\newpage
238
239\section{Copy constructor and assignment operator}
240In C++, objects can be copied by assignment or by initialisation.
241Copying by initialisation corresponds to creating an object and
242initialising its value through the copy constructor.
243The copy constructor has its first argument as a reference, or
244const reference to the object's class type. It can have
245more arguments, if default values are provided.
246Copying by assignment applies to an existing object and
247is performed through the assignment operator (=).
248The copy constructor implements this for identical type objects:
249\begin{verbatim}
250class MyObject {
251public:
252 MyObject(); // Default constructor
253 MyObject(MyObject const & a); // Copy constructor
254 MyObject & operator = (MyObject const & a) // Assignment operator
255}
256\end{verbatim}
257The copy constructors play an important role, as they are
258called when class objects are passed by value,
259returned by value, or thrown as an exception.
260\begin{verbatim}
261// A function declaration with an argument of type MyObject,
262// passed by value, and returning a MyObject
263MyObject f(MyObject x)
264{
265 MyObject r;
266 ...
267 return(r); // Copy constructor is called here
268}
269// Calling the function :
270MyObject a;
271f(a); // Copy constructor called for a
272\end{verbatim}
273It should be noted that the C++ syntax is ambiguous for the
274assignment operator. {\tt MyObject x; x=y; } and
275{\tt MyObject x=y;} have different meaning.
276\begin{verbatim}
277MyObject a; // default constructor call
278MyObject b(a); // copy constructor call
279MyObject bb = a; // identical to bb(a) : copy constructor call
280MyObject c; // default constructor call
281c = a; // assignment operator call
282\end{verbatim}
283
284As a general rule in SOPHYA, objects which implements
285reference sharing on their data members have a copy constructor
286which shares the data, while the assignment operator copies or
287duplicate the data.
288
289\newpage
290\section{Module SysTools}
291
292{\bf SysTools} contains utility classes such as {\tt DataCards} or
293{\tt DVlist}, an hierarchy of exception classes for Sophya, a template
294class {\tcls{NDataBlock}} for handling reference counting on numerical
295arrays, as well as classes providing the services for implementing simple
296serialisation.
297\vspace*{5mm}
298
299\subsection{SOPHYA persistence}
300\index{PPersist} \index{PInPersist} \index{POutPersist}
301\begin{figure}[hbt]
302\dclsa{PPersist}
303\dclsbb{PIOPersist}{PInPersist}
304\dclsb{POutPersist}
305\caption{partial class diagram for classes handling persistence in Sophya}
306\end{figure}
307A simple persistence mechanism is defined in SOPHYA. Its main
308features are:
309\begin{itemize}
310\item[] Portable file format, containing the description of the data structures
311and object hierarchy. \\
312{\bf PPF} {\bf P}ortable {\bf P}ersistence file {\bf F}ormat.
313\index{PPF}
314\item[] Handling of read/write for multiply referenced objects.
315\item[] All write operations are carried using sequential access only. This
316holds also for read operations, unless positional tags are used.
317SOPHYA persistence services can thus be used to transfer objects
318through network links.
319\item[] The serialisation (reading/writing) for objects for a given class
320is implemented through a handler object. The handler class inherits
321from {\tt PPersist} class.
322\item[] A run time registration mechanism is used in conjunction with
323RTTI (Run Time Type Identification) for identifying handler classes
324when reading {\bf PInPersist} streams, or for associating handlers
325with data objects {\bf AnyDataObject} for write operations.
326\end{itemize}
327A complete description of SOPHYA persistence mechanism and guidelines
328for writing delegate classes for handling object persistence is beyond
329the scope of this document. The most useful methods for using Sophya
330persistence are listed below:
331\begin{itemize}
332\item[] {\tt POutPersist::PutObject(AnyDataObj \& o)} \\
333Writes the data object {\bf o} to the output stream.
334\item[] {\tt POutPersist::PutObject(AnyDataObj \& o, string tagname)} \\
335Writes the data object {\bf o} to the output stream, associated with an
336identification tag {\bf tagname}.
337\item[] {\tt PInPersist::GetObject(AnyDataObj \& o)} \\
338Reads the next object in stream into {\bf o}. An exception is
339generated for incompatible object types.
340\item[] {\tt PInPersist::GetObject(AnyDataObj \& o, string tagname)} \\
341Reads the object associated with the tag {\bf tagname} into {\bf o}.
342An exception is generated for incompatible object types.
343\end{itemize}
344The operators {\tt operator << (POutPersist ...) } and
345{\tt operator >> (PInPersist ...) } are often overloaded
346to perform {\tt PutObject()} and {\tt GetObject()} operations,
347as illustrated in the example below:
348\begin{verbatim}
349// Creating and filling a histogram
350Histo hw(0.,10.,100);
351...
352// Writing histogram to a PPF stream
353POutPersist os("hw.ppf");
354os << hw;
355// Reading a histogram from a PPF stream
356PInPersist is("hr.ppf");
357is >> hr;
358\end{verbatim}
359
360The {\bf scanppf} program can be used to list the content of a PPF file.
361\index{scanppf}
362\begin{verbatim}
363csh> scanppf -h
364SOPHYA Version 1.1 Revision 0 (V_Fev2001) -- Feb 28 2001 11:19:17 cxx
365 Usage: scanppf filename [s/n/a0/a1/a2/a3]
366 s[=default} : Sequential reading of objects
367 n : Object reading at NameTags
368 a0...a3 : Tag List with PInPersist.AnalyseTags(0...3)
369\end{verbatim}
370
371
372\subsection{\tcls{NDataBlock}}
373\index{\tcls{NDataBlock}}
374\begin{figure}[hbt]
375\dclsbb{AnyDataObj}{\tcls{NDataBlock}}
376\dclsbb{PPersist}{\tcls{FIO\_NDataBlock}}
377\end{figure}
378The {\bf \tcls{NDataBlock}} is designed to handle reference counting
379and sharing of memory blocs (contiguous arrays) for numerical data
380types. Initialisation, resizing, basic arithmetic operations, as
381well as persistence handling services are provided.
382The persistence handler class ({\tt \tcls{FIO\_NDataBlock}}) insures
383that a single copy of data is written for multiply referenced objects,
384and the data is shared among objects when reading.
385\par
386The example below shows writing of NDataBlock objects through the
387use of overloaded operator $ << $ :
388\begin{verbatim}
389#include "fiondblock.h"
390// ...
391POutPersist pos("aa.ppf");
392NDataBlock<r_4> rdb(40);
393rdb = 567.89;
394pos << rdb;
395// We can also use the PutObject method
396NDataBlock<int_4> idb(20);
397idb = 123;
398pos.PutObject(idb);
399\end{verbatim}
400The following sample programs show the reading of the created PPF file :
401\begin{verbatim}
402PInPersist pis("aa.ppf");
403NDataBlock<r_4> rdb;
404pis >> rdb;
405cout << rdb;
406NDataBlock<int_4> idb;
407cout << idb;
408\end{verbatim}
409
410\subsection{Using DVList}
411\index{DVList} \index{MuTyV}
412\begin{figure}[hbt]
413\dclsbb{AnyDataObj}{DVList}
414\dclsbb{PPersist}{\tclsc{ObjFileIO}{DVList}}
415\end{figure}
416The {\bf DVList} class objects can be used to create and manage list
417of values, associated with names. A list of pairs of (MuTyV, name(string))
418is maintained by DVList objects. {\bf MuTyV} is a simple class
419capable of holding string, integer, float or complex values,
420providing easy conversion methods between these objects.
421\begin{verbatim}
422// Using MuTyV objects
423MuTyV s("hello"); // string type value
424MuTyV x;
425x = "3.14159626"; // string type value, ASCII representation for Pi
426double d = x; // x converted to double = 3.141596
427x = 314; // x contains the integer value = 314
428// Using DVList
429DVList dvl;
430dvl("Pi") = 3.14159626; // float value, named Pi
431dvl("Log2") = 0.30102999; // float value, named Log2
432dvl("FileName") = "myfile.fits"; // string value, named myfile.fits
433// Printing DVList object
434cout << dvl;
435\end{verbatim}
436
437\subsection{Using DataCards}
438\index{DataCards}
439The {\bf DataCards} class can be used to read parameters from a file.
440Each line in the file starting with \@ defines a set of values
441associated with a keyword. In the example below, we read the
442parameters corresponding with the keyword {\tt SIZE} from the
443file {\tt ex.d}. We suppose that {\tt ex.d} contains the line: \\
444{\tt @SIZE 400 250} \\
445\begin{verbatim}
446#include "datacards.h"
447// ...
448// Initialising DataCards object dc from file ex.d
449DataCards dc( "ex.d" );
450// Getting the first and second parameters for keyword size
451// We define a default value 100
452int size_x = dc.IParam("SIZE", 0, 100);
453int size_y = dc.IParam("SIZE", 1, 100);
454cout << " size_x= " << size_x << " size_y= " << size_y << endl;
455\end{verbatim}
456
457\subsection{Dynamic linker}
458\index{PDynLinkMgr}
459The class {\bf PDynLinkMgr} can be used for managing shared libraries
460at run time. The example below shows the run time linking of a function:\\
461{\tt extern "C" { void myfunc(); } } \\
462\begin{verbatim}
463#include "pdlmgr.h"
464// ...
465string soname = "mylib.so";
466string funcname = "myfunc";
467PDynLinkMgr dyl(soname);
468DlFunction f = dyl.GetFunction(funcname);
469if (f != NULL) {
470// Calling the function
471 f();
472}
473\end{verbatim}
474
475\subsection{CxxCompilerLinker class}
476\index{CxxCompilerLinker}
477This class provides the services to compile C++ code and building
478shared libraries, using the same compiler and options which have
479been used to create the SOPHYA shared library.
480The sample program below illustrates using this class to build
481the shared library (myfunc.so) from the source file myfunc.cc :
482\begin{verbatim}
483#include "cxxcmplnk.h"
484// ...
485string flnm = "myfunc.cc";
486string oname, soname;
487int rc;
488CxxCompilerLinker cxx;
489// The Compile method provides a default object file name
490rc = cxx.Compile(flnm, oname);
491if (rc != 0 ) { // Error when compiling ... }
492// The BuildSO method provides a default shared object file name
493rc = cxx.BuildSO(oname, soname);
494if (rc != 0 ) { // Error when creating shared object ... }
495\end{verbatim}
496
497\newpage
498\section{Module TArray}
499\index{\tcls{TArray}}
500{\bf TArray} module contains template classes for handling standard
501operations on numerical arrays. Using the class {\tt \tcls{TArray} },
502it is possible to create and manipulate up to 5-dimension numerical
503arrays {\tt (int, float, double, complex, \ldots)}. The include
504file {\tt array.h} declares all the classes and definitions
505in module TArray. {\bf Array} is a typedef for arrays
506with double precision floating value elements. \\
507{\tt typedef TArray$<$r\_8$>$ Array ; }
508
509\begin{figure}[hbt]
510\dclsccc{AnyDataObj}{BaseArray}{\tcls{TArray}}
511\dclsbb{PPersist}{\tcls{FIO\_TArray}}
512\end{figure}
513
514
515\subsection{Using arrays}
516\index{Sequence} \index{RandomSequence} \index{RegularSequence}
517\index{EnumeratedSequence}
518The example below shows basic usage of arrays, creation, initialisation
519and arithmetic operations. Different kind of {\bf Sequence} objects
520can be used for initialising arrays.
521
522\begin{figure}[hbt]
523\dclsbb{Sequence}{RandomSequence}
524\dclsb{RegularSequence}
525\dclsb{EnumeratedSequence}
526\end{figure}
527
528The example below shows basic usage of arrays:
529\index{\tcls{TArray}}
530\begin{verbatim}
531// Creating and initialising a 1-D array of integers
532TArray<int> ia(5);
533EnumeratedSequence es;
534es = 24, 35, 46, 57, 68;
535ia = es;
536cout << "Array<int> ia = " << ia;
537// 2-D array of floats
538TArray<r_4> b(6,4), c(6,4);
539// Initializing b with a constant
540b = 2.71828;
541// Filling c with random numbers
542c = RandomSequence();
543// Arithmetic operations
544TArray<r_4> d = b+0.3f*c;
545cout << "Array<float> d = " << d;
546\end{verbatim}
547
548The copy constructor shares the array data, while the assignment operator
549copies the array elements, as illustrated in the following example:
550\begin{verbatim}
551TArray<int> a1(4,3);
552a1 = RegularSequence(0,2);
553// Array a2 and a1 shares their data
554TArray<int> a2(a1);
555// a3 and a1 have the same size and identical elements
556TArray<int> a3;
557a3 = a1;
558// Changing one of the a2 elements
559a2(1,1,0) = 555;
560// a1(1,1) is also changed to 555, but not a3(1,1)
561cout << "Array<int> a1 = " << a1;
562cout << "Array<int> a3 = " << a3;
563\end{verbatim}
564
565\subsection{Matrices and vectors}
566\index{\tcls{TMatrix}} \index{\tcls{TVector}}
567\begin{figure}[hbt]
568\dclsccc{\tcls{TArray}}{\tcls{TMatrix}}{\tcls{TVector}}
569\end{figure}
570Vectors and matrices are 2 dimensional arrays. The array size
571along one dimension is equal 1 for vectors. Column vectors
572have {\tt NCols() = 1} and row vectors have {\tt NRows() = 1}.
573Mathematical expressions involving matrices and vectors can easily
574be translated into C++ code using {\tt TMatrix} and
575{\tt TVector} objects. {\bf Matrix} and {\bf Vector} are
576typedefs for double precision float matrices and vectors.
577The operator {\bf *} beteween matrices is redefined to
578perform matrix multiplication. One can then write: \\
579\begin{verbatim}
580 // We create a row vector
581 Vector v(1000, BaseArray::RowVector);
582 // Initialize values with a random sequence
583 v = RandomSequence();
584 // Compute the vector length (norm)
585 double norm = (v*v.Transpose()).toScalar();
586 cout << "Norm(v) = " << norm << endl;
587\end{verbatim}
588
589This module contains basic array and matrix operations
590such as the Gauss matrix inversion algorithm
591which can be used to solve linear systems, as illustrated by the
592example below:
593\begin{verbatim}
594#include "sopemtx.h"
595// ...
596// Creation of a random 5x5 matrix
597Matrix A(5,5);
598A = RandomSequence(RandomSequence::Flat);
599Vector X0(5);
600X0 = RandomSequence(RandomSequence::Gaussian);
601// Computing B = A*X0
602Vector B = A*X0;
603// Solving the system A*X = B
604Vector X;
605LinSolve(A, B, X);
606// Checking the result
607Vector diff = X-X0;
608cout << "X-X0= " << diff ;
609double min,max;
610diff.MinMax(min, max);
611cout << " Min(X-X0) = " << min << " Max(X-X0) = " << max << endl;
612\end{verbatim}
613
614\subsection{Working with sub-arrays and Ranges}
615\index{Range}
616A powerful mechanism is included in array classes for working with
617sub-arrays. The class {\bf Range} can be used to specify range of array
618indexes in any of the array dimensions. Any regularly spaced index
619range can be specified, using the {\tt start} and {\tt end} index
620and an optional step (or stride). It is also possible to specify
621the {\tt start} index and the number of elements:
622\begin{center}
623
624\begin{tabular}{ll}
625\multicolumn{2}{c}{ {\bf Range} {\tt (start=0, end=0, size=1, step=1) } } \\[2mm]
626\hline \\
627{\bf Range} {\tt r(3,6); } & index range 3,4,5,6 \\
628{\bf Range} {\tt r(7,0,3); } & index range 7,8,9 \\
629{\bf Range} {\tt r(10,0,3,5); } & index range 10,12,14,16,18 \\
630\end{tabular}
631\end{center}
632
633In the following example, a simple low-pass filter, on a one
634dimensional stream (Vector) has been written using sub-arrays:
635
636\begin{verbatim}
637// Input Vector containing a noisy periodic signal
638 Vector in(1024), out(1024);
639 in = RandomSequence(RandomSequence::Gaussian, 0., 1.);
640 for(int kk=0; kk<in.Size(); kk++)
641 in(kk) += 2*sin(kk*0.05);
642// Compute the output vector by a simple low pass filter
643 Vector out(1024);
644 int w = 2;
645 for(int k=w; k<in.Size()-w; k++)
646 out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.);
647\end{verbatim}
648
649\subsection{Input, Output}
650Arrays can easily be saved to, or restored from files in different formats.
651SOPHYA library can handle array I/O to ASCII formatted files, to PPF streams,
652as well as to files in FITS format.
653FITS format input/output is provided through the classes in
654{\bf FitsIOServer} module. Onnly arrays with data types
655supported by the FITS standard can be handled during
656I/O operations to and from FITS streams (See the FitsIOServer section
657for additional details).
658
659\subsubsection{PPF streams}
660
661SOPHYA persistence (PPF streams) handles reference sharing, and multiply
662referenced objects are only written once. A hierarchy of arrays and sub-arrays
663written to a PPF stream is thus completely recovered, when the stream is read.
664The following example illustrates this point:
665\begin{verbatim}
666{
667// Saving an array with a sub-array into a POutPersist file
668Matrix A(3,4);
669A = RegularSequence(10,5);
670// Create a sub-array of A
671Matrix AS = A(Range(1,2), Range(2,3));
672// Save the two arrays to a PPF stream
673POutPersist pos("aas.ppf");
674pos << A << AS;
675}
676{
677// Reading arrays from the previously created PPF file aas.ppf
678PInPersist pis("aas.ppf");
679Matrix B,BS;
680pis >> B >> BS;
681// BS is a sub-array of B, modifying BS changes also B
682BS(1,1) = 98765.;
683cout << " B , BS after BS(1,1) = 98765. "
684 << B << BS << endl;
685}
686\end{verbatim}
687The execution of this sample code creates the file {\tt aas.ppf} and
688its output is reproduced here. Notice that the array hierarch is
689recovered. BS is a sub-array of B, and modifying BS changes also
690the corresponding element in B.
691\begin{verbatim}
692 B , BS after BS(1,1) = 98765.
693
694--- TMatrix<double>(NRows=3, NCols=4) ND=2 SizeX*Y*...= 4x3 ---
69510 15 20 25
69630 35 40 45
69750 55 60 98765
698
699--- TMatrix<double>(NRows=2, NCols=2) ND=2 SizeX*Y*...= 2x2 ---
70040 45
70160 98765
702\end{verbatim}
703
704\centerline{\bf Warning: }
705
706There is a drawback in this behaviour: only a single
707copy of an array is written to a file, even if the array is modified,
708without being resized and written to a PPF stream.
709\begin{verbatim}
710{
711POutPersist pos("mca.ppf");
712TArray<int_4> ia(5,3);
713ia = 8;
714pos << ia;
715ia = 16;
716pos << ia;
717ia = 32;
718pos << ia;
719}
720\end{verbatim}
721
722Only a single copy of the data is effectively written to the output
723PPF file, corresponding to the value 8 for array elements. When we
724read the three array from the file mca.ppf, the same array elements
725are obtained three times (all elements equal to 8):
726\begin{verbatim}
727{
728PInPersist pis("mca.ppf");
729TArray<int_4> ib;
730pis >> ib;
731cout << " First array read from mca.ppf : " << ib;
732pis >> ib;
733cout << " Second array read from mca.ppf : " << ib;
734pis >> ib;
735cout << " Third array read from mca.ppf : " << ib;
736}
737\end{verbatim}
738
739\subsubsection{ASCII streams}
740
741The {\bf WriteASCII} method can be used to dump an array to an ASCII
742formatted file, while the {\bf ReadASCII} method can be used to decode
743ASCII formatted files. Space or tabs are the possible separators.
744Complex numbers should be specified as a pair of comma separated
745real and imaginary parts, enclosed in parenthesis.
746
747\begin{verbatim}
748{
749// Creating array A and writing it to an ASCII file (aaa.txt)
750Array A(4,6);
751A = RegularSequence(0.5, 0.2);
752ofstream ofs("aaa.txt");
753A.WriteASCII(ofs);
754}
755{
756// Decoding the ASCII file aaa.txt
757ifstream ifs("aaa.txt");
758Array B;
759sa_size_t nr, nc;
760B.ReadASCII(ifs,nr,nc);
761cout << " Array B; B.ReadASCII() from file " << endl;
762cout << B ;
763}
764\end{verbatim}
765
766
767\subsection{Complex arrays}
768The {\bf TArray} module provides few functions for manipulating
769arrays of complex numbers (single and double precision).
770These functions are declared in {\tt matharr.h}.
771\begin{itemize}
772\item[\bul] Creating a complex array through the specification of the
773real and imaginary parts.
774\item[\bul] Functions returning arrays corresponding to real and imaginary
775parts of a complex array: {\tt real(za) , imag(za) }
776({\bf Warning:} Note that the present implementation does not provide
777shared memory access to real and imaginary parts.)
778\item[\bul] Functions returning arrays corresponding to the module,
779phase, and module squared of a complex array:
780 {\tt phase(za) , module(za) , module2(za) }
781\end{itemize}
782
783\begin{verbatim}
784 TVector<r_4> p_real(10, BaseArray::RowVector);
785 TVector<r_4> p_imag(10, BaseArray::RowVector);
786 p_real = RegularSequence(0., 0.5);
787 p_imag = RegularSequence(0., 0.25);
788 TVector< complex<r_4> > zvec = ComplexArray(p_real, p_imag);
789 cout << " :: zvec= " << zvec;
790 cout << " :: real(zvec) = " << real(zvec) ;
791 cout << " :::: imag(zvec) = " << imag(zvec) ;
792 cout << " :::: module2(zvec) = " << module2(zvec) ;
793 cout << " :::: module(zvec) = " << module(zvec) ;
794 cout << " :::: phase(zvec) = " << phase(zvec) ;
795\end{verbatim}
796
797The decoding of complex numbers from an ASCII formatted stream
798is illustrated by the next example. As mentionned already,
799complex numbers should be specified as a pair of comma separated
800real and imaginary parts, enclosed in parenthesis.
801
802\begin{verbatim}
803csh> cat zzz.txt
804(1.,-1) (2., 2.5) -3. 12.
805-24. (-6.,7.) 14.2 (8.,64.)
806
807// Decoding of complex numbers from an ASCII file
808// Notice that the << operator can be used instead of ReadASCII
809TArray< complex<r_4> > Z;
810ifstream ifs("zzz.txt");
811ifs >> Z;
812cout << " TArray< complex<r_4> > Z from file zzz.txt " << Z ;
813\end{verbatim}
814
815
816\subsection{Memory organisation}
817{\tt \tcls{TArray} } can handle numerical arrays with various memory
818organisation, as long as the spacing (steps) along each axis is
819regular. The five axis are labeled X,Y,Z,T,U. The examples below
820illustrates the memory location for a 2-dimensional, $N_x=4 \times N_y=3$.
821The first index is along the X axis and the second index along the Y axis.
822\begin{verbatim}
823 | (0,0) (0,1) (0,2) (0,3) |
824 | (1,0) (1,1) (1,2) (1,3) |
825 | (2,0) (2,1) (2,2) (2,3) |
826\end{verbatim}
827In the first case, the array is completely packed
828($Step_X=1, Step_Y=N_X=4$), with zero offset,
829while in the second case, $Step_X=2, Step_Y=10, Offset=10$:
830\begin{verbatim}
831 | 0 1 2 3 | | 10 12 14 16 |
832Ex1 | 4 5 6 7 | Ex2 | 20 22 24 26 |
833 | 8 9 10 11 | | 30 32 34 36 |
834\end{verbatim}
835
836For matrices and vectors, an optional argument ({\tt MemoryMapping})
837can be used to select the memory mapping, where two basic schemes
838are available: \\
839{\tt CMemoryMapping} and {\tt FortranMemoryMapping}. \\
840In the case where {\tt CMemoryMapping} is used, a given matrix line
841is packed in memory, while the columns are packed when
842{\tt FortranMemoryMapping} is used. The first index when addressing
843the matrix elements (line number index) runs along
844the Y-axis if {\tt CMemoryMapping} is used, and along the X-axis
845in the case of {\tt FortranMemoryMapping}.
846Arithmetic operations between matrices
847with different memory organisation is allowed as long as
848the two matrices have the same sizes (Number of rows and columns).
849The following code example and the corresponding output illustrates
850these two memory mappings. The {\tt \tcls{TMatrix}::TransposeSelf() }
851method changes effectively the matrix memory mapping, which is also
852the case of {\tt \tcls{TMatrix}::Transpose() } method without argument.
853
854\begin{verbatim}
855TArray<r_4> X(4,2);
856X = RegularSequence(1,1);
857cout << "Array X= " << X << endl;
858TMatrix<r_4> X_C(X, true, BaseArray::CMemoryMapping);
859cout << "Matrix X_C (CMemoryMapping) = " << X_C << endl;
860TMatrix<r_4> X_F(X, true, BaseArray::FortranMemoryMapping);
861cout << "Matrix X_F (FortranMemoryMapping) = " << X_F << endl;
862\end{verbatim}
863This code would produce the following output (X\_F = Transpose(X\_C)) :
864\begin{verbatim}
865Array X=
866--- TArray<f> ND=2 SizeX*Y*...= 4x2 ---
8671, 2, 3, 4
8685, 6, 7, 8
869
870Matrix X_C (CMemoryMapping) =
871--- TMatrix<f>(NRows=2, NCols=4) ND=2 SizeX*Y*...= 4x2 ---
8721, 2, 3, 4
8735, 6, 7, 8
874
875Matrix X_F (FortranMemoryMapping) =
876--- TMatrix<f>(NRows=4, NCols=2) ND=2 SizeX*Y*...= 4x2 ---
8771, 5
8782, 6
8793, 7
8804, 8
881\end{verbatim}
882
883\newpage
884
885\section{Module HiStats}
886\begin{figure}[hbt]
887\dclsccc{AnyDataObj}{Histo}{HProf}
888\dclsbb{AnyDataObj}{Histo2D}
889\dclsbb{AnyDataObj}{Ntuple}
890\dclsb{XNtuple}
891\caption{partial class diagram for histograms and ntuples}
892\end{figure}
893
894{\bf HiStats} contains classes for creating, filling, printing and
895doing various operations on one or two dimensional histograms
896{\tt Histo} and {\tt Histo2D} as well as profile histograms {\tt HProf}. \\
897This module also contains {\tt NTuple} and {\tt XNTuple} which are
898more or less the same that the binary FITS tables.
899
900\subsection{1D Histograms}
901\index{Histo}
902For 1D histograms, various numerical methods are provided such as
903computing means and sigmas, finding maxima, fitting, rebinning,
904integrating \dots \\
905The example below shows creating and filling a one dimensional histogram
906of 100 bins from $-5.$ to $+5.$ to create a Gaussian normal distribution
907with errors~:
908\begin{verbatim}
909#include "histos.h"
910// ...
911Histo H(-0.5,0.5,100);
912H.Errors();
913for(int i=0;i<25000;i++) {
914 double x = NorRand();
915 H.Add(x);
916}
917H.Print(80);
918\end{verbatim}
919
920\subsection{2D Histograms}
921\index{Histo2D}
922Much of these operations are also valid for 2D histograms. 1D projection
923or slices can be set~:
924\begin{verbatim}
925#include "histos2.h"
926// ...
927Histo2D H2(-1.,1.,100,0.,60.,50);
928H2.SetProjX(); // create the 1D histo for X projection
929H2.SetBandX(25.,35.); // create 1D histo projection for 25.<y<35.
930H2.SetBandX(35.,45.); // create 1D histo projection for 35.<y<45.
931H2.SetBandX(40.,55.); // create 1D histo projection for 40.<y<55.
932//... fill H2 with what ever you want
933H2.Print();
934Histo *hx = H2.HProjX();
935 hx->Print(80);
936Histo *hbx2 = HBandX(1); // Get the second X band (35.<y<45.)
937 hbx2->Print(80);
938\end{verbatim}
939
940\subsection{Profile Histograms}
941\index{HProf}
942Profiles histograms {\bf HProf} contains the mean and the
943sigma of the distribution
944of the values filled in each bin. The sigma can be changed to
945the error on the mean. When filled, the profile histogram looks
946like a 1D histogram and much of the operations that can be done on 1D histo
947may be applied onto profile histograms.
948
949\subsection{Data tables (tuples)}
950\index{NTuple}
951NTuple are memory resident tables of 32 bits floating values (float).
952They are arranged in columns. Each line is often called an event.
953These objects are frequently used to analyze data.
954Graphicals tools (spiapp) can plot a column against an other one
955with respect to various selection cuts. \\
956Here is an example of creation and filling~:
957\begin{verbatim}
958#include "ntuple.h"
959#include "srandgen.h"
960// ...
961char* nament[4] = {"i","x","y","ey"};
962r_4 xnt[4];
963NTuple NT(4,nament);
964for(i=0;i<5000;i++) {
965 xnt[0] = i+1;
966 xnt[1] = 5.*drandpm1(); // a random value between -5 and +5
967 xnt[2] = 100.*exp(-0.5*xnt[1]*xnt[1]) + 1.;
968 xnt[3] = sqrt(xnt[2]);
969 xnt[2] += xnt[3] * NorRand(); // add a random gaussian error
970 NT.Fill(xnt);
971}
972\end{verbatim}
973
974XNTuple are sophisticated NTuple : they accept various types
975of column values (double,float,int,string,...) and can handle
976very large data sets, through swap space on disk.
977\index{XNTuple}
978In the sample code below we show how to create a XNTuple
979object with four columns (double, double, int, string).
980Several entries (lines) are then appended to the table,
981which is saved to a PPF file.
982\begin{verbatim}
983#include "xntuple.h"
984// ...
985char * names[4] = {"X", "X2", "XInt","XStr"};
986// XNTuple (Table) creation with 4 columns, of integer,
987// double(2) and string type
988XNTuple xnt(2,0,1,1, names);
989// Filling the NTuple
990r_8 xd[2];
991int_4 xi[2];
992char xss[2][32];
993char * xs[2] = {xss[0], xss[1]} ;
994for(int i=0; i<50; i++) {
995 xi[0] = i; xd[0] = i+0.5; xd[1] = xd[0]*xd[0];
996 sprintf(xs[0],"X=%g", xd[0]);
997 xnt.Fill(xd, NULL, xi, xs);
998}
999// Printing table info
1000cout << xnt ;
1001// Saving object into a PPF file
1002POutPersist po("xnt.ppf");
1003po << xnt ;
1004\end{verbatim}
1005
1006\subsection{Writing, viewing \dots }
1007
1008All these objects have been design to be written to or read from a persistent file.
1009The following example shows how to write the previously created objects
1010into such a file~:
1011\begin{verbatim}
1012//-- Writing
1013{
1014char *fileout = "myfile.ppf";
1015string tag;
1016POutPersist outppf(fileout);
1017tag = "H"; outppf.PutObject(H,tag);
1018tag = "H2"; outppf.PutObject(H2,tag);
1019tag = "NT"; outppf.PutObject(NT,tag);
1020} // closing ``}'' destroy ``outppf'' and automatically close the file !
1021\end{verbatim}
1022
1023Sophya graphical tools (spiapp) can automatically display and operate
1024all these objects.
1025
1026\newpage
1027\section{Module NTools}
1028
1029This module provides elementary numerical tools for numerical integration,
1030fitting, sorting and ODE solving. FFTs are also provided (Mayer,FFTPack).
1031
1032\subsection{Fitting}
1033\index{Fitting} \index{Minimisation}
1034Fitting is done with two classes {\tt GeneralFit} and {\tt GeneralFitData}
1035and is based on the Levenberg-Marquardt method.
1036\index{GeneralFit} \index{GeneralFitData}
1037GeneralFitData is a class which provide a description of the data
1038to be fitted. GeneralFit is the fitter class. Parametrized functions
1039can be given as classes which inherit {\tt GeneralFunction}
1040or as simple C functions. Classes of pre-defined functions are provided
1041(see files fct1dfit.h and fct2dfit.h). The user interface is very close
1042from that of the CERN {\tt Minuit} fitter.
1043Number of objects (Histo, HProf \dots ) are interfaced with GeneralFit
1044and can be easily fitted. \\
1045Here is a very simple example for fitting the previously created NTuple
1046with a Gaussian~:
1047\begin{verbatim}
1048#include "fct1dfit.h"
1049// ...
1050
1051// Read from ppf file
1052NTuple nt;
1053{
1054PInPersist pis("myfile.ppf");
1055string tag = "NT"; pis.GetObject(nt,tag);
1056}
1057
1058// Fill GeneralData
1059GeneralData mGdata(nt.NEntry());
1060for(int i=0; i<nt.NEntry(); i++)
1061 mGdata.AddData1(xnt[1],xnt[2],xnt[3]); // Fill x, y and error on y
1062mGData.PrintStatus();
1063
1064// Function for fitting : y = f(x) + noise
1065Gauss1DPol mFunction; // gaussian + constant
1066
1067// Prepare for fit
1068GeneralFit mFit(&mFunction); // create a fitter for the choosen function
1069mFit.SetData(&mGData); // connect data to the fitter
1070
1071// Set and initialise the parameters (that's non-linear fitting!)
1072// (num par, name, guess start, step, [limits min and max])
1073mFit.SetParam(0,"high",90.,1..);
1074mFit.SetParam(1,"xcenter",0.05,0.01);
1075mFit.SetParam(2,"sigma",sig,0.05,0.01,10.);
1076 // Give limits to avoid division by zero
1077mFit.SetParam(3,"constant",0.,1.);
1078
1079// Fit and print result
1080int rcfit = mFit.Fit();
1081mFit.PrintFit();
1082if(rcfit>0) {)
1083 cout<<"Reduce_Chisquare = "<<mFit.GetChi2Red()
1084 <<" nstep="<<mFit.GetNStep()<<" rc="<<rcfit<<endl;
1085} else {
1086 cout<<"Fit_Error, rc = "<<rcfit<<" nstep="<<mFit.GetNStep()<<endl;
1087 mFit.PrintFitErr(rcfit);
1088}
1089
1090// Get the result for further use
1091TVector<r_8> ParResult = mFit.GetParm();
1092cout<<ParResult;
1093\end{verbatim}
1094
1095Much more usefull possibilities and detailed informations might be found
1096in the HTML pages of the Sophya manual.
1097
1098\subsection{Polynomial}
1099\index{Polynomial} \index{Poly} \index{Poly2}
1100Polynomials of 1 or 2 variables are supported ({\tt Poly} and {\tt Poly2}).
1101Various operations are supported~:
1102\begin{itemize}
1103\item elementary operations between polynomials $(+,-,*,/) $
1104\item setting or getting coefficients
1105\item computing the value of the polynomial for a given value
1106 of the variable(s),
1107\item derivating
1108\item computing roots (degre 1 or 2)
1109\item fitting the polynomial to vectors of data.
1110\end{itemize}
1111Here is an example of polynomial fitting~:
1112\begin{verbatim}
1113#include "poly.h"
1114// ...
1115Poly pol(2);
1116pol[0] = 100.; pol[1] = 0.; pol[2] = 0.01; // Setting coefficients
1117TVector<r_8> x(100);
1118TVector<r_8> y(100);
1119TVector<r_8> ey(100);
1120for(int i=0;i<100;i++) {
1121 x(i) = i;
1122 ey(i) = 10.;
1123 y(i) = pol((double) i) + ey(i)*NorRand();
1124 ey(i) *= ey(i)
1125}
1126
1127TVector<r_8> errcoef;
1128Poly polfit;
1129polfit.Fit(x,y,ey,2,errcoef);
1130
1131cout<<"Fit Result"<<polfit<<endl;
1132cout<<"Errors :"<<errcoef;
1133\end{verbatim}
1134
1135Similar operations can be done on polynomials with 2 variables.
1136
1137\subsection{Integration, Differential equations}
1138\index{Integration}
1139The NTools module provide also simple classes for numerical integration
1140of functions and differential equations.
1141\begin{figure}[hbt]
1142\dclsbb{Integrator}{GLInteg}
1143\dclsb{TrpzInteg}
1144\end{figure}
1145
1146\index{GLInteg} \index{TrpzInteg}
1147{\bf GLInteg} implements the integration through Gauss-Legendre method
1148and {\bf TrpzInteg} implements trapeze integration. For {\bf TrpzInteg},
1149number of steps specify the number of trapeze, and integration step,
1150their width.
1151The sample code below illustrates the use of TrpzInteg class:
1152\begin{verbatim}
1153#include "integ.h"
1154// ......................................................
1155// Function to be integrated
1156double myf(double x)
1157{
1158// Simple a x + b x^2 (a=2 b=3)
1159return (x*(2.+3.*x));
1160}
1161// ......................................................
1162
1163// Compute Integral(myf, 2., 5.) between xmin=2., xmax=5.
1164TrpzInteg trpz(myf, 2., 5.);
1165// We specify an integration step
1166trpz.DX(0.01);
1167// The integral can be computed as trpz.Value()
1168double myf_integral = trpz.Value();
1169// We could have used the cast operator :
1170cout << "Integral[myf, 2., 5.]= " << (double)trpz << endl;
1171// Limits can be specified through ValueBetween() method
1172cout << "Integral[myf, 0., 4.]= " << trpz.ValueBetween(0.,4.) << endl;
1173\end{verbatim}
1174
1175\subsection{Fourier transform (FFT)}
1176\index{FFT} \index{FFTPackServer}
1177An abstract interface for performing FFT operations is defined by the
1178{\bf FFTServerInterface} class. The {\bf FFTPackSever} class implements
1179one dimensional FFT, on real and complex data. FFTPackServer uses an
1180adapted and extended version of FFTPack (available from netlib),
1181translated in C, and can operate on single and double precision
1182({\tt float, double}) data.
1183
1184The sample code below illustrates the use of FFTServers:
1185\begin{verbatim}
1186#include "fftpserver.h"
1187 // ...
1188TVector<r_8> in(32);
1189TVector< complex<r_8> > out;
1190in = RandomSequence();
1191FFTPackServer ffts;
1192ffts.setNormalize(true); // To have normalized transforms
1193cout << " FFTServer info string= " << ffts.getInfo() << endl;
1194cout << "in= " << in << endl;
1195cout << " Calling ffts.FFTForward(in, out) : " << endl;
1196ffts.FFTForward(in, out);
1197cout << "out= " << out << endl;
1198\end{verbatim}
1199
1200\newpage
1201\section{Module SkyMap}
1202\begin{figure}[hbt]
1203\dclsbb{AnyDataObj}{PixelMap}
1204\dclsccc{PixelMap}{Sphericalmap}{SphereHEALPix}
1205\dclsc{SphereThetaPhi}
1206\dclsb{LocalMap}
1207\caption{partial class diagram for pixelization classes in Sophya}
1208\end{figure}
1209The {\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.
1210\subsection {Spherical maps}
1211There are two kinds of spherical maps according pixelization algorithms. SphereHEALPix represents spheres pixelized following the HEALPIix algorithm (E. Hivon, K. Gorski)
1212\footnote{see the HEALPix Homepage: http://www.eso.org/kgorski/healpix/ }
1213, 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) :
1214\index{\tcls{SphereHEALPix}}
1215\index{\tcls{SphereThetaPhi}}
1216
1217\begin{verbatim}
1218#include "spherehealpix.h"
1219// ...
1220SphereHEALPix<double> sph(8);
1221for (int k=0; k< sph.NbPixels(); k++) sph(k) = (double)(10*k);
1222\end{verbatim}
1223
1224SphereThetaPhi is used in a similar way with an argument representing number of slices in theta (Euler angle) for an hemisphere.
1225\index{\tcls{SphereThetaPhi}}
1226
1227\subsection {Local maps}
1228\index{\tcls{LocalMap}}
1229A 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).
1230
1231Internally, 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(...))
1232
1233The 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).
1234\begin{verbatim}
1235#include "localmap.h"
1236//..............
1237 LocalMap<r_4> locmap(4,5);
1238 for (int k=0; k<locmap.NbPixels();k++) locmap(k)=10.*k;
1239 locmap.SetOrigin();
1240 locmap.SetSize(30.,30.);
1241\end{verbatim}
1242
1243\subsection{Writing, viewing \dots }
1244
1245All these objects have been design to be written to or read from a persistant file.
1246The following example shows how to write the previously created objects
1247into such a file~:
1248\begin{verbatim}
1249//-- Writing
1250
1251#include "fiospherehealpix.h"
1252//................
1253
1254char *fileout = "myfile.ppf";
1255POutPersist outppf(fileout);
1256FIO_SphereHEALPix<r_8> outsph(sph);
1257outsph.Write(outppf);
1258FIO_LocalMap<r_8> outloc(locmap);
1259outloc.Write(outppf);
1260// It is also possible to use the << operator
1261POutPersist os("sph.ppf");
1262os << outsph;
1263os << outloc;
1264\end{verbatim}
1265
1266Sophya graphical tools (spiapp) can automatically display and operate
1267all these objects.
1268
1269\newpage
1270\section{Module Samba}
1271\index{Spherical Harmonics}
1272\index{SphericalTransformServer}
1273The 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 analysed back to Cl's...
1274\begin{verbatim}
1275#include "skymap.h"
1276#include "samba.h"
1277....................
1278
1279// Generate input spectra a + b* l + c * gaussienne(l, 50, 20)
1280int lmax = 92;
1281Vector clin(lmax);
1282for(int l=0; l<lmax; l++) {
1283 double xx = (l-50.)/10.;
1284 clin(l) = 1.e-2 -1.e-4*l + 0.1*exp(-xx*xx);
1285}
1286
1287// Compute map from spectra
1288SphericalTransformServer<r_8> ylmserver;
1289int m = 128; // HealPix pixelisation parameter
1290SphereHEALPix<r_8> map(m);
1291ylmserver.GenerateFromCl(map, m, clin, 0.);
1292// Compute power spectrum from map
1293Vector clout = ylmserver.DecomposeToCl(map, lmax, 0.);
1294\end{verbatim}
1295
1296\newpage
1297\section{Module SkyT}
1298\index{RadSpectra} \index{SpectralResponse}
1299The SkyT module is composed of two types of classes:
1300\begin{itemize}
1301\item{} one which corresponds to an emission spectrum of
1302radiation, which is called RadSpectra
1303\item{} one which corresponds to the spectral response
1304of a given detector (i.e. corresponding to a detector
1305filter in a given frequency domain), which is called
1306SpectralResponse.
1307\end{itemize}
1308\begin{figure}[hbt]
1309\dclsbb{RadSpectra}{RadSpectraVec}
1310\dclsb{BlackBody}
1311\dclsccc{AnyDataObj}{SpectralResponse}{SpecRespVec}
1312\dclsc{GaussianFilter}
1313\caption{partial class for SkyT module}
1314\end{figure}
1315
1316\begin{verbatim}
1317#include "skyt.h"
1318// ....
1319// Compute the flux from a blackbody at 2.73 K through a square filter
1320BlackBody myBB(2.73);
1321// We define a square filter from 100 - 200 GHz
1322SquareFilter mySF(100,200);
1323// Compute the filtered integrated flux :
1324double flux = myBB.filteredIntegratedFlux(mySF);
1325\end{verbatim}
1326
1327A more detailed description of SkyT module can be found in:
1328{\it The SkyMixer (SkyT and PMixer modules) - Sophya Note No 2. }
1329available also from Sophya Web site.
1330
1331\newpage
1332\section{Module FitsIOServer}
1333\begin{figure}[hbt]
1334\dclsbb{FitsFile}{FitsInFile}
1335\dclsb{FitsOutFile}
1336\end{figure}
1337\index{FITS} \index{FitsInFile} \index{FitsOutFile}
1338This 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 :
1339\begin{verbatim}
1340#include "spherehealpix.h"
1341#include "fitsspherehealpix.h"
1342#include "fitstarray.h"
1343#include "tmatrix.h"
1344//...........................
1345
1346int m=...;
1347SphereHEALPix<r_8> sph(m);
1348................
1349int dim1=...;
1350int dim2=...;
1351TMatrix<r_8> mat(dim1,dim2);
1352............
1353
1354FITS_SphereHEALPix<r_8> sph_temp(sph);
1355FITS_TArray<r_8> mat_temp(mat);
1356// writing
1357
1358FitsOutFile os("myfile.fits");
1359sph_temp.Write(os);
1360mat_temp.Write(os);
1361
1362// reading
1363FitsInFile is("myfile.fits");
1364sph_temp.Read(is);
1365mat_temp.Read(is);
1366SphereHEALPix<r_8> new_sph=(SphereHEALPix<r_8>)sph_temp;
1367TMatrix<r_8> new_mat=(TMatrix<r_8>)mat_temp;
1368................
1369
1370\end{verbatim}
1371
1372The operators {\tt operator << (FitsOutFile ...)} and
1373{\tt operator >> (FitsInFile ...)} are defined in order
1374to facilitate the FITS file operations:
1375\begin{verbatim}
1376// Writing an array object to a FITS file
1377#include "fitstarray.h"
1378FitsOutFile fio("arr.fits");
1379Matrix m(20,30);
1380m = 12345.;
1381fio << m;
1382// .....
1383// Reading a binary table to a XNTuple
1384#include "fitsxntuple.h"
1385XNTuple xn;
1386FitsInFile fii("table.fits");
1387fii >> xn;
1388\end{verbatim}
1389
1390The class {\bf FITS\_AutoReader} provides a limited FITS files reading
1391and decoding capabilities. A partial class diagram of FITS persistence
1392handling classes is shown below:
1393\begin{figure}[hbt]
1394\dclsbb{FitsIOhandler}{FITS\_TArray}
1395\dclsb{FITS\_NTuple}
1396% \dclsb{FITS\_XNTuple}
1397\dclsb{FITS\_SphereHEALPix}
1398% \dclsb{FITS\_LocalMap}
1399\end{figure}
1400
1401\newpage
1402\section{LinAlg and IFFTW modules}
1403An interface to use LAPACK library (available from {\tt http://www.netlib.org})
1404is implemented by the {\bf LapackServer} class, in module LinAlg.
1405\index{LapackServer}.
1406The sample code below shows how to use SVD (Singular Value Decomposition)
1407through LapackServer:
1408\begin{verbatim}
1409#include "intflapack.h"
1410// ...
1411// Use FortranMemoryMapping as default
1412BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
1413// Create an fill the arrays A and its copy AA
1414int n = 20;
1415Matrix A(n , n), AA;
1416A = RandomSequence(RandomSequence::Gaussian, 0., 4.);
1417AA = A; // AA is a copy of A
1418// Compute the SVD decomposition
1419Vector S; // Vector of singular values
1420Matrix U, VT;
1421LapackServer<r_8> lpks;
1422lpks.SVD(AA, S, U, VT);
1423// We create a diagonal matrix using S
1424Matrix SM(n, n);
1425for(int k=0; k<n; k++) SM(k,k) = S(k);
1426// Check the result : A = U*SM*VT
1427Matrix diff = U*(SM*VT) - A;
1428double min, max;
1429diff.MinMax(min, max);
1430cout << " Min/Max difference Matrix (?=0) , Min= " << min
1431 << " Max= " << max << endl;
1432\end{verbatim}
1433
1434\index{FFTWServer}
1435The {\bf FFTWServer} class (in module FFTW) implements FFTServerInterface class
1436methods, for one dimensional and multi-dimensional Fourier
1437transforms on double precision data using the FFTW package
1438(available from {\tt http://www.fftw.org}).
1439
1440\newpage
1441\section{Building and installing Sophya}
1442Presently, the Sophya library has been tested with the following
1443compiler/platform pairs:
1444
1445\begin{center}
1446\begin{tabular}{ll}
1447Compaq/DEC OSF1 & cxx (6.0 , 6.2) \\
1448Compaq/DEC OSF1 & g++ (2.95) \\
1449Linux & g++ (2.91 , 2.95) \\
1450Linux & KCC (3.4) \\
1451Solaris & g++ (2.95) \\
1452SGI IRIX64 & CC (7.3) \\
1453\end{tabular}
1454\end{center}
1455
1456Some of the modules in the Sophya package uses external libraries. The
1457{\bf FitsIOServer} is the example of such a module, where the {\tt libcfitsio.a}
1458is used.
1459The build procedure expects to find the include files and the libraries in: \\
1460{\tt \$EXTLIBDIR/Include/FitsIO } \\
1461{\tt \$EXTLIBDIR/`uname`-\$EROSCXX/Libs} \\
1462
1463The object files from a given Sophya module are grouped in an archive library
1464with the module's name ({\tt libmodulename.a}). All Sophya modules
1465 are grouped in a single shared library ({\tt libsophya.so}), while the
1466modules with reference to external libraries are grouped in
1467({\tt libextsophya.so}). The {\bf PI} and {\bf PIext} modules are
1468grouped in ({\tt libPI.so}).
1469
1470The environment variables {\bf DPCDEVREP}, {\bf EXTLIBDIR} and {\bf EROSCXX}
1471must be defined in order to install the Sophya package.
1472In the example below, we assume that we want to install Sophya from a
1473released (tagged) version in the source directory {\tt \$SRC} in the
1474{\tt /usr/local/Sophya} diretory, using {\tt g++}. We assume that
1475the external libraries directory tree has been set up in
1476{\tt /usr/local/ExtLibs/}. \\[3mm]
1477\centerline{
1478 \rule{20mm}{0.5mm}
1479 {\bf \large the use of GNU make is mandatory}
1480 \rule{20mm}{0.5mm} }
1481
1482\vspace*{3mm}
1483\begin{verbatim}
1484# We select our C++ compiler
1485csh> setenv EROSCXX g++
1486# Setup the build directory
1487csh> mkdir /usr/local/Sophya/
1488csh> setenv DPCDEVREP /usr/local/Sophya/
1489csh> setenv EXTLIBDIR /usr/local/ExtLibs/
1490# Use the top level makefile in Mgr/
1491csh> cd \$SRC
1492csh> cp Mgr/Makefile Makefile
1493# Step 1: Create the directory tree and copy the include files (.h)
1494csh> make depend
1495# Step 2: Compile the modules without external library reference
1496csh> make libs
1497# Step 3: Compile the modules WITH external library reference (optional)
1498csh> make extlibs
1499# Step 4: Build libsophya.so
1500csh> make slb
1501# Step 5: Build libextsophya.so (optional)
1502csh> make slbext
1503# Step 6: Compile the PI and PIext modules (optional)
1504csh> make PI
1505# Step 7: Build the corresponding shared library libPI.so (optional)
1506csh> make slbpi
1507\end{verbatim}
1508
1509To compile all modules and build the shared libraries, it is possible
1510to use:
1511\begin{verbatim}
1512# Step 2,3,6
1513csh> make all
1514# Step 4,5,7
1515csh> make slball
1516\end{verbatim}
1517
1518At this step, all libraries should have been made. Programs using
1519Sophya libraries can now be built:
1520\begin{verbatim}
1521# To compile test programs
1522csh> cd Tests
1523csh> make arrt ...
1524csh> cd ..
1525# To compile other programs, for example from the PMixer module
1526csh> cd PMixer
1527csh> make
1528csh> cd ..
1529# To build (s)piapp (libPI.so is needed)
1530csh> cd ProgPI
1531csh> make
1532csh> cd ..
1533\end{verbatim}
1534
1535\subsection{Mgr module}
1536This module contains scripts which can be used for generating the
1537makefiles for each module.
1538\begin{itemize}
1539\item {\bf Makefile} Top level Makefile for building the libraries.
1540\item {\bf Makefile.h} contains the definition of compilation flags for the
1541different compilers and systems. This file is used for building the
1542library and generating {\bf MakefileUser.h} (to be included in makefiles).
1543\item {\bf Makefile.slb} contains the rules for building shared libraries
1544for the different compilers and systems. (to be included in makefiles)
1545\item {\bf crerep\_sophya} c-shell script for creating the directory tree
1546under {\tt \$DPCBASEREP} and {\tt \$DPCDEVREP}
1547\item {\bf install\_sophya} c-shell script for installing the Sophya package.
1548Usually from {\tt \$DPCDEVREP} to {\tt \$DPCBASEREP}
1549\item {\bf mkmflien} c-shell script for making symbolic links or copying
1550include files to {\tt \$DPCDEVREP/Include} or {\tt \$DPCBASEREP/Include}
1551\item {\bf mkmf} c-shell script for generating module makefiles and the
1552top level makefile (named GNUmakefile)
1553\item {\bf mkmflib} c-shell script for generating each library module
1554makefile (named GNUmakefile)
1555\item {\bf mkmfprog} c-shell script for generating makefile for a module
1556containing the source for executable programs (named GNUmakefile).
1557\item {\bf mkmfPI} c-shell script for generating makefile for PI and PIext
1558modules (named GNUmakefile)
1559\item {\bf libdirs} List of Sophya modules without reference to external
1560libraries.
1561\item {\bf extlibdirs} List of Sophya modules with reference to external
1562libraries.
1563
1564\end{itemize}
1565
1566\newpage
1567\appendix
1568\section{SOPHYA Exceptions}
1569\index{Exception classes} \index{PThrowable} \index{PError} \index{PException}
1570SOPHYA library defines a set of exceptions which are used
1571for signalling error conditions. The figure below shows a partial
1572class diagram for exception classes in SOPHYA.
1573\begin{figure}[hbt]
1574\dclsbb{PThrowable}{PError}
1575\dclscc{PError}{AllocationError}
1576\dclscc{PError}{NullPtrError}
1577\dclscc{PError}{ForbiddenError}
1578\dclscc{PError}{AssertionFailedError}
1579\dclsbb{PThrowable}{PException}
1580\dclscc{PException}{IOExc}
1581\dclscc{PException}{SzMismatchError}
1582\dclscc{PException}{RangeCheckError}
1583\dclscc{PException}{ParmError}
1584\dclscc{PException}{TypeMismatchExc}
1585\dclscc{PException}{MathExc}
1586\dclscc{PException}{CaughtSignalExc}
1587\caption{partial class diagram for exception handling in Sophya}
1588\end{figure}
1589
1590For simple programs, it is a good practice to handle
1591the exceptions at least at high level, in the {\tt main()} function.
1592The example below shows the exception handling and the usage
1593of Sophya persistence.
1594
1595\input{ex1.inc}
1596
1597
1598\newpage
1599\addcontentsline{toc}{section}{Index}
1600\printindex
1601\end{document}
1602
Note: See TracBrowser for help on using the repository browser.