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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::default::Default;
use std::fmt::Display;
use std::io;
use std::mem::take;

use crate::event::*;

use hepmc2_macros::write_bound;
use log::error;

const DEFAULT_HEADER: &str = "HepMC::Version 2.06.09
HepMC::IO_GenEvent-START_EVENT_LISTING
";

const DEFAULT_FOOTER: &[u8] = b"HepMC::IO_GenEvent-END_EVENT_LISTING\n";

/// Write formatted data into a buffer.
///
/// If the `sync` feature is enabled this just passes the arguments to
/// [`std::write`].
///
/// In all other cases this creates an intermediate format string (which incurs
/// a performance cost) and uses the first argument's async `write_all` method
/// to write to buffer.
///
/// Any Future is awaited and then errors are bubbled up.
macro_rules! maybe_write {
    ($dst: expr, $fmt: expr, $($arg: tt)*) => {{
        #[cfg(feature = "sync")]
        ::std::write!($dst, $fmt, $($arg)*)?;
        #[cfg(not(feature = "sync"))]
        $dst.write_all(::std::format!($fmt, $($arg)*).as_bytes()).await?;
    }};
}

/// Writer for the HepMC2 format
#[write_bound]
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct Writer<T> {
    stream: T,
    finished: bool,
}

#[write_bound]
impl<T: Default> Writer<T> {
    /// Retrieve the underlying writer
    pub fn into_inner(mut self) -> T {
        // ensure that the destructor doesn't do anything
        self.finished = true;
        take(&mut self.stream)
    }
}

#[write_bound]
impl<T> Writer<T> {
    /// Construct new `Writer`
    ///
    /// This automatically tries to write the mandatory HepMC header,
    /// which may fail.
    ///
    /// # Example
    ///
    #[cfg_attr(feature = "sync", doc = "```")]
    #[cfg_attr(not(feature = "sync"), doc = "```ignore")]
    /// use hepmc2::writer::Writer;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::new(&mut output)?;
    /// // always call finish at the end
    /// writer.finish()?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    #[cfg_attr(feature = "sync", doc = "```ignore")]
    #[cfg_attr(not(feature = "sync"), doc = "```")]
    /// # tokio_test::block_on(async {
    /// use hepmc2::writer::Writer;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::new(&mut output).await.unwrap();
    /// // always call finish at the end
    /// writer.finish().await.unwrap();
    /// # })
    /// ```
    #[maybe_async::maybe_async]
    pub async fn new(stream: T) -> Result<Self, io::Error> {
        Self::with_header(stream, DEFAULT_HEADER).await
    }

    /// Construct new `Writer`, trying to write a custom header
    ///
    /// `hepmc2` ignores headers, but other implementations of the
    /// format may be less lenient.
    ///
    /// # Example
    ///
    /// ## Sync
    ///
    #[cfg_attr(feature = "sync", doc = "```")]
    #[cfg_attr(not(feature = "sync"), doc = "```ignore")]
    /// use hepmc2::writer::Writer;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::with_header(output, "")?;
    /// // always call finish at the end
    /// writer.finish()?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ## Async
    ///
    #[cfg_attr(feature = "sync", doc = "```ignore")]
    #[cfg_attr(not(feature = "sync"), doc = "```")]
    /// # tokio_test::block_on(async {
    /// use hepmc2::writer::Writer;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::with_header(output, "").await.unwrap();
    /// // always call finish at the end
    /// writer.finish().await.unwrap();
    /// # })
    /// ```
    #[maybe_async::maybe_async]
    pub async fn with_header<U: Display>(
        stream: T,
        header: U,
    ) -> Result<Self, io::Error> {
        let mut writer = Self {
            stream,
            finished: false,
        };
        writer.write_header(header).await?;
        Ok(writer)
    }

    /// Finish writing, consuming the `Writer`
    ///
    /// This tries to write the mandatory HepMC footer, which may fail.
    ///
    /// # Example
    ///
    /// ## Sync
    ///
    #[cfg_attr(feature = "sync", doc = "```")]
    #[cfg_attr(not(feature = "sync"), doc = "```ignore")]
    /// use hepmc2::writer::Writer;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::new(&mut output)?;
    /// // always call finish at the end
    /// writer.finish()?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ## Async
    ///
    #[cfg_attr(feature = "sync", doc = "```ignore")]
    #[cfg_attr(not(feature = "sync"), doc = "```")]
    /// # tokio_test::block_on(async {
    /// use hepmc2::writer::Writer;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::new(&mut output).await.unwrap();
    /// // always call finish at the end
    /// writer.finish().await.unwrap();
    /// # })
    /// ```
    #[maybe_async::maybe_async]
    pub async fn finish(mut self) -> Result<(), std::io::Error> {
        self.ref_finish().await
    }

