mctools / mcpl

Monte Carlo Particle Lists
https://mctools.github.io/mcpl/
Other
28 stars 13 forks source link

Feature request: Enable filtering options in the non-DG framework mcpltool #44

Open willend opened 5 years ago

willend commented 5 years ago

Having access to the nice "filtering grammar" from the DG code "ess_mcplextra_filterfile" in the general mcpltool (fat version) would be nice.

For now I have implemented a quick and dirty solution / example for McStas specific use, available through the instrument files

https://github.com/McStasMcXtrace/McCode/blob/master/mcstas-comps/examples/MCPL_filter_energy.instr and https://github.com/McStasMcXtrace/McCode/blob/master/mcstas-comps/examples/MCPL_filter_radius.instr

(which for the McStas savvy allows to do to similar filtering operations)

tkittel commented 5 years ago

Absolutely, I would like to make some of the mcplextra tools from dgcode available in the mcpl release as well. It won't be part of the mcpltool executable itself though, as that one should not depend on C++ code, but likely in some other optional command line tool.

Great with the McStas instrument examples, we should at some point link them from https://mctools.github.io/mcpl/hooks_mcstas/.

I would like to remind anyone interested in this that it is not difficult to write a simple C programme which does the same as ess_mcplextra_filterfile. Here is the example from https://mctools.github.io/mcpl/usage_c/ without comments, showing how to extract all neutrons with ekin below some threshold:

#include "mcpl.h"
#include <stdio.h>

int main(int argc,char**argv) {
  if (argc!=3) {
    printf("Please supply input and output filenames\n");
    return 1;
  }
  mcpl_file_t fi = mcpl_open_file(argv[1]);
  mcpl_outfile_t fo = mcpl_create_outfile(argv[2]);
  mcpl_transfer_metadata(fi, fo);
  mcpl_hdr_add_comment(fo,"Applied custom filter to select neutrons with ekin<0.1MeV");
  const mcpl_particle_t* particle;
  while ( ( particle = mcpl_read(fi) ) ) {
    if ( particle->pdgcode == 2112 && particle->ekin < 0.1 )
      mcpl_add_particle(fo,particle);
  }
  mcpl_close_outfile(fo);
  mcpl_close_file(fi);
}
willend commented 5 years ago

Thanks for the comments - and the c-snippet. Really simple indeed.

I will give myself a McCode ticket to remember to document all our MCPL oriented example instruments.