1 package org.lcsim.analysis;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import org.lcsim.conditions.ConditionsManager;
10 import org.lcsim.conditions.ConditionsSet;
11 import org.lcsim.event.EventHeader;
12 import org.lcsim.event.SimCalorimeterHit;
13 import org.lcsim.geometry.Detector;
14 import org.lcsim.geometry.IDDecoder;
15 import org.lcsim.util.Driver;
16 import org.lcsim.util.aida.AIDA;
17
18
19
20
21
22
23
24 public class CalorimeterOccupancyDriver extends Driver
25 {
26
27 private boolean _debug = true;
28 private Set<String> collections = new HashSet<String>();
29 private Map<String, Map<Long, Integer>> cellCountMaps = new HashMap<String, Map<Long, Integer>>();
30 private Map<String, IDDecoder> _idDecoders = new HashMap<String, IDDecoder>();
31 private AIDA aida = AIDA.defaultInstance();
32
33 private ConditionsSet _cond;
34 private double _ECalMipCut;
35
36 public CalorimeterOccupancyDriver()
37 {
38
39 }
40
41 @Override
42 protected void detectorChanged(Detector detector)
43 {
44 ConditionsManager mgr = ConditionsManager.defaultInstance();
45 try {
46 _cond = mgr.getConditions("CalorimeterCalibration");
47 System.out.println("found conditions for " + detector.getName());
48 _ECalMipCut = _cond.getDouble("ECalMip_Cut");
49 System.out.println("_ECalMipCut = "+_ECalMipCut);
50 } catch (ConditionsManager.ConditionsSetNotFoundException e) {
51 System.out.println("ConditionSet CalorimeterCalibration not found for detector " + mgr.getDetector());
52 System.out.println("Please check that this properties file exists for this detector ");
53 }
54 }
55
56 @Override
57 protected void process(EventHeader event)
58 {
59
60 for (String collectionName : collections) {
61
62 List<SimCalorimeterHit> hits = event.get(SimCalorimeterHit.class, collectionName);
63 log("There are " + hits.size() + " " + collectionName);
64
65 Map<Long, Integer> map = cellCountMaps.get(collectionName);
66
67 for (SimCalorimeterHit hit : hits) {
68 if (!_idDecoders.containsKey(collectionName)) {
69 _idDecoders.put(collectionName, hit.getIDDecoder());
70 }
71 double rawEnergy = hit.getRawEnergy();
72 aida.cloud1D(collectionName + " hit Energy").fill(rawEnergy);
73 if (rawEnergy > _ECalMipCut) {
74 aida.cloud1D(collectionName + " hit Energy after cut").fill(rawEnergy);
75 long cellId = hit.getCellID();
76
77 if (map.containsKey(cellId)) {
78
79 map.put(cellId, map.get(cellId) + 1);
80 } else {
81 map.put(cellId, 1);
82 }
83 }
84 }
85 }
86 }
87
88 @Override
89 protected void endOfData()
90 {
91
92
93 for (String collectionName : collections) {
94
95 System.out.println(collectionName);
96 Map<Long, Integer> map = cellCountMaps.get(collectionName);
97
98 IDDecoder idDecoder = _idDecoders.get(collectionName);
99
100 Set<Long> keys = map.keySet();
101
102 for (Long key : keys) {
103 idDecoder.setID(key);
104 int layer = idDecoder.getLayer();
105 double[] pos = idDecoder.getPosition();
106 Integer hitCount = map.get(key);
107
108 if (hitCount > 3) {
109 System.out.println(collectionName + " id " + key + " has " + hitCount + " hits.");
110 }
111 aida.histogram1D(collectionName + "layer " + layer + " occupancy rates", 100, 0., 100.).fill(hitCount);
112 aida.cloud2D(collectionName + "layer " + layer + " occupancy rates vs position").fill(pos[0],pos[1],hitCount);
113
114 }
115 }
116 }
117
118 public void setCollectionNames(String[] collectionNames)
119 {
120 System.out.println("there are " + collectionNames.length+ " collections to process: ");
121 collections.addAll(Arrays.asList(collectionNames));
122 System.out.println("processing: ");
123 for (String collectionName : collections) {
124 System.out.println(collectionName);
125 cellCountMaps.put(collectionName, new HashMap<Long, Integer>());
126 }
127 }
128
129 private void log(String s)
130 {
131 if (_debug) {
132 System.out.println(s);
133 }
134 }
135 }