    /// Write an event
    ///
    /// # Example
    ///
    /// ## Sync
    ///
    #[cfg_attr(feature = "sync", doc = "```")]
    #[cfg_attr(not(feature = "sync"), doc = "```ignore")]
    /// use hepmc2::writer::Writer;
    /// use hepmc2::event::Event;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::new(&mut output)?;
    /// let event = Event::default();
    /// writer.write(&event)?;
    /// // always call finish at the end
    /// writer.finish()?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// ## Async
    ///
    #[cfg_attr(feature = "sync", doc = "```ignore")]
    #[cfg_attr(not(feature = "sync"), doc = "```")]
    /// # tokio_test::block_on(async {
    /// use hepmc2::writer::Writer;
    /// use hepmc2::event::Event;
    ///
    /// let mut output = Vec::new();
    /// let mut writer = Writer::new(&mut output).await.unwrap();
    /// let event = Event::default();
    /// writer.write(&event).await.unwrap();
    /// // always call finish at the end
    /// writer.finish().await.unwrap();
    /// # })
    /// ```
    #[maybe_async::maybe_async]
    pub async fn write(&mut self, event: &Event) -> Result<(), io::Error> {
        self.write_event_line(event).await?;
        if !event.weight_names.is_empty() {
            self.write_weight_names_line(&event.weight_names).await?;
        }
        self.write_unit_line(event).await?;
        self.write_cross_section_line(&event.xs).await?;
        self.write_pdf_info_line(&event.pdf_info).await?;
        if let Some(hi) = event.heavy_ion_info {
            self.write_heavy_ion_info_line(&hi).await?;
        }
        for vertex in &event.vertices {
            self.write_vertex_line(vertex).await?;
            let particles = vertex
                .particles_in
                .iter()
                .chain(vertex.particles_out.iter());
            for particle in particles {
                self.write_particle_line(particle).await?;
            }
        }
        Ok(())
    }

    #[maybe_async::maybe_async]
    pub async fn try_from(stream: T) -> Result<Self, io::Error> {
        Self::with_header(stream, DEFAULT_HEADER).await
    }

    #[maybe_async::maybe_async]
    async fn ref_finish(&mut self) -> Result<(), std::io::Error> {
        self.stream.write_all(DEFAULT_FOOTER).await?;
        self.finished = true;
        Ok(())
    }

    #[maybe_async::maybe_async]
    async fn write_header<U: Display>(
        &mut self,
        header: U,
    ) -> Result<(), io::Error> {
        maybe_write!(self.stream, "{}", header);
        Ok(())
    }

    #[maybe_async::maybe_async]
    async fn write_event_line(
        &mut self,
        event: &Event,
    ) -> Result<(), io::Error> {
        maybe_write!(
            self.stream,
            "E {} {} {} {} {} {} {} {} 0 0 {}",
            event.number,
            event.mpi,
            ryu::Buffer::new().format(event.scale),
            ryu::Buffer::new().format(event.alpha_qcd),
            ryu::Buffer::new().format(event.alpha_qed),
            event.signal_process_id,
            event.signal_process_vertex,
            event.vertices.len(),
            event.random_states.len()
        );
        for state in &event.random_states {
            maybe_write!(self.stream, " {}", state);
        }
        maybe_write!(self.stream, " {}", event.weights.len());
        let mut buffer = ryu::Buffer::new();
        for weight in &event.weights {
            maybe_write!(self.stream, " {}", buffer.format(*weight));
        }
        self.stream.write_all(b"\n").await
    }

    #[maybe_async::maybe_async]
    async fn write_vertex_line(
        &mut self,
        vertex: &Vertex,
    ) -> Result<(), io::Error> {
        maybe_write!(
            self.stream,
            "V {} {} {} {} {} {} 0 {} {}",
            vertex.barcode,
            vertex.status,
            ryu::Buffer::new().format(vertex.x),
            ryu::Buffer::new().format(vertex.y),
            ryu::Buffer::new().format(vertex.z),
            ryu::Buffer::new().format(vertex.t),
            vertex.particles_in.len() + vertex.particles_out.len(),
            vertex.weights.len()
        );
        for weight in &vertex.weights {
            maybe_write!(self.stream, " {}", weight);
        }
        self.stream.write_all(b"\n").await
    }

