1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use ffi;
use enums;
use libc::{feof, fread};
use std::marker::PhantomData;
use std::ffi::CString;
pub struct NTuples<T> {
n: *mut ffi::gsl_ntuple,
p: PhantomData<T>
}
impl<T> NTuples<T> {
pub fn create(filename: &str, data: &mut T) -> Option<NTuples<T>> {
let t_data = unsafe { ::std::mem::transmute(data) };
let c_str = CString::new(filename.as_bytes()).unwrap();
let tmp = unsafe {
ffi::gsl_ntuple_create(c_str.as_ptr() as *mut i8, t_data, ::std::mem::size_of::<T>() as usize)
};
if tmp.is_null() {
None
} else {
Some(NTuples {
n: tmp,
p: PhantomData
})
}
}
pub fn open(filename: &str, data: &mut T) -> Option<NTuples<T>> {
let t_data = unsafe { ::std::mem::transmute(data) };
let c_str = CString::new(filename.as_bytes()).unwrap();
let tmp = unsafe {
ffi::gsl_ntuple_open(c_str.as_ptr() as *mut i8, t_data, ::std::mem::size_of::<T>() as usize)
};
if tmp.is_null() {
None
} else {
Some(NTuples {
n: tmp,
p: PhantomData
})
}
}
pub fn write(&self) -> enums::Value {
unsafe { ffi::gsl_ntuple_write(self.n) }
}
pub fn bookdata(&self) -> enums::Value {
unsafe { ffi::gsl_ntuple_bookdata(self.n) }
}
pub fn read(&self) -> enums::Value {
unsafe { ffi::gsl_ntuple_read(self.n) }
}
pub fn project<U, V>(&self, h: &::Histogram, value_func: ::value_function<T, U>, value_arg: &mut U,
select_func: ::select_function<T, V>, select_arg: &mut V) -> enums::Value {
unsafe {
loop {
let nread = fread((*self.n).ntuple_data, (*self.n).size, 1, (*self.n).file);
if nread == 0 && feof((*self.n).file) != 0 {
break;
}
if nread != 1 {
rgsl_error!("failed to read ntuple for projection", ::Value::Failed);
}
if select_func(::std::mem::transmute((*self.n).ntuple_data), select_arg) {
ffi::gsl_histogram_increment(ffi::FFI::unwrap(h), value_func(::std::mem::transmute((*self.n).ntuple_data), value_arg));
}
}
::Value::Success
}
}
}
impl<T> Drop for NTuples<T> {
fn drop(&mut self) {
unsafe { ffi::gsl_ntuple_close(self.n) };
self.n = ::std::ptr::null_mut();
}
}
impl<T> ffi::FFI<ffi::gsl_ntuple> for NTuples<T> {
fn wrap(n: *mut ffi::gsl_ntuple) -> NTuples<T> {
NTuples {
n: n,
p: PhantomData
}
}
fn unwrap(n: &NTuples<T>) -> *mut ffi::gsl_ntuple {
n.n
}
}