2 added + 2 modified, total 4 files
lcsim/src/org/lcsim/contrib/seedtracker
diff -N StrategyXMLUtils.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ StrategyXMLUtils.java 11 Jul 2008 21:40:10 -0000 1.1
@@ -0,0 +1,138 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.lcsim.contrib.seedtracker;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import org.jdom.Comment;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.jdom.output.Format;
+import org.jdom.output.XMLOutputter;
+import org.lcsim.contrib.seedtracker.SeedLayer.SeedType;
+import org.lcsim.geometry.subdetector.BarrelEndcapFlag;
+
+/**
+ *
+ * @author cozzy
+ */
+public class StrategyXMLUtils {
+
+
+
+ /**
+ * Attempts to parse the given file as an XML file containing a well-formed
+ * list of strategies. If successful, a list of SeedStrategies corresponding
+ * to the strategies defined in the file is generated.
+ *
+ * @param file A File object corresponding to the XML file.
+ * @return
+ */
+ public static List<SeedStrategy> getStrategyListFromFile(File file) {
+
+ if (! file.exists() ){
+ throw new RuntimeException("File "+file.toString()+" not found");
+ }
+
+ Document doc;
+ SAXBuilder builder = new SAXBuilder();
+ try {
+ doc = builder.build(file);
+ } catch (JDOMException jdom) {
+ jdom.printStackTrace();
+ throw new RuntimeException("JDOM exception occurred");
+ } catch (IOException io ) {
+ io.printStackTrace();
+ throw new RuntimeException("IO Exception occurred");
+ }
+
+ Element listElement = doc.getRootElement();
+ List<SeedStrategy> ret = new ArrayList<SeedStrategy>();
+
+ try {
+ for (Object o : listElement.getChildren("Strategy")){
+ Element e = (Element) o;
+ SeedStrategy strat = new SeedStrategy(e.getAttributeValue("name"));
+ strat.putBadHitChisq(Double.valueOf(e.getChild("BadHitChisq").getText()));
+ strat.putMaxChisq(Double.valueOf(e.getChild("MaxChisq").getText()));
+ strat.putMaxDCA(Double.valueOf(e.getChild("MaxDCA").getText()));
+ strat.putMaxZ0(Double.valueOf(e.getChild("MaxZ0").getText()));
+ strat.putMinConfirm(Integer.valueOf(e.getChild("MinConfirm").getText()));
+ strat.putMinHits(Integer.valueOf(e.getChild("MinHits").getText()));
+ strat.putMinPT(Double.valueOf(e.getChild("MinPT").getText()));
+
+ for (Object ol : e.getChildren("Layer")){
+ Element l = (Element) ol;
+ String detName = l.getAttributeValue("detector_name");
+ int layer_number = Integer.valueOf(l.getAttributeValue("layer_number")).intValue();
+ SeedType type = SeedType.valueOf(l.getAttributeValue("type"));
+ BarrelEndcapFlag beflag = BarrelEndcapFlag.valueOf(l.getAttributeValue("be_flag"));
+ SeedLayer lyr = new SeedLayer(detName, layer_number, beflag, type);
+ strat.addLayer(lyr);
+ }
+ ret.add(strat);
+ }
+ } catch (NullPointerException npe) {
+ npe.printStackTrace();
+ throw new RuntimeException("NullPointerException thrown. See output for details. This probably means the XML is malformed");
+ }
+
+ return ret;
+ }
+ /**
+ * Generates an XML file representing the given strategy list.
+ * @param strategyList
+ * @param file
+ * @return
+ */
+ public static boolean writeStrategyListToFile(List<SeedStrategy> strategyList, File file) {
+
+ Element listElement = new Element("StrategyList");
+ Document doc = new Document(listElement);
+
+ for (SeedStrategy strat : strategyList){
+
+ Element strategy = new Element("Strategy");
+ strategy.setAttribute("name",strat.getName());
+ strategy.addContent(new Comment("Cutoffs"));
+ strategy.addContent(new Element("MinPT").addContent(String.valueOf(strat.getMinPT())));
+ strategy.addContent(new Element("MinHits").addContent(String.valueOf(strat.getMinHits())));
+ strategy.addContent(new Element("MinConfirm").addContent(String.valueOf(strat.getMinConfirm())));
+ strategy.addContent(new Element("MaxDCA").addContent(String.valueOf(strat.getMaxDCA())));
+ strategy.addContent(new Element("MaxZ0").addContent(String.valueOf(strat.getMaxZ0())));
+ strategy.addContent(new Element("MaxChisq").addContent(String.valueOf(strat.getMaxChisq())));
+ strategy.addContent(new Element("BadHitChisq").addContent(String.valueOf(strat.getBadHitChisq())));
+
+ strategy.addContent(new Comment("Layers"));
+ for (SeedLayer lyr : strat.getLayerList()){
+ Element layer = new Element("Layer");
+ layer.setAttribute("type", lyr.getType().toString());
+ layer.setAttribute("layer_number", String.valueOf(lyr.getLayer()));
+ layer.setAttribute("detector_name", lyr.getDetName());
+ layer.setAttribute("be_flag", lyr.getBarrelEndcapFlag().toString());
+ strategy.addContent(layer);
+ }
+ listElement.addContent(strategy);
+
+ }
+ try {
+ XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
+ FileWriter fw = new FileWriter(file);
+ out.output(doc, fw);
+ } catch (Exception e){
+ e.printStackTrace();
+ return false;
+ }
+ return true;
+ }
+
+
+}
lcsim/src/org/lcsim/contrib/seedtracker
diff -u -r1.3 -r1.4
--- SeedLayer.java 11 Aug 2007 00:17:32 -0000 1.3
+++ SeedLayer.java 11 Jul 2008 21:40:10 -0000 1.4
@@ -82,4 +82,14 @@
public SeedType getType() {
return _type;
}
+
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) return true;
+ if (!(other instanceof SeedLayer)) return false;
+ SeedLayer lyr = (SeedLayer) other;
+
+ return (this._beflag.equals(lyr._beflag) && this._detname.equals(lyr.getDetName())
+ && this._layer == lyr.getLayer() && this._type.equals(lyr.getType()));
+ }
}
lcsim/src/org/lcsim/contrib/seedtracker
diff -u -r1.6 -r1.7
--- SeedStrategy.java 4 Apr 2008 23:41:33 -0000 1.6
+++ SeedStrategy.java 11 Jul 2008 21:40:10 -0000 1.7
@@ -228,4 +228,23 @@
}
return layers;
}
+
+ @Override
+ public boolean equals(Object other) {
+
+ if (this == other) return true;
+ if (!(other instanceof SeedStrategy)) return false;
+ SeedStrategy strat = (SeedStrategy) other;
+
+ return ( this._BadHitChisq == strat.getBadHitChisq()
+ && this._MaxChisq == strat.getMaxChisq()
+ && this._MaxDCA == strat.getMaxDCA()
+ && this._MaxZ0 == strat.getMaxZ0()
+ && this._MinConfirm == strat.getMinConfirm()
+ && this._MinHits == strat.getMinHits()
+ && this._MinPT == strat.getMinPT()
+ && this._Name.equals(strat.getName())
+ && this._LayerList.equals(strat.getLayerList()) ) ;
+
+ }
}
\ No newline at end of file
lcsim/test/org/lcsim/contrib/seedtracker
diff -N SeedStrategyXMLTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SeedStrategyXMLTest.java 11 Jul 2008 21:40:10 -0000 1.1
@@ -0,0 +1,31 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.lcsim.contrib.seedtracker;
+
+import java.util.List;
+import junit.framework.TestCase;
+import org.lcsim.contrib.seedtracker.digiexample.BetterPlanarStrategy;
+import org.lcsim.util.test.TestUtil.TestOutputFile;
+
+/**
+ *
+ * @author cozzy
+ */
+public class SeedStrategyXMLTest extends TestCase{
+
+ public SeedStrategyXMLTest(String testName) {
+ super(testName);
+ }
+
+ public void testXMLReadingAndWriting() throws Exception {
+ List<SeedStrategy> strategies = new BetterPlanarStrategy().getStrategies();
+ TestOutputFile file = new TestOutputFile("SeedStrategyXMLTest.xml");
+ StrategyXMLUtils.writeStrategyListToFile(strategies, file);
+ List<SeedStrategy> readStrategies = StrategyXMLUtils.getStrategyListFromFile(file);
+ assertTrue(strategies.equals(readStrategies));
+ }
+
+}
CVSspam 0.2.8