fastpasta 1.22.0

CLI for verifying or examining readout data from the ALICE detector.
Documentation
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Contains the [Cfg] struct that parses and stores the command line arguments
//!
//! [Cfg] uses procedural macros from the `clap` library to implement most of the argument parsing and validation logic.
//! The [Cfg] struct implements several options and subcommands, as well as convenience functions to get various parts of the configuration

// Unfortunately needed because of the arg_enum macro not handling doc comments properly
#![allow(non_camel_case_types)]

use crate::util::*;
use crate::words::its::layer_stave_string_to_feeid;
use clap::{Args, Parser, Subcommand};
use clap_complete::Shell;

pub mod check;
pub mod custom_checks;
pub mod inputoutput;
pub mod lib;
pub mod prelude;
pub mod test_util;
pub mod util;
pub mod view;
/// The [CONFIG] static variable is used to store the [Cfg] created from the parsed command line arguments
pub static CONFIG: OnceLock<Cfg> = OnceLock::new();
/// The [CUSTOM_CHECKS] static variable is used to store the [CustomChecks] created from the a TOML file specified through the parsed command line arguments
static CUSTOM_CHECKS: OnceLock<CustomChecks> = OnceLock::new();

/// The [Cfg] struct uses procedural macros and implements the [Config] trait, to provide convenient access to the command line arguments.
#[derive(Parser, Debug)]
#[command(name = "fastPASTA - fast Protocol Analysis Scanning Tool for ALICE")]
#[command(bin_name = "fastpasta", version)]
#[command(author = "Marc König <mbkj@tutamail.com>")]
#[command(about = "fastPASTA scans through ALICE Readout System's raw data output.")]
#[command(
    long_about = "\nfastpasta scans through ALICE Readout System's raw data output.\n\
It can report validation fails, display data in a human\n\
readable way, or filter the data.\n\
\n\
Project home page: https://gitlab.cern.ch/mkonig/fastpasta"
)]
#[command(propagate_version = true)]
#[command(styles = lib::styles())]
pub struct Cfg {
    /// Input file (default: stdin)
    #[arg(name = "Raw Data", global = true, value_hint = clap::ValueHint::FilePath)]
    file: Option<PathBuf>,

    /// Commands such as `Check` or `View` that accepts further subcommands
    #[command(subcommand)]
    cmd: Option<Command>,

    /// Verbosity level 0-4 (Errors, Warnings, Info, Debug, Trace)
    #[arg(short = 'v', long = "verbosity", default_value_t = 1, global = true)]
    verbosity: u8,

