Regular expression matching variable at end of string

39 Views Asked by At

I have some histograms in a .root file which are called "Name/NameEvent1Ch0", "Name/NameEvent1Ch1"... "Name/NameEvent1Ch31" ... "Name/NameEvent2Ch0"... "Name/NameEvent{x}Ch32"...

I want to create two lists, which match the characters after Ch, the last characters in the string. All the histograms of channels in x_channels should go into analogues_x and the others should go into analogues_y.

When there was only one moving part, that is when the strings were only named for the channel and not the event (Name/NameCh{0-31}) it was easy, I just did analogues_x = [root_file.Get(f"Target/TargetCh{ch}") for ch in x_channels]. Now that there's another variable I don't know how to do this in the most efficient way.

I'm extremely inexpert in regular expressions but it seems like this might be the way forward?

The following code:

x_channels = [8,7,9,6,10,5,11,4,12,3,13,2,14,1,15,0]
y_channels = [27,28,26,29,25,30,24,31,23,16,22,17,21,18,20,19]
analogues_x = [root_file.Get(re.compile(rf"Ch{ch}")) for ch in x_channels]

Gives me the error:

TypeError: bad argument type for built-in operation

Edit: I was asked to make it clearer what the input was and what the desired output was so: The input variable is a .root file. It contains histograms which you access using root_file.Get("name_of_my_histogram"). The histograms are named "histoevent1ch0", "histoevent1ch1", ... "histoevent19ch31", where 19 is not really 19 but is some number. I want to select all of the histograms that end with numbers in x_channels and put them in one list, and all of the histograms ending with numbers in y_channls and put them in another list.

I hope that's clear enough

0

There are 0 best solutions below