    #[maybe_async::maybe_async]
    async fn write_particle_line(
        &mut self,
        particle: &Particle,
    ) -> Result<(), io::Error> {
        maybe_write!(
            self.stream,
            "P 0 {} {} {} {} {} {} {} {} {} {} {}",
            particle.id,
            ryu::Buffer::new().format(particle.p[1]),
            ryu::Buffer::new().format(particle.p[2]),
            ryu::Buffer::new().format(particle.p[3]),
            ryu::Buffer::new().format(particle.p[0]),
            ryu::Buffer::new().format(particle.m),
            particle.status,
            ryu::Buffer::new().format(particle.theta),
            ryu::Buffer::new().format(particle.phi),
            particle.end_vtx,
            particle.flows.len()
        );
        for (idx, val) in &particle.flows {
            maybe_write!(self.stream, " {} {}", idx, val);
        }
        self.stream.write_all(b"\n").await
    }

    #[maybe_async::maybe_async]
    async fn write_weight_names_line(
        &mut self,
        names: &[String],
    ) -> Result<(), io::Error> {
        maybe_write!(self.stream, "N {}", names.len());
        for name in names {
            maybe_write!(self.stream, r#" "{}""#, name);
        }
        self.stream.write_all(b"\n").await
    }

    #[maybe_async::maybe_async]
    async fn write_unit_line(
        &mut self,
        event: &Event,
    ) -> Result<(), io::Error> {
        maybe_write!(
            self.stream,
            "U {:?} {:?}\n",
            event.energy_unit,
            event.length_unit
        );
        Ok(())
    }

    #[maybe_async::maybe_async]
    async fn write_cross_section_line(
        &mut self,
        xs: &CrossSection,
    ) -> Result<(), io::Error> {
        maybe_write!(
            self.stream,
            "C {} {}\n",
            ryu::Buffer::new().format(xs.cross_section),
            ryu::Buffer::new().format(xs.cross_section_error)
        );
        Ok(())
    }

    #[maybe_async::maybe_async]
    async fn write_pdf_info_line(
        &mut self,
        pdf: &PdfInfo,
    ) -> Result<(), io::Error> {
        maybe_write!(
            self.stream,
            "F {} {} {} {} {} {} {} {} {}\n",
            pdf.parton_id[0],
            pdf.parton_id[1],
            ryu::Buffer::new().format(pdf.x[0]),
            ryu::Buffer::new().format(pdf.x[1]),
            ryu::Buffer::new().format(pdf.scale),
            ryu::Buffer::new().format(pdf.xf[0]),
            ryu::Buffer::new().format(pdf.xf[1]),
            pdf.pdf_id[0],
            pdf.pdf_id[1],
        );
        Ok(())
    }

    #[maybe_async::maybe_async]
    async fn write_heavy_ion_info_line(
        &mut self,
        hi: &HeavyIonInfo,
    ) -> Result<(), io::Error> {
        maybe_write!(
            self.stream,
            "H {} {} {} {} {} {} {} {} {} {} {} {} {}\n",
            hi.ncoll_hard,
            hi.npart_proj,
            hi.npart_targ,
            hi.ncoll,
            hi.spectator_neutrons,
            hi.spectator_protons,
            hi.n_nwounded_collisions,
            hi.nwounded_n_collisions,
            hi.nwounded_nwounded_collisions,
            ryu::Buffer::new().format(hi.impact_parameter),
            ryu::Buffer::new().format(hi.event_plane_angle),
            ryu::Buffer::new().format(hi.eccentricity),
            ryu::Buffer::new().format(hi.sigma_inel_nn),
        );
        Ok(())
    }
}

#[write_bound]
impl<T> Drop for Writer<T> {
    fn drop(&mut self) {
        if !self.finished {
            error!("Hepmc2 writer dropped before finished.");
            error!("Call finish() manually to fix this error.");
            #[cfg(feature = "sync")]
            if let Err(err) = self.ref_finish() {
                error!("Error writing footer: {}", err);
            }
            #[cfg(feature = "tokio")]
            tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async {
                    if let Err(err) = self.ref_finish().await {
                        error!("Error writing footer: {}", err);
                    }
                })
            });
        }
    }
}