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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use serde::{Deserialize, Serialize, Serializer};

use crate::ParseErr;

#[derive(Deserialize, Serialize)]
#[derive(Clone, Debug, Default, PartialEq, PartialOrd)]
pub struct Normalization {
    #[serde(rename = "@name")]
    pub name: String,
    //#[serde(rename = "$field")]
    #[serde(rename = "XSection")]
    pub xsection: XSection,
    #[serde(rename = "Contribution")]
    pub contribution: Contribution,
    #[serde(rename = "NumberOfRejectedEvents")]
    pub number_of_rejected_events: String,
}

// // TODO: this would be the proper way to do this, but it doesn't work
// #[derive(Deserialize, Serialize)]
// #[derive(Clone, Debug, PartialEq, PartialOrd)]
// #[serde(untagged)]
// pub enum XSection {
//     Neg(XSectionNeg),
//     Pos(XSectionPos),
// }

// #[derive(Deserialize, Serialize)]
// #[derive(Clone, Debug, PartialEq, PartialOrd)]
// #[serde(rename_all = "PascalCase")]
// pub struct XSectionNeg {
//     #[serde(rename = "XSNeg")]
//     pub xs_neg: XSScale,
//     pub max_weight_neg: f64,
//     pub total_events_neg: u64,
//     pub accepted_events_neg: u64,
//     pub factor_neg: String,
// }

// #[derive(Deserialize, Serialize)]
// #[derive(Clone, Debug, PartialEq, PartialOrd)]
// #[serde(rename_all = "PascalCase")]
// pub struct XSectionPos {
//     #[serde(rename = "XSPos")]
//     pub xs_pos: XSScale,
//     pub max_weight_pos: f64,
//     pub total_events_pos: u64,
//     pub accepted_events_pos: u64,
//     pub factor_pos: String,
// }

#[derive(Deserialize, Serialize)]
#[derive(Clone, Debug, Default, PartialEq, PartialOrd)]
#[serde(rename_all = "PascalCase")]
pub struct XSection {
    // TODO: the `alias` is an evil hack and breaks serialization
    #[serde(rename = "XSPos", alias = "XSNeg")]
    pub xs_pos: XSScale,
    #[serde(alias = "MaxWeightNeg")]
    pub max_weight_pos: f64,
    #[serde(alias = "TotalEventsNeg")]
    pub total_events_pos: u64,
    #[serde(alias = "AcceptedEventsNeg")]
    pub accepted_events_pos: u64,
    #[serde(alias = "FactorNeg")]
    pub factor_pos: String,
}

#[derive(Deserialize, Serialize)]
#[derive(Clone, Debug, Default, PartialEq, PartialOrd)]
pub struct Contribution {
    #[serde(rename = "@name")]
    pub name: String,
    pub xsection: XSScale,
    pub rw: Reweight,
}

#[derive(Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Reweight {
    pub rwentry: Vec<String>,
}

#[derive(Clone, Debug, Default, PartialEq, PartialOrd)]
pub struct XSScale (pub [f64; 2]);

impl<'de> Deserialize<'de> for XSScale {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de> {
        let xs_scale_str = String::deserialize(deserializer)?;
        let mut entries = xs_scale_str.split(',');
        let mut xs_scale = [0.; 2];
        for q in &mut xs_scale {
            let Some(p) = entries.next() else {
                return Err(serde::de::Error::custom(
                    ParseErr::NumEntries(xs_scale_str, 2)
                ));
            };
            *q = p.parse().map_err(serde::de::Error::custom)?;
        }
        if entries.next().is_some() {
            return Err(serde::de::Error::custom(
                ParseErr::NumEntries(xs_scale_str, 2)
            ));
        }
        Ok(Self(xs_scale))
    }
}

impl Serialize for XSScale {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let p = self.0;
        serializer.serialize_str(
            &format!("{},{}", p[0], p[1])
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deser_xsection_neg() {
        pub const REF_XS: &str = r#"<XSection>
 <XSNeg> 687.103,0.978277 </XSNeg>
 <MaxWeightNeg> 796475 </MaxWeightNeg>
 <TotalEventsNeg> 488338734 </TotalEventsNeg>
 <AcceptedEventsNeg> 493310 </AcceptedEventsNeg>
 <FactorNeg> 803.98,1.14468 </FactorNeg>
</XSection>"#;
        let _xs: XSection = quick_xml::de::from_str(REF_XS).unwrap();
    }

    #[test]
    fn deser_xsection_pos() {
        pub const REF_XS: &str = r#"<XSection>
 <XSPos> 687.103,0.978277 </XSPos>
 <MaxWeightPos> 796475 </MaxWeightPos>
 <TotalEventsPos> 488338734 </TotalEventsPos>
 <AcceptedEventsPos> 493310 </AcceptedEventsPos>
 <FactorPos> 803.98,1.14468 </FactorPos>
</XSection>"#;
        let _xs: XSection = quick_xml::de::from_str(REF_XS).unwrap();
    }

    #[test]
    fn deser_normalization() {
        pub const REF_NORM: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<Normalization name="Cm">
<!--
File generated with STRIPPER v0.1 for online data base
-->
<XSection>
 <XSNeg> 687.103,0.978277 </XSNeg>
 <MaxWeightNeg> 796475 </MaxWeightNeg>
 <TotalEventsNeg> 488338734 </TotalEventsNeg>
 <AcceptedEventsNeg> 493310 </AcceptedEventsNeg>
 <FactorNeg> 803.98,1.14468 </FactorNeg>
</XSection>
<Contribution name="Cm">
  <xsection> 687.103,0.978277</xsection>
  <rw>
    <rwentry> x1 </rwentry>
    <rwentry> x2 </rwentry>
    <rwentry> log(muR**2) </rwentry>
    <rwentry> log(muF**2) </rwentry>
  </rw>
</Contribution>
<NumberOfRejectedEvents> 0 , 100</NumberOfRejectedEvents>
</Normalization>
"#;
        let norm: Normalization = quick_xml::de::from_str(REF_NORM).unwrap();
        assert_eq!(norm.name, "Cm");
        assert_eq!(norm.contribution.name, "Cm");
        assert_eq!(norm.contribution.xsection.0, [687.103,0.978277]);
        assert_eq!(
            norm.contribution.rw.rwentry,
            ["x1", "x2", "log(muR**2)", "log(muF**2)"]
        );
        let XSection {
            xs_pos,
            max_weight_pos,
            total_events_pos,
            accepted_events_pos,
            factor_pos
        } = norm.xsection;
        assert_eq!(xs_pos, XSScale([687.103, 0.978277]));
        assert_eq!(max_weight_pos, 796475.);
        assert_eq!(accepted_events_pos, 493310);
        assert_eq!(total_events_pos, 488338734);
        assert_eq!(factor_pos, "803.98,1.14468");
    }
}