    /// Max tolerate errors before exiting, if set to 0 -> no limit to errors
    #[arg(
        short = 'e',
        long = "max-tolerate-errors",
        visible_aliases = ["max-errors", "tolerate-errors", "stop-at-error-count"],
        default_value_t = 0,
        global = true
    )]
    max_tolerate_errors: u32,

    /// Set the exit code for if any errors are detected in the input data (cannot be 0)
    #[arg(
        short = 'E',
        long = "any-errors-exit-code",
        visible_alias = "exit-code",
        global = true
    )]
    any_errors_exit_code: Option<u8>,

    /// Set CRU link ID to filter by (e.g. 5)
    #[arg(
        short = 'f',
        long,
        visible_alias = "link",
        global = true,
        group = "filter"
    )]
    filter_link: Option<u8>,

    /// Set FEE ID to filter by (e.g. 20522)
    #[arg(
        short = 'F',
        long,
        visible_alias = "fee",
        global = true,
        group = "filter"
    )]
    filter_fee: Option<u16>,

    /// Set ITS layer & stave to filter by (e.g. L5_42)
    #[arg(
        short = 's',
        long,
        name = "FILTER-ITS-STAVE",
        visible_aliases = ["its-stave", "stave"],
        global = true,
        group = "filter"
    )]
    filter_its_stave: Option<String>,

    /// Enables checks on the ITS trigger period with the specified value, usable with the `check all its-stave` command
    #[arg(short = 'p', long, global = true, requires = "FILTER-ITS-STAVE")]
    its_trigger_period: Option<u16>,

    /// Output raw data (default: stdout), requires setting a filter option. If Checks or Views are enabled, the output is supressed.
    #[arg(
        name = "OUTPUT DATA",
        short = 'o',
        long = "output",
        visible_alias = "out",
        global = true,
        requires = "filter"
    )]
    output: Option<PathBuf>,

    /// Don't show error messages - helpful if there's a large amount of errors and you just want to see the report
    #[arg(short, long, default_value_t = false, global = true)]
    mute_errors: bool,

    /// Generate a check TOML file in the current directory that can be used as a template to configure checks against the raw data.
    #[arg(short, long, default_value_t = false, global = true, visible_aliases = ["gen-toml", "gen-checks"],)]
    generate_checks_toml: bool,

    /// Path to a checks TOML file that can be used to specify and customize certain checks against the raw data.
    #[arg(
        short = 'c',
        long,
        global = true,
        visible_aliases = ["custom-checks", "checks-file"],
        value_hint = clap::ValueHint::FilePath
      )]
    checks_toml: Option<PathBuf>,

    /// Output stats (default: none), requires setting a data format option (JSON, TOML)
    #[arg(
        name = "OUTPUT FINAL STATS",
        short = 'S',
        long = "output-stats",
        default_value_t = DataOutputMode::None,
        visible_aliases = ["output-stats-report","output-final-stats"],
        global = true,
        requires = "STATS FORMAT",
    )]
    stats_output: DataOutputMode,

    /// Output stats format (JSON/TOML), requires setting a stats output option
    #[arg(
        name = "STATS FORMAT",
        short = 'D',
        long = "stats-format",
        visible_alias = "stats-data-format",
        global = true,
        requires = "OUTPUT FINAL STATS"
    )]
    stats_output_format: Option<DataOutputFormat>,

    /// Input stats file to read from and verify match with collected stats at end of analysis.
    /// The file extension determines the format (JSON/TOML)
    #[arg(
        name = "INPUT STATS FILE",
        short = 'i',
        long = "input-stats-file",
        visible_aliases = ["input-stats", "stats-file", "verify-stats"],
        global = true,
        value_hint = clap::ValueHint::FilePath
    )]
    input_stats_file: Option<PathBuf>,

    /// Show only error with the specified error codes, e.g. `-w 32 002 5` to only show errors with error codes 32, 002 and 5
    /// Note: ALPIDE errors cannot be filtered this way as they are connected to a lane eror code.
    #[arg(
        short = 'w',
        long = "show-only-errors-with-codes",
        visible_aliases = ["error-codes", "errors-with-code", "show-with-code", "show-errors-with-codes"],
        global = true,
        value_delimiter = ' ',
        num_args = 1..
    )]
    show_error_codes: Vec<String>,

    /// Generate completion scripts for the specified shell.
    /// Note: The completion script is printed to stdout
    #[arg(
        long = "generate-completions",
        value_hint = clap::ValueHint::Other,
        value_name = "SHELL"
    )]
    pub generate_completions: Option<clap_complete::Shell>,

    /// Output from view commands is styled by default, set this flag to disable styled views
    #[arg(short = 'd', long, global = true, default_value_t = false)]
    disable_styled_views: bool,
}

impl Cfg {
    /// Get a reference to the global config
    pub fn global() -> &'static Cfg {
        CONFIG.get().expect("Config is not initialized")
    }

    /// If a checks TOML file is specified, parse it and set the custom checks static variable.
    /// If the checks TOML file is not specified, but the `--gen-checks-toml` flag is set, generate a checks TOML file in the current directory.
    pub fn handle_custom_checks(&self) {
        if let Some(checks_toml) = &self.checks_toml {
            let custom_checks = self.custom_checks_from_path(checks_toml);
            CUSTOM_CHECKS
                .set(custom_checks)
                .expect("Custom checks already initialized");
        } else if self.generate_custom_checks_toml_enabled() {
            self.generate_custom_checks_toml("custom_checks.toml");
        }
    }
}

