Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xData_2d_xshared_yhistogram.cc
Go to the documentation of this file.
1 /*
2 # <<BEGIN-copyright>>
3 # Copyright (c) 2010, Lawrence Livermore National Security, LLC.
4 # Produced at the Lawrence Livermore National Laboratory
5 # Written by Bret R. Beck, beck6@llnl.gov.
6 # CODE-461393
7 # All rights reserved.
8 #
9 # This file is part of GIDI. For details, see nuclear.llnl.gov.
10 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov.
11 #
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
14 #
15 # 1) Redistributions of source code must retain the above copyright notice,
16 # this list of conditions and the disclaimer below.
17 # 2) Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the disclaimer (as noted below) in the
19 # documentation and/or other materials provided with the distribution.
20 # 3) Neither the name of the LLNS/LLNL nor the names of its contributors may be
21 # used to endorse or promote products derived from this software without
22 # specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
28 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # <<END-copyright>>
35 */
36 #include <stdlib.h>
37 #include <limits.h>
38 #include <ctype.h>
39 #include "xData.h"
40 
41 #if defined __cplusplus
42 namespace GIDI {
43 using namespace GIDI;
44 #endif
45 
46 //char const * const xData_twod_xShared_yHistogram_ID = "2d_xShared_yHistogram";
47 
48 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text );
49 static char *toString( statusMessageReporting *smr, xDataType *xDT );
50 static int release( statusMessageReporting *smr, xDataType *xDT );
51 /*
52 ************************************************************
53 */
55 
56  xDataType *xDT = &(element->xDataTypeInfo);
57 
60  xDT->element = element;
61  xDT->toData = toData;
62  xDT->toString = toString;
63  xDT->release = release;
64  xDT->data = NULL;
65  return( xData_xDataTypeConvertAttributes( smr, element ) );
66 }
67 /*
68 ************************************************************
69 */
71 
72  return( xData_is_xDataType( smr, xDT, xData_twod_xShared_yHistogram_ID, setMsg ) );
73 }
74 /*
75 ************************************************************
76 */
78 
79  return( xData_is_2d_xShared_yHistogram( smr, &(element->xDataTypeInfo), setMsg ) );
80 }
81 /*
82 ************************************************************
83 */
84 //double *xData_2d_xShared_yHistogram_copyData( statusMessageReporting *smr, xData_element *element, xData_Int *n ) {
86 
87  xDataType *xDT = &(element->xDataTypeInfo);
88  xData_Int i;
89  double *p, *values, *d = (double *) xDT->data;
90 
91  *n = xDT->end - xDT->start;
92  if( xDT->length == 0 ) return( NULL );
93  if( *n == 0 ) return( NULL );
94  //if(( values = xData_malloc2( NULL, *n * sizeof( double ), 0, "values" ) )) {
95  if(( values = (double*) xData_malloc2( NULL, *n * sizeof( double ), 0, "values" ) )) {
96  p = values;
97  for( i = 0; i < *n; i++, p++, d++ ) *p = *d;
98  }
99  return( values );
100 }
101 /*
102 ************************************************************
103 */
105 
106  xData_free( smr, data );
107  return( 0 );
108 }
109 /*
110 ************************************************************
111 */
112 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
113 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
114 
115  xData_Int i, n = xDT->end - xDT->start, status = 0;
116  char *e;
117  const char *s;
118  double *p;
119  void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
120 
121  if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__, 1, "bad xDataType instance" ) );
122  release( smr, xDT );
123  if( ( xDT->data = xData_malloc2( smr, n * sizeof( double ), 0, "data" ) ) == NULL ) return( 1 );
124  for( i = 0, s = text, p = (double *) xDT->data; i < n; i++, p++, s = e ) {
125  if( xData_stringTo_double( smr, smrUser, s, p, " \n", &e ) ) { status = 1; break; }
126  }
127  if( status == 0 ) {
128  while( isspace( *e ) ) e++;
129  if( *e != 0 ) {
130  smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "text contains extra data = %s", e );
131  status = 1;
132  }
133  }
134  if( status != 0 ) release( smr, xDT );
135  return( status );
136 }
137 /*
138 ************************************************************
139 */
140 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
141 static char *toString( statusMessageReporting *, xDataType *xDT ) {
142 
143  xData_Int i, n = xDT->length, recordSize = 16 + 1;
144  char *str, *p, fmt[32] = " %15.7e\n";
145  //double *d = xDT->data;
146  double *d = (double*) xDT->data;
147 
148  if( n < 0 ) n = 0;
149  if( ( str = (char *) malloc( recordSize * ( n + 1 ) ) ) == NULL ) return( NULL );
150  for( i = 0, p = str; i < n; i++, p += recordSize, d++ ) sprintf( p, fmt, d ); *p = 0;
151  return( str );
152 }
153 /*
154 ************************************************************
155 */
156 static int release( statusMessageReporting *smr, xDataType *xDT ) {
157 
158  if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
159  return( xDT->status = xData_xDataType_Ok );
160 }
161 /*
162 ************************************************************
163 */
165 /*
166 * Returns NULL if length is 0, memory could not be allocated, or nXs != xDT->length + 1.
167 */
168  xData_Int i;
169  //double *p, *values, *d = xDT->data;
170  double *p, *values, *d = (double*) xDT->data;
171 
172  if( xDT->length == 0 ) return( NULL );
173  if( ( xDT->length + 1 ) != nXs ) return( NULL );
174  //if( ( values = xData_malloc2( NULL, 4 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
175  if( ( values = (double*) xData_malloc2( NULL, 4 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
176  p = values;
177  for( i = 0; i < xDT->length; i++ ) {
178  *(p++) = Xs[i];
179  *(p++) = 0.;
180  *(p++) = Xs[i+1];
181  *(p++) = 0.;
182  }
183  p = &(values[4 * xDT->start + 1]);
184  for( i = xDT->start; i < xDT->end; i++, d++, p += 2 ) {
185  *p = *d;
186  p += 2;
187  *p = *d;
188  }
189  return( values );
190 }
191 
192 #if defined __cplusplus
193 }
194 #endif
xData_Int length
Definition: xData.h:162
const char *const xData_twod_xShared_yHistogram_ID
Definition: xData.h:74
int xData_xDataTypeConvertAttributes(statusMessageReporting *smr, xData_element *element)
Definition: xData.cc:668
void * xData_get_smrUserInterfaceFromElement(xData_element *element)
Definition: xData.cc:952
int xData_isElement_2d_xShared_yHistogram(statusMessageReporting *smr, xData_element *element, int setMsg)
xData_Int start
Definition: xData.h:162
int xData_init_2d_xShared_yHistogram(statusMessageReporting *smr, xData_element *element)
const XML_Char * s
double * xData_2d_xShared_yHistogram_copyData(statusMessageReporting *, xData_element *element, xData_Int *n)
const char * p
Definition: xmltok.h:285
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
int xData_is_xDataType(statusMessageReporting *smr, xDataType *xDT, char const *const type, int setMsg)
Definition: xData.cc:900
#define xData_malloc2(smr, size, zero, forItem)
Definition: xData.h:313
void * xData_free(statusMessageReporting *smr, void *p)
Definition: xDataMisc.cc:89
xDT_toStringFunction toString
Definition: xData.h:159
enum xData_xDataType status
Definition: xData.h:155
double * xData_2d_xShared_yHistogram_toFilledXYs(xDataType *xDT, xData_Int nXs, double *Xs)
xDataType xDataTypeInfo
Definition: xData.h:187
xDT_releaseFunction release
Definition: xData.h:160
const G4int n
int status
Definition: tracer.cxx:24
xData_element * element
Definition: xData.h:157
int xData_setMessageError_ReturnInt(int value, statusMessageReporting *smr, void *userData, const char *file, int line, int code, const char *fmt,...)
Definition: xDataMisc.cc:160
int xData_2d_xShared_yHistogram_free_copyData(statusMessageReporting *smr, void *data)
int xData_Int
Definition: xData.h:50
const char * typeString
Definition: xData.h:156
void * malloc(size_t __size)
Definition: hjmalloc.cc:30
int xData_is_2d_xShared_yHistogram(statusMessageReporting *smr, xDataType *xDT, int setMsg)
xDT_toDataFunction toData
Definition: xData.h:158
const XML_Char const XML_Char * data
int xData_stringTo_double(statusMessageReporting *smr, void *smrUserInterface, char const *c, double *value, char const *endings, char **e)
Definition: xData.cc:1044
xData_Int end
Definition: xData.h:162
void * data
Definition: xData.h:163