blib/lib/Math/SimpleHisto/XS.pm

filter: Statementstmt 84.0% 142 / 169 Branchbran 60.4% 52 / 86 Conditioncond 52.6% 10 / 19 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 93.3% 14 / 15 Podpod 50.0% 3 / 6 totaltotal 73.5% 225 / 306 CC 48 SCAR 29.4
CC
48
Cov
74%
new_from_dump
36.1
to_soot
20.0
dump
13.2
CRAP 18.9
↑ Summary blib/lib/Math/SimpleHisto/XS/RNG.pm »
1 package Math::SimpleHisto::XS;
2 11 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '1.30'; # Committed to floating point version numbers!
8  
9 11 require XSLoader;
10 11 XSLoader::load('Math::SimpleHisto::XS', $VERSION);
11  
12 11 require Math::SimpleHisto::XS::RNG;
13  
14 11 require Exporter;
15 11 our @ISA = qw(Exporter);
16 11 our @EXPORT_OK = qw(
17 INTEGRAL_CONSTANT
18 );
19 #INTEGRAL_POL1
20  
21 11 our %EXPORT_TAGS = (
22 'all' => \@EXPORT_OK,
23 );
24  
25 11 our @JSON_Modules = qw(JSON::XS JSON::PP JSON);
26 11 our $JSON_Implementation;
27 11 our $JSON;
28  
29 11 foreach my $json_module (@JSON_Modules) {
30 22 ▶ if (eval "require $json_module; 1;") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require $json_module; 1;") 11 11 22
31 11 $JSON = $json_module->new;
32 11 ▶ $JSON->indent(0) if $JSON->can('indent');
Branch: 1 branch
BranchTrueFalseTotal
if $Math::SimpleHisto::XS::JSON->can("indent") 11 0 11
33 11 ▶ $JSON->space_before(0) if $JSON->can('space_before');
Branch: 1 branch
BranchTrueFalseTotal
if $Math::SimpleHisto::XS::JSON->can("space_before") 11 0 11
34 11 ▶ $JSON->space_after(0) if $JSON->can('space_after');
Branch: 1 branch
BranchTrueFalseTotal
if $Math::SimpleHisto::XS::JSON->can("space_after") 11 0 11
35 11 ▶ $JSON->canonical(0) if $JSON->can('canonical');
Branch: 1 branch
BranchTrueFalseTotal
if $Math::SimpleHisto::XS::JSON->can("canonical") 11 0 11
36 11 $JSON_Implementation = $json_module;
37 11 ▶ last if $JSON;
Branch: 1 branch
BranchTrueFalseTotal
if $Math::SimpleHisto::XS::JSON 11 0 11
38 }
39 }
40  
41 26 ▶sub new {
Subroutine new: called CC=8 SCAR=22.6
Pod documented
42 26 my $class = shift;
43 26 my %opt = @_;
44  
45 26 ▶ if (defined $opt{bins}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'bins'}) { } 10 16 26
46 10 my $bins = $opt{bins};
47 10 ▶ croak("Cannot combine the 'bins' parameter with other parameters") if keys %opt > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %opt > 1 0 10 10
48 10 ▶ croak("The 'bins' parameter needs to be a reference to an array of bins")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bins and ref $bins eq "ARRAY" and @$bins > 1 0 10 10
Condition33%2/6
ref $bins and ref $bins eq "ARRAY" and @$bins > 1
!ll&&!rl&&r
0010
ref $bins and ref $bins eq "ARRAY"
!ll&&!rl&&r
0010
MC/DC0%0/3
ref $bins and ref $bins eq "ARRAY" and @$bins > 1
ref $bins ref $bins eq "ARRAY" @$bins > 1
Truth table25%1/4
A and B and C
A= ref $binsB= ref $bins eq "ARRAY"C= @$bins > 1
ABCresult
0XX0
10X0
1100
1111
49 if not ref($bins)
50 or not ref($bins) eq 'ARRAY'
51 or not @$bins > 1;
52 10 return $class->_new_histo_bins($bins);
53 }
54 else {
55 16 foreach (qw(min max nbins)) {
56 48 ▶ croak("Need parameter '$_'") if not defined $opt{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{$_} 0 48 48
57 }
58 }
59  
60 16 return $class->_new_histo(@opt{qw(nbins min max)});
61 }
62  
63 # See ExtUtils::Constant
64 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
65 # This AUTOLOAD is used to 'autoload' constants from the constant()
66 # XS function.
67  
68 1 my $constname;
69 1 our $AUTOLOAD;
70 1 ($constname = $AUTOLOAD) =~ s/.*:://;
71 1 ▶ croak('&' . __PACKAGE__ . "::constant not defined") if $constname eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 1 1
72 1 my ($error, $val) = constant($constname);
73 0 ▶ if ($error) { croak($error); }
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 1 1
74 {
75 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
77 }
78 1 goto &$AUTOLOAD;
79 }
80  
81  
82 11 ▶use constant _PACK_FLAG_VARIABLE_BINS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 11 my $native_pack_len;
85 SCOPE: {
86 11 require bytes;
87 11 my $intlen = bytes::length(pack('I', 0));
88 11 $native_pack_len = 8 + 4 + 16 + 4 + $intlen*2 + 16;
89 }
90  
91 11 ▶sub dump {
Subroutine dump: called CC=12 SCAR=25.8
Pod documented
92 11 my $self = shift;
93 11 my $type = shift;
94 11 $type = lc($type);
95  
96 11 my ($min, $max, $nbins, $nfills, $overflow, $underflow, $data_ary, $bins_ary)
97 = $self->_get_info;
98  
99 11 ▶ if ($type eq 'simple') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'simple') { } 7 4 11
elsif ($type eq 'json' or $type eq 'yaml') { } 2 2 4
elsif ($type eq 'native_pack') { } 2 0 2
100 7 ▶ return join(
Branch: 1 branch
BranchTrueFalseTotal
defined $bins_ary ? : 3 4 7
101 ';',
102 $VERSION,
103 $min, $max, $nbins,
104 $nfills, $overflow, $underflow,
105 join('|', @$data_ary),
106 (defined($bins_ary) ? join('|', @$bins_ary) : ''),
107 );
108 }
109 ▶ elsif ($type eq 'json' or $type eq 'yaml') {
Condition66%2/3
$type eq "json" or $type eq "yaml"
l!l&&r!l&&!r
202
MC/DC50%1/2
$type eq "json" or $type eq "yaml"
$type eq "json" $type eq "yaml"
Truth table66%2/3
A or B
A= $type eq "json"B= $type eq "yaml"
ABresult
1X1
011
000
110 2 my $struct = {
111 version => $VERSION,
112 min => $min, max => $max, nbins => $nbins,
113 nfills => $nfills, overflow => $overflow, underflow => $underflow,
114 data => $data_ary,
115 };
116 2 ▶ $struct->{bins} = $bins_ary if defined $bins_ary;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bins_ary 1 1 2
117  
118 2 ▶ if ($type eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'json') { } 2 0 2
119 2 ▶ if (not defined $JSON) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Math::SimpleHisto::XS::JSON) 0 2 2
120 0 die "Cannot use JSON dump mode since no JSON handling module could be loaded: "
121 . join(', ', @JSON_Modules);
122 }
123 2 return $JSON->encode($struct);
124 }
125 else { # type eq yaml
126 0 require YAML::Tiny;
127 0 return YAML::Tiny::Dump($struct);
128 }
129 }
130 elsif ($type eq 'native_pack') {
131 2 my $flags = 0;
132 2 ▶ vec($flags, _PACK_FLAG_VARIABLE_BINS, 1) = $bins_ary ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bins_ary ? : 1 1 2
133  
134 2 ▶ my $len = $native_pack_len + 8 * (scalar(@$data_ary) + scalar(@{$bins_ary || []}));
Condition100%2/2
$bins_ary || []
l!l
11
MC/DC100%1/1
$bins_ary || []
$bins_ary
Truth table100%2/2
A
A= $bins_ary
Aresult
11
00
135 return pack(
136 'd V d2 V I2 d2 d*',
137 $VERSION,
138 $len,
139 $min, $max,
140 $flags,
141 $nbins,
142 $nfills, $overflow, $underflow,
143 @$data_ary,
144 2 ▶ @{$bins_ary || []}
Condition100%2/2
$bins_ary || []
l!l
11
MC/DC100%1/1
$bins_ary || []
$bins_ary
Truth table100%2/2
A
A= $bins_ary
Aresult
11
00
145 );
146 }
147 else {
148 0 croak("Unknown dump type: '$type'");
149 }
150 0 die "Must not be reached";
151 }
152  
153  
154 11 ▶sub _check_version {
Subroutine _check_version: called CC=3 SCAR=13.3
155 11 my $version = shift;
156 11 my $type = shift;
157 11 ▶ if (not $version) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $version) { } 0 11 11
elsif ($Math::SimpleHisto::XS::VERSION - $version < -1.0) { } 0 11 11
158 0 croak("Invalid '$type' dump format");
159 }
160 elsif ($VERSION-$version < -1.) {
161 0 croak("Dump was generated with an incompatible newer version ($version) of this module ($VERSION)");
162 }
163 }
164  
165 11 ▶sub new_from_dump {
Subroutine new_from_dump: called CC=23 SCAR=35.9
Pod documented
166 11 my $class = shift;
167 11 my $type = shift;
168 11 my $dump = shift;
169 11 $type = lc($type);
170  
171 11 ▶ croak("Need dump string") if not defined $dump;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dump 0 11 11
172  
173 11 my $version;
174 my $hashref;
175 11 ▶ if ($type eq 'simple') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'simple') { } 7 4 11
elsif ($type eq 'json') { } 2 2 4
elsif ($type eq 'yaml') { } 0 2 2
elsif ($type eq 'native_pack') { } 2 0 2
176 7 ($version, my @rest) = split /;/, $dump, -1;
177 7 my $nexpected = 9;
178  
179 7 _check_version($version, 'simple');
180 7 ▶ if ($version <= 1.) { # no bins array in VERSION < 1
Branch: 2 branches
BranchTrueFalseTotal
if ($version <= 1.0) { } 0 7 7
elsif (@rest != $nexpected - 1) { } 0 7 7
181 0 $nexpected--;
182 }
183 elsif (@rest != $nexpected-1) {
184 0 croak("Invalid 'simple' dump format, wrong number of elements in top level structure");
185 }
186  
187 $hashref = {
188 7 min => $rest[0], max => $rest[1], nbins => $rest[2],
189 nfills => $rest[3], overflow => $rest[4], underflow => $rest[5],
190 data => [split /\|/, $rest[6]]
191 };
192 7 ▶ if ($version >= 1. and $rest[7] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 1 and $rest[7] ne "") 3 4 7
Condition66%2/3
$version >= 1 and $rest[7] ne ""
!ll&&!rl&&r
043
MC/DC50%1/2
$version >= 1 and $rest[7] ne ""
$version >= 1 $rest[7] ne ""
Truth table66%2/3
A and B
A= $version >= 1B= $rest[7] ne ""
ABresult
0X0
100
111
193 3 $hashref->{bins} = [split /\|/, $rest[7]];
194 }
195 }
196 elsif ($type eq 'json') {
197 2 ▶ if (not defined $JSON) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Math::SimpleHisto::XS::JSON) 0 2 2
198 0 die "Cannot use JSON dump mode since no JSON handling module could be loaded: "
199 . join(', ', @JSON_Modules);
200 }
201 2 $hashref = $JSON->decode($dump);
202 2 $version = $hashref->{version};
203 2 _check_version($version, 'json');
204 2 ▶ croak("Invalid JSON dump, not a hashref") if not ref($hashref) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hashref eq "HASH" 0 2 2
205 }
206 elsif ($type eq 'yaml') {
207 0 require YAML::Tiny;
208 0 my @docs = YAML::Tiny::Load($dump);
209 0 ▶ if (@docs != 1 or not ref($docs[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@docs != 1 or not ref $docs[0] eq "HASH") 0 0 0
Condition0%0/3
@docs != 1 or not ref $docs[0] eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
@docs != 1 or not ref $docs[0] eq "HASH"
@docs != 1 not ref $docs[0] eq "HASH"
Truth table0%0/3
A or not(B)
A= @docs != 1B= not ref $docs[0] eq "HASH"
ABresult
1X1
011
000
210 0 croak("Invalid YAML dump, not a single YAML document or not containing a hashref");
211 }
212 0 $hashref = $docs[0];
213 0 $version = $hashref->{version};
214 0 _check_version($version, 'yaml');
215 }
216 elsif ($type eq 'native_pack') {
217 2 my $version = unpack('d', $dump);
218 2 _check_version($version, 'native_pack');
219 2 my $flags_support = $version >= 1.;
220 2 my $prepended_length = $version >= 1.28;
221 2 my $ndoubles;
222  
223 # We go through all this pain about the length and the number of elements in the packed
224 # dump because that'll allow us to prevent reading beyond the end of a given dump.
225 2 ▶ if ($prepended_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prepended_length) 2 0 2
226 2 (undef, my $len) = unpack('d V', $dump);
227 2 $len -= $native_pack_len;
228 2 $ndoubles = $len / 8;
229 }
230 2 ▶ my $pack_str = $flags_support
Branch: 2 branches
BranchTrueFalseTotal
$flags_support ? : 2 0 2
$prepended_length ? : 2 0 2
231 ? ($prepended_length ? "d V d2 V I2 d2 d$ndoubles" : 'd3 V I2 d2 d*')
232 : 'd3 I2 d2 d*';
233  
234 2 my @things = unpack($pack_str, $dump);
235 2 $version = shift @things;
236 2 $hashref = {version => $version};
237  
238 2 ▶ foreach (($prepended_length ? ('data_length') : ()),
Branch: 2 branches
BranchTrueFalseTotal
$prepended_length ? : 2 0 2
$flags_support ? : 2 0 2
239 qw(min max),
240 ($flags_support ? ('flags') : ()),
241 qw(nbins nfills overflow underflow))
242 {
243 16 $hashref->{$_} = shift(@things);
244 }
245  
246 2 ▶ if ($flags_support) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags_support) 2 0 2
247 2 my $flags = delete $hashref->{flags};
248 2 ▶ if (vec($flags, _PACK_FLAG_VARIABLE_BINS, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $flags, 0, 1) 1 1 2
249 1 $hashref->{bins} = [splice(@things, $hashref->{nbins})];
250 }
251 }
252  
253 2 $hashref->{data} = \@things;
254 }
255 else {
256 0 croak("Unknown dump type: '$type'");
257 }
258  
259 11 my $self;
260 11 ▶ if (defined $hashref->{bins}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hashref->{'bins'}) { } 5 6 11
261 5 $self = $class->new(bins => $hashref->{bins});
262 }
263 else {
264 $self = $class->new(
265 min => $hashref->{min},
266 max => $hashref->{max},
267 nbins => $hashref->{nbins},
268 6 );
269 }
270  
271 11 $self->set_nfills($hashref->{nfills});
272 11 $self->set_overflow($hashref->{overflow});
273 11 $self->set_underflow($hashref->{underflow});
274 11 $self->set_all_bin_contents($hashref->{data});
275  
276 11 return $self;
277 }
278  
279  
280 4 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
281 4 my $self = shift;
282 4 my $cloning = shift;
283 4 my $serialized = $self->dump('simple');
284 4 return $serialized;
285 }
286  
287 4 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
288 4 my $self = shift;
289 4 my $cloning = shift;
290 4 my $serialized = shift;
291 4 my $new = ref($self)->new_from_dump('simple', $serialized);
292 4 $$self = $$new;
293 # Pesky DESTROY :P
294 4 bless($new => 'Math::SimpleHisto::XS::Doesntexist');
295 4 $new = undef;
296 }
297  
298 0 ▶sub to_soot {
Subroutine to_soot: not called CC=4 SCAR=30.0
Pod undocumented
299 0 my ($self, $name, $title) = @_;
300 0 ▶ $name = '' if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
301 0 ▶ $title = '' if not defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 0 0
302  
303 0 require SOOT;
304 0 my $th1d = TH1D->new($name, $title, $self->nbins, $self->min, $self->max);
305 0 $th1d->SetBinContent($_, $self->bin_content($_-1)) for 1..$self->nbins;
306 0 $th1d->SetEntries($self->nfills);
307  
308 0 return $th1d;
309 }
310  
311 11 1;
312 __END__
↑ Summary blib/lib/Math/SimpleHisto/XS/RNG.pm »