impl Cfg {
    /// Generate completion scripts for the specified shell.
    pub fn generate_completion_script(shell: Shell) {
        clap_complete::generate(
            shell,
            &mut <Cfg as clap::CommandFactory>::command(),
            "fastpasta",
            &mut io::stdout(),
        );
    }
}

/// Implementing the config super trait requires implementing all the sub traits
impl Config for Cfg {}

impl ViewOpt for Cfg {
    #[inline]
    fn view(&self) -> Option<ViewCommands> {
        if let Some(sub_cmd) = &self.cmd {
            match sub_cmd {
                Command::View(view_sub_cmd) => Some(view_sub_cmd.cmd),
                Command::Check(_) => None,
            }
        } else {
            None
        }
    }
}

impl FilterOpt for Cfg {
    fn skip_payload(&self) -> bool {
        match (self.view(), self.check(), self.output_mode()) {
            // Skip payload in these cases
            (Some(ViewCommands::Rdh), _, _) => true,
            (_, Some(CheckCommands::All(arg)), _) | (_, Some(CheckCommands::Sanity(arg)), _)
                if arg.target.is_none() =>
            {
                true
            }
            // Don't skip payload in all other cases than above
            (_, _, _) => false,
        }
    }

    #[inline]
    fn filter_link(&self) -> Option<u8> {
        self.filter_link
    }

    fn filter_fee(&self) -> Option<u16> {
        self.filter_fee
    }

    fn filter_its_stave(&self) -> Option<u16> {
        if let Some(stave_layer) = &self.filter_its_stave {
            // Start with something like "l2_1"
            // 1. check if the first char is an L, if so, it's the Lx_x format
            if stave_layer.to_uppercase().starts_with('L') {
                Some(layer_stave_string_to_feeid(stave_layer).expect("Invalid FEE ID"))
            } else {
                panic!("Invalid ITS layer & stave format, expected L[x]_[y], e.g. L2_13")
            }
        } else {
            None
        }
    }
}

impl ChecksOpt for Cfg {
    #[inline]
    fn check(&self) -> Option<CheckCommands> {
        if let Some(sub_cmd) = &self.cmd {
            match sub_cmd {
                Command::Check(checks) => match checks.cmd.clone() {
                    CheckCommands::All(arg) => Some(CheckCommands::All(arg)),
                    CheckCommands::Sanity(arg) => Some(CheckCommands::Sanity(arg)),
                },
                Command::View(_) => None,
            }
        } else {
            None
        }
    }

    fn check_its_trigger_period(&self) -> Option<u16> {
        self.its_trigger_period
    }
}

impl InputOutputOpt for Cfg {
    #[inline]
    fn input_file(&self) -> Option<&Path> {
        self.file.as_deref()
    }
    #[inline]
    fn output(&self) -> Option<&Path> {
        self.output.as_deref()
    }
    // Determine data output mode
    #[inline]
    fn output_mode(&self) -> DataOutputMode {
        if self.output().is_some() {
            // if output is set to "stdout" output to stdout
            if self.output().as_ref().unwrap().to_str() == Some("stdout") {
                DataOutputMode::Stdout
            }
            // if output is set and a file path is given, output to file
            else {
                DataOutputMode::File(self.output().unwrap().into())
            }
        }
        // if output is not set, but checks or prints are enabled, suppress output
        else if self.check().is_some() || self.view().is_some() {
            DataOutputMode::None
        }
        // if output is not set and no checks are enabled, output to stdout
        else {
            DataOutputMode::Stdout
        }
    }

    fn stats_output_mode(&self) -> DataOutputMode {
        self.stats_output.clone()
    }

    fn stats_output_format(&self) -> Option<DataOutputFormat> {
        self.stats_output_format
    }

    fn input_stats_file(&self) -> Option<&Path> {
        self.input_stats_file.as_deref()
    }
}

