View Javadoc

1   package org.lcsim.util;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import org.lcsim.event.Cluster;
8   import org.lcsim.event.EventHeader;
9   import org.lcsim.event.Track;
10  import org.lcsim.event.EventHeader.LCMetaData;
11  import org.lcsim.lcio.LCIOConstants;
12  import org.lcsim.lcio.LCIOUtil;
13  
14  /**
15   * Driver to allow modification of collection LCIO bits from XML.
16   * 
17   * @author <a href="mailto:christian.grefe@cern.ch">Christian Grefe</a>
18   */
19  public class LCIOFlagDriver extends Driver {
20  	
21  	protected Map<String, Boolean> subsetCollections;
22  	protected Map<String, Boolean> transientCollections;
23  	protected Map<String, Boolean> keepClusterHits;
24  	protected Map<String, Boolean> keepTrackHits;
25  	
26  	public LCIOFlagDriver() {
27  		subsetCollections = new HashMap<String, Boolean>();
28  		transientCollections = new HashMap<String, Boolean>();
29  		keepClusterHits = new HashMap<String, Boolean>();
30  		keepTrackHits = new HashMap<String, Boolean>();
31  	}
32  	
33  	@Override
34  	protected void process(EventHeader event) {
35  		
36  		for (String collection : subsetCollections.keySet()) {
37  			getMetaData(event, collection).setSubset(subsetCollections.get(collection));
38  		}
39  		
40  		for (String collection : transientCollections.keySet()) {
41  			getMetaData(event, collection).setTransient(transientCollections.get(collection));
42  		}
43  		
44  		for (String collection : keepClusterHits.keySet()) {
45  			LCMetaData metaData = getMetaData(event, collection);
46  			if (metaData.getType().isAssignableFrom(Cluster.class)) {
47  				int flags = metaData.getFlags();
48  				boolean isSet = keepClusterHits.get(collection);
49  				if (LCIOUtil.bitTest(flags, LCIOConstants.CLBIT_HITS) != isSet) {
50  					flags = LCIOUtil.bitSet(flags, LCIOConstants.CLBIT_HITS, isSet);
51  					replaceMetaDataFlags(metaData, flags);
52  				}
53  			} else {
54  				System.out.println(getName()+": "+collection+" is not a Cluster collection. Bit not changed.");
55  			}
56  		}
57  		
58  		for (String collection : keepTrackHits.keySet()) {
59  			LCMetaData metaData = getMetaData(event, collection);
60  			if (metaData.getType().isAssignableFrom(Track.class)) {
61  				int flags = metaData.getFlags();
62  				boolean isSet = keepTrackHits.get(collection);
63  				if (LCIOUtil.bitTest(flags, LCIOConstants.TRBIT_HITS) != isSet) {
64  					flags = LCIOUtil.bitSet(flags, LCIOConstants.TRBIT_HITS, isSet);
65  					replaceMetaDataFlags(metaData, flags);
66  				}
67  			} else {
68  				System.out.println(getName()+": "+collection+" is not a Cluster collection. Bit not changed.");
69  			}
70  		}
71  		
72  	}
73  	
74  	public void setSubset(String[] collection) {
75  		parseAndAdd(subsetCollections, collection);
76  	}
77  	
78  	public void setTransient(String[] collection) {
79  		parseAndAdd(transientCollections, collection);
80  	}
81  	
82  	public void setKeepClusterHits(String[] collection) {
83  		parseAndAdd(keepClusterHits, collection);
84  	}
85  	
86  	public void setKeepTrackHits(String[] collection) {
87  		parseAndAdd(keepTrackHits, collection);
88  	}
89  	
90  	protected void replaceMetaDataFlags(LCMetaData metaData, int flags) {
91  		EventHeader event = metaData.getEvent();
92  		String collectionName = metaData.getName();
93  		Class type = metaData.getType();
94  		List collection = (List) event.get(collectionName);
95  		event.remove(collectionName);
96  		event.put(collectionName, collection, type, flags);
97  	}
98  	
99  	protected LCMetaData getMetaData(EventHeader event, String collection) {
100 		LCMetaData metaData = null;
101 		try {
102 			List list = (List) event.get(collection);
103 			metaData = event.getMetaData(list);
104 		} catch (IllegalArgumentException e) {
105 			System.err.println(getName()+": "+e.getMessage());
106 		}
107 		return metaData;
108 	}
109 	
110 	protected void parseAndAdd(Map<String, Boolean> map, String[] collection) {
111 		if (collection.length != 2) {
112 			throw new RuntimeException(getName()+": Has to be a String of length 2.");
113 		}
114 		String collectionName = collection[0];
115 		String setString = collection[1];
116 		Boolean isSet = null;
117 		if (setString.toLowerCase().equals("true")) {
118 			isSet = true;
119 		} else if (setString.toLowerCase().equals("false")) {
120 			isSet = false;
121 		} else {
122 			throw new RuntimeException(getName()+": Second String has to be either \"true\" or \"false\"");
123 		}
124 		map.put(collectionName, isSet);
125 	}
126 }