1
$\begingroup$

Consider the following file. It has the form

HepMC::Version 2.06.09
HepMC::IO_GenEvent-START_EVENT_LISTING
E ...
N ...
U ...
C ...
V ...
P ... YY 0 0 XX 0
V ...
P ... 2 0 0 0 0
P ... 2 0 0 0 0
P ... 1 0 0 0 0
P ... 1 0 0 0 0
E ...
...

Could you please tell me how to extract only the rows

E ...

and those starting from P with

P ... 1 0 0 0 0

This is how I tried to do it:

file = Import["tag_1_pythia8_events.hepmc", "Table"];
Cases[file, {"E", __} || {__, 1, 0, 0, 0, 0}]

But I do not know the syntax well: the "||" construction does not work.

$\endgroup$
1
  • $\begingroup$ Use Alternatives instead of Or. That is, use single | instead of double ||. $\endgroup$
    – LouisB
    Aug 22, 2023 at 1:54

2 Answers 2

2
$\begingroup$

In a method very similar to what I did here, I split the data by newlines, and find all line numbers that either start with "E" or "P ... 1 0 0 0 0"

splitData = StringSplit[data, "\n"];
pTest = "P ... 1 0 0 0 0";
l = pTest // StringLength;
firstLetters = StringTake[splitData, 1];
moreLetters = StringTake[splitData, {1, l}];

ePos = Flatten[Position[firstLetters, "E"]];
pPos = Flatten[Position[moreLetters, pTest]];
allPos = Join[ePos, pPos] // Sort
(*{3, 12, 13, 14}*)

and then just grab each of those lines from splitData:

splitData[[allPos]]
(*{"E ...", "P ... 1 0 0 0 0", "P ... 1 0 0 0 0", "E ..."}*)
$\endgroup$
1
$\begingroup$

Building up on the comment from @LouisB:

file = Import["C:/tag_1_pythia8_events.hepmc", "Table"];
SequenceCases[file, {{"E", ___}} | {{"P", ___, 1, 0 ..}}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.