impl UtilOpt for Cfg {
    #[inline]
    fn verbosity(&self) -> u8 {
        self.verbosity
    }
    #[inline]
    fn max_tolerate_errors(&self) -> u32 {
        self.max_tolerate_errors
    }
    fn any_errors_exit_code(&self) -> Option<u8> {
        self.any_errors_exit_code
    }
    fn mute_errors(&self) -> bool {
        self.mute_errors
    }

    fn error_code_filter(&self) -> Option<&[String]> {
        if self.show_error_codes.is_empty() {
            None
        } else {
            Some(&self.show_error_codes)
        }
    }

    #[inline]
    fn disable_styled_views(&self) -> bool {
        self.disable_styled_views
    }
}

impl CustomChecksOpt for Cfg {
    /// Get a reference to the [CustomChecks] struct, if it is initialized
    fn custom_checks(&self) -> Option<&'static CustomChecks> {
        CUSTOM_CHECKS.get()
    }

    fn custom_checks_enabled(&'static self) -> bool {
        self.custom_checks()
            .is_some_and(|c| *c != CustomChecks::default())
    }

    fn generate_custom_checks_toml_enabled(&self) -> bool {
        self.generate_checks_toml
    }

    fn cdps(&'static self) -> Option<u32> {
        if self.checks_toml.is_some() {
            self.custom_checks()
                .expect("Custom checks are not initialized")
                .cdps()
        } else {
            None
        }
    }

    fn triggers_pht(&'static self) -> Option<u32> {
        if self.checks_toml.is_some() {
            self.custom_checks()
                .expect("Custom checks are not initialized")
                .triggers_pht()
        } else {
            None
        }
    }

    fn rdh_version(&'static self) -> Option<u8> {
        if self.checks_toml.is_some() {
            self.custom_checks()
                .expect("Custom checks are not initialized")
                .rdh_version()
        } else {
            None
        }
    }

    fn chip_orders_ob(&'static self) -> Option<&[Vec<u8>]> {
        if self.checks_toml.is_some() {
            self.custom_checks()
                .expect("Custom checks are not initialized")
                .chip_orders_ob()
        } else {
            None
        }
    }

    fn chip_count_ob(&'static self) -> Option<u8> {
        if self.checks_toml.is_some() {
            self.custom_checks()
                .expect("Custom checks are not initialized")
                .chip_count_ob()
        } else {
            None
        }
    }
}

/// Holds the [CheckCommands] subcommands
#[derive(Debug, Args, Clone)]
#[command(args_conflicts_with_subcommands = true)]
#[command(arg_required_else_help = true)]
pub struct CheckArgs {
    #[command(subcommand)]
    cmd: CheckCommands,
}
/// Holds the [ViewCommands] subcommands
#[derive(Debug, Args, Clone, Copy)]
#[command(args_conflicts_with_subcommands = true)]
#[command(arg_required_else_help = true)]
pub struct ViewArgs {
    #[command(subcommand)]
    cmd: ViewCommands,
}

#[derive(Debug, Subcommand, Clone)]
/// Subcommands to enable checks or views, needs to be followed by a [CheckCommands] (and optionally a target system) or [ViewCommands] subcommand.
pub enum Command {
    /// Enable check mode, requires a `check mode` subcommand [all/sanity] and optionally a target system
    #[command(arg_required_else_help = true)]
    Check(CheckArgs),
    /// Enable view mode, requires a `focus` type subcommand e.g. `rdh` or `its-readout-frames`
    #[command(arg_required_else_help = true)]
    View(ViewArgs),
}

impl CheckCommands {
    /// Get the target system for the check
    pub fn target(&self) -> Option<check::System> {
        match self {
            CheckCommands::All(arg) | CheckCommands::Sanity(arg) => arg.target,
        }
    }
}

/// Get the [config][super::config::Cfg] from the command line arguments and set the static [CONFIG] variable.
pub fn init_config() -> Result<(), String> {
    let cfg = <super::config::Cfg as clap::Parser>::parse();
    cfg.validate_args()?;
    cfg.handle_custom_checks();
    crate::config::CONFIG.set(cfg).unwrap();
    Ok(())
}