1 package org.lcsim.util;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.lcsim.event.EventHeader;
11 import org.lcsim.event.SimCalorimeterHit;
12 import org.lcsim.lcio.LCIOWriter;
13
14
15
16
17
18
19
20 public class MergeEventsDriver extends Driver {
21
22 protected int nEvts;
23 protected String outputFile;
24 protected EventHeader currentEvent;
25 protected int currentEventNumber;
26 protected LCIOWriter writer;
27 protected Collection<String> ignoreCollections;
28 protected boolean writeOnlyFullEvents;
29 protected Map<String, Map<Long, SimCalorimeterHit>> caloHitMaps;
30
31 public MergeEventsDriver() {
32 nEvts = 1;
33 ignoreCollections = new ArrayList<String>();
34 writeOnlyFullEvents = false;
35 }
36
37
38
39
40
41
42 public void setNumberOfEvents(int nEvts) {
43 this.nEvts = nEvts;
44 }
45
46
47
48
49
50
51 public void setWriteOnlyFullEvents(boolean writeOnlyFullEvents) {
52 this.writeOnlyFullEvents = writeOnlyFullEvents;
53 }
54
55
56
57
58
59 public void setOutputFile(String outputFile) {
60 this.outputFile = outputFile;
61 }
62
63 public void setIgnoreCollection(String collectionName) {
64 this.ignoreCollections.add(collectionName);
65 }
66
67 public void setIgnoreCollections(String[] collectionNames) {
68 this.ignoreCollections.addAll(Arrays.asList(collectionNames));
69 }
70
71 @Override
72 protected void startOfData() {
73 try {
74 writer = new LCIOWriter(outputFile);
75 } catch (IOException e) {
76 throw new RuntimeException(e.getMessage());
77 }
78 writer.addAllIgnore(ignoreCollections);
79
80 caloHitMaps = new HashMap<String, Map<Long,SimCalorimeterHit>>();
81 currentEventNumber = 0;
82 };
83
84 @Override
85 protected void process(EventHeader event) {
86 if (currentEventNumber == 0 && nEvts != 1) {
87 currentEvent = event;
88 } else if (nEvts > 0 && currentEventNumber % nEvts == 0) {
89 writeCurrentEvent();
90 currentEvent = event;
91 caloHitMaps.clear();
92 } else {
93 MergeEventTools.mergeEvents(currentEvent, event, ignoreCollections, caloHitMaps);
94 }
95 currentEventNumber++;
96 }
97
98 @Override
99 protected void endOfData() {
100 if (!writeOnlyFullEvents || (nEvts > 0 && currentEventNumber % nEvts == 0)) {
101 writeCurrentEvent();
102 }
103 try {
104 writer.close();
105 } catch (IOException e) {
106 e.printStackTrace();
107 }
108 }
109
110 protected void writeCurrentEvent() {
111 if (currentEvent != null) {
112 try {
113 writer.write(currentEvent);
114 } catch (IOException e) {
115 e.printStackTrace();
116 }
117 }
118 }
119 }