2
$\begingroup$

Consider a file with the following block structure:

Event XX Muon YY:
data
End of event
Event XX' Muon YY':
data'
End of Event
...

where data is some dataset.

Is it possible to import such a file in the form of the following array:

XX YY data
XX' YY' data'
...

An example of the file is given below:

Event 579 Muon 103:
DecayVacuum_block1_0 -59.70285441316312 -11.036168992861251 -2427.058656624044 0.006298803597993789 12.988555908203125 7.6875 18.069478857455444 0.6584001421142602 0.08786208907228883 6.908344436247385 False False 1.0 0 1 0 -59.325958251953125 -10.744438171386719 -2479.03369140625 T1Lidbox_1 -211 211 57.90705871582031 50.38629913330078 57.90813069937492 50.39601779535441 -13 -13 103 8.02 0.015148748410865664 768 320
103 1 -59.325958251953125 -10.744438171386719 -2479.03369140625 T1Lidbox_1 3.016469955444336 -0.06643134355545044 -218.59584045410156 218.61668767860326 0 -13
103 2 -59.325958251953125 -10.744438171386719 -2479.03369140625 T1Lidbox_1 -0.9576877951622009 -0.20693859457969666 50.38629913330078 50.39601779535441 0 211
103 3 -59.325958251953125 -10.744438171386719 -2479.03369140625 T1Lidbox_1 -0.29368579387664795 -0.13572342693805695 57.90705871582031 57.90813069937492 0 -211
103 4 -59.325958251953125 -10.744438171386719 -2479.03369140625 T1Lidbox_1 -1.5919358730316162 0.01566511206328869 110.2023696899414 110.21391905794624 0 -13
103 5 -88.69776916503906 -21.007896423339844 3629.83740234375 LeadTile2_47 -0.22401176393032074 0.13002203404903412 6.7392497062683105 6.745669234149708 3 -211
103 6 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 -0.42545315623283386 -0.003640354610979557 0.3338720202445984 0.5585463092988446 5 -211
103 7 -112.54698944091797 -22.38661766052246 3671.681884765625 Hcal_1 -0.4936700761318207 0.06373818218708038 0.4781147539615631 1.1658256664829363 6 2112
103 8 -123.22480010986328 -21.007997512817383 3682.023193359375 Hcal_1 -0.28808534145355225 -0.16402196884155273 0.36775708198547363 1.0620379975837424 7 2112
103 9 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 0.11317602545022964 -0.07085563242435455 0.4059564173221588 0.4481613855729009 5 111
103 10 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 0.07441836595535278 -0.009844702668488026 0.34658995270729065 0.3546260095928353 9 22
103 11 -89.05522155761719 -20.898391723632812 3652.631591796875 LeadTile2_120 0.026991134509444237 -0.0030921336729079485 0.11994204670190811 0.12298145518598066 10 -11
103 12 -89.05522155761719 -20.898391723632812 3652.631591796875 LeadTile2_120 0.04879576712846756 -0.006259504705667496 0.22635973989963531 0.23164455801099038 10 11
103 13 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 0.20005565881729126 0.1437157839536667 1.0877169370651245 1.1239592705074106 5 -211
103 14 -88.51561737060547 -20.1934871673584 3656.058349609375 LeadTile2_131 -0.1636590212583542 -0.007074369583278894 0.46632620692253113 1.0616390723523434 13 2112
103 15 -91.81292724609375 -20.336017608642578 3665.45361328125 Hcal_1 0.03333818167448044 0.02326338179409504 0.45641598105430603 1.0441850946230768 14 2212
103 16 -88.51561737060547 -20.1934871673584 3656.058349609375 LeadTile2_131 0.3463969826698303 0.4674844443798065 0.43424779176712036 1.1873865363615275 13 2112
103 17 -81.11677551269531 -10.208284378051758 3665.33349609375 Hcal_1 0.4825921356678009 0.4339957535266876 0.24390113353729248 1.1676978689569182 16 2112
103 18 -55.82482147216797 12.536803245544434 3678.115966796875 Hcal_1 0.324808806180954 0.5985442996025085 0.04506365582346916 1.1602332166495282 17 2212
103 19 -88.51561737060547 -20.1934871673584 3656.058349609375 LeadTile2_131 -0.5540199279785156 0.5081797242164612 -0.35060909390449524 1.2533533085960697 13 2112
103 20 -93.40139770507812 -15.71196174621582 3652.96630859375 LeadTile2_121 -0.332576185464859 0.5430411696434021 -0.10555848479270935 1.1399238095223332 19 2112
103 21 -953.5825805664062 1013.046142578125 3502.44189453125 rockD_1 -0.34888577461242676 0.46342331171035767 -0.1325988471508026 1.1121366690148184 20 2112
103 22 -956.9946899414062 1017.578369140625 3501.14501953125 rockD_1 -0.38570085167884827 0.4363413453102112 -0.10547628998756409 1.1093414754456066 21 2212
103 23 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 -0.17379210889339447 0.5633984208106995 1.086050033569336 1.5516057804361711 5 2212
103 24 -90.04611206054688 -18.519445419311523 3655.73046875 LeadTile2_130 -0.718821108341217 0.19321556389331818 0.5590537190437317 1.322633799448011 23 2112
103 25 -94.03666687011719 -17.44680404663086 3658.834228515625 LeadTile2_140 -0.5999429821968079 0.06679429113864899 0.49571433663368225 1.2218464836051341 24 2112
103 26 -98.15168762207031 -16.98866081237793 3662.234375 Hcal_1 -0.5163719654083252 -0.2267114222049713 0.4111629128456116 1.1704170552652122 25 2112
103 27 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 -0.2882811427116394 -0.3462800681591034 3.5342562198638916 3.5654174526785245 5 111
103 28 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 -0.1668267548084259 -0.2658022940158844 2.0288240909576416 2.052951343231037 27 22
103 29 -89.49765014648438 -21.046911239624023 3652.589111328125 LeadTile2_120 -0.16185806691646576 -0.259988933801651 1.9835622310638428 2.0070654361898828 28 11
103 30 -89.60833740234375 -21.244556427001953 3654.1708984375 LeadTile2_125 -0.02107236534357071 -0.05223633721470833 0.4087047278881073 0.41256785394026213 29 22
103 31 -89.72205352783203 -21.526439666748047 3656.37646484375 LeadTile2_132 -0.007515617646276951 -0.01951507292687893 0.15430068969726562 0.1557121913687522 30 -11
103 32 -89.72205352783203 -21.526439666748047 3656.37646484375 LeadTile2_132 -0.013325988315045834 -0.03243802860379219 0.25444990396499634 0.2568556450848018 30 11
103 33 -89.5932388305664 -21.209577560424805 3653.887939453125 LeadTile2_124 -0.010133491829037666 -0.021490365266799927 0.18179777264595032 nan 29 22
103 34 -89.63020324707031 -21.287975311279297 3654.55126953125 LeadTile2_126 -0.00784902274608612 -0.01739034429192543 0.1465730518102646 0.14781052692381355 33 11
103 35 -89.56925201416016 -21.165510177612305 3653.52783203125 LeadTile2_123 -0.030253421515226364 -0.04851561784744263 0.3983611762523651 0.40244336425597205 29 22
103 36 -89.64429473876953 -21.285844802856445 3654.515869140625 LeadTile2_126 -0.025957126170396805 -0.03879397362470627 0.32807591557502747 0.3313801627872035 35 -11
103 37 -89.52398681640625 -21.09075164794922 3652.924560546875 LeadTile2_121 -0.05257654935121536 -0.08682318031787872 0.6697489619255066 0.677396656459482 29 22
103 38 -89.6243667602539 -21.25650978088379 3654.203125 LeadTile2_125 -0.04865559563040733 -0.08089394122362137 0.6301157474517822 0.6371477952715674 37 11
103 39 -90.1339111328125 -22.402902603149414 3661.717041015625 Hcal_1 -0.0018571860855445266 -0.02257460542023182 0.1173102855682373 0.11947704821486378 38 22
103 40 -89.7898178100586 -21.45747947692871 3655.7998046875 LeadTile2_130 -0.010960398241877556 -0.012866056524217129 0.10362830013036728 0.10499757296249462 38 22
103 41 -89.3776626586914 -20.855737686157227 3651.1298828125 TvTile2_229 -0.1214543804526329 -0.08047778159379959 1.5054320096969604 nan 27 22
103 42 -89.55453491210938 -20.972938537597656 3653.322265625 ScTile2_123 -0.0845247209072113 -0.05622992292046547 1.048455834388733 1.0533594497889684 41 -11
103 43 -89.90038299560547 -21.28132438659668 3657.935302734375 LeadTile2_137 -0.008341298438608646 -0.0025697287637740374 0.1013646200299263 0.1017397019819335 42 22
103 44 -89.7550277709961 -21.1678524017334 3656.09912109375 LeadTile2_131 -0.012116490863263607 -0.017255468294024467 0.19086869060993195 nan 42 22
103 45 -90.62996673583984 -22.41387939453125 3669.8818359375 Hcal_1 -0.006325222551822662 -0.009636974893510342 0.10792633146047592 0.10854139241692716 44 11
End of event
Event 2349 Muon 103:
DecayVacuum_block1_0 -60.969564687377435 -11.593466041824653 -2421.4118877575133 0.006298803597993789 12.988555908203125 7.6875 16.99922908311631 0.5607433764960206 0.6527958193819055 3.0962135146752705 True False 0.010000000000000009 1 1 0 -59.33159255981445 -10.744314193725586 -2478.62548828125 T1Lidbox_1 -211 211 26.736848831176758 13.327919006347656 26.741718600671877 13.332855440318848 -13 -13 103 8.02 0.06059624068439007 278 120
103 1 -59.33159255981445 -10.744314193725586 -2478.62548828125 T1Lidbox_1 3.016469955444336 -0.06643134355545044 -218.59584045410156 218.61668767860326 0 -13
103 2 -59.33159255981445 -10.744314193725586 -2478.62548828125 T1Lidbox_1 -0.1687319427728653 0.28923848271369934 13.327919006347656 13.332855440318848 0 211
103 3 -59.33159255981445 -10.744314193725586 -2478.62548828125 T1Lidbox_1 -0.31043481826782227 -0.38023510575294495 26.736848831176758 26.741718600671877 0 -211
103 4 -59.33159255981445 -10.744314193725586 -2478.62548828125 T1Lidbox_1 -2.4468164443969727 0.05356593802571297 178.51512145996094 178.5319286108239 0 -13
103 5 -131.4512481689453 -92.88449096679688 3603.236328125 T6Lid_1 0.21759210526943207 0.426509827375412 0.26712551712989807 1.0867232139816851 3 2212
103 6 -131.4512481689453 -92.88449096679688 3603.236328125 T6Lid_1 -0.2044437974691391 -0.7036125659942627 1.8508656024932861 1.9955077552443752 3 211
103 7 -144.5005340576172 -116.862060546875 3676.145751953125 Hcal_1 -0.38729825615882874 0.33661219477653503 -0.04765758290886879 1.0704827410500564 6 2212
103 8 -144.5005340576172 -116.862060546875 3676.145751953125 Hcal_1 -0.10732777416706085 -0.38572221994400024 0.3896907866001129 1.0931344189830194 6 2112
103 9 -147.68170166015625 -128.29473876953125 3687.696044921875 Hcal_1 -0.07755265384912491 -0.41723012924194336 0.1595889776945114 1.0432387566640802 8 2112
103 10 -144.5005340576172 -116.862060546875 3676.145751953125 Hcal_1 -0.04303813725709915 0.2628801465034485 0.573736310005188 1.1315855914971635 6 2212
103 11 -144.5005340576172 -116.862060546875 3676.145751953125 Hcal_1 -0.5061129331588745 -0.02835562638938427 0.4105550944805145 1.1438063188322853 6 2112
103 12 -144.5005340576172 -116.862060546875 3676.145751953125 Hcal_1 -0.26829180121421814 -0.22035817801952362 -0.06405741721391678 0.3796332220946274 6 211
103 13 -131.4512481689453 -92.88449096679688 3603.236328125 T6Lid_1 0.3474636971950531 -0.16186915338039398 0.8522919416427612 0.944888358946551 3 -211
103 14 -89.15900421142578 -99.92935180664062 3695.212646484375 Hcal_1 -0.18618591129779816 0.4343188405036926 0.025961406528949738 1.052024291615631 13 2112
103 15 -131.4512481689453 -92.88449096679688 3603.236328125 T6Lid_1 0.08798656612634659 0.7213903665542603 7.485574722290039 7.521980676732306 3 111
103 16 -131.4512481689453 -92.88447570800781 3603.236572265625 T6Lid_1 -0.03626123443245888 0.14526064693927765 1.5588064193725586 1.5659798803780591 15 22
103 17 -131.7501678466797 -91.68704223632812 3616.08642578125 LeadTile2_3 -0.016280310228466988 0.06542555242776871 0.6983613967895508 0.7016084756167978 16 -11
103 18 -131.79483032226562 -91.50151824951172 3618.322509765625 LeadTile2_10 -0.0014094897778704762 0.011693130247294903 0.13387292623519897 0.13439001575708354 17 22
103 19 -131.79025268554688 -91.54283905029297 3617.73583984375 LeadTile2_8 -0.0004590509633999318 0.012512938119471073 0.15493659675121307 nan 17 22
End of event
Event 4853 Muon 103:
DecayVacuum_block1_0 -60.6301598604297 -10.789843121298905 -2386.051380819185 0.006298803597993789 12.988555908203125 7.6875 18.56888487891804 1.2623359815515869 0.1274715850358829 5.231242867844793 True False 1.0000000000000098e-22 11 1 0 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 321 -321 41.26478958129883 73.19168090820312 41.27471041334095 73.19734585997665 -13 -13 103 8.02 3.030286294495454 720 329
103 1 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 3.016469955444336 -0.06643134355545044 -218.59584045410156 218.61668767860326 0 -13
103 2 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 -0.7547829747200012 -0.1259859800338745 73.19168090820312 73.19734585997665 0 -321
103 3 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 -0.7580353021621704 -0.022935261949896812 41.26478958129883 41.27471041334095 0 321
103 4 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 -1.1621145009994507 0.04731705039739609 94.92681121826172 94.9339949685971 0 -13
103 5 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 -0.7203018069267273 0.018894435837864876 3.533877372741699 3.6402194918803956 0 321
103 6 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 -0.09451156854629517 -0.1545814573764801 0.2316783219575882 0.32554952419132394 0 -211
103 7 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 -0.3496589958667755 0.009996508248150349 1.5533064603805542 1.598312262266069 0 211
103 8 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 0.7649329304695129 0.2625168561935425 2.6809680461883545 3.096537493972363 0 3312
103 9 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 0.2720939517021179 -0.058406487107276917 0.4951633810997009 0.5849042610150575 0 211
103 10 -59.33674621582031 -10.744200706481934 -2478.251953125 T1Lidbox_1 -0.21409180760383606 0.08961568772792816 0.7175712585449219 0.9035455018652363 0 130
103 11 -106.47944641113281 8.98904037475586 -2320.243896484375 LiScY_210220_210222 0.2558553218841553 -0.17366552352905273 0.05524223670363426 0.588466697270301 10 310
End of event
$\endgroup$
3
  • 1
    $\begingroup$ Maybe ImportString[#, "Table"] & /@ StringTrim@StringSplit[StringDelete[text, ":"], "End of event"] gets close? It's unclear what structure you want returned. $\endgroup$
    – Michael E2
    Jun 23 at 14:04
  • $\begingroup$ @MichaelE2 : just a table XX YY data, where data has its own structure. $\endgroup$ Jun 23 at 14:05
  • 1
    $\begingroup$ So delete the first row or two from each event in the output of my code? (The first row definitely looks different; the second has some data but it's not like the succeeding ones.) $\endgroup$
    – Michael E2
    Jun 23 at 14:08

1 Answer 1

4
$\begingroup$
text = Import["/tmp/data.txt"]; (* Import as "Text" *)

data = ImportString[#, "Table"] & /@ 
   StringTrim@StringSplit[StringDelete[text, ":"], "End of event"];

ds = Dataset@ Replace[
   data,
   {{e_, en_, mu_, mun_}, {decay_, ddata__}, data__} :>
     <|e -> en, mu -> mun, decay -> {ddata}, "Data" -> {data}|>,
   1]

XX YY:

ds[All, {"Event", "Muon"}]
(*
  Dataset[{
    <|"Event" -> 579, "Muon" -> 103|>,
    <|"Event" -> 2349, "Muon" -> 103|>,
    <|"Event" -> 4853, "Muon" -> 103|>}]
*)

Data:

ds[Select[#Event == 4853 &], "Data"] // Normal // First
(*
{{103, 1, -59.3367, -10.7442, -2478.25, "T1Lidbox_1",...},
 {103, 2, -59.3367, -10.7442, -2478.25, "T1Lidbox_1",...},
 ...
 {103, 11, -106.479, 8.98904, -2320.24, "LiScY_210220_210222",...}}
*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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