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

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

suite maj sophya.tex (overview) pour V=2.1 , Reza 06/12/2007

File size: 92.8 KB
RevLine 
[3015]1\documentclass[twoside,10pt]{article}
[887]2% Package standard : Utilisation de caracteres accentues, mode francais et graphique
[988]3\usepackage{url}
[887]4\usepackage[latin1]{inputenc}
5\usepackage[T1]{fontenc}
6\usepackage[english]{babel}
7\usepackage{graphicx}
8% package a mettre pour faire du pdf
[978]9\usepackage{palatino}
[887]10
11% Extension de symboles mathematiques
12\usepackage{amssymb}
13
[988]14% Definition pour Docs Sophya
[974]15\usepackage{defsophya}
16
[1362]17% Constitution d'index
18\usepackage{makeidx}
[2280]19
20\usepackage[ps2pdf,bookmarks,bookmarksnumbered,%
21 urlcolor=blue,citecolor=blue,linkcolor=blue,%
22 pagecolor=blue,%hyperindex,%
23 colorlinks=true,hyperfigures=true,hyperindex=true
24 ]{hyperref}
[887]25
[2280]26\makeindex % Constitution d'index
27
[3006]28\newcommand{\rond}{$\bullet \ $}
29\newcommand{\etoile}{$\star \ $}
30\newcommand{\cercle}{$\circ \ $}
31\newcommand{\carre}{$\Box \ $}
32
33
[887]34\begin{document}
35
36\begin{titlepage}
[988]37% The title page - top of the page with the title of the paper
[978]38\titrehp{Sophya \\ An overview }
[988]39% Authors list
[978]40\auteurs{
[988]41R. Ansari & ansari@lal.in2p3.fr \\
[1340]42E. Aubourg & aubourg@hep.saclay.cea.fr \\
[988]43G. Le Meur & lemeur@lal.in2p3.fr \\
44C. Magneville & cmv@hep.saclay.cea.fr \\
45S. Henrot-Versille & versille@in2p3.fr
[887]46}
[978]47% \auteursall
[988]48% The title page - bottom of the page with the paper number
[1362]49\vspace{1cm}
50\begin{center}
[3417]51{\bf \Large Sophya Version: 2.1 (V\_Nov2007) }
[1362]52\end{center}
[988]53\titrebp{1}
[887]54\end{titlepage}
55
56\tableofcontents
57
58\newpage
59
60\section{Introduction}
61
[1362]62{\bf SOPHYA} ({\bf SO}ftware for {\bf PHY}sics {\bf A}nalysis)
[887]63is a collection of C++ classes designed for numerical and
64physics analysis software development. Our goal is to provide
65easy to use, yet powerful classes which can be used by scientists.
[2790]66Although some of the SOPHYA modules (SkyMap, Samba, SkyT)
[3045]67have been designed with the specific goal of CMB data analysis, most
[2790]68modules presented here have a much broader scope and can be
69used in scientific data analysis and modeling/simulation.
[3006]70Whenever possible, we use existing numerical packages and libraries,
71encapsulating them in classes in order to facilitate their usage.
[1435]72\par
[3006]73Our main requirements in designing SOPHYA classes can be summarized as
74follow:
75\begin{itemize}
76\item[\rond] Provide a comprehensive set of data containers, such as arrays and tables
77(tuple) covering the most common needs in scientific simulation and data analysis
78softwares.
79\item[\rond] Take advantage of the C++ language and define methods and operators
80for most basic operation, such as arithmetic operations, in a rather intuitive way, while
81maintaining performances comparable to low level coding in other languages
82(C, Fortran, F90 \ldots)
83\item[\rond] Simplify memory management for programmers using the class library.
84This has been a strong requirement for most SOPHYA classes. Automatic reference
85sharing and memory management is implemented in SOPHYA classes intended
86for large size objects. We recommend to allocate SOPHYA objects on the stack,
87including when objects are returned by methods or functions.
88See section \ref{memgt} for more information.
89\item[\rond] Archiving, importing (reading) and exporting (writing) data in a
90efficient and consistent way is a major concern in many scientific software
91and projects. SOPHYA provide a native data I/O or persistence system,
92(PPF, \ref{ppfdesc}) as well as import/export services for ASCII and FITS formats.
93\end{itemize}
94
95% \vspace*{2mm}
[1435]96This documents
[1434]97presents only a brief overview of the class library,
98mainly from the user's point of view. A more complete description
[2278]99can be found in the reference manual, available from the SOPHYA
[2280]100web site: % {\bf http://www.sophya.org}.
101\href{http://www.sophya.org}{http://www.sophya.org}.
[3006]102%%%
103%%%
104\subsection{SOPHYA modules}
[3045]105\label{sopmodules}
[1434]106The source directory tree
[2790]107\footnote{ CVS: cvsserver.lal.in2p3.fr:/exp/eros/CVSSophya}
[887]108is organised into a number of modules.
109
110\begin{itemize}
[2790]111\item[] {\bf BuildMgr/} Scripts for code management,
[887]112makefile generation and software installation
[2278]113\item[] {\bf BaseTools/} General architecture support classes such
[887]114as {\tt PPersist, NDataBlock<T>}, and few utility classes
[2278]115such as the dynamic variable list manager ({\tt DVList}) as well
116as the basic set of exception classes used in SOPHYA.
[1340]117\item[] {\bf TArray/} template numerical arrays, vectors and matrices \\
[1648]118({\tt TArray<T> TMatrix<T> TVector<T> } \ldots)
[887]119\item[] {\bf NTools/} Some standard numerical analysis tools
120(linear, and non linear parameter fitting, FFT, \ldots)
[1648]121\item[] {\bf HiStats/} Histogram-ming and data set handling classes (tuples) \\
[2790]122({\tt Histo Histo2D NTuple DataTable} \ldots)
123\item[] {\bf SkyMap/} Local and full sky maps, and some 3D geometry
[1648]124handling utility classes. \\
125({\tt PixelMap<T>, LocalMap<T>, SphericalMap<T>, \ldots})
[2790]126\item[] {\bf SUtils/} This module contains few utility classes, such as the
[2278]127{\tt DataCard} class, as well as string manipulation functions in C and C++.
128\item[] {\bf SysTools/} This module contains classes implementing
129an interface to various OS specific services, such
[2790]130threads and dynamic link/shared library handling.
[2278]131
[887]132\end{itemize}
133
[978]134The modules listed below are more tightly related to the
135CMB (Cosmic Microwave Background) data analysis problem:
[887]136\begin{itemize}
137\item[] {\bf SkyT/}
138classes for spectral emission and detector frequency response modelling \\
139({\tt SpectralResponse, RadSpectra, BlackBody} \ldots)
[1436]140\item[] {\bf Samba/} Spherical harmonic analysis, noise generators \ldots
[887]141\end{itemize}
142
[978]143The following modules contain the interface classes with
144external libraries:
[887]145\begin{itemize}
[988]146\item[] {\bf FitsIOServer/} Classes for handling file input-output
[3006]147in FITS format using the cfitsio library.
148FITS is maintained by NASA and SAO and is available from: \\
149\href{http://heasarc.gsfc.nasa.gov/docs/software/fitsio/fitsio.html}
150{http://heasarc.gsfc.nasa.gov/docs/software/fitsio/fitsio.html}
151\item[] {\bf LinAlg/} Interface with Lapack linear algebra package.
152Lapack is a linear algebra package and can be downloaded from: \\
153\href{http://www.netlib.org/lapack/}{http://www.netlib.org/lapack/}
154\item[] {\bf IFFTW/} Interface with FFTW package (libfftw.a).
155FFTW is a package for performing Fourier transforms, written in C.
156Documentation and source code can be found at: \\
157\href{http://www.fftw.org/}{http://www.fftw.org/}
[1648]158\item[] {\bf XAstroPack/} Interface to some common astronomical
159computation libraries. Presently, this module uses an external library
160extracted from the {\bf Xephem } source code. The corresponding source
161code is also available from SOPHYA cvs repository, module {\bf XephemAstroLib}.
[3006]162Information on Xephem can be found at : \\
163\href{http://www.clearskyinstitute.com/xephem/}{http://www.clearskyinstitute.com/xephem/}
[2790]164
[887]165\end{itemize}
166
[2278]167The following modules contain each a set of related programs using the
168SOPHYA library.
[887]169\begin{itemize}
[3415]170\item[] {\bf Tests/} Simple test programs. Many of these test programs can be also used
171as examples for using SOPHYA.
[1340]172\item[] {\bf PrgUtil/} Various utility programs (runcxx, scanppf, scanfits, \ldots)
[2278]173\item[] {\bf PrgMap/} Programs performing operations on skymaps: projections,
174power spectrum in harmonic space, \ldots
175\end{itemize}
176
177As a companion to SOPHYA, the {\bf (s)piapp} interactive data analysis
178program is built on top of SOPHYA and the {\bf PI} GUI class library
179and application framework. The {\bf PI} ({\bf P}eida {\bf Interactive})
180development started in 1995, in the EROS \footnote{EROS: {\bf E}xp\'erience
181de {\bf R}echerche d'{\bf O}bjets {\bf S}ombres - http://eros.in2p3.fr}
182microlensing search collaboration, with PEIDA++ \footnote {PEIDA++:
183The EROS data analysis class library -
184http://www.lal.in2p3.fr/recherche/eros/PeidaDoc/}.
185The {\bf PI} documentation and the {\bf piapp} user's guide are available
[2280]186from \href{http://www.sophya.org}{http://www.sophya.org}.
[2278]187%\href{http://www.sophya.org}{http://www.sophya.org}.
188The {\bf PI} is organized as the following modules:
189\begin{itemize}
[2280]190\item[] {\bf PI/} Portable GUI class library and application development
[2278]191framework kernel.
[2280]192\item[] {\bf PIGcont/} Contour-plot drawing classes.
193\item[] {\bf PIext/} Specific drawers and adapters for SOPHYA objects,
[2278]194and the {\bf piapp} interactive data analysis framework.
195\item[] {\bf ProgPI/} interactive analysis tool main program and pre-loaded
196modules.
[887]197\end{itemize}
198
[3006]199Modules containing examples and demo programs and scripts:
[1434]200\begin{itemize}
201\item[] {\bf Examples/} Sample SOPHYA codes and example programs and
[2790]202makefiles.
203\item[] {\bf DemoPIApp/} Sample scripts and programs for (s)piapp
[1434]204interactive analysis tools.
205\end{itemize}
[3415]206
207The following modules contains additional programs or libraries, based on SOPHYA :
208\begin{itemize}
209\item[] {\bf AnaLC} contains program files extracted from the EROS/PEIDA software
210and adapted to be compiled with SOPHYA. It can be used in particular to read and analyse
211EROS light curve data files.
212\item[] {\bf LUC} {\bf L}ittle {\bf U}niverse {\bf C}alculator is a module containing classes to
213perform basic computation related to the universe geometry (FRW metric).
214\item[] {\bf PMixer/} skymixer and related programs
215\end{itemize}
216
[988]217\newpage
218
[978]219\section{Using Sophya}
[2996]220The organisation of SOPHYA directories and some of the associated
221utility programs are described in this section.
[3045]222Basic usage of Sophya classes is described in the following sections.
[2278]223Complete Sophya documentation can be found at our web site
[1434]224{\bf http://www.sophya.org}.
[1362]225
[2790]226\subsection{Directories, environment variables, configuration files}
[3015]227\label{directories}
[2790]228The environment variable {\bf SOPHYABASE} is used
[3006]229to define the path where the Sophya libraries and binaries are installed.
[978]230\begin{itemize}
[2790]231\item \$SOPHYABASE/include : Include (.h) files
232\item \$SOPHYABASE/lib : Path for the archive libraries (.a)
[3015]233\item \$SOPHYABASE/slb: Shared library path (.so or .dylib on Darwin/MacOS)
[3006]234\item \$SOPHYABASE/exe : Path for binary program files
[978]235\end{itemize}
236
[2790]237The directory { \tt \$SOPHYABASE/include/SophyaConfInfo/ } contains files
238describing the installed configuration of SOPHYA software.
[978]239
[2790]240The file { \tt \$SOPHYABASE/include/machdefs.h } contains definitions
[3015]241(flags, typedef) used in SOPHYA, while some more specific flags,
242are found in { \tt \$SOPHYABASE/include/sspvflags.h }
[2790]243
244The file { \tt \$SOPHYABASE/include/sophyamake.inc } contains the
245compilation commands and flags used for building the software.
246Users can use most of compilation and link commands defined in this file:
247 {\tt \$CCOMPILE , \$CXXCOMPILE . \$CXXLINK \ldots}.
248 (See module Example).
249
250The configure script (BuildMgr/configure) creates the directory tree and the
[3015]251above files. It also copy (or create symbolic link) for all SOPHYA include
252files as well as symbolic links for external libraries
253include files path in {\tt \$SOPHYABASE/include} (FitsIO, FFTW, XAstro \ldots).
[2790]254
[3015]255Object files for each module are grouped in a static archive library
256by the build procedure (libXXX.a for module
257XXX, with XXX = BaseTools, TArray, HiStats, FitsIOServer \ldots).
258
259When shared libraries are build, all stand alone SOPHYA modules
260are grouped in {\tt libsophya.so}, {\tt libextsophya.so} contains
261the interface modules with external libraries {\bf (FitsIOServer, LinAlg \ldots)},
262while {\bf PI, PIext, PIGcont} modules are grouped in {\tt libPI.so}.
263Alternatively, it is possible to group all modules in a single shared
264library {\tt libAsophyaextPI.so} (See \ref{build})
265
266In order to use the shared libraries, the {\bf LD\_LIBRARY\_PATH} variable
267should contain the Sophya shared library path
268({\tt \$SOPHYABASE/slb}).
269On Silicon Graphics machines with IRIX64 operating system,
270the default SOPHYA configuration correspond to the 64 bit architecture.
271The environment variable { \bf LD\_LIBRARY64\_PATH } replace in
272this case the usual {\bf LD\_LIBRARY\_PATH} variable.
273On IBM machines with AIX, the {\bf LIBPATH} environment variables
274contains the shared libraries search path.
275
276When using the dynamic load services in SOPHYA ({\tt PDynLinkMgr}
277class), in runcxx or (s)piapp applications for example, the shared
278library search path must contain the current working directory (
279dot . in unix).
280
[1362]281\subsection{the runcxx program}
[3037]282\index{runcxx} \label{runcxx}
[1362]283{\bf runcxx} is a simple program which can be used to compile, link
284and run simple C++ programs. It handles the creation of a
285complete program file, containing the basic set C++ include files,
286the necessary include files for SOPHYA SysTools, TArray, HiStats
287and NTools modules, and the main program with exception handling.
288Other Sophya modules can be included using the {\tt -import} flag.
[1435]289Use of additional include files can be specified using the
290{\tt -inc} flag.
[1362]291\begin{verbatim}
292csh> runcxx -h
[2790]293 PIOPersist::Initialize() Starting Sophya Persistence management service
294SOPHYA Version 1.9 Revision 0 (V_Mai2005) -- May 31 2005 15:11:32 cxx
295 runcxx : compiling and running of a piece of C++ code
296 Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions]
297 [-tmpdir TmpDirectory] [-f C++CodeFileName]
298 [-inc includefile] [-inc includefile ...]
299 [-import modulename] [-import modulename ...]
300 [-uarg UserArg1 UserArg2 ...]
301 if no file name is specified, read from standard input
302 modulenames: SkyMap, Samba, SkyT, FitsIOServer,
303 LinAlg, IFFTW, XAstroPack
[1362]304\end{verbatim}
305Most examples in this manual can be tested using runcxx. The
306example below shows how to compile, link and run a sample
307code.
308\begin{verbatim}
309// File example.icc
310Matrix a(3,3);
311a = IdentityMatrix(1.);
312cout << a ;
313// Executing this sample code
314csh> runcxx -f example.icc
315\end{verbatim}
[2278]316
317\subsection{the scanppf program}
[3037]318\index{scanppf} \label{scanppf}
[2278]319{\bf scanppf} is a simple SOPHYA application which can be used to check
[2790]320PPF files and list their contents. It can also provide the list of all registered
321PPF handlers.
[2278]322\begin{verbatim}
323csh> scanppf -h
[2790]324 PIOPersist::Initialize() Starting Sophya Persistence management service
[3034]325SOPHYA Version 2.0 Revision 0 (V_Jul2006) -- Jul 17 2006 14:13:27 cxx
[2790]326 Usage: scanppf [flags] filename
[3034]327 flags = -s -n -a0 -a1 -a2 -a3 -lh -lho -lmod
[2790]328 -s[=default} : Sequential reading of objects
329 -n : Object reading at NameTags
330 -a0...a3 : Tag List with PInPersist.AnalyseTags(0...3)
331 -lh : List PPersist handler classes
332 -lho : List PPersist handler and dataobj classes
[3034]333 -lmod : List initialized/registered modules
[2278]334\end{verbatim}
335
[3037]336\subsection{the scanfits program}
337\index{scanfits} \label{scanfits}
[2996]338{\bf scanfits} is a SOPHYA program using the FitsIOServer
339\footnote{FitsIOServer module uses the cfitsio library. scanfits has to be linked with
340with FitsIOServer module and cfitsio libraries, or libextsophya.so}
341module which can be used
342to analyse the content of FITS files. It can list the FITS headers, the appropriate
343SOPHYA-FITS handler (implementing {\tt FitsHandlerInterface}) class, and the list of
344all registered FITS handlers.
345\begin{verbatim}
346csh> scanfits -h
347 PIOPersist::Initialize() Starting Sophya Persistence management service
[3037]348SOPHYA Version 2.0 Revision 0 (V_Jul2006) -- Jul 17 2006 14:13:27 cxx
[2996]349 Usage: scanfits [flags] filename
350 flags = -V1 -lh -rd -header
351 -V1 : Scan using old (V1) code version
352 -lh : Print the list of registered handlers (FitsHandlerInterface)
353 -rd : try to read each HDU data using appropriate handler
354 -header : List header information
355\end{verbatim}
[2278]356
[3415]357\subsection{the spiapp program}
358\index{spiapp} \label{spiapp}
359{\bf spiapp} is an interactive data analysis program, built on top of the SOPHYA
360library, , the PI GUI library. The interactive data analysis framework is defined
361by the classes in the {\bf PIext} module. spiapp has a c-shell like
362command interpreter and can be used to manipulate SOPHYA and display
363objects. Refer to the piapp user manual for more information.
364\begin{verbatim}
365csh> spiapp -h
366 SophyaInitiator::SophyaInitiator() BaseTools Init
367 PIOPersist::Initialize() Starting Sophya Persistence management service
368SOPHYA Version 2.1 Revision 0 (V_Nov2007) -- Nov 24 2007 13:08:58 gcc 3.3 20030304 (Apple Computer, Inc. build 1495)
369
370 piapp: Interactive data analysis and visualisation program
371 Usage: piapp [-nored] [-doublered] [-termread] [-term]
372 [-hidezswin] [-small] [-nosig] [-nosigfpe] [-nosigsegv]
373 [-tmpdir TmpDirectory] [-help2tex] [-exec file [args]]
374 -nored : Don't redirect stdout/stderr to piapp console
375 -doublered : Redirect stdout/stderr to piapp console AND the terminal
376 -termread : Read commands on terminal (stdin)
377 -term : equivalent to -nored -termread -small
378 -hidezswin : Hide Zoom/Stat/ColMap window
379 -small : Create small size main piapp window
380 -nosig : Don't catch SigFPE, SigSEGV
381 -nosigfpe -nosigsegv: Don t catch SigFPE / SigSEGV
382 -tmpdir TmpDirectory: defines TMDIR for temporary files
383 -help2tex: Create a LaTeX help file (piahelp.tex)
384 -exec file [args] : Execute command file (last option)
385
386\end{verbatim}
[1435]387\newpage
[978]388
[1362]389\section{Copy constructor and assignment operator}
[3006]390\label{memgt}
[1435]391In C++, objects can be copied by assignment or by initialisation.
392Copying by initialisation corresponds to creating an object and
393initialising its value through the copy constructor.
[1362]394The copy constructor has its first argument as a reference, or
395const reference to the object's class type. It can have
396more arguments, if default values are provided.
397Copying by assignment applies to an existing object and
398is performed through the assignment operator (=).
399The copy constructor implements this for identical type objects:
400\begin{verbatim}
401class MyObject {
402public:
403 MyObject(); // Default constructor
404 MyObject(MyObject const & a); // Copy constructor
405 MyObject & operator = (MyObject const & a) // Assignment operator
406}
407\end{verbatim}
408The copy constructors play an important role, as they are
409called when class objects are passed by value,
410returned by value, or thrown as an exception.
411\begin{verbatim}
412// A function declaration with an argument of type MyObject,
413// passed by value, and returning a MyObject
414MyObject f(MyObject x)
415{
416 MyObject r;
417 ...
418 return(r); // Copy constructor is called here
419}
420// Calling the function :
421MyObject a;
422f(a); // Copy constructor called for a
423\end{verbatim}
424It should be noted that the C++ syntax is ambiguous for the
425assignment operator. {\tt MyObject x; x=y; } and
426{\tt MyObject x=y;} have different meaning.
427\begin{verbatim}
428MyObject a; // default constructor call
429MyObject b(a); // copy constructor call
430MyObject bb = a; // identical to bb(a) : copy constructor call
431MyObject c; // default constructor call
432c = a; // assignment operator call
433\end{verbatim}
[1299]434
[1362]435As a general rule in SOPHYA, objects which implements
436reference sharing on their data members have a copy constructor
437which shares the data, while the assignment operator copies or
438duplicate the data.
439
[3415]440\section{Multi-thread programming with SOPHYA}
441Multi-thread programming is usually safe as long as different threads DO NOT access
442the same memory locations. SOPHYA is mainly organized as classes having only
443data members, with very few cases having static data members (memory locations
444common to all instances of a class), or seldom, global variables. \\
445Using different instances of a class in different threads is thus safe for most
446classes / methods in SOPHYA. \\
447A major exception is the reference sharing mechanism, where different instances
448of a class may shared memory locations. This reference sharing mechanism has been
449made thread-safe in SOPHYA, from version V=2.1. \\
450As a consequence, different execution threads can access non overlapping sub-arrays
451and access (read/write) the corresponding elements without the need of
452mutex and thread synchonisation. Moreover, thread safe filling of
453NTuple and DataTable objects can be activated, on an object per object basis. \\
454The {\tt ZThread, ZMutex \ldots} classes in the {\bf SysTools } module offer a relatively
455easy way of writing multi-threaded programs.
456
[1435]457\newpage
[2278]458\section{Module BaseTools}
[1299]459
[2278]460{\bf BaseTools} contains utility classes such as
[1299]461{\tt DVlist}, an hierarchy of exception classes for Sophya, a template
462class {\tcls{NDataBlock}} for handling reference counting on numerical
463arrays, as well as classes providing the services for implementing simple
[1435]464serialisation.
[1299]465\vspace*{5mm}
466
[3034]467\subsection{Initialisation}
468\index{SophyaInitiator}
469A number of actions have to be taken before
470some of the services provided by SOPHYA become operational. This is the case
471of SOPHYA persistence, as well as FITS I/O facilities.
472Initialisation of many SOPHYA modules is performed through an initialiser class,
[3040]473which inherits from {\bf SophyaInitiator}.
474\par
475Static instance of each initialiser class exist in the library and the various SOPHYA services
476should be operational when the user code ({\tt main()}) starts, except for
477modules in the second or third shared libraries
478({\tt libextsophya.so libPI.so}). Indeed, a problem related
479to the initialisation of shared libraries arises on some systems
480(Darwin/Mac OS X in particular) causing program crash at start-up,
481if static instance of initialiser class is present in the second shared library.
482The FitsIOServer module should thus be explicitly initialised in the user
483program.
484\par
485In cases where the run time loader does not perform correctly the static
486object initialisation, the initialiser class for the modules used in the
487program must be instanciated in the beginning of your main program: \\
488{\tt TArrayInitiator , HiStatsInitiator , SkyMapInitiator , FitsIOServer \ldots}
[3034]489%%%
[1340]490\subsection{SOPHYA persistence}
[3006]491\label{ppfdesc}
[1362]492\index{PPersist} \index{PInPersist} \index{POutPersist}
[1299]493\begin{figure}[hbt]
494\dclsa{PPersist}
[2790]495\dclsccc{PPFBinarIOStream}{PPFBinaryInputStream}{PInPersist}
496\dclscc{PPFBinaryOutputStream}{POutPersist}
[1299]497\caption{partial class diagram for classes handling persistence in Sophya}
498\end{figure}
[1340]499A simple persistence mechanism is defined in SOPHYA. Its main
500features are:
501\begin{itemize}
502\item[] Portable file format, containing the description of the data structures
503and object hierarchy. \\
504{\bf PPF} {\bf P}ortable {\bf P}ersistence file {\bf F}ormat.
[1648]505\index{PPF}
[1435]506\item[] Handling of read/write for multiply referenced objects.
[1340]507\item[] All write operations are carried using sequential access only. This
508holds also for read operations, unless positional tags are used.
509SOPHYA persistence services can thus be used to transfer objects
510through network links.
[1360]511\item[] The serialisation (reading/writing) for objects for a given class
[1435]512is implemented through a handler object. The handler class inherits
[1360]513from {\tt PPersist} class.
[1435]514\item[] A run time registration mechanism is used in conjunction with
515RTTI (Run Time Type Identification) for identifying handler classes
516when reading {\bf PInPersist} streams, or for associating handlers
517with data objects {\bf AnyDataObject} for write operations.
[1340]518\end{itemize}
[1360]519A complete description of SOPHYA persistence mechanism and guidelines
520for writing delegate classes for handling object persistence is beyond
[1435]521the scope of this document. The most useful methods for using Sophya
522persistence are listed below:
523\begin{itemize}
524\item[] {\tt POutPersist::PutObject(AnyDataObj \& o)} \\
525Writes the data object {\bf o} to the output stream.
526\item[] {\tt POutPersist::PutObject(AnyDataObj \& o, string tagname)} \\
527Writes the data object {\bf o} to the output stream, associated with an
528identification tag {\bf tagname}.
529\item[] {\tt PInPersist::GetObject(AnyDataObj \& o)} \\
530Reads the next object in stream into {\bf o}. An exception is
531generated for incompatible object types.
532\item[] {\tt PInPersist::GetObject(AnyDataObj \& o, string tagname)} \\
533Reads the object associated with the tag {\bf tagname} into {\bf o}.
534An exception is generated for incompatible object types.
535\end{itemize}
536The operators {\tt operator << (POutPersist ...) } and
537{\tt operator >> (PInPersist ...) } are often overloaded
[2790]538to perform {\tt PutObject()} and {\tt GetObject()} operations.
539the {\bf PPFNameTag} (ppfnametag.h) class can be used in conjunction with
540{\tt << >> } operators to write objects with a name tag or to retrieve
541an object identified with a name tag. The example below shows the
542usage of these operators:
[1435]543\begin{verbatim}
544// Creating and filling a histogram
545Histo hw(0.,10.,100);
546...
547// Writing histogram to a PPF stream
548POutPersist os("hw.ppf");
[2790]549os << PPFNameTag("myhisto") << hw;
550
[1435]551// Reading a histogram from a PPF stream
552PInPersist is("hr.ppf");
[2790]553is >> PPFNameTag("myhisto") >> hr;
[1435]554\end{verbatim}
[1360]555
[1435]556The {\bf scanppf} program can be used to list the content of a PPF file.
557
[1360]558\subsection{\tcls{NDataBlock}}
[1362]559\index{\tcls{NDataBlock}}
560\begin{figure}[hbt]
561\dclsbb{AnyDataObj}{\tcls{NDataBlock}}
562\dclsbb{PPersist}{\tcls{FIO\_NDataBlock}}
563\end{figure}
[1360]564The {\bf \tcls{NDataBlock}} is designed to handle reference counting
565and sharing of memory blocs (contiguous arrays) for numerical data
566types. Initialisation, resizing, basic arithmetic operations, as
567well as persistence handling services are provided.
568The persistence handler class ({\tt \tcls{FIO\_NDataBlock}}) insures
569that a single copy of data is written for multiply referenced objects,
570and the data is shared among objects when reading.
571\par
572The example below shows writing of NDataBlock objects through the
573use of overloaded operator $ << $ :
[1340]574\begin{verbatim}
575#include "fiondblock.h"
576// ...
577POutPersist pos("aa.ppf");
578NDataBlock<r_4> rdb(40);
579rdb = 567.89;
580pos << rdb;
581// We can also use the PutObject method
582NDataBlock<int_4> idb(20);
583idb = 123;
584pos.PutObject(idb);
585\end{verbatim}
586The following sample programs show the reading of the created PPF file :
587\begin{verbatim}
588PInPersist pis("aa.ppf");
589NDataBlock<r_4> rdb;
590pis >> rdb;
591cout << rdb;
592NDataBlock<int_4> idb;
593cout << idb;
594\end{verbatim}
[1299]595
[2996]596\subsection{DVList, MuTyV and TimeStamp classes}
597\index{DVList} \index{MuTyV} \index{TimeStamp}
[1362]598\begin{figure}[hbt]
[2996]599\dclsa{MuTyV}
[1362]600\dclsbb{AnyDataObj}{DVList}
601\dclsbb{PPersist}{\tclsc{ObjFileIO}{DVList}}
602\end{figure}
603The {\bf DVList} class objects can be used to create and manage list
604of values, associated with names. A list of pairs of (MuTyV, name(string))
605is maintained by DVList objects. {\bf MuTyV} is a simple class
606capable of holding string, integer, float or complex values,
607providing easy conversion methods between these objects.
[2996]608{\bf MuTyV} objects can also hold {\bf TimeStamp } objects.
[1362]609\begin{verbatim}
610// Using MuTyV objects
611MuTyV s("hello"); // string type value
612MuTyV x;
[1435]613x = "3.14159626"; // string type value, ASCII representation for Pi
[1362]614double d = x; // x converted to double = 3.141596
615x = 314; // x contains the integer value = 314
616// Using DVList
617DVList dvl;
618dvl("Pi") = 3.14159626; // float value, named Pi
619dvl("Log2") = 0.30102999; // float value, named Log2
620dvl("FileName") = "myfile.fits"; // string value, named myfile.fits
621// Printing DVList object
622cout << dvl;
623\end{verbatim}
624
[2996]625\begin{figure}[hbt]
626\dclsbb{AnyDataObj}{TimeStamp}
627\end{figure}
628%
629The {\bf TimeStamp} class represent date and time and provides
630many standard operations, such as Initialisation from strings,
631conversion to strings and time interval computations. \\
632Usage example:
633\begin{verbatim}
634// Create a object with the current date and time and prints it to cout
635TimeStamp ts;
636cout << ts << endl;
637// Create an object with a specified date and time
638TimeStamp ts2("01/01/1905","00:00:00");
639// Get the number of days since 0 Jan 1901
640cout << ts2.ToDays() << endl;
641
642// Combined use of TimeStamp and MuTyV
643string s;
644TimeStamp ts; // Current date/time
645MuTyV mvt = ts;
646s = mvt; // s contains the current date in string format
647cout << s << endl;
648\end{verbatim}
649
[2790]650\subsection{\tcls{SegDataBlock} , \tcls{SwSegDataBlock}}
[3037]651\index{\tcls{SegDataBlock}} \index{\tcls{SwSegDataBlock}}
652%%
[2790]653\begin{figure}[hbt]
654\dclsccc{AnyDataObj}{\tcls{SegDBInterface}}{ \tcls{SegDataBlock} }
655\dclscc{\tcls{SegDBInterface}}{ \tcls{SwSegDataBlock} }
656\end{figure}
657\begin{itemize}
[3415]658\item[\rond] \tcls{SegDataBlock} handles arrays of object of
[2790]659type {\bf T} with reference sharing in memory. The array can be extended
660(increase in array size) with fixed segment size. It implements the interface
[2996]661defined by \tcls{SegDBInterface}.
[3415]662\item[\rond] \tcls{SwSegDataBlock} Implements the same \tcls{SegDBInterface}
[2996]663using a data swapper object. Data swappers implement the interface defined in
664(\tcls{DataSwapperInterface} class. \tcls{SwSegDataBlock} can
665thus be used to handle arrays with very large number of objects.
666These classes handles reference sharing.
[2790]667\end{itemize}
668
[1435]669\newpage
[1299]670\section{Module TArray}
[1362]671\index{\tcls{TArray}}
[1299]672{\bf TArray} module contains template classes for handling standard
673operations on numerical arrays. Using the class {\tt \tcls{TArray} },
674it is possible to create and manipulate up to 5-dimension numerical
[1362]675arrays {\tt (int, float, double, complex, \ldots)}. The include
676file {\tt array.h} declares all the classes and definitions
[1435]677in module TArray. {\bf Array} is a typedef for arrays
[1411]678with double precision floating value elements. \\
679{\tt typedef TArray$<$r\_8$>$ Array ; }
[1299]680
681\begin{figure}[hbt]
682\dclsccc{AnyDataObj}{BaseArray}{\tcls{TArray}}
[1362]683\dclsbb{PPersist}{\tcls{FIO\_TArray}}
[1299]684\end{figure}
685
[2919]686The development of this module started around 1999-2000,
687after evaluation of a number of publicly available
688C++ array hadling packages, including TNT, Lapack++, Blitz++,
689as well as commercial packages from RogueWave (math.h++ \ldots).
690Most of these packages provide interesting functionalities, however,
691not any one package seemed to fulfill most of our requirements.
692\begin{itemize}
693\item Capability to handle {\bf large - multidimensional - dense}
694arrays, for numerical data types. Although we have used templates, for
695data type specialisation, the actual code, apart inline functions is
696not in header files. Instead, we use explicit instanciation, and the
697compiled code for the various numerical types of arrays is the
698library .
699\item The shape and size of the arrays can be defined and changed
700at run time. The classes ensure the memory management of the
701created objets, with reference sharing for the array data.
702The default behaviour of the copy constructor is to share the data,
703avoiding expensive memory copies.
704\item The package provides transparent management of sub-arrays
705and slices, in an intuitive way, somehow similar to what is
706available in Mathlab or Scilab.
707\item The memory organisation for arrays, specially matrices
708(row-major or column major) can be
709controled. This provide compatibility when using existing C or
710Fortran coded numerical libraries.
711\item The classes provide efficient methods to perform basic arithmetic
712and mathematical operations on arrays. In addition, operator overload
713provides intuitive programming for element acces and most basic
714arithmetic operations.
715\item Conversion can be performed between arrays with different
716data types. Copy and arithmetic operations can be done transparently
717between arrays with different memory organisation patterns.
718\item This module does not provide more complex operations
719such as FFT or linear algebra. Additional libraries are used, with interface
720classes for these operations.
721\item ASCII formatted I/O, for printing and read/write operations to/from text files.
722\item Efficient binary I/O for object persistence (PPF format), or import/export
723to other data formats, such as FITS are provided by helper or handler classes.
724\end{itemize}
[1360]725
[1299]726\subsection{Using arrays}
[1362]727\index{Sequence} \index{RandomSequence} \index{RegularSequence}
728\index{EnumeratedSequence}
[1435]729The example below shows basic usage of arrays, creation, initialisation
[1362]730and arithmetic operations. Different kind of {\bf Sequence} objects
[1435]731can be used for initialising arrays.
[1411]732
[1362]733\begin{figure}[hbt]
734\dclsbb{Sequence}{RandomSequence}
735\dclsb{RegularSequence}
736\dclsb{EnumeratedSequence}
737\end{figure}
[1299]738
[1340]739The example below shows basic usage of arrays:
[1414]740\index{\tcls{TArray}}
[1340]741\begin{verbatim}
[1435]742// Creating and initialising a 1-D array of integers
[1362]743TArray<int> ia(5);
744EnumeratedSequence es;
745es = 24, 35, 46, 57, 68;
746ia = es;
747cout << "Array<int> ia = " << ia;
748// 2-D array of floats
749TArray<r_4> b(6,4), c(6,4);
750// Initializing b with a constant
751b = 2.71828;
752// Filling c with random numbers
753c = RandomSequence();
754// Arithmetic operations
755TArray<r_4> d = b+0.3f*c;
756cout << "Array<float> d = " << d;
[1340]757\end{verbatim}
758
[1362]759The copy constructor shares the array data, while the assignment operator
760copies the array elements, as illustrated in the following example:
761\begin{verbatim}
762TArray<int> a1(4,3);
763a1 = RegularSequence(0,2);
764// Array a2 and a1 shares their data
765TArray<int> a2(a1);
766// a3 and a1 have the same size and identical elements
767TArray<int> a3;
768a3 = a1;
769// Changing one of the a2 elements
770a2(1,1,0) = 555;
771// a1(1,1) is also changed to 555, but not a3(1,1)
772cout << "Array<int> a1 = " << a1;
773cout << "Array<int> a3 = " << a3;
774\end{verbatim}
775
[3037]776\subsection{Arithmetic operations}
777The four usual arithmetic operators ({\bf + \, - \, * \, / }) are defined
778to perform constant addition, subtraction, multiplication and division.
779The three operators ({\bf + \, - \, / }) between two arrays of the same type
780are defined to perform element by element addition, subtraction
781and division. In order to avoid confusion with matrix multiplication,
782element by element multiplication is defined by overloading the
783operator {\bf \, \&\& \, }, as shown in the example below:
784\begin{verbatim}
785TArray<int_4> a(4,3), b(4,3), c , d, e;
786a = RegularSequence(1.,1.);
787b = RegularSequence(10.,10.);
788cout << a << b ;
789c = a && b;
790d = c / a;
791e = (c / b) - a;
792cout << c << d << e;
793\end{verbatim}
794
[1362]795\subsection{Matrices and vectors}
796\index{\tcls{TMatrix}} \index{\tcls{TVector}}
797\begin{figure}[hbt]
798\dclsccc{\tcls{TArray}}{\tcls{TMatrix}}{\tcls{TVector}}
799\end{figure}
800Vectors and matrices are 2 dimensional arrays. The array size
801along one dimension is equal 1 for vectors. Column vectors
802have {\tt NCols() = 1} and row vectors have {\tt NRows() = 1}.
[1411]803Mathematical expressions involving matrices and vectors can easily
804be translated into C++ code using {\tt TMatrix} and
805{\tt TVector} objects. {\bf Matrix} and {\bf Vector} are
806typedefs for double precision float matrices and vectors.
807The operator {\bf *} beteween matrices is redefined to
808perform matrix multiplication. One can then write: \\
809\begin{verbatim}
810 // We create a row vector
811 Vector v(1000, BaseArray::RowVector);
812 // Initialize values with a random sequence
813 v = RandomSequence();
814 // Compute the vector length (norm)
815 double norm = (v*v.Transpose()).toScalar();
816 cout << "Norm(v) = " << norm << endl;
817\end{verbatim}
[1362]818
[1414]819This module contains basic array and matrix operations
820such as the Gauss matrix inversion algorithm
[1411]821which can be used to solve linear systems, as illustrated by the
822example below:
[1340]823\begin{verbatim}
[1411]824#include "sopemtx.h"
825// ...
826// Creation of a random 5x5 matrix
827Matrix A(5,5);
828A = RandomSequence(RandomSequence::Flat);
829Vector X0(5);
830X0 = RandomSequence(RandomSequence::Gaussian);
831// Computing B = A*X0
832Vector B = A*X0;
833// Solving the system A*X = B
834Vector X;
835LinSolve(A, B, X);
836// Checking the result
837Vector diff = X-X0;
838cout << "X-X0= " << diff ;
839double min,max;
840diff.MinMax(min, max);
841cout << " Min(X-X0) = " << min << " Max(X-X0) = " << max << endl;
842\end{verbatim}
843
[2919]844{\bf Warning: } The operations defined in {\tt sopemtx.h}, such as
845matrix inversion and linear system solver use a basic Gauss pivot
846algorithm which are not adapted for large matrices ($>\sim 100x100$).
847The services provided in other modules, such as {\bf LinAlg} should
848be preferred in such cases.
849
[1411]850\subsection{Working with sub-arrays and Ranges}
[1414]851\index{Range}
[1411]852A powerful mechanism is included in array classes for working with
853sub-arrays. The class {\bf Range} can be used to specify range of array
854indexes in any of the array dimensions. Any regularly spaced index
855range can be specified, using the {\tt start} and {\tt end} index
856and an optional step (or stride). It is also possible to specify
[2919]857the {\tt start} index and the number of elements.
858\begin{itemize}
859\item {\bf Range::all()} {\tt = Range(Range::firstIndex(), Range::lastIndex())} \\
860return a Range objects representing all valid indexes along the
861corresponding axe.
862\item {\bf Range::first()} {\tt = Range(Range::firstIndex())} \\
863return a Range object representing the first valid index
864\item {\bf Range::last()} {\tt = Range(Range::lastIndex())}
865return a Range object representing the last valid index
866\item {\bf Range(idx)} represents a single index ({\bf = idx})
867\item {\bf Range(first, last)} represents the range of indices
[3415]868{\bf first} $\leq$ index $\leq$ {\bf last}.\\
[2919]869The static method {\tt Range::lastIndex()} can be used
870to specify the last valid index.
871\item {\bf Range(first, last, step)} represents the range of index
872which is equivalent to \\ {\tt for(index=first; index <= last; index += step) }
873\item { \bf Range (first, last, size, step) } the general form can be used
874to specify an index range, using the number of elements.
875It is possible to specify a range of index, ending with the last valid index.
876For example \\
877\hspace*{5mm}
878{\tt Range(Range::lastIndex(), Range::lastIndex(), 3, 2) } \\
879defines the index range: \hspace*{5mm} last-4, last-2, last.
880
[1648]881\begin{center}
882\begin{tabular}{ll}
[3034]883\hline \\
[2919]884\multicolumn{2}{c}{ {\bf Range} {\tt (start, end, size, step) } } \\[2mm]
[1648]885\hline \\
[3034]886{\bf Range} {\tt r(7); } & index range: \hspace{2mm} 7 \\
[2919]887{\bf Range} {\tt r(3,6); } & index range: \hspace{2mm} 3,4,5,6 \\
[3034]888{\bf Range} {\tt r(3,7,2); } & index range: \hspace{2mm} 3,5,7 \\
[2919]889{\bf Range} {\tt r(7,0,3,1); } & index range: \hspace{2mm} 7,8,9 \\
[3034]890{\bf Range} {\tt r(10,0,5,2); } & index range: \hspace{2mm} 10,12,14,16,18 \\[2mm]
891\hline
[1648]892\end{tabular}
893\end{center}
[2919]894\end{itemize}
[1648]895
[2919]896The method {\tt TArray<T>SubArray(Range ...)} can be used
897to extract subarrays and slices. The operator {\tt operator() (Range rx, Range ry ...)}
898is also overloaded for sub-array extraction.
899For matrices, {\tt TMatrix<T>::Row()} and {\tt TMatrix<T>::Column()}
900extract selected matrix rows and columns.
901
902The example illustrates the sub-array extraction using Range objects:
903\begin{verbatim}
904 // Creating and initialising a 2-D (6 x 4) array of integers
905 TArray<int> iaa(6, 4);
906 iaa = RegularSequence(1,2);
907 cout << "Array<int> iaa = \n" << iaa;
908 // We extract a sub-array - data is shared with iaa
909 TArray<int> iae = iaa(Range(1, Range::lastIndex(), 3) ,
910 Range::all(), Range::first() );
911 cout << "Array<int> iae=subarray(iaa) = \n" << iae;
912 // Changing iae elements changes corresponding iaa elements
913 iae = 0;
914 cout << "Array<int> iae=0 --> iaa = \n" << iaa;
915
916\end{verbatim}
917
[1648]918In the following example, a simple low-pass filter, on a one
[1411]919dimensional stream (Vector) has been written using sub-arrays:
920
921\begin{verbatim}
[1340]922// Input Vector containing a noisy periodic signal
923 Vector in(1024), out(1024);
924 in = RandomSequence(RandomSequence::Gaussian, 0., 1.);
925 for(int kk=0; kk<in.Size(); kk++)
926 in(kk) += 2*sin(kk*0.05);
927// Compute the output vector by a simple low pass filter
928 Vector out(1024);
929 int w = 2;
930 for(int k=w; k<in.Size()-w; k++)
931 out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.);
932\end{verbatim}
933
[1648]934\subsection{Input, Output}
935Arrays can easily be saved to, or restored from files in different formats.
936SOPHYA library can handle array I/O to ASCII formatted files, to PPF streams,
937as well as to files in FITS format.
938FITS format input/output is provided through the classes in
[3037]939{\bf FitsIOServer} module. Only arrays with data types
[1648]940supported by the FITS standard can be handled during
941I/O operations to and from FITS streams (See the FitsIOServer section
942for additional details).
943
944\subsubsection{PPF streams}
945
946SOPHYA persistence (PPF streams) handles reference sharing, and multiply
947referenced objects are only written once. A hierarchy of arrays and sub-arrays
948written to a PPF stream is thus completely recovered, when the stream is read.
949The following example illustrates this point:
950\begin{verbatim}
951{
952// Saving an array with a sub-array into a POutPersist file
953Matrix A(3,4);
954A = RegularSequence(10,5);
955// Create a sub-array of A
956Matrix AS = A(Range(1,2), Range(2,3));
957// Save the two arrays to a PPF stream
958POutPersist pos("aas.ppf");
959pos << A << AS;
960}
961{
962// Reading arrays from the previously created PPF file aas.ppf
963PInPersist pis("aas.ppf");
964Matrix B,BS;
965pis >> B >> BS;
966// BS is a sub-array of B, modifying BS changes also B
967BS(1,1) = 98765.;
968cout << " B , BS after BS(1,1) = 98765. "
969 << B << BS << endl;
970}
971\end{verbatim}
972The execution of this sample code creates the file {\tt aas.ppf} and
[2919]973its output is reproduced here. Notice that the array hierarchy is
[1648]974recovered. BS is a sub-array of B, and modifying BS changes also
975the corresponding element in B.
976\begin{verbatim}
977 B , BS after BS(1,1) = 98765.
978
979--- TMatrix<double>(NRows=3, NCols=4) ND=2 SizeX*Y*...= 4x3 ---
98010 15 20 25
98130 35 40 45
98250 55 60 98765
983
984--- TMatrix<double>(NRows=2, NCols=2) ND=2 SizeX*Y*...= 2x2 ---
98540 45
98660 98765
987\end{verbatim}
988
[3417]989{\bf Warning: }
[1648]990There is a drawback in this behaviour: only a single
991copy of an array is written to a file, even if the array is modified,
[3417]992without being resized and written (dumped) again to the same PPF stream.
993However, this behavior can be changed using the {\tt RenewObjId()} method,
994as illustrated below.
[1648]995\begin{verbatim}
996{
997POutPersist pos("mca.ppf");
998TArray<int_4> ia(5,3);
999ia = 8;
[3417]1000pos << ia; // (1)
[1648]1001ia = 16;
[3417]1002pos << ia; // (2) Only a reference to the previously ia array is written
[1648]1003ia = 32;
[3417]1004ia.RenewObjId(); // We change the object Id
1005pos << ia; // (3) The complete array is dumped again
[1648]1006}
1007\end{verbatim}
1008
1009Only a single copy of the data is effectively written to the output
[3417]1010PPF file, corresponding to the value 8 for array elements, for the first two
1011write operations. When we
[1648]1012read the three array from the file mca.ppf, the same array elements
[3417]1013are obtained two times (all elements equal to 8), and a different array is obtained
1014the third time
[1648]1015\begin{verbatim}
1016{
1017PInPersist pis("mca.ppf");
1018TArray<int_4> ib;
1019pis >> ib;
1020cout << " First array read from mca.ppf : " << ib;
1021pis >> ib;
1022cout << " Second array read from mca.ppf : " << ib;
1023pis >> ib;
1024cout << " Third array read from mca.ppf : " << ib;
1025}
1026\end{verbatim}
1027
1028\subsubsection{ASCII streams}
1029
1030The {\bf WriteASCII} method can be used to dump an array to an ASCII
1031formatted file, while the {\bf ReadASCII} method can be used to decode
1032ASCII formatted files. Space or tabs are the possible separators.
1033Complex numbers should be specified as a pair of comma separated
1034real and imaginary parts, enclosed in parenthesis.
1035
1036\begin{verbatim}
1037{
1038// Creating array A and writing it to an ASCII file (aaa.txt)
1039Array A(4,6);
1040A = RegularSequence(0.5, 0.2);
1041ofstream ofs("aaa.txt");
1042A.WriteASCII(ofs);
1043}
1044{
1045// Decoding the ASCII file aaa.txt
1046ifstream ifs("aaa.txt");
1047Array B;
1048sa_size_t nr, nc;
1049B.ReadASCII(ifs,nr,nc);
1050cout << " Array B; B.ReadASCII() from file " << endl;
1051cout << B ;
1052}
1053\end{verbatim}
1054
[3417]1055\subsection{Cast without conversion}
1056Data conversion between arrays with different data type is handled transparently,
1057through the copy constructor or the assignment (=) operator . However, in rare cases,
1058one wants to access the same memory locations, without data type conversion.
1059The template functions defined in {\tt arrctcast.h} can be used to access the same
1060memory locations, by arrays with different data types. The SOPHYA/NDataBlock
1061reference sharing mechanism is effective When using these functions.
1062Notice that the array size or stride may change during these cast operations. \\
1063 {\tt arrctcast.h} has been introduced in version V=2.1 (Nov 2007), and has not been
1064 fully tested for non packed arrays.
1065\begin{verbatim}
1066// We define and initialize a real array :
1067TArray<r_4> fa(5);
1068fa = RegularSequence(1.25,0.5);
1069cout << " fa= " << fa;
1070// We construct an integer array from fa, where the floating point values
1071// are converted to integer values
1072TArray<uint_2> ia(fa);
1073cout << " ia= " << ia;
1074cout << " ia (in hex)= " << hex << ia << dec;
1075// We can also access the fa memory locations interpreted as short integers
1076uint_2 ui2;
1077// Note that sfia size is double the fa size
1078TArray<uint_2> sfia = ArrayCast(fa, ui2);
1079cout << " sfia= " << sfia;
1080cout << " sfia (in hex)= " << hex << sfia << dec;
1081\end{verbatim}
1082One of the most useful case of these array type cast without conversion
1083correspond to accessing the real or imaginary part of a complex array.
1084Two specific template functions {\tt SDRealPart()} and {\tt SDImagPart()}
1085are also defined in {\tt arrctcast.h}. Two other functions {\tt ArrCastR2C()}
1086and {\tt ArrCastC2R() } are also defined for real to complex, and
1087complex to real cast.
1088Their usage is shown in the next paragraph on complex arrays.
[1648]1089
1090\subsection{Complex arrays}
1091The {\bf TArray} module provides few functions for manipulating
1092arrays of complex numbers (single and double precision).
1093These functions are declared in {\tt matharr.h}.
1094\begin{itemize}
1095\item[\bul] Creating a complex array through the specification of the
1096real and imaginary parts.
1097\item[\bul] Functions returning arrays corresponding to real and imaginary
1098parts of a complex array: {\tt real(za) , imag(za) }
[3417]1099({\bf Warning:} Note that the these functions do not provide
[1648]1100shared memory access to real and imaginary parts.)
1101\item[\bul] Functions returning arrays corresponding to the module,
1102phase, and module squared of a complex array:
1103 {\tt phase(za) , module(za) , module2(za) }
1104\end{itemize}
1105
1106\begin{verbatim}
1107 TVector<r_4> p_real(10, BaseArray::RowVector);
1108 TVector<r_4> p_imag(10, BaseArray::RowVector);
1109 p_real = RegularSequence(0., 0.5);
1110 p_imag = RegularSequence(0., 0.25);
1111 TVector< complex<r_4> > zvec = ComplexArray(p_real, p_imag);
1112 cout << " :: zvec= " << zvec;
1113 cout << " :: real(zvec) = " << real(zvec) ;
1114 cout << " :::: imag(zvec) = " << imag(zvec) ;
1115 cout << " :::: module2(zvec) = " << module2(zvec) ;
1116 cout << " :::: module(zvec) = " << module(zvec) ;
1117 cout << " :::: phase(zvec) = " << phase(zvec) ;
1118\end{verbatim}
1119
1120The decoding of complex numbers from an ASCII formatted stream
1121is illustrated by the next example. As mentionned already,
1122complex numbers should be specified as a pair of comma separated
1123real and imaginary parts, enclosed in parenthesis.
1124
1125\begin{verbatim}
1126csh> cat zzz.txt
1127(1.,-1) (2., 2.5) -3. 12.
1128-24. (-6.,7.) 14.2 (8.,64.)
1129
1130// Decoding of complex numbers from an ASCII file
1131// Notice that the << operator can be used instead of ReadASCII
1132TArray< complex<r_4> > Z;
1133ifstream ifs("zzz.txt");
1134ifs >> Z;
1135cout << " TArray< complex<r_4> > Z from file zzz.txt " << Z ;
1136\end{verbatim}
1137
[3417]1138\noindent {\bf Shared data access :} It is possible to access a complex array
1139elements (real and imaginary parts) through the template functions defined
1140in {\tt arrctcast.h} and discussed above. The example below shows how to use
1141these functions.
[1648]1142
[3417]1143\begin{verbatim}
1144// We define a complex array
1145TArray< complex<r_4> > za(5);
1146cout << " za= " << za;
1147// And two real arrays, corresponding to the real and imaginary parts
1148TArray<r_4> rza = SDRealPart(za);
1149TArray<r_4> iza = SDImagPart(za);
1150// We initialize the real and imaginary parts of the complex array
1151rza = RegularSequence(10.,2.);
1152iza = RegularSequence(5.,0.75);
1153cout << " rza=..., iza=... ----> za = " << za;
1154// The complex array seen as a real array (double size)
1155TArray<r_4> aza = ArrCastC2R(za);
1156cout << " za --> aza= " << aza;
1157\end{verbatim}
1158
[1360]1159\subsection{Memory organisation}
1160{\tt \tcls{TArray} } can handle numerical arrays with various memory
1161organisation, as long as the spacing (steps) along each axis is
1162regular. The five axis are labeled X,Y,Z,T,U. The examples below
1163illustrates the memory location for a 2-dimensional, $N_x=4 \times N_y=3$.
1164The first index is along the X axis and the second index along the Y axis.
1165\begin{verbatim}
1166 | (0,0) (0,1) (0,2) (0,3) |
1167 | (1,0) (1,1) (1,2) (1,3) |
1168 | (2,0) (2,1) (2,2) (2,3) |
1169\end{verbatim}
1170In the first case, the array is completely packed
1171($Step_X=1, Step_Y=N_X=4$), with zero offset,
1172while in the second case, $Step_X=2, Step_Y=10, Offset=10$:
1173\begin{verbatim}
1174 | 0 1 2 3 | | 10 12 14 16 |
1175Ex1 | 4 5 6 7 | Ex2 | 20 22 24 26 |
1176 | 8 9 10 11 | | 30 32 34 36 |
1177\end{verbatim}
1178
1179For matrices and vectors, an optional argument ({\tt MemoryMapping})
1180can be used to select the memory mapping, where two basic schemes
1181are available: \\
1182{\tt CMemoryMapping} and {\tt FortranMemoryMapping}. \\
1183In the case where {\tt CMemoryMapping} is used, a given matrix line
1184is packed in memory, while the columns are packed when
1185{\tt FortranMemoryMapping} is used. The first index when addressing
1186the matrix elements (line number index) runs along
1187the Y-axis if {\tt CMemoryMapping} is used, and along the X-axis
1188in the case of {\tt FortranMemoryMapping}.
1189Arithmetic operations between matrices
1190with different memory organisation is allowed as long as
1191the two matrices have the same sizes (Number of rows and columns).
1192The following code example and the corresponding output illustrates
[1414]1193these two memory mappings. The {\tt \tcls{TMatrix}::TransposeSelf() }
1194method changes effectively the matrix memory mapping, which is also
1195the case of {\tt \tcls{TMatrix}::Transpose() } method without argument.
1196
[1360]1197\begin{verbatim}
1198TArray<r_4> X(4,2);
1199X = RegularSequence(1,1);
1200cout << "Array X= " << X << endl;
1201TMatrix<r_4> X_C(X, true, BaseArray::CMemoryMapping);
1202cout << "Matrix X_C (CMemoryMapping) = " << X_C << endl;
1203TMatrix<r_4> X_F(X, true, BaseArray::FortranMemoryMapping);
1204cout << "Matrix X_F (FortranMemoryMapping) = " << X_F << endl;
1205\end{verbatim}
1206This code would produce the following output (X\_F = Transpose(X\_C)) :
1207\begin{verbatim}
1208Array X=
1209--- TArray<f> ND=2 SizeX*Y*...= 4x2 ---
12101, 2, 3, 4
12115, 6, 7, 8
1212
1213Matrix X_C (CMemoryMapping) =
1214--- TMatrix<f>(NRows=2, NCols=4) ND=2 SizeX*Y*...= 4x2 ---
12151, 2, 3, 4
12165, 6, 7, 8
1217
1218Matrix X_F (FortranMemoryMapping) =
1219--- TMatrix<f>(NRows=4, NCols=2) ND=2 SizeX*Y*...= 4x2 ---
12201, 5
12212, 6
12223, 7
12234, 8
1224\end{verbatim}
1225
[988]1226\newpage
1227
[1299]1228\section{Module HiStats}
1229\begin{figure}[hbt]
[2996]1230\dclsbb{AnyDataObj}{Histo}
1231\dclscc{Histo}{HProf}
[1299]1232\dclsbb{AnyDataObj}{Histo2D}
[3137]1233\dclsbb{AnyDataObj}{HistoErr}
1234\dclsbb{AnyDataObj}{Histo2DErr}
[1299]1235\caption{partial class diagram for histograms and ntuples}
1236\end{figure}
1237
[1347]1238{\bf HiStats} contains classes for creating, filling, printing and
1239doing various operations on one or two dimensional histograms
1240{\tt Histo} and {\tt Histo2D} as well as profile histograms {\tt HProf}. \\
[3006]1241This module also contains {\tt NTuple} and {\tt DataTable} which are
1242more or less the same as the binary or ascii FITS tables.
[1347]1243
[3006]1244\subsection{Histograms}
1245\subsubsection{1D Histograms}
[1414]1246\index{Histo}
[1347]1247For 1D histograms, various numerical methods are provided such as
1248computing means and sigmas, finding maxima, fitting, rebinning,
1249integrating \dots \\
[1435]1250The example below shows creating and filling a one dimensional histogram
1251of 100 bins from $-5.$ to $+5.$ to create a Gaussian normal distribution
[1347]1252with errors~:
1253\begin{verbatim}
1254#include "histos.h"
1255// ...
1256Histo H(-0.5,0.5,100);
1257H.Errors();
1258for(int i=0;i<25000;i++) {
1259 double x = NorRand();
1260 H.Add(x);
1261}
1262H.Print(80);
1263\end{verbatim}
1264
[3006]1265\subsubsection{2D Histograms}
[1414]1266\index{Histo2D}
[1347]1267Much of these operations are also valid for 2D histograms. 1D projection
1268or slices can be set~:
1269\begin{verbatim}
1270#include "histos2.h"
1271// ...
[1414]1272Histo2D H2(-1.,1.,100,0.,60.,50);
[1347]1273H2.SetProjX(); // create the 1D histo for X projection
1274H2.SetBandX(25.,35.); // create 1D histo projection for 25.<y<35.
1275H2.SetBandX(35.,45.); // create 1D histo projection for 35.<y<45.
1276H2.SetBandX(40.,55.); // create 1D histo projection for 40.<y<55.
1277//... fill H2 with what ever you want
1278H2.Print();
1279Histo *hx = H2.HProjX();
1280 hx->Print(80);
1281Histo *hbx2 = HBandX(1); // Get the second X band (35.<y<45.)
1282 hbx2->Print(80);
1283\end{verbatim}
1284
[3006]1285\subsubsection{Profile Histograms}
[1414]1286\index{HProf}
1287Profiles histograms {\bf HProf} contains the mean and the
1288sigma of the distribution
[1347]1289of the values filled in each bin. The sigma can be changed to
1290the error on the mean. When filled, the profile histogram looks
1291like a 1D histogram and much of the operations that can be done on 1D histo
1292may be applied onto profile histograms.
1293
[3137]1294\subsubsection{Histograms HistoErr and Histo2DErr}
[3061]1295\index{HistoErr}
[3137]1296The {\bf HistoErr} are basic histograms where the number of entries for each bin is kept.
1297Methods to compute of the mean and the variance in each bin are provided.
1298The {\bf Histo2DErr} is the same for $2$ dimensions.
[3061]1299
[1435]1300\subsection{Data tables (tuples)}
[3006]1301\begin{figure}[hbt]
1302\dclsbb{AnyDataObj}{NTuple}
1303\dclsccc{AnyDataObj}{BaseDataTable}{DataTable}
1304\dclscc{BaseDataTable}{SwPPFDataTable}
1305\end{figure}
1306
1307\subsubsection{NTuple}
[1414]1308\index{NTuple}
[3048]1309{\bf NTuple} are memory resident tables of 32 or 64 bits floating values
[2790]1310(float/double).They are arranged in columns. Each line is often called an event.
[1347]1311These objects are frequently used to analyze data.
[2790]1312The piapp graphicals tools can plot a column against an other one
[1347]1313with respect to various selection cuts. \\
1314Here is an example of creation and filling~:
1315\begin{verbatim}
1316#include "ntuple.h"
1317#include "srandgen.h"
1318// ...
1319char* nament[4] = {"i","x","y","ey"};
1320r_4 xnt[4];
1321NTuple NT(4,nament);
1322for(i=0;i<5000;i++) {
1323 xnt[0] = i+1;
1324 xnt[1] = 5.*drandpm1(); // a random value between -5 and +5
1325 xnt[2] = 100.*exp(-0.5*xnt[1]*xnt[1]) + 1.;
1326 xnt[3] = sqrt(xnt[2]);
1327 xnt[2] += xnt[3] * NorRand(); // add a random gaussian error
1328 NT.Fill(xnt);
1329}
1330\end{verbatim}
1331
[3048]1332{\bf XNTuple} provide additional functionalities, compared to NTuple. However,
1333this class is deprecated and superseded by classes inheriting from BaseDataTable.
1334It is only kept for backward compatibility and should not be used anymore.
1335Use DataTable and SwPPFDataTable instead.
[2790]1336Object of type XNTuple handle various types
[1414]1337of column values (double,float,int,string,...) and can handle
1338very large data sets, through swap space on disk.
[2790]1339
[3006]1340\subsubsection{DataTables}
[3034]1341\label{datatables}
[2790]1342\index{DataTable}
1343The class {\bf DataTable} extends significantly the functionalities provided by
1344NTuple. DataTable is a memory resident implementation of the interface
1345{\bf BaseDataTable } which organizes the data as a 2-D table. User can define
1346the name and data type of each column. Data is added to the table as rows.
1347The table is extended as necessary when adding rows.
1348The sample code below shows an example of DataTable usage :
[1414]1349\begin{verbatim}
[2790]1350 #include "datatable.h"
1351 // ...
[3037]1352 {
[2790]1353 DataTable dt(64);
1354 dt.AddFloatColumn("X0_f");
1355 dt.AddFloatColumn("X1_f");
1356 dt.AddDoubleColumn("X0X0pX1X1_d");
1357 double x[5];
1358 for(int i=0; i<63; i++) {
1359 x[0] = (i/9)-4.; x[1] = (i/9)-3.; x[2] = x[0]*x[0]+x[1]*x[1];
1360 dt.AddLine(x);
1361 }
1362 // Printing table info
1363 cout << dt ;
1364 // Saving object into a PPF file
1365 POutPersist po("dtable.ppf");
1366 po << dt ;
[3037]1367 }
[1414]1368\end{verbatim}
[1347]1369
[2790]1370
1371\index{SwPPFDataTable}
1372The class {\bf SwPPFDataTable} implements the BaseDataTable interface
1373using segmented data blocks with swap on PPF streams. Very large data sets
[3048]1374can be created and manipulated through this class. A similar class
1375SwFitsDataTable (\ref{SwFitsDataTable}), using
1376FITS files as swap space is also provided in the FitsIOServer module.
[2790]1377
[3006]1378\index{DataTableRow}
1379The class {\bf DataTableRow } is an auxiliary class which simplifies the manipulation
1380of BaseDataTable object rows.
[3034]1381The example below show how to create and filling a table, using a PPF stream as
1382swap space. In addition, we have used a {\tt DataTableRow} to prepare data
1383for each table line.
[3006]1384\begin{verbatim}
[3034]1385 #include "swppfdtable.h"
[3006]1386 // ...
[3034]1387 {
1388 // ---------- Create an output PPF stream (file)
1389 POutPersist po("swdtable.ppf");
1390 // ------------------
1391 // Create a table with 3 columns, using the above stream as swap space
1392 SwPPFDataTable dtrow(po, 64);
[3006]1393 dtrow.AddStringColumn("sline");
1394 dtrow.AddIntegerColumn("line");
[3034]1395 dtrow.AddDateTimeColumn("datime");
1396 //
[3006]1397 TimeStamp ts, ts2; // Initialize current date and time
1398 string sline;
[3034]1399 //---- Create a table row with the required structure
[3006]1400 DataTableRow row = dtrow.EmptyRow();
[3034]1401 // ----- Fill the table
1402 for(int k = 0; k<2500; k++) {
[3006]1403 sline = "L-";
1404 sline += (string)MuTyV(k);
1405 row["sline"] = sline;
1406 row[1] = k;
1407 ts2.Set(ts.ToDays()+(double)k);
1408 row["datime"] = ts2;
1409 dtrow.AddRow(row);
1410 }
[3034]1411 //------ Write the table itself to the stream, before closing the file
1412 po << PPFNameTag("SwTable") << dtrow;
1413 }
[3006]1414\end{verbatim}
[3034]1415%%
1416The previously created table can easily be read in, as shown below:
1417%%
1418\begin{verbatim}
1419 #include "swppfdtable.h"
1420 // ...
1421 {
1422 // ------ Create the input PPF stream (file)
1423 PInPersist pin("swdtable.ppf");
1424 // ------ Read in the SwPPFDataTable object
1425 SwPPFDataTable dtr;
1426 pin >> PPFNameTag("SwTable") >> dtr;
1427 // ---- Create a table row with the required structure
1428 DataTableRow row = dtr.EmptyRow();
1429 // ---- Acces and print two of the table rows :
1430 cout << dtr.GetRow(6, row) << endl;
1431 cout << dtr.GetRow(17, row) << endl;
1432 }
1433\end{verbatim}
1434
[1362]1435\subsection{Writing, viewing \dots }
[1347]1436
[1435]1437All these objects have been design to be written to or read from a persistent file.
[1347]1438The following example shows how to write the previously created objects
1439into such a file~:
1440\begin{verbatim}
1441//-- Writing
1442{
1443char *fileout = "myfile.ppf";
1444string tag;
1445POutPersist outppf(fileout);
1446tag = "H"; outppf.PutObject(H,tag);
1447tag = "H2"; outppf.PutObject(H2,tag);
1448tag = "NT"; outppf.PutObject(NT,tag);
1449} // closing ``}'' destroy ``outppf'' and automatically close the file !
1450\end{verbatim}
1451
1452Sophya graphical tools (spiapp) can automatically display and operate
1453all these objects.
1454
[1414]1455\newpage
[1299]1456\section{Module NTools}
1457
[1347]1458This module provides elementary numerical tools for numerical integration,
1459fitting, sorting and ODE solving. FFTs are also provided (Mayer,FFTPack).
1460
1461\subsection{Fitting}
[1435]1462\index{Fitting} \index{Minimisation}
[1347]1463Fitting is done with two classes {\tt GeneralFit} and {\tt GeneralFitData}
1464and is based on the Levenberg-Marquardt method.
[1414]1465\index{GeneralFit} \index{GeneralFitData}
[1347]1466GeneralFitData is a class which provide a description of the data
1467to be fitted. GeneralFit is the fitter class. Parametrized functions
1468can be given as classes which inherit {\tt GeneralFunction}
1469or as simple C functions. Classes of pre-defined functions are provided
1470(see files fct1dfit.h and fct2dfit.h). The user interface is very close
1471from that of the CERN {\tt Minuit} fitter.
1472Number of objects (Histo, HProf \dots ) are interfaced with GeneralFit
1473and can be easily fitted. \\
1474Here is a very simple example for fitting the previously created NTuple
[1435]1475with a Gaussian~:
[1347]1476\begin{verbatim}
1477#include "fct1dfit.h"
1478// ...
1479
1480// Read from ppf file
1481NTuple nt;
1482{
1483PInPersist pis("myfile.ppf");
1484string tag = "NT"; pis.GetObject(nt,tag);
1485}
1486
1487// Fill GeneralData
1488GeneralData mGdata(nt.NEntry());
1489for(int i=0; i<nt.NEntry(); i++)
1490 mGdata.AddData1(xnt[1],xnt[2],xnt[3]); // Fill x, y and error on y
1491mGData.PrintStatus();
1492
1493// Function for fitting : y = f(x) + noise
1494Gauss1DPol mFunction; // gaussian + constant
1495
1496// Prepare for fit
1497GeneralFit mFit(&mFunction); // create a fitter for the choosen function
1498mFit.SetData(&mGData); // connect data to the fitter
1499
[1435]1500// Set and initialise the parameters (that's non-linear fitting!)
[1347]1501// (num par, name, guess start, step, [limits min and max])
1502mFit.SetParam(0,"high",90.,1..);
1503mFit.SetParam(1,"xcenter",0.05,0.01);
1504mFit.SetParam(2,"sigma",sig,0.05,0.01,10.);
1505 // Give limits to avoid division by zero
1506mFit.SetParam(3,"constant",0.,1.);
1507
1508// Fit and print result
1509int rcfit = mFit.Fit();
1510mFit.PrintFit();
1511if(rcfit>0) {)
1512 cout<<"Reduce_Chisquare = "<<mFit.GetChi2Red()
1513 <<" nstep="<<mFit.GetNStep()<<" rc="<<rcfit<<endl;
1514} else {
1515 cout<<"Fit_Error, rc = "<<rcfit<<" nstep="<<mFit.GetNStep()<<endl;
1516 mFit.PrintFitErr(rcfit);
1517}
1518
1519// Get the result for further use
1520TVector<r_8> ParResult = mFit.GetParm();
1521cout<<ParResult;
1522\end{verbatim}
1523
1524Much more usefull possibilities and detailed informations might be found
1525in the HTML pages of the Sophya manual.
1526
[1350]1527\subsection{Polynomial}
[1414]1528\index{Polynomial} \index{Poly} \index{Poly2}
[1350]1529Polynomials of 1 or 2 variables are supported ({\tt Poly} and {\tt Poly2}).
1530Various operations are supported~:
1531\begin{itemize}
1532\item elementary operations between polynomials $(+,-,*,/) $
1533\item setting or getting coefficients
1534\item computing the value of the polynomial for a given value
1535 of the variable(s),
1536\item derivating
1537\item computing roots (degre 1 or 2)
1538\item fitting the polynomial to vectors of data.
1539\end{itemize}
1540Here is an example of polynomial fitting~:
1541\begin{verbatim}
1542#include "poly.h"
1543// ...
1544Poly pol(2);
1545pol[0] = 100.; pol[1] = 0.; pol[2] = 0.01; // Setting coefficients
1546TVector<r_8> x(100);
1547TVector<r_8> y(100);
1548TVector<r_8> ey(100);
1549for(int i=0;i<100;i++) {
1550 x(i) = i;
1551 ey(i) = 10.;
1552 y(i) = pol((double) i) + ey(i)*NorRand();
1553 ey(i) *= ey(i)
1554}
1555
1556TVector<r_8> errcoef;
1557Poly polfit;
1558polfit.Fit(x,y,ey,2,errcoef);
1559
1560cout<<"Fit Result"<<polfit<<endl;
1561cout<<"Errors :"<<errcoef;
1562\end{verbatim}
1563
1564Similar operations can be done on polynomials with 2 variables.
1565
[1435]1566\subsection{Integration, Differential equations}
1567\index{Integration}
1568The NTools module provide also simple classes for numerical integration
1569of functions and differential equations.
1570\begin{figure}[hbt]
1571\dclsbb{Integrator}{GLInteg}
1572\dclsb{TrpzInteg}
1573\end{figure}
1574
1575\index{GLInteg} \index{TrpzInteg}
1576{\bf GLInteg} implements the integration through Gauss-Legendre method
1577and {\bf TrpzInteg} implements trapeze integration. For {\bf TrpzInteg},
1578number of steps specify the number of trapeze, and integration step,
1579their width.
1580The sample code below illustrates the use of TrpzInteg class:
1581\begin{verbatim}
1582#include "integ.h"
1583// ......................................................
1584// Function to be integrated
1585double myf(double x)
1586{
1587// Simple a x + b x^2 (a=2 b=3)
1588return (x*(2.+3.*x));
1589}
1590// ......................................................
1591
1592// Compute Integral(myf, 2., 5.) between xmin=2., xmax=5.
1593TrpzInteg trpz(myf, 2., 5.);
1594// We specify an integration step
1595trpz.DX(0.01);
1596// The integral can be computed as trpz.Value()
1597double myf_integral = trpz.Value();
1598// We could have used the cast operator :
1599cout << "Integral[myf, 2., 5.]= " << (double)trpz << endl;
1600// Limits can be specified through ValueBetween() method
1601cout << "Integral[myf, 0., 4.]= " << trpz.ValueBetween(0.,4.) << endl;
1602\end{verbatim}
1603
1604\subsection{Fourier transform (FFT)}
1605\index{FFT} \index{FFTPackServer}
1606An abstract interface for performing FFT operations is defined by the
1607{\bf FFTServerInterface} class. The {\bf FFTPackSever} class implements
1608one dimensional FFT, on real and complex data. FFTPackServer uses an
1609adapted and extended version of FFTPack (available from netlib),
1610translated in C, and can operate on single and double precision
1611({\tt float, double}) data.
1612
1613The sample code below illustrates the use of FFTServers:
1614\begin{verbatim}
1615#include "fftpserver.h"
1616 // ...
1617TVector<r_8> in(32);
1618TVector< complex<r_8> > out;
1619in = RandomSequence();
1620FFTPackServer ffts;
1621ffts.setNormalize(true); // To have normalized transforms
1622cout << " FFTServer info string= " << ffts.getInfo() << endl;
1623cout << "in= " << in << endl;
1624cout << " Calling ffts.FFTForward(in, out) : " << endl;
1625ffts.FFTForward(in, out);
1626cout << "out= " << out << endl;
1627\end{verbatim}
1628
[2278]1629% \newpage
1630\section{Module SUtils}
1631Some utility classes and C/C++ string manipulation functions are gathered
1632in {\bf SUtils} module.
1633\subsection{Using DataCards}
1634\index{DataCards}
1635The {\bf DataCards} class can be used to read parameters from a file.
1636Each line in the file starting with \@ defines a set of values
1637associated with a keyword. In the example below, we read the
1638parameters corresponding with the keyword {\tt SIZE} from the
1639file {\tt ex.d}. We suppose that {\tt ex.d} contains the line: \\
1640{\tt @SIZE 400 250} \\
1641\begin{verbatim}
1642#include "datacards.h"
1643// ...
1644// Initialising DataCards object dc from file ex.d
1645DataCards dc( "ex.d" );
1646// Getting the first and second parameters for keyword size
1647// We define a default value 100
1648int size_x = dc.IParam("SIZE", 0, 100);
1649int size_y = dc.IParam("SIZE", 1, 100);
1650cout << " size_x= " << size_x << " size_y= " << size_y << endl;
1651\end{verbatim}
1652
1653\section{Module SysTools}
1654The {\bf SysTools} module contains classes implementing interface to some
[3015]1655OS specific services, such as thread creation and management, dynamic loading and
1656resource usage information. For example, yhe class {\bf Periodic} provides the
1657necessary services needed to implement the execution of a periodic action.
1658
1659\subsection{Resource usage (CPU, memory \ldots) }
1660 The class {\bf ResourceUsage} \index{ResourceUsage}
[3037]1661and {\bf Timer} \index{Timer} provides access to information
[2304]1662about various resource usage (memory, CPU, ...).
[3037]1663The class {\bf Timer} \index{time (CPU, elapsed)} and c-functions
[3015]1664{\tt InitTim() , PrtTim(const char * Comm) } can be used to print
1665the amount of CPU and elapsed time in programs.
[2790]1666
[3015]1667The following sample code illustrates the use of {\bf ResourceUsage} :
1668\begin{verbatim}
1669 // How to check resource usage for a given part of the program
1670 ResourceUsage res;
1671 // --- Part of the program to be checked : Start
1672 // ...
1673 res.Update();
1674 cout << " Memory size increase (KB):" << res.getDeltaMemorySize() << endl;
1675 cout << " Resource usage info : \n" << res << endl;
1676\end{verbatim}
1677
[2790]1678\subsection{Thread management classes}
[3037]1679\index{ZThread} \index{ZMutex}
1680A basic interface to POSIX threads is also provided
[3015]1681through the \index{threads} {\bf ZThread}, {\bf ZMutex} and {\bf ZSync}
1682classes. The best way to use thread management classes is by inheriting
1683from {\bf ZThread} and redefining the {\tt run() } method.
1684It is also possible to use the default {\tt run() } implementation and associate
1685a function to perform the action, as in the example below :
1686\begin{verbatim}
1687 // The functions to perform computing
1688 void fun1(void *arg) { }
1689 void fun2(void *arg) { }
1690 // ...
1691 ZThread zt1;
1692 zt1.setAction(fun1, arg[1]);
1693 ZThread zt2;
1694 zt2.setAction(fun2, arg[1]);
1695 cout << " Starting threads ... " << endl;
1696 zt1.start();
1697 zt2.start();
1698 cout << " Waiting for threads to end ... " << endl;
1699 zt1.join();
1700 zt2.join();
1701\end{verbatim}
1702The classes {\bf ZMutex} \index{mutex} and {\bf ZSync} can be used
1703to perform synchronisation and signaling between threads.
[2790]1704
1705\subsection{Dynamic linker and C++ compiler classes}
[2278]1706\index{PDynLinkMgr}
1707The class {\bf PDynLinkMgr} can be used for managing shared libraries
1708at run time. The example below shows the run time linking of a function:\\
1709{\tt extern "C" { void myfunc(); } } \\
1710\begin{verbatim}
1711#include "pdlmgr.h"
1712// ...
1713string soname = "mylib.so";
1714string funcname = "myfunc";
1715PDynLinkMgr dyl(soname);
1716DlFunction f = dyl.GetFunction(funcname);
1717if (f != NULL) {
1718// Calling the function
1719 f();
1720}
1721\end{verbatim}
1722
1723\index{CxxCompilerLinker}
[2790]1724The {\bf CxxCompilerLinker} class provides the services to compile C++ code and building
[2278]1725shared libraries, using the same compiler and options which have
1726been used to create the SOPHYA shared library.
1727The sample program below illustrates using this class to build
1728the shared library (myfunc.so) from the source file myfunc.cc :
1729\begin{verbatim}
1730#include "cxxcmplnk.h"
1731// ...
1732string flnm = "myfunc.cc";
1733string oname, soname;
1734int rc;
1735CxxCompilerLinker cxx;
1736// The Compile method provides a default object file name
1737rc = cxx.Compile(flnm, oname);
1738if (rc != 0 ) { // Error when compiling ... }
1739// The BuildSO method provides a default shared object file name
1740rc = cxx.BuildSO(oname, soname);
1741if (rc != 0 ) { // Error when creating shared object ... }
1742\end{verbatim}
1743
[2790]1744\subsection{Command interpreter}
1745The class {\bf Commander} can be used in interactive programs to provide
1746c-shell like command interpreter and scripting capabilties.
1747Arithmetic expression evaluation is implemented through the {\bf CExpressionEvaluator}
1748and {\bf RPNExpressionEvaluator} classes.
[3015]1749The command language provides variable manipulation through the usual
1750{\tt \$varname} vector variable and arithmetic expression extensions, as well
1751as the control and test blocs.
1752\begin{verbatim}
1753#include "commander.h"
1754...
1755Commander cmd;
1756char* ss[3] = {"foreach f ( AA bbb CCCC ddddd )", "echo $f" , "end"};
1757for(int k=0; k<3; k++) {
1758 string line = ss[k];
1759 cmd.Interpret(line);
1760}
1761\end{verbatim}
[2790]1762
[1435]1763\newpage
1764\section{Module SkyMap}
1765\begin{figure}[hbt]
1766\dclsbb{AnyDataObj}{PixelMap}
1767\dclsccc{PixelMap}{Sphericalmap}{SphereHEALPix}
1768\dclsc{SphereThetaPhi}
[3006]1769\dclsc{SphereECP}
[1435]1770\dclsb{LocalMap}
[3006]1771\caption{partial class diagram for spherical map classes in Sophya}
[1435]1772\end{figure}
1773The {\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.
1774\subsection {Spherical maps}
[3006]1775SkyMap module provides three kinds of complete ($4 \pi$) spherical maps according to the
1776pixelization scheme.
1777SphereHEALPix represents spheres pixelized following the HEALPIix algorithm (E. Hivon, K. Gorski)
[1435]1778\footnote{see the HEALPix Homepage: http://www.eso.org/kgorski/healpix/ }
1779, 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) :
1780\index{\tcls{SphereHEALPix}}
1781
1782\begin{verbatim}
1783#include "spherehealpix.h"
1784// ...
1785SphereHEALPix<double> sph(8);
1786for (int k=0; k< sph.NbPixels(); k++) sph(k) = (double)(10*k);
1787\end{verbatim}
1788
1789SphereThetaPhi is used in a similar way with an argument representing number of slices in theta (Euler angle) for an hemisphere.
1790\index{\tcls{SphereThetaPhi}}
[3006]1791The SphereECP class correspond to the cylindrical projection and can be used for representing
1792partial or full spherical maps. However, it has the disadvantage of having non uniform pixel
1793size.
1794\index{\tcls{SphereECP}}
[1435]1795
1796\subsection {Local maps}
1797\index{\tcls{LocalMap}}
1798A 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).
1799
1800Internally, 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(...))
1801
1802The 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).
1803\begin{verbatim}
1804#include "localmap.h"
1805//..............
1806 LocalMap<r_4> locmap(4,5);
1807 for (int k=0; k<locmap.NbPixels();k++) locmap(k)=10.*k;
1808 locmap.SetOrigin();
1809 locmap.SetSize(30.,30.);
1810\end{verbatim}
1811
1812\subsection{Writing, viewing \dots }
1813
1814All these objects have been design to be written to or read from a persistant file.
1815The following example shows how to write the previously created objects
1816into such a file~:
1817\begin{verbatim}
1818//-- Writing
1819
1820#include "fiospherehealpix.h"
1821//................
1822
1823char *fileout = "myfile.ppf";
1824POutPersist outppf(fileout);
1825FIO_SphereHEALPix<r_8> outsph(sph);
1826outsph.Write(outppf);
1827FIO_LocalMap<r_8> outloc(locmap);
1828outloc.Write(outppf);
1829// It is also possible to use the << operator
1830POutPersist os("sph.ppf");
1831os << outsph;
1832os << outloc;
1833\end{verbatim}
1834
1835Sophya graphical tools (spiapp) can automatically display and operate
1836all these objects.
1837
1838\newpage
[3015]1839\section{Samba and SkyT}
1840\subsection{Samba}
[1414]1841\index{Spherical Harmonics}
1842\index{SphericalTransformServer}
[1435]1843The 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...
[1387]1844\begin{verbatim}
1845#include "skymap.h"
1846#include "samba.h"
1847....................
1848
1849// Generate input spectra a + b* l + c * gaussienne(l, 50, 20)
1850int lmax = 92;
1851Vector clin(lmax);
1852for(int l=0; l<lmax; l++) {
1853 double xx = (l-50.)/10.;
1854 clin(l) = 1.e-2 -1.e-4*l + 0.1*exp(-xx*xx);
1855}
1856
1857// Compute map from spectra
1858SphericalTransformServer<r_8> ylmserver;
1859int m = 128; // HealPix pixelisation parameter
1860SphereHEALPix<r_8> map(m);
1861ylmserver.GenerateFromCl(map, m, clin, 0.);
1862// Compute power spectrum from map
1863Vector clout = ylmserver.DecomposeToCl(map, lmax, 0.);
1864\end{verbatim}
1865
[3015]1866\subsection{Module SkyT}
[3037]1867\index{RadSpectra} \index{SpectralResponse}
[1435]1868The SkyT module is composed of two types of classes:
1869\begin{itemize}
1870\item{} one which corresponds to an emission spectrum of
1871radiation, which is called RadSpectra
1872\item{} one which corresponds to the spectral response
1873of a given detector (i.e. corresponding to a detector
1874filter in a given frequency domain), which is called
1875SpectralResponse.
1876\end{itemize}
1877\begin{figure}[hbt]
1878\dclsbb{RadSpectra}{RadSpectraVec}
1879\dclsb{BlackBody}
1880\dclsccc{AnyDataObj}{SpectralResponse}{SpecRespVec}
1881\dclsc{GaussianFilter}
1882\caption{partial class for SkyT module}
1883\end{figure}
[1299]1884
[1435]1885\begin{verbatim}
1886#include "skyt.h"
1887// ....
1888// Compute the flux from a blackbody at 2.73 K through a square filter
1889BlackBody myBB(2.73);
1890// We define a square filter from 100 - 200 GHz
1891SquareFilter mySF(100,200);
1892// Compute the filtered integrated flux :
1893double flux = myBB.filteredIntegratedFlux(mySF);
1894\end{verbatim}
1895
1896A more detailed description of SkyT module can be found in:
1897{\it The SkyMixer (SkyT and PMixer modules) - Sophya Note No 2. }
1898available also from Sophya Web site.
1899
1900\newpage
[1387]1901\section{Module FitsIOServer}
[3037]1902This module provides classes for handling file input-output in FITS
1903\footnote{http://heasarc.gsfc.nasa.gov/docs/software/fitsio/fitsio.html}
1904format using the cfitsio library. Its
[3015]1905design is similar to the SOPHYA persistence (see Module BaseTools).
1906Delegate classes or handlers perform the actual read/write from/to fits files.
[3037]1907\par
[3015]1908Compared to the SOPHYA native persistence (PPF format),
1909FITS format has the advantage of being used extensively, and handled
1910by a many different software tools. It is a de facto standard in
1911astronomy and astrophysics.
1912However, FITS lacks some of the features present in SOPHYA PPF, and although
1913many SOPHYA objects can be saved in FITS files, FITS persistence has
1914some limitations. For example, FITS does not currently handle complex arrays.
[3037]1915\subsection{FITS streams}
1916\index{FITS} \index{FitsInOutFile}
1917%%
[3015]1918The class {\bf FitsInOutFile} can be seen as a wrapper class for the cfitsio library functions.
1919This class has been introduced in 2005 (V=1.9), when the module has been
1920extensively changed. In order to keep backward compatibility, the old fits wrapper
[3037]1921classes ({\bf FitsFile, FitsInFile, FitsOutFile}) has been changed to inherit from
[3015]1922 {\bf FitsInOutFile}. The use of class FitsFile and specific services of these old classes
1923 should be avoided, but FitsInFile, FitsOutFile can be safely considered a specialisation
1924 of FitsInOutFile for read/input and write/output operations respectively.
[3037]1925 Most c-fitsio errors are converted to an exception: {\bf FitsIOException}.
1926 \par
1927 File names are passed to cfitsio library. It is thus possible to use cfitsio file name conventions,
1928 such as {\bf ! } as the first character, for overwriting existing files (when creating files).
[3015]1929 The diagram below shows the class hierarchy for cfitsio wrapper classes.
[1387]1930\begin{figure}[hbt]
[3015]1931\dclsa{FitsInOutFile}
1932\dclscc{FitsFile}{FitsInFile}
1933\dclscc{FitsFile}{FitsOutFile}
[1387]1934\end{figure}
[3015]1935%%%%
[3048]1936\subsection{FITS handlers and I/O operators}
[3037]1937\index{FitsManager}
[3015]1938Handlers classes inheriting from {\bf FitsHandlerInterface} perform write/read operations
1939for AnyDataObj objects to/from FitsInOutFile streams. The {\bf FitsManager} class provides
1940top level services for object read/write in FITS files.
[3037]1941\par In most cases,
1942\hspace{5mm} {\tt FitsInOutFile\& $<<$ } \, and \, {\tt FitsInOutFile\& $>>$ } \hspace{5mm}
1943operators can be used to write and read objects.
[3048]1944When reading objects from a fits file using the {\tt FitsInOutFile\& $>>$ } operator,
1945the fits file is positioned on the next HDU, after reading. Also, if the {\bf FitsInOutFile}
1946object is positioned on a first empty HDU (without data, naxis=0), reading in objects
1947corresponding to a binary or ascii table using the operator $>>$ will skip automatically
1948the empty HDU and position the fits file on the second HDU, before trying to read in
1949the object.
[3037]1950\par
1951The two main types of fits data structures, images and tables
[3015]1952{\tt (IMAGE\_HDU , BINARY\_TBL , ASCII\_TBL)} are handled by the generic handlers: \\
[3037]1953{\bf \tcls{FitsArrayHandler}} and {\bf FitsHandler$<$BaseDataTable$>$}.
1954\par
[3015]1955A number of more specific handlers are also available, in particular for NTuple,
[3034]1956\tcls{SphereHealPix} and \tcls{SphereThetaPhi}. \\[2mm]
[3048]1957{\bf Warning:} Some handlers were written with the old FitsIOServer classes.
1958They inherit from the intermediate class {\bf FitsIOHandler} and
1959have been adapted to the new scheme. \\[2mm]
[3015]1960%%%
[3037]1961The examples below illustrates the usage of FitsIOServer classes. They can be compiled
1962and executed using runcxx, without the {\tt include} lines: \\[1mm]
1963\hspace*{5mm} {\tt csh> runcxx -import SkyMap -import FitsIOServer -inc fiosinit.h }
1964%%%
[3015]1965\begin{enumerate}
1966\item Saving an array and a HealPix map to a Fits file
[1387]1967\begin{verbatim}
[3026]1968#include "fitsioserver.h"
[3034]1969#include "fiosinit.h"
[3015]1970// ....
1971{
[3034]1972// Make sure FitsIOServer module is initialised :
[3015]1973FitsIOServerInit();
1974// Create and open a fits file named myfile.fits
1975FitsInOutFile fos("myfile.fits", FitsInOutFile ::Fits_Create);
1976// Create and save a 15x11 matrix of integers
1977TMatrix<int_4> mxi(15, 11);
1978mxi = RegularSequence(10.,10.);
1979fos << mxi;
1980// Save a HEALPix spherical map using FitsManager services
1981SphereHEALPix<r_8> sph(16);
1982sph = 48.3;
1983FitsManager::Write(fos, sph);
[3037]1984// --- The << operator could have been used instead : fos << sph;
[3015]1985}
1986\end{verbatim}
[3034]1987%%%%
1988%%%%
1989\item Reading objects and the header from the previously created fits file:
[3015]1990\begin{verbatim}
[3034]1991{
1992FitsIOServerInit(); // Initialisation
1993// ---- Open the fits file named myfile.fits
1994FitsInFile fis("myfile.fits");
1995//---- print file information on cout
1996cout << fis << endl;
1997//--- Read in the array
1998TArray<int_4> arr;
1999fis >> arr;
2000arr.Show();
2001//--- Position on second HDU
2002fis.MoveAbsToHDU(2);
2003//--- read and display header information
2004DVList hdu2;
2005fis.GetHeaderRecords(hdu2, true, true);
2006cout << hdu2;
2007//--- read in the HEALPix map
2008SphereHEALPix<r_8> sph;
2009FitsManager::Read(fis, sph);
[3037]2010// --- The >> operator could have been used instead : fis >> sph;
[3034]2011sph.Show();
2012}
[1387]2013\end{verbatim}
[3034]2014%%%%%%%
2015%%%
2016\item DataTable objects can be read from and written to FITS files as ASCII or
2017binary tables. The example belo show reading the DataTable created in the example
[3037]2018in section \ref{datatables} from a PPF file and saving it to a fits file.
[1435]2019\begin{verbatim}
[3037]2020#include "swfitsdtable.h"
2021// ....
[3034]2022{
[3037]2023FitsIOServerInit(); // FitsIOServer Initialisation
2024//--- Reading in DataTable object from PPF file
2025PInPersist pin("dtable.ppf");
2026DataTable dt;
2027pin >> dt;
2028dt.Show();
2029//--- Saving table to FITS
2030FitsInOutFile fos("!dtable.fits", FitsInOutFile ::Fits_Create);
2031fos << dt;
[3034]2032}
[1435]2033\end{verbatim}
[3034]2034%%%%
2035\end{enumerate}
[3037]2036%%%
[3015]2037A partial class diagram of FITS persistence handling classes is shown below. The
2038class {\tt FitsIOhandler} conforms to the old FitsIOServer module design and
2039should not be used anymore.
[1648]2040\begin{figure}[hbt]
[3015]2041\dclsbb{FitsHandlerInterface}{FitsArrayHandler$<$T$>$}
2042\dclsb{\tcls{FitsHandler}}
2043\dclscc{FitsIOhandler}{FITS\_NTuple}
2044\dclsc{FITS\_SphereHEALPix}
[1648]2045% \dclsb{FITS\_LocalMap}
2046\end{figure}
[1387]2047
[3034]2048\subsection{SwFitsDataTable and other classes}
[3048]2049\label{SwFitsDataTable}
[3037]2050\index{SwFitsDataTable}
2051The {\bf SwFitsDataTable} class implements the BaseDataTable interface
2052using a FITS file as swap space. Compared to SwPPFDataTable, they can be
2053used in R/W mode (reading from the table, when it is being created / filled).
2054They can be used in a way similar to DataTable and SwPPFDataTable.
2055When creating the table, a {\tt FitsInOutFile } stream, opened for writing has
2056to be passed to the creator. No further operation is needed.
2057\begin{verbatim}
2058// ....
2059FitsInOutFile so("!myswtable.fits", FitsInOutFile::Fits_Create);
2060SwFitsDataTable dt(so, 16);
2061// define table columns
2062dt.AddIntegerColumn("X0_i");
2063dt.AddFloatColumn("X1_f");
2064// ... Fill the table
2065r_8 x[5];
2066for(int i=0; i<63; i++) {
2067 x[0] = (i%9)-4.; x[1] = (i/9)-3.;
2068 dt.AddLine(x);
2069}
2070\end{verbatim}
2071The class {\bf FitsBTNtuIntf } provide an alternative tool to read FITS tables.
2072{\bf FitsABTColRd} , {\bf FitsABTWriter } and {\bf FitsImg2DWriter } can also
2073be used to manipulate FITS files.
2074\par
2075The {\bf scanfits} program can be used to check FITS files and analyse their
2076content (See \ref{scanfits}).
[3034]2077
[3037]2078%%%%
[1340]2079\newpage
[1435]2080\section{LinAlg and IFFTW modules}
2081An interface to use LAPACK library (available from {\tt http://www.netlib.org})
2082is implemented by the {\bf LapackServer} class, in module LinAlg.
2083\index{LapackServer}.
2084The sample code below shows how to use SVD (Singular Value Decomposition)
2085through LapackServer:
2086\begin{verbatim}
2087#include "intflapack.h"
2088// ...
2089// Use FortranMemoryMapping as default
2090BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
2091// Create an fill the arrays A and its copy AA
2092int n = 20;
2093Matrix A(n , n), AA;
2094A = RandomSequence(RandomSequence::Gaussian, 0., 4.);
2095AA = A; // AA is a copy of A
2096// Compute the SVD decomposition
2097Vector S; // Vector of singular values
2098Matrix U, VT;
2099LapackServer<r_8> lpks;
2100lpks.SVD(AA, S, U, VT);
2101// We create a diagonal matrix using S
2102Matrix SM(n, n);
2103for(int k=0; k<n; k++) SM(k,k) = S(k);
2104// Check the result : A = U*SM*VT
2105Matrix diff = U*(SM*VT) - A;
2106double min, max;
2107diff.MinMax(min, max);
2108cout << " Min/Max difference Matrix (?=0) , Min= " << min
2109 << " Max= " << max << endl;
2110\end{verbatim}
2111
2112\index{FFTWServer}
2113The {\bf FFTWServer} class (in module FFTW) implements FFTServerInterface class
2114methods, for one dimensional and multi-dimensional Fourier
2115transforms on double precision data using the FFTW package
2116(available from {\tt http://www.fftw.org}).
2117
2118\newpage
[978]2119\section{Building and installing Sophya}
[3045]2120\subsection{Supported platforms}
[1436]2121Presently, the Sophya library has been tested with the following
[978]2122compiler/platform pairs:
2123
2124\begin{center}
[2790]2125\begin{tabular}{|l|l|}
2126\hline
2127OS & compiler \\
2128\hline
[3415]2129Linux (32 bits) & g++ (3.x \, 4.0) \\
2130Linux (SCL, 32) & icc - Intel compiler (9.0) \\
2131Linux (SCL, 64) & g++ 3.3 \, 3.4 \\
[3092]2132MacOSX/Darwin (PowerPC) 10.3 \, 10.4 & g++ (3.3 \, 4.0)\\
2133MacOSX/Darwin (Intel) 10.4 & g++ (4.0)\\
[3045]2134HP/Compaq/DEC Tru64 ( OSF1) & cxx (6.1 6.3) \\
[1436]2135SGI IRIX64 & CC (7.3) \\
[3415]2136IBM AIX (5.3) & xlC (8.0) \\
[2790]2137\hline
[978]2138\end{tabular}
2139\end{center}
2140
[3045]2141\subsection{Library and makefile structure}
2142%
[978]2143The object files from a given Sophya module are grouped in an archive library
2144with the module's name ({\tt libmodulename.a}). All Sophya modules
2145 are grouped in a single shared library ({\tt libsophya.so}), while the
2146modules with reference to external libraries are grouped in
2147({\tt libextsophya.so}). The {\bf PI} and {\bf PIext} modules are
2148grouped in ({\tt libPI.so}).
[3015]2149Alternatively, it is possible to group all modules in a single shared
2150library {\tt libAsophyaextPI.so}.
[3037]2151\par
2152Each library module has a {\tt Makefile} which compiles the source files
2153and build the correspond static (archive) library using the compilation
[3045]2154rules and flags defined in \\
2155\hspace*{5mm} {\tt \$SOPHYABASE/include/sophyamake.inc}. \\
[3037]2156Each program module has a {\tt Makefile} which compiles and link the
2157corresponding programs using the compilation rules and libraries
2158defined in {\$SOPHYABASE/include/sophyamake.inc}.
2159The top level Makefile in BuildMgr/ compiles each library modules
2160and builds shared libraries.
[3045]2161\par
2162Some of the modules in the Sophya package uses external libraries. The
2163{\bf FitsIOServer} is an example of such a module, where the {\tt libcfitsio.a}
2164is used. The list of all Sophya modules using external libraries is
2165presented in section \ref{sopmodules}.
2166The external libraries should be installed before the configure step
2167(see below) and the compilation of the corresponding Sophya modules.
[3037]2168\par
2169The series of Makefiles use the link to {\tt sophyamake.inc} in BuildMgr.
2170There are also the {\tt smakefile} series which uses the explicit path, using
2171{\tt \$SOPHYABASE} environment variable.
[978]2172
[3045]2173\subsection{Build instructions}
[3015]2174\label{build}
2175The build procedure has two main steps:
2176\begin{enumerate}
2177\item The configure step (BuildMgr/configure) setup the directory structure and
2178the necessary configuration file. Refer to section \ref{directories} for
2179the description of SOPHYA directory tree and files.
2180\item The make step compiles the different sources files, create the library and optionaly
[2790]2181builds all or some of the associated executables.
[3015]2182\end{enumerate}
[2790]2183
2184{\tt BuildMgr/configure } is a c-shell script with a number of arguments:
2185\begin{verbatim}
2186csh> ./configure -h
2187configure [-sbase SOPHYABASE] [-scxx SOPHYACXX] [-incln]
[3415]2188 [-minc mymake.inc] [-compopt 'cc/cxxOptions']
2189 [-arch64] [-sasz64] [-nofpic] [-nothsafe] [-boundcheck] [-sodebug]
2190 [-extp dir1 -extp dir2 ...] [-extip dir1 -extip dir2 ... ] [-extlp dir1 -extlp dir2 ... ]
2191 [-noextlib] [-noext fits] [-noext fftw] [-noext lapack] [-noext astro]
2192 [-noPI] [-slballinone]
2193 [-alsofftwfloat] [-usefftw2] [-uselapack2]
2194 (See SOPHYA manual/web pages for a detailed description of configure options)
[2790]2195\end{verbatim}
2196\begin{itemize}
2197\item[] -sbase : define SOPHYA installation base directory. \$SOPHYABASE is used
2198if not specified.
2199\item[] -scxx : selects the C++ compiler. \$SOPHYACXX s used
2200if not specified.
2201\item[] -incln : creates symbolic link for include files, instead of copying them.
2202\item[] -minc : give an explicit name for the file used to generate
[3415]2203\$SOPHYABASE/include/sophyamake.inc. If {\tt -minc} is not specified, one of
2204the files in BuildMgr/ directory is selected, based on the system name and the
2205compiler {\tt Linux\_g++\_make.inc , OSF1\_cxx\_make.inc , AIX\_xlC\_make.inc \ldots}
[2790]2206\item[] -extp : Adds the specied path to the search path of the external libraries
2207include files and archive library.
2208\item[] -extip : Adds the specied path to the search path of the external libraries
2209include files.
2210\item[] -extp : Adds the specied path to the search path of the external libraries
2211archive (libxxx.a).
2212\item[] -noextlib : Disable compiling of modules referencing external libraries.
2213\item[] -noext : Disable compiling of the specified module (with reference to external
[3415]2214library : {\tt -noext fits , -noext fftw \ldots }
2215\item[] -usefftw2: Use FFTW V2 instead of the default FFTW V3 - A preprocessor
2216flag will be defined in sspvflags.h
2217\item[] -uselapack2: Lapack V2 is being used (defaulr V3) - A preprocessor
2218flag will be defined in sspvflags.h
2219\item[] -alsofftwfloat : compile single precision (float) version of the Fourier
2220transform methods (module IFFTW, class FFTWServer). A preprocessor
2221flag will be defined in sspvflags.h and float version of the FFTW library
2222(libfftw3f.a) will be linked with SOPHYA, in addition to the default double
2223precision library (libfftw3.a).
2224\item[] -noPI : has currently no effect
2225\item[] -slballinone: A single shared library for all SOPHYA, PI and external library interface
[3015]2226modules will be build. A compilation flag
[3415]2227will be defined in sspvflags.h . \\ See also target {\tt slballinone} below.
[2790]2228\end{itemize}
2229
[3415]2230{\large \bf configure steps } \\[1mm]
2231The configure script performs the following actions :
2232\begin{enumerate}
2233\item Creating directory tree under {\tt \$SOPHYABASE }
2234\item Cpoying include files (or creating symbolic) in {\tt \$SOPHYABASE/include/ }
2235\item Search for external libraries include files and create the necessary links
2236in {\tt \$SOPHYABASE/include}
2237\item Search for external libraries (-lfits \ldots) and add the corresponding
2238directories to the library search path, in {\tt sophyamake.inc}
2239\item Creates the file { \tt \$SOPHYABASE/include/sophyamake.inc }
2240\item Creates the file {\tt machdefs.h} from { BaseTools/machdefs\_mkmf.h } and
2241{\tt sspvflags.h }
2242\item Creates the object list files for shared library creation
2243\end{enumerate}
2244
2245{\large \bf Example } \\[1mm]
2246
[978]2247In the example below, we assume that we want to install Sophya from a
2248released (tagged) version in the source directory {\tt \$SRC} in the
[2790]2249{\tt /usr/local/Sophya} directory, using {\tt g++}. We assume that
2250the external libraries can be found in {\tt /usr/local/ExtLibs/}.
[3180]2251We disable the compilation of the XAstroPack package.
[2790]2252
[978]2253\vspace*{3mm}
2254\begin{verbatim}
[2790]2255# Create the top level directory
[978]2256csh> mkdir /usr/local/Sophya/
[2790]2257csh> cd $SRC/BuildMgr
2258# Step 1.a : Run the configuration script
2259csh> ./configure -sbase /usr/local/Sophya -scxx g++ -extp /usr/local/ExtLibs/ \
[3180]2260-noext astro
[3015]2261# Step 1.b : Check the generated file $SOPHYABASE/include/sophyamake.inc
2262csh> ls -lt *.inc
2263csh> more sophyamake.inc
2264\end{verbatim}
2265If necessary, edit the generated file {\tt sophyamake.inc } in order to modify
2266compilation flags, library list. The file is rather short and self documented.
2267\begin{verbatim}
[2790]2268# Step 2.a: Compile the modules without external library reference
[978]2269csh> make libs
[2790]2270# Step 2.b: Compile the modules WITH external library reference (optional)
[978]2271csh> make extlibs
[2790]2272# Step 2.c: Build libsophya.so
[978]2273csh> make slb
[2790]2274# Step 2.d: Build libextsophya.so (optional)
[978]2275csh> make slbext
[2790]2276# Step 2.e: Compile the PI and PIext modules (optional)
[978]2277csh> make PI
[2790]2278# Step 2.f: Build the corresponding shared library libPI.so (optional)
[978]2279csh> make slbpi
2280\end{verbatim}
2281
2282To compile all modules and build the shared libraries, it is possible
[3015]2283to perform the steps 2.a to 2.f using the targets {\tt all} and {\tt slball}
2284defined in the Makefile
[978]2285\begin{verbatim}
[2790]2286# Step 2.a ... 2.f
[3015]2287csh> make all slball
[978]2288\end{verbatim}
2289
[3015]2290It is also possible to group all modules in a single shared library using
2291the target {\tt slballinone}.
2292\begin{verbatim}
2293# Step 2.a ... 2.f
2294csh> make all slballinone
2295\end{verbatim}
2296
[1435]2297At this step, all libraries should have been made. Programs using
[978]2298Sophya libraries can now be built:
2299\begin{verbatim}
[3015]2300# To compile some of the test programs
2301csh> make basetests
2302# To compile runcxx , scanppf , scanfits
2303csh> make prgutil
[978]2304# To build (s)piapp (libPI.so is needed)
[3015]2305csh> make piapp
[978]2306\end{verbatim}
2307
[3037]2308If no further modification or update of source files is foreseen, it is possible
2309to remove all .o files:
2310\begin{verbatim}
2311# To clean $SOPHYABASE/obj directory :
2312csh> make cleanobj
2313\end{verbatim}
2314
2315
2316\subsection{Notes}
[988]2317\begin{itemize}
[3037]2318\item[{\bf Makefile}] List of top level Makefile build targets
2319\begin{verbatim}
2320> libs extlibs PI = all
2321> slb slbext slbpi = slball (OR = slballinone)
2322> clean cleanobj
2323> tests prgutil prgmap progpi = prgall
2324> basetests piapp (ou progpi) pmixer
2325\end{verbatim}
2326\item[{\bf MacOS X}] A high performance mathematic and signal processing
2327library, including LAPACK and BLAS is packaged in Darwin/MacOS X (10.3, 10.4) : \\
2328\hspace*{5mm} {\bf -framework Accelerate}
2329\item[{\bf Tru64/OSF}] An optimised math library with LAPACK and BLAS might
2330optionaly be installed {\bf (-lcxlm) }. On our system, this libray contained Lapack V2.
2331So we used the LAPACK, as compiled from the public sources, and linked with
2332the Tru64 native BLAS.
2333\item[{\bf IRIX64}] We used the math library with LAPACK V2 and BLAS
2334from SGI : {\bf -lcomplib.sgimath}
[3015]2335\item[{\bf AIX}] There seem to be a problem on AIX when several shared
2336libraries are used. We have been able to run SOPHYA programs either
2337using static libraries, or a single shared library (libAsophyaextPI.so)
2338if extlibs and PI are needed, in addition to stand alone SOPHYA modules.
[3037]2339It has not been possible to link SOPHYA with fortran libraries
2340\item[{\bf Mgr}] This module contains makefiles and build scripts
2341that were used in SOPHYA up to version 1.7 (2004) : OBSOLETE.
[3015]2342\end{itemize}
[887]2343
[3015]2344\subsection{Files and scripts in BuildMgr/ }
2345\begin{itemize}
2346\item[] {\bf Makefile:} Top level Makefile for building SOPHYA.
2347{\tt smakefile} is similar to Makefile, except that it uses
2348the smakefiles in each module.
2349\item[] {\bf mkmflib:} c-shell script for creation of library module
[3045]2350Makefile / smakefile. \\
2351\hspace*{5mm} {\tt ./mkmflib -sbase /tmp/sbase SUtils }
[3015]2352\item[] {\b mkmfprog:}
[3045]2353c-shell script for creation of programs module Makefile / smakefile \\
2354\hspace*{5mm} {\tt ./mkmfprog -sbase /tmp/sbase ProgPI }
[3015]2355\item[] {\bf domkmf:} c-shell script - calls mkmflib for all modules \\
[3045]2356\hspace*{5mm} {\tt ./domkmf -sbase /tmp/sbase}
2357\item[] {\bf xxx\_make.inc:} Configuration files for different compilers and OS
2358{\tt ( Linux\_g++\_make.inc , OSF1\_cxx\_make.inc \ldots )}.
[3015]2359These files are used to generate {\tt sophyamake.inc}
[988]2360\end{itemize}
[3015]2361
2362
2363
[988]2364\newpage
[1299]2365\appendix
[1362]2366\section{SOPHYA Exceptions}
2367\index{Exception classes} \index{PThrowable} \index{PError} \index{PException}
2368SOPHYA library defines a set of exceptions which are used
[1435]2369for signalling error conditions. The figure below shows a partial
[1362]2370class diagram for exception classes in SOPHYA.
2371\begin{figure}[hbt]
2372\dclsbb{PThrowable}{PError}
2373\dclscc{PError}{AllocationError}
2374\dclscc{PError}{NullPtrError}
2375\dclscc{PError}{ForbiddenError}
2376\dclscc{PError}{AssertionFailedError}
2377\dclsbb{PThrowable}{PException}
2378\dclscc{PException}{IOExc}
2379\dclscc{PException}{SzMismatchError}
2380\dclscc{PException}{RangeCheckError}
2381\dclscc{PException}{ParmError}
2382\dclscc{PException}{TypeMismatchExc}
2383\dclscc{PException}{MathExc}
2384\dclscc{PException}{CaughtSignalExc}
2385\caption{partial class diagram for exception handling in Sophya}
2386\end{figure}
2387
[1299]2388For simple programs, it is a good practice to handle
[988]2389the exceptions at least at high level, in the {\tt main()} function.
2390The example below shows the exception handling and the usage
2391of Sophya persistence.
[1299]2392
[988]2393\input{ex1.inc}
[887]2394
[1292]2395
[1362]2396\newpage
2397\addcontentsline{toc}{section}{Index}
2398\printindex
[1299]2399\end{document}
[1362]2400
Note: See TracBrowser for help on using the repository browser.