Report problems to ATLAS LXR Team (with time and IP address indicated)

The LXR Cross Referencer

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Architecture: linux ]
Version: head ] [ nightly ] [ GaudiDev ]
  Links to LXR source navigation pages for stable releases [ 12.*.* ]   [ 13.*.* ]   [ 14.*.* ] 

001 package triggertool.XML;
002 
003 import java.io.File;
004 import java.sql.SQLException;
005 import java.util.Enumeration;
006 import java.util.Hashtable;
007 import java.util.Vector;
008 import java.util.HashMap;
009 import java.util.Map;
010 import java.util.logging.Logger;
011 import javax.xml.parsers.DocumentBuilder;
012 import javax.xml.parsers.DocumentBuilderFactory;
013 import org.w3c.dom.Document;
014 import org.w3c.dom.Element;
015 import org.w3c.dom.Node;
016 import org.w3c.dom.NodeList;
017 
018 import triggertool.L1Links.L1TM_TT;
019 import triggertool.L1Links.L1TM_TTM;
020 import triggertool.L1Records.L1Bunch;
021 import triggertool.L1Records.L1BunchGroup;
022 import triggertool.L1Records.L1BunchGroupSet;
023 import triggertool.L1Records.L1CaloInfo;
024 import triggertool.L1Records.L1CtpFiles;
025 import triggertool.L1Records.L1CtpSmx;
026 import triggertool.L1Records.L1Deadtime;
027 import triggertool.L1Records.L1Item;
028 import triggertool.L1Records.L1Master;
029 import triggertool.L1Records.L1Menu;
030 import triggertool.L1Records.L1MuctpiInfo;
031 import triggertool.L1Records.L1MuonThresholdSet;
032 import triggertool.L1Records.L1Prescale;
033 import triggertool.L1Records.L1PrescaledClock;
034 import triggertool.L1Records.L1Random;
035 import triggertool.L1Records.L1Threshold;
036 import triggertool.L1Records.L1ThresholdValue;
037 
038 ///Read L1 XML file and save to database.
039 /**
040  * Functionality to read a L1 XML file and convert, and save it to the database.
041  * 
042  * @author Simon Head
043  */
044 public class L1_XMLtoDB {
045 
046     ///Message Log.
047     protected static Logger logger = Logger.getLogger(L1_XMLtoDB.class.getName());
048     ///Used when parsing XML.
049     private NodeList sections;
050     ///Used by XML parser.
051     private Document document;
052     ///Hold the priority set.
053     private String[] priority_array = new String[256];
054     ///These are all the thresholds 
055     private Hashtable<String, Integer> threshold_ids = new Hashtable<String, Integer>();
056     ///forced thresholds
057     private Hashtable<String, Integer> forcedthreshold_ids = new Hashtable<String, Integer>();
058     ///List of threshold IDs.
059     private Vector<Integer> threshold_id_list = new Vector<Integer>();
060     ///Map of Thresholds that are "InternalTriggers" and have been loaded & saved
061     private Map<String, L1Threshold> internaltrigthresholdsLoaded = null;
062     ///Vector of L1 TM to TT links - required for cables.
063     private Vector<L1TM_TT> cables_to_save = new Vector<L1TM_TT>();
064     ///Threshold counter for when we combine thresholds into an item.
065     private int mThresholdCounter = 0;
066     ///A vector of the problems encountered before the upload.
067     private Vector<String> faults = new Vector<String>();
068     ///to store the new LVL1 prescale key.
069     private int newLVL1PrescaleKey = 0;
070     ///to store the new LVL1 bunch group key
071     //private int newLVL1BGKey = 0;
072     
073     ///Constructor takes the filename of the L1 XML file to read only.
074     /**
075      * Constructor takes the filename.  The XML file is parsed and the information contained
076      * within it is written to the database 
077      * 
078      * @param filename Path and filename of the L1 XML file to read in
079      */
080     public int readXML(String filename, String configname) {
081         logger.info("In LVL1 read XML to DB, filename: " + filename);
082         internaltrigthresholdsLoaded = new HashMap<String, L1Threshold>(100);
083         
084         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
085         factory.setIgnoringComments(true);
086         factory.setCoalescing(true);
087         factory.setNamespaceAware(false);
088         factory.setValidating(false);
089 
090         for (int i = 0; i < 256; ++i) {
091             priority_array[i] = "";
092         }
093 
094         DocumentBuilder parser;
095 
096         int l1_id = -1;
097 
098         try {
099             parser = factory.newDocumentBuilder();
100             document = parser.parse(new File(filename));
101 
102             check_xml();
103 
104             if (faults.size() == 0) {
105                 logger.fine("Passed all checks, will start upload");
106 
107                 logger.fine("Reading Priority Set");
108                 priority_set();
109 
110                 logger.fine("Reading Threshold List");
111                 trigger_threshold_list();
112 
113                 logger.fine("Reading Random");
114                 int rand_id = random();
115 
116                 logger.fine("Reading Deadtime");
117                 int dt_id = deadtime();
118 
119                 logger.fine("Reading Prescaled Clock");
120                 int pc_id = prescaled_clock();
121 
122                 logger.fine("Reading MUCTPI info");
123                 int mu_id = muctpi_info();
124 
125                 logger.fine("Reading Calorimeter Info");
126                 int ci_id = calo_info();
127 
128                 //PJB need to read PS and BGS before menu
129                 logger.fine("Reading Bunch Groups");
130                 int bg_id = bunch_group_set();
131 
132                 logger.fine("Reading Prescale Set");
133                 newLVL1PrescaleKey = prescale_set(configname);
134 
135                 logger.fine("Reading Trigger Menu");
136                 int menu_id = trigger_menu(configname);
137                 
138                 logger.fine("Reading CTP Monitor counters");
139                 mon_counters(menu_id, bg_id);
140 
141                 logger.fine("Making a muon threshold set table");
142                 int mts_id = muon_threshold_set();
143                 
144                 logger.fine("Making a L1 master " + menu_id + " " 
145                                                   + dt_id + " "
146                                                   + mu_id + " "
147                                                   + rand_id + " "
148                                                   + pc_id + " "
149                                                   + ci_id + " "
150                                                   + mts_id);
151                 
152                 l1_id = trigger_master(menu_id, dt_id, mu_id, rand_id, pc_id, ci_id, mts_id, configname);
153 
154                 logger.info("Finished uploading L1" +
155                             "\n\tL1 Master ID =     " + l1_id);
156             } else {
157                 logger.warning("XML file has errors");
158 
159                 int i = 1;
160                 for (String line : faults) {
161                     logger.warning(i + " " + line);
162                     ++i;
163                 }
164             }
165         } catch (Exception e) {
166             logger.warning(e.getMessage());
167             e.printStackTrace();
168         }
169 
170         return l1_id;
171     }
172 
173     ///Get the prescale ID.
174     /**
175      * If this has made a new key, return the ID with this.
176      * 
177      * @return Integer ID of the prescale key.
178      */
179     public int getLVLPrescaleSetKey() {
180         return newLVL1PrescaleKey;
181     }
182 
183     ///Get the BG ID.
184     /**
185      * If this has made a new key, return the ID with this.
186      * 
187      * @return Integer ID of the BG key.
188      */
189     //public int getLVLBGSetKey() {
190     //    return newLVL1BGKey;
191     //}
192     
193     ///Read the CTP monitoring information from the XML file.
194     private void mon_counters(int menu_id, int bgs_id) throws SQLException {
195         sections = document.getElementsByTagName("TriggerCounterList");
196 
197         if (sections.getLength() == 1) {
198             Element section = (Element) sections.item(0);
199 
200             logger.fine(section.getNodeName());
201 
202             NodeList counterlist = section.getElementsByTagName("TriggerCounter");
203             for (int i = 0; i < counterlist.getLength(); ++i) {
204                 Element thiscounter = (Element) counterlist.item(i);
205 
206                 String threshold = thiscounter.getAttribute("triggerthreshold");
207                 logger.fine("Counter " + thiscounter.getAttribute("name") + ", " + threshold + ", " + thiscounter.getAttribute("multi") + ", " + thiscounter.getAttribute("type"));
208 
209                 int saved_threshold_id = threshold_ids.get(threshold);
210                 //logger.info("Adding to set threshold " + threshold + " at id " + saved_threshold_id);
211 
212                 L1TM_TTM db_counter = new L1TM_TTM(-1);
213                 db_counter.set_menu_id(menu_id);
214                 db_counter.set_threshold_id(saved_threshold_id);
215                 db_counter.set_bunch_group_id(-1);
216                 db_counter.set_counter_name(thiscounter.getAttribute("name"));
217                 db_counter.set_multiplicity(Integer.parseInt(thiscounter.getAttribute("multi")));
218                 db_counter.set_type(thiscounter.getAttribute("type"));
219 
220                 db_counter.save();
221                 logger.fine("Counter save ok");
222             }
223         } else {
224             logger.warning("Probably no TRIGGERCOUNTERLIST NODE in this xml");
225         }
226     }
227 
228     ///Read the priority set node.
229     /**
230      * Deal with the priority set in the XML.  Here we just want to read them
231      * in to memory because they're stored as part of an item.  Which we are
232      * creating later.
233      */
234     private void priority_set() {
235         sections = document.getElementsByTagName("PrioritySet");
236 
237         if (sections.getLength() == 1) {
238             Element section = (Element) sections.item(0);
239 
240             NodeList priority = section.getElementsByTagName("Priority");
241             for (int i = 0; i < priority.getLength(); ++i) {
242                 Element thispriority = (Element) priority.item(i);
243                 priority_array[Integer.parseInt(thispriority.getAttribute("ctpid").trim())] = thispriority.getTextContent().trim();
244                 logger.fine("  " + thispriority.getNodeName() + "- ctpid=" + thispriority.getAttribute("ctpid") + ", value=" + thispriority.getTextContent().trim());
245             }
246         } else {
247             logger.warning("TOO MANY PRIORITYSET NODES IN XML");
248         }
249     }
250 
251     ///Read a threshold value.
252     /**
253      * Reads the L1 threshold value node and converts it into the object used
254      * by the trigger tool.  Also saves to the database.
255      * 
256      * @param ttv_node The XML TriggerThresholdValue node.
257      * @return The ID of the trigger threshold value.
258      */
259     private L1ThresholdValue threshold_value(Element ttv_node) throws SQLException {
260         logger.fine("Threshold value name " + ttv_node.getAttribute("name"));
261         L1ThresholdValue newThresholdV = new L1ThresholdValue(-1);
262         newThresholdV.set_name(ttv_node.getAttribute("name"));
263         newThresholdV.set_em_isolation(ttv_node.getAttribute("em_isolation"));
264         newThresholdV.set_eta_max(Integer.parseInt(ttv_node.getAttribute("etamax")));
265         newThresholdV.set_eta_min(Integer.parseInt(ttv_node.getAttribute("etamin")));
266         newThresholdV.set_phi_min(Integer.parseInt(ttv_node.getAttribute("phimin")));
267         newThresholdV.set_phi_max(Integer.parseInt(ttv_node.getAttribute("phimax")));
268         newThresholdV.set_priority(Integer.parseInt(ttv_node.getAttribute("priority")));
269         newThresholdV.set_window(Integer.parseInt(ttv_node.getAttribute("window")));
270         newThresholdV.set_had_veto(ttv_node.getAttribute("had_veto"));
271         newThresholdV.set_had_isolation(ttv_node.getAttribute("had_isolation"));
272         newThresholdV.set_pt_cut(ttv_node.getAttribute("thresholdval"));
273         newThresholdV.set_type(ttv_node.getAttribute("type"));
274 
275         if (newThresholdV.get_type().equals("BG")) {
276             newThresholdV.set_type(L1Threshold.BG_TYPE);
277         }
278 
279         if (newThresholdV.get_type().equals("RND")) {
280             newThresholdV.set_type(L1Threshold.RND_TYPE);
281         }
282 
283         newThresholdV.save();
284 
285         return newThresholdV;
286     }
287 
288     ///Read a threhold.
289     /**
290      * Read a threshold from the XML file.  If the old style XML notation is
291      * used for Bunch Group or Random (type=BG or RND) replace it with the
292      * new style notation as defined in L1Threshold.
293      * 
294      * @param threshold XML node for the element.
295      * @return ID of the trigger threshold.
296      * @throws java.sql.SQLException Stop on SQL problems.
297      */
298     private int trigger_threshold(Element threshold) throws SQLException {
299         int id = -1;
300 
301         L1Threshold needthis = new L1Threshold(-1);
302         needthis.set_name(threshold.getAttribute("name"));
303         needthis.set_bitnum(Integer.parseInt(threshold.getAttribute("bitnum")));
304         needthis.set_type(threshold.getAttribute("type"));
305 
306         //CATCH when we've used the old style notation and change to the official threshold types
307         if (needthis.get_type().equals("BG")) {
308             needthis.set_type(L1Threshold.BG_TYPE);
309         }
310 
311         if (needthis.get_type().equals("RND")) {
312             needthis.set_type(L1Threshold.RND_TYPE);
313         }
314 
315         int map = 0;
316         try {
317             map = Integer.parseInt(threshold.getAttribute("mapping"));
318         } catch (NumberFormatException ex) {
319             map = 0;
320         }
321 
322         needthis.set_mapping(map);
323 
324         NodeList List_thresholds = threshold.getElementsByTagName("TriggerThresholdValue");
325 
326         logger.fine("Threshold " + needthis.get_name());
327 
328         for (int j = 0; j < List_thresholds.getLength(); ++j) {
329             Element ttv_node = (Element) List_thresholds.item(j);
330             L1ThresholdValue ttv = threshold_value(ttv_node);
331             logger.fine("  Adding threshold value " + ttv.get_name() + " ? ");
332             needthis.getThresholdValues().add(ttv);
333         }
334 
335         id = needthis.save();
336         threshold_ids.put(needthis.get_name(), id);
337 
338         logger.fine("Saved L1 Threshold name " + needthis.get_name() + " with id " + id);
339 
340         NodeList cables = threshold.getElementsByTagName("Cable");
341 
342         for (int j = 0; j < cables.getLength(); ++j) {
343             Element cable_node = (Element) cables.item(j);
344             cable(cable_node, id);
345         }
346 
347         return id;
348     }
349 
350     ///Read the trigger threshold list.
351     /**
352      * Go through the trigger threshold list in the XML and write to the 
353      * database.
354      */
355     private void trigger_threshold_list() throws SQLException {
356         sections = document.getElementsByTagName("TriggerThresholdList");
357 
358         if (sections.getLength() == 1) {
359             Element thresholdlist = (Element) sections.item(0);
360 
361             NodeList thresholds = thresholdlist.getElementsByTagName("TriggerThreshold");
362             for (int i = 0; i < thresholds.getLength(); ++i) {
363                 Element threshold = (Element) thresholds.item(i);
364                 logger.finer("  " + threshold.getNodeName() + "- bitnum=" + threshold.getAttribute("bitnum") + ", id=" + threshold.getAttribute("id") + ", name=" + threshold.getAttribute("name") + ", type=" + threshold.getAttribute("type") + ", version=" + threshold.getAttribute("version") + ", active=" + threshold.getAttribute("active") + ", mapping=" + threshold.getAttribute("mapping"));
365 
366                 threshold_id_list.addElement(trigger_threshold(threshold));
367             }
368             //now set forced thresholds to contain all thresholds
369             //forcedthreshold_ids = threshold_ids; cant do this!
370 
371         } else {
372             logger.warning("TOO MANY TRIGGERMENU NODES IN XML");
373         }
374     }
375 
376     ///Read the cable information for a single cable.
377     private void cable(Element cable_node, int threshold_id) {
378         String name = cable_node.getAttribute("name");
379         String ctpin = cable_node.getAttribute("ctpin");
380         String connector = cable_node.getAttribute("connector");
381 
382         String start = "";
383         String end = "";
384 
385         NodeList signals = cable_node.getElementsByTagName("Signal");
386         for (int k = 0; k < signals.getLength(); ++k) {
387             Element signal = (Element) signals.item(k);
388             logger.fine("      " + signal.getNodeName() + "- range_begin=" + signal.getAttribute("range_begin") + ", range_end=" + signal.getAttribute("range_end"));
389             start = signal.getAttribute("range_begin");
390             end = signal.getAttribute("range_end");
391         }
392 
393         logger.fine("cable part 1 " + name + ", " + ctpin + ", " + connector);
394         logger.fine("cable part 2 " + start + ", " + end);
395 
396         Hashtable<String, Object> check_me = new Hashtable<String, Object>();
397         check_me.put("CABLE_NAME", name);
398         check_me.put("CABLE_CTPIN", ctpin);
399         check_me.put("CABLE_CONNECTOR", connector);
400         check_me.put("CABLE_START", start);
401         check_me.put("CABLE_END", end);
402 
403         L1TM_TT link = new L1TM_TT();
404         link.set_threshold_id(threshold_id);
405         link.set_cable_name(name);
406         link.set_cable_ctpin(ctpin);
407         link.set_cable_connector(connector);
408         link.set_cable_start(Integer.parseInt(start));
409         link.set_cable_end(Integer.parseInt(end));
410 
411         cables_to_save.addElement(link);
412     }
413 
414     ///Read Bunch Group information.
415     /** 
416      * Check and insert a Bunch Group (and bunches) if required.
417      * Called by BunchGroupSet
418      * 
419      * \verbatim
420      * <BunchGroup internalNumber="1" name="col_Bunch" version="1">
421      *  which contains <Bunch>
422      * </BunchGroup>
423      * \endverbatim
424      * 
425      * @param bg_node the XML node we want to parse
426      * @return the id for the bunch group
427      */
428     private L1BunchGroup bunch_group(Element bg_node) throws SQLException {
429         String bg_name = bg_node.getAttribute("name");
430         int num = Integer.parseInt(bg_node.getAttribute("internalNumber"));
431         //logger.info("bunch group: " + bg_name);
432 
433         NodeList List_bunches = bg_node.getElementsByTagName("Bunch");
434 
435         L1BunchGroup bg = new L1BunchGroup(-1);
436         bg.set_name(bg_name);
437         bg.set_internal_number(num);
438 
439         for (int i = 0; i < List_bunches.getLength(); ++i) {
440             Element b_node = (Element) List_bunches.item(i);
441             int bn = Integer.parseInt(b_node.getAttribute("bunchNumber"));
442 
443             L1Bunch b = new L1Bunch(-1);
444             b.set_bunch_number(bn);
445             bg.getBunches().addElement(b);
446         }
447 
448         bg.save();
449 
450         return bg;
451     }
452 
453     ///Read a bunch group set.
454     /**
455      * Check for the bunch group set and insert one if required.
456      * 
457      * \verbatim
458      * <BunchGroupSet name="bg_set1" version="bg_set1">
459      *   Contains <BunchGroup> which contains <Bunch>
460      * </BunchGroupSet>
461      * \endverbatim
462      * 
463      * @return id of the bunch group set
464      */
465     private int bunch_group_set() throws SQLException {
466         sections = document.getElementsByTagName("BunchGroupSet");
467 
468         int bgs_id = -1;
469 
470         if (sections.getLength() == 1) {
471             Element section = (Element) sections.item(0);
472 
473             NodeList List_groups = section.getElementsByTagName("BunchGroup");
474 
475             L1BunchGroupSet needthis = new L1BunchGroupSet(-1);
476             needthis.set_name(section.getAttribute("name"));
477 
478             for (int i = 0; i < List_groups.getLength(); ++i) {
479                 Element bg_node = (Element) List_groups.item(i);
480                 needthis.getBunchGroups().add(bunch_group(bg_node));
481             }
482 
483             bgs_id = needthis.save();
484         }
485         return bgs_id;
486     }
487 
488     ///Read Deadtime from the XML.
489     /**
490      * Read a deadtime node from the XML.
491      * 
492      * \verbatim
493      * <Deadtime complex1_level="111" complex1_rate="1" complex2_level="1" complex2_rate="1" name="sdt01" version="1" simple="1"/>
494      * \endverbatim
495      * 
496      * @return ID of the deadtime record.
497      */
498     private int deadtime() throws SQLException {
499         int id = -1;
500         sections = document.getElementsByTagName("Deadtime");
501 
502         if (sections.getLength() == 1) {
503             Element section = (Element) sections.item(0);
504 
505             L1Deadtime deadtime = new L1Deadtime(-1);
506             deadtime.set_name(section.getAttribute("name"));
507             deadtime.set_simple(Integer.parseInt(section.getAttribute("simple")));
508             deadtime.set_complex1_level(Integer.parseInt(section.getAttribute("complex1_level")));
509             deadtime.set_complex1_rate(Integer.parseInt(section.getAttribute("complex1_rate")));
510             deadtime.set_complex2_level(Integer.parseInt(section.getAttribute("complex2_level")));
511             deadtime.set_complex2_rate(Integer.parseInt(section.getAttribute("complex2_rate")));
512 
513             id = deadtime.save();
514         } else {
515             logger.warning("TOO MANY DEADTIME NODES IN XML");
516         }
517 
518         return id;
519     }
520 
521     ///Read the random node from the XML.
522     /**
523      * Read the random node from the XML.  The version is ignored by the upload 
524      * script.
525      * 
526      * \verbatim
527      * <Random name="rand01" version="1" rate1="20" seed1="1" rate2="1000" seed2="123" />
528      * \endverbatim
529      * 
530      * @return id for the random record
531      */
532     private int random() throws SQLException {
533         sections = document.getElementsByTagName("Random");
534         int id = -1;
535 
536         if (sections.getLength() == 1) {
537             Element section = (Element) sections.item(0);
538 
539             L1Random random = new L1Random(-1);
540 
541             random.set_name(section.getAttribute("name"));
542             random.set_rate1(Integer.parseInt(section.getAttribute("rate1")));
543             random.set_rate2(Integer.parseInt(section.getAttribute("rate2")));
544             random.set_seed1(Integer.parseInt(section.getAttribute("seed1")));
545             random.set_seed2(Integer.parseInt(section.getAttribute("seed2")));
546 
547             if (random.get_seed1() <= 0) {
548                 random.set_autoseed1(1);
549             } else {
550                 random.set_autoseed1(0);
551             }
552 
553             if (random.get_seed2() <= 0) {
554                 random.set_autoseed2(1);
555             } else {
556                 random.set_autoseed2(0);
557             }
558 
559             id = random.save();
560         } else {
561             logger.warning("Too many Random nodes in XML");
562         }
563 
564         return id;
565     }
566 
567     ///Read Muctpi Info.
568     /**
569      * Read the MuctpiInfo node.
570      * 
571      * \verbatim
572      * <MuctpiInfo name="muctpi01" version="1">
573      *    <low_pt>2</low_pt>
574      *    <high_pt>6</high_pt>
575      *    <max_cand>13</max_cand>
576      * </MuctpiInfo>
577      * \endverbatim
578      *
579      * @return id for the muctpi info table (uploaded or existing)
580      */
581     private int muctpi_info() throws SQLException {
582         sections = document.getElementsByTagName("MuctpiInfo");
583         int id = -1;
584 
585         if (sections.getLength() == 1) {
586             Element section = (Element) sections.item(0);
587 
588             String low_pt = section.getElementsByTagName("low_pt").item(0).getTextContent().trim();
589             String high_pt = section.getElementsByTagName("high_pt").item(0).getTextContent().trim();
590             String max_cand = section.getElementsByTagName("max_cand").item(0).getTextContent().trim();
591 
592             L1MuctpiInfo newInfo = new L1MuctpiInfo(-1);
593             newInfo.set_name(section.getAttribute("name"));
594             newInfo.set_low_pt(Integer.valueOf(low_pt));
595             newInfo.set_high_pt(Integer.valueOf(high_pt));
596             newInfo.set_max_cand(Integer.valueOf(max_cand));
597             id = newInfo.save();
598         } else {
599             logger.warning("TOO MANY MUCTPI NODES IN XML");
600         }
601 
602         return id;
603     }
604 
605     ///Read the prescaled clock node.
606     /** 
607      * Read the prescaled clock node from the XML.  
608      * 
609      * It is expected to look like this, and there is only one:
610      * 
611      * \verbatim
612      * <PrescaledClock name="psc01" version="1" clock1="10" clock2="100"/>
613      * \endverbatim
614      * 
615      * @return id for the prescaled clock table (uploaded or existing)
616      */
617     private int prescaled_clock() throws SQLException {
618         sections = document.getElementsByTagName("PrescaledClock");
619         int id = -1;
620 
621         if (sections.getLength() == 1) {
622             Element section = (Element) sections.item(0);
623 
624             L1PrescaledClock pc = new L1PrescaledClock(-1);
625             pc.set_name(section.getAttribute("name"));
626             pc.set_clock1(Integer.valueOf(section.getAttribute("clock1")));
627             pc.set_clock2(Integer.valueOf(section.getAttribute("clock2")));
628             id = pc.save();
629         } else {
630             logger.warning("TOO MANY PRESCALED CLOCK NODES IN XML");
631         }
632 
633         return id;
634     }
635 
636     ///Read the Calo Info node.
637     /**
638      * Read a CaloInfo node
639      * 
640      * \verbatim
641      * <CaloInfo name="standard" version="1" global_scale="4.0">
642      *    <JetWeight num="1"> 10 </JetWeight>
643      *    ...
644      *    <JetWeight num="12"> 10 </JetWeight>
645      * </CaloInfo>
646      * \endverbatim
647      */
648     private int calo_info() throws SQLException {
649         sections = document.getElementsByTagName("CaloInfo");
650         int id = -1;
651 
652         if (sections.getLength() == 1) {
653             Element section = (Element) sections.item(0);
654 
655             Integer[] weightArray = new Integer[12];
656 
657             for (int i = 0; i < 12; ++i) {
658                 weightArray[i] = 0;
659             }
660 
661             NodeList weights = section.getElementsByTagName("JetWeight");
662             for (int i = 0; i < weights.getLength(); ++i) {
663                 Element weight = (Element) weights.item(i);
664                 weightArray[Integer.parseInt(weight.getAttribute("num").trim()) - 1] = Integer.parseInt(weight.getTextContent().trim());
665             //logger.info("  " + weight.getNodeName() + "- " + weight.getAttribute("num") + " - " + weight.getTextContent());
666             }
667 
668             L1CaloInfo newInfo = new L1CaloInfo(-1);
669             newInfo.set_name(section.getAttribute("name"));
670             newInfo.set_global_scale(section.getAttribute("global_scale"));
671 
672             for (int i = 0; i < 12; ++i) {
673                 newInfo.set_jet_weight(i + 1, weightArray[i]);
674             }
675 
676             id = newInfo.save();
677         } else {
678             logger.warning("TOO MANY CALOINFO NODES IN XML");
679         }
680 
681         return id;
682     }
683 
684     ///Read a prescale set.
685     /**
686      * Read a prescale_set from XML and write to the database.
687      * 
688      * @return ID of the Prescale Set
689      */
690     private int prescale_set(String name) throws SQLException {
691         int id = -1;
692         sections = document.getElementsByTagName("PrescaleSet");
693 
694         if (sections.getLength() == 1) {
695             Element section = (Element) sections.item(0);
696             //String name = section.getAttribute("name");
697             String version = section.getAttribute("version");
698 
699             logger.fine(section.getNodeName() + "- name=" + name + ", version=" + version);
700 
701             //ctp id counts from 0 to 255 in xml...
702             int[] prescaleArray = new int[256];
703             for (int i = 0; i < 256; i++) {
704                 prescaleArray[i] = -1;
705             }
706 
707             //...but prescale vals are 1 to 256 in db
708             //The xml always has 256 prescales, even if fewer items that this
709             //For empty items, want value to be -1.
710             //Have to read some of the menu here!
711             Vector<Integer> ctpids_v = new Vector<Integer>();
712             Element menu = (Element) document.getElementsByTagName("TriggerMenu").item(0);
713             NodeList List_items = menu.getElementsByTagName("TriggerItem");
714             for (int i = 0; i < List_items.getLength(); ++i) {
715                 Element item_node = (Element) List_items.item(i);
716                 ctpids_v.add(Integer.parseInt(item_node.getAttribute("ctpid")));
717             }
718             
719             NodeList prescales = section.getElementsByTagName("Prescale");
720             for (int i = 0; i < prescales.getLength(); i++) {
721                 Element prescale = (Element) prescales.item(i);
722                 //only those with items
723                 if(ctpids_v.contains(Integer.parseInt(prescale.getAttribute("ctpid").trim()))){
724                     prescaleArray[Integer.parseInt(prescale.getAttribute("ctpid").trim())] = Integer.parseInt(prescale.getTextContent().trim());
725                 }else{
726                     prescaleArray[Integer.parseInt(prescale.getAttribute("ctpid").trim())] = -1;
727                 }
728                 //logger.info("  " + prescale.getNodeName() + ": ctpid = " + prescale.getAttribute("ctpid") + ", value = " + prescaleArray[Integer.parseInt(prescale.getAttribute("ctpid").trim())]);
729             }
730 
731             L1Prescale newInfo = new L1Prescale(-1);
732             newInfo.set_name(name);
733             
734             for (int i = 0; i <= 255; i++) {
735                 newInfo.set_val(i+1, prescaleArray[i]);
736             }
737 
738             id = newInfo.save();
739         } else {
740             logger.warning("TOO MANY PRESCALESET NODES IN XML");
741         }
742 
743         return id;
744     }
745 
746     ///Read all the trigger items.
747     /**
748      * Write the trigger item table to the database.
749      * 
750      * @param item_node the node in the XML for the trigger item.
751      * @return ID for the trigger item in the database.
752      */
753     private L1Item trigger_item(Element item_node) throws SQLException {
754         L1Item ti = new L1Item(-1);
755         ti.set_ctp_id(Integer.parseInt(item_node.getAttribute("ctpid")));
756         ti.set_name(item_node.getAttribute("name"));
757 
758         //System.out.println("L1 Trigger Item: " + ti.get_name());
759 
760         //int ttype = 0;
761         //try {
762         //    ttype = Integer.parseInt(item_node.getAttribute("trigger_type"), 2);
763         //} catch (NumberFormatException e) {
764         //    ttype = 0;
765         //}
766         String ttype = "";
767         ttype = item_node.getAttribute("trigger_type");
768         ti.set_trigger_type(ttype);
769 
770         if (item_node.getAttribute("comment").equals("")) {
771             ti.set_comment("~");
772         } else {
773             ti.set_comment(item_node.getAttribute("comment"));
774         }
775 
776         int group = 0;
777         try {
778             group = Integer.parseInt(item_node.getAttribute("group"));
779         } catch (NumberFormatException e) {
780             group = 0;
781         }
782 
783         ti.set_group(group);
784         ti.set_priority(priority_array[ti.get_ctp_id()]);
785 
786         //work out the logic
787         mThresholdCounter = 1;
788         String def_string = read(item_node);
789 
790         if (def_string.equals("")) {
791             def_string = "(1)";
792         }
793 
794         ti.set_definition(def_string);
795 
796         NodeList List_conditions = item_node.getElementsByTagName("*");
797         int position_counter = 1;
798 
799         for (int i = 0; i < List_conditions.getLength(); ++i) {
800             Element condition_node = (Element) List_conditions.item(i);
801 
802             logger.fine((i + 1) + " TagName: " + condition_node.getTagName());
803 
804             if (condition_node.getTagName().equals("InternalTrigger")) {
805                 String name = condition_node.getAttribute("name");
806                 L1Threshold threshold = null;
807                 /// See if we already loaded this threshold
808                 String test = new String(name);
809                 test += "_p";
810                 test += position_counter;
811                 if(internaltrigthresholdsLoaded.containsKey(test)) {
812                     threshold = internaltrigthresholdsLoaded.get(test); //get from the map
813                 }
814                 else { /// need to load from xml
815                     threshold = new L1Threshold(-1);
816                     threshold.set_position(position_counter);
817                     threshold.set_multiplicity(1);
818                     threshold.set_name(name);
819 
820                     String type = "";
821 
822                     if (name.contains(L1Threshold.BG_TYPE)) {
823                         type = L1Threshold.BG_TYPE;
824                     } else if (name.contains(L1Threshold.RND_TYPE)) {
825                         type = L1Threshold.RND_TYPE;
826                     } else if (name.contains(L1Threshold.PSC_TYPE)) {
827                         type = L1Threshold.PSC_TYPE;
828                     }
829 
830                     threshold.set_type(type);
831                     threshold.save();
832                     // add threshold to the loaded list
833                     internaltrigthresholdsLoaded.put(test, threshold);
834                 }
835                 
836                 //System.out.println("  " + threshold.get_position() + " Internal  " + threshold.get_multiplicity() + "x " + threshold.get_name());
837 
838                 ti.getThresholds().add(threshold);
839                 ++position_counter;
840             } else if (condition_node.getTagName().equals("TriggerCondition")) {
841                 String me = condition_node.getAttribute("triggerthreshold");
842                 int multi = Integer.parseInt(condition_node.getAttribute("multi"));
843 
844                 L1Threshold threshold = new L1Threshold(threshold_ids.get(me));
845                 logger.fine("Recovering threshold " + threshold_ids.get(me) + " name=" + threshold.get_name());
846                 threshold.set_multiplicity(multi);
847                 threshold.set_position(position_counter);
848 
849                 //System.out.println("  " + threshold.get_position() + " Condition " + threshold.get_multiplicity() + "x " + threshold.get_name());
850 
851                 //when we add the thresholds, add them to forced thresholds
852                 //diff between ft and t sets are actually the fts!
853                 ti.getThresholds().add(threshold);
854                 forcedthreshold_ids.remove(threshold.get_name());
855                 
856                 ++position_counter;
857             }
858         }
859 
860         ti.save();
861         return ti;
862     }
863 
864     ///Master key needs a L1 muon prescale set to point at
865     /**
866      * Load empty L1 muon prescale set
867      * 
868      * @return ID of the l1 muon prescale set
869      */
870     private int muon_threshold_set() throws SQLException {
871         int id = -1;
872 
873         L1MuonThresholdSet l1mts = new L1MuonThresholdSet(-1);
874         l1mts.set_name("Unassigned");
875         id = l1mts.save();
876         
877         return id;
878     }
879     
880     ///Read the trigger menu from the XML file.
881     /**
882      * Load a trigger menu from XML.
883      * 
884      * @return ID of the trigger menu.
885      */
886     private int trigger_menu(String configname) throws SQLException {
887         int id = -1;
888 
889         sections = document.getElementsByTagName("TriggerMenu");
890 
891         L1Menu needthis = new L1Menu(-1);
892 
893         if (sections.getLength() == 1) {
894             
895             //put all thresholds into forced thresholds
896             forcedthreshold_ids.putAll(threshold_ids);
897             
898             Element menu = (Element) sections.item(0);
899 
900             //needthis.set_name(menu.getAttribute("name"));
901             //set the name as supplied
902             needthis.set_name(configname);
903             needthis.set_phase(menu.getAttribute("phase"));
904 
905             L1CtpSmx l1CtpSmx = new L1CtpSmx(-1);
906             l1CtpSmx.set_name("Empty");
907             needthis.set_ctp_smx(l1CtpSmx);
908             needthis.set_ctp_smx_id(l1CtpSmx.save());
909 
910             L1CtpFiles l1CtpFiles = new L1CtpFiles(-1);
911             l1CtpFiles.set_name("Empty");
912             needthis.set_ctp_files(l1CtpFiles);
913             needthis.set_ctp_files_id(l1CtpFiles.save());
914 
915             NodeList List_items = menu.getElementsByTagName("TriggerItem");
916             for (int i = 0; i < List_items.getLength(); ++i) {
917                 Element item_node = (Element) List_items.item(i);
918                 needthis.getItems().add(trigger_item(item_node));
919             }
920         
921             //Now forced thresholds should contain all thresholds not in items
922             //System.out.println("FORCED THRESHOLDS: " + forcedthreshold_ids.toString());
923             //...if there are any forced thresholds we need to save them!
924             if(forcedthreshold_ids.size()>0){
925                 //System.out.println("Found " + forcedthreshold_ids.size() + " forced thresholds - will append to menu");
926                 for (Enumeration e= forcedthreshold_ids.keys(); e.hasMoreElements();){
927                     Integer ftid = forcedthreshold_ids.get(e.nextElement());
928                     //System.out.println("Forced threshold id: " + ftid);
929                     needthis.getForcedThresholds().add(new L1Threshold(ftid));
930                 }
931             }
932         }
933      
934         for (L1TM_TT cable : cables_to_save) {
935             logger.fine(cable.get_cable_name());
936             cable.set_menu_id(id);
937             needthis.getTMTT().add(cable);
938         }
939      
940         
941         //add prescales
942         logger.fine("PJB adding to l1 menu the l1 ps with id " + newLVL1PrescaleKey);
943         needthis.getPrescaleSets().add(new L1Prescale(newLVL1PrescaleKey));
944         
945         //add bgs
946         //logger.info("PJB adding to l1 menu the l1 bgs with id " + newLVL1BGKey);
947         //needthis.getBunchGroupSets().add(new L1BunchGroupSet(newLVL1BGKey));
948         
949         id = needthis.save();
950 
951         return id;
952     }
953 
954     ///Write the master table to the database.
955     /**
956      * Now write the L1 Master Table
957      * 
958      * @param menu_id menu ID
959      * @param ps_id Prescale Set ID
960      * @param dt_id Deadtime ID
961      * @param mu_id MU CTP ID
962      * @param rand_id Random ID
963      * @param pc_id Prescaled Clock ID
964      * @param bgs_id Bunch Group Set ID
965      * @param ci_id Calo Info ID
966      */
967     private int trigger_master(int menu_id, int dt_id, int mu_id, int rand_id, int pc_id, int ci_id, int mts_id, String configname) throws SQLException {
968         sections = document.getElementsByTagName("LVL1Config");
969 
970         int l1m_id = -1;
971         if (sections.getLength() == 1) {
972             
973             //Set the name as supplied
974             //Element menu = (Element) sections.item(0);
975             //String name = menu.getAttribute("name");
976             String name = configname;
977             
978             L1Master master = new L1Master(-1);
979             master.set_name(name);
980             master.set_menu_id(menu_id);
981             master.set_muon_threshold_set_id(mts_id);
982             master.set_deadtime_id(dt_id);
983             master.set_muctpi_info_id(mu_id);
984             master.set_random_id(rand_id);
985             master.set_prescaled_clock_id(pc_id);
986             //master.set_bunch_group_set_id(bgs_id);
987             master.set_calo_info_id(ci_id);
988             logger.info("Calling save of L1 master...");
989             l1m_id = master.save();
990             logger.info("Save of L1 master finished");
991         }
992         return l1m_id;
993     }
994 
995     ///Logic between thresholds from the old trigger tool.
996     public String read(Element e) {
997         String mLogicString = read(e, "");
998         if (mLogicString.length() > 0 && !mLogicString.contains("(")) {
999             mLogicString = "(" + mLogicString + ")";
1000         }
1001         return mLogicString;
1002     }
1003 
1004     ///Logic class taken from the old trigger tool.
1005     private String read(Element e, String insert_symbol) {
1006         String OPEN = "(";
1007         String CLOSE = ")";
1008 
1009         String logic_string = "";
1010         Node node = null;
1011         Element e_next = null;
1012         String tag = "";
1013         NodeList nodes = e.getChildNodes();
1014         int elem_counter = 0;
1015         //String s_multi;
1016 
1017         for (int i = 0; i < nodes.getLength(); ++i) {
1018             node = nodes.item(i);
1019             if (node.getNodeType() != Node.ELEMENT_NODE) {
1020                 continue;
1021             }
1022             e_next = (Element) node;
1023             tag = node.getNodeName();
1024 
1025             if (elem_counter > 0) {
1026                 logic_string += insert_symbol;
1027             }
1028             if (tag.equals("AND")) {
1029                 logic_string += OPEN;
1030                 logic_string += read(e_next, "&");
1031                 logic_string += CLOSE;
1032             } else if (tag.equals("OR")) {
1033                 logic_string += OPEN;
1034                 logic_string += read(e_next, "|");
1035                 logic_string += CLOSE;
1036             } else if (tag.equals("NOT")) {
1037                 logic_string += "!";
1038                 logic_string += read(e_next, "");
1039             } else if (tag.equals("TriggerCondition") || tag.equals("InternalTrigger")) {
1040                 logic_string += String.valueOf(mThresholdCounter++);
1041             //mThresholdNames.add(e_next.getAttribute(LVL1ConfigModel.ktriggerthreshold));
1042             //s_multi = e_next.getAttribute(LVL1ConfigModel.kmulti);
1043             //mMultiplicities.add(Integer.valueOf(s_multi));
1044             }
1045             elem_counter++;
1046         }
1047         return logic_string;
1048     }
1049 
1050     
1051     ///Perform a check on a file to see if the basic xml is valid.
1052     private void check_xml() {
1053         //check all the sections are ok
1054 
1055         //deadtime
1056         int count = document.getElementsByTagName("Deadtime").getLength();
1057         if (count != 1) {
1058             if (count < 1) {
1059                 faults.add("Deadtime XML node missing");
1060             } else {
1061                 faults.add("Too many Deadtime XML nodes.  Should have 1, found " + count);
1062             }
1063         } else {
1064             Element dt = (Element) document.getElementsByTagName("Deadtime").item(0);
1065 
1066             if (dt.getAttribute("complex1_level").length() == 0) {
1067                 faults.add("Deadtime node needs complex1_level=\"a_number\"");
1068             }
1069 
1070             if (dt.getAttribute("complex1_rate").length() == 0) {
1071                 faults.add("Deadtime node needs complex1_rate=\"a_number\"");
1072             }
1073 
1074             if (dt.getAttribute("complex2_level").length() == 0) {
1075                 faults.add("Deadtime node needs complex2_level=\"a_number\"");
1076             }
1077 
1078             if (dt.getAttribute("complex2_rate").length() == 0) {
1079                 faults.add("Deadtime node needs complex2_rate=\"a_number\"");
1080             }
1081 
1082             if (dt.getAttribute("simple").length() == 0) {
1083                 faults.add("Deadtime node needs simple=\"a_number\"");
1084             }
1085 
1086             if (dt.getAttribute("name").length() == 0) {
1087                 faults.add("Deadtime node needs name=\"a_string\"");
1088             }
1089         }
1090 
1091         //random
1092         count = document.getElementsByTagName("Random").getLength();
1093         if (count != 1) {
1094             if (count < 1) {
1095                 faults.add("Random XML node missing");
1096             } else {
1097                 faults.add("Too many Random XML nodes.  Should have 1, found " + count);
1098             }
1099         } else {
1100             Element dt = (Element) document.getElementsByTagName("Random").item(0);
1101 
1102             if (dt.getAttribute("name").length() == 0) {
1103                 faults.add("Random node needs name=\"a_string\"");
1104             }
1105 
1106             if (dt.getAttribute("rate1").length() == 0) {
1107                 faults.add("Random node needs rate1=\"a_number\"");
1108             }
1109 
1110             if (dt.getAttribute("rate2").length() == 0) {
1111                 faults.add("Random node needs rate2=\"a_number\"");
1112             }
1113 
1114             if (dt.getAttribute("seed1").length() == 0) {
1115                 faults.add("Random node needs seed1=\"a_number\"");
1116             }
1117 
1118             if (dt.getAttribute("seed2").length() == 0) {
1119                 faults.add("Random node needs seed2=\"a_number\"");
1120             }
1121         }
1122 
1123         //PrescaledClock
1124         count = document.getElementsByTagName("PrescaledClock").getLength();
1125         if (count != 1) {
1126             if (count < 1) {
1127                 faults.add("PrescaledClock XML node missing");
1128             } else {
1129                 faults.add("Too many PrescaledClock XML nodes.  Should have 1, found " + count);
1130             }
1131         } else {
1132             Element dt = (Element) document.getElementsByTagName("PrescaledClock").item(0);
1133 
1134             if (dt.getAttribute("name").length() == 0) {
1135                 faults.add("PrescaledClock node needs name=\"a_string\"");
1136             }
1137 
1138             if (dt.getAttribute("clock1").length() == 0) {
1139                 faults.add("PrescaledClock node needs clock1=\"a_number\"");
1140             }
1141 
1142             if (dt.getAttribute("clock2").length() == 0) {
1143                 faults.add("PrescaledClock node needs clock2=\"a_number\"");
1144             }
1145         }
1146 
1147         count = document.getElementsByTagName("MuctpiInfo").getLength();
1148         if (count != 1) {
1149             if (count < 1) {
1150                 faults.add("MuctpiInfo XML node missing");
1151             } else {
1152                 faults.add("Too many MuctpiInfo XML nodes.  Should have 1, found " + count);
1153             }
1154         } else {
1155             Element mi = (Element) document.getElementsByTagName("MuctpiInfo").item(0);
1156 
1157             if (mi.getAttribute("name").length() == 0) {
1158                 faults.add("MuctpiInfo node needs name=\"a_string\"");
1159             }
1160             if (mi.getElementsByTagName("low_pt").getLength() != 1) {
1161                 faults.add("Need 1 <low_pt>int</low_pt> in <PrescaledClock></PrescaledClock>");
1162             }
1163             if (mi.getElementsByTagName("high_pt").getLength() != 1) {
1164                 faults.add("Need 1 <high_pt>int</high_pt> in <PrescaledClock></PrescaledClock>");
1165             }
1166             if (mi.getElementsByTagName("max_cand").getLength() != 1) {
1167                 faults.add("Need 1 <max_cand>int</max_cand> in <PrescaledClock></PrescaledClock>");
1168             }
1169         }
1170 
1171         count = document.getElementsByTagName("CaloInfo").getLength();
1172         if (count != 1) {
1173             if (count < 1) {
1174                 faults.add("CaloInfo XML node missing");
1175             } else {
1176                 faults.add("Too many CaloInfo XML nodes.  Should have 1, found " + count);
1177             }
1178         } else {
1179             Element ci = (Element) document.getElementsByTagName("CaloInfo").item(0);
1180 
1181             if (ci.getAttribute("name").length() == 0) {
1182                 faults.add("CaloInfo node needs name=\"a_string\"");
1183             }
1184 
1185             if (ci.getAttribute("global_scale").length() == 0) {
1186                 faults.add("CaloInfo node needs global_scale=\"an_integer\"");
1187             }
1188 
1189         }
1190 
1191         sections = document.getElementsByTagName("LVL1Config");
1192         if (sections.getLength() == 1) {
1193             Element menu = (Element) sections.item(0);
1194 
1195             if (menu.getAttribute("name").length() == 0) {
1196                 faults.add("<LVL1Config> tag needs name=\"a_string\" adding");
1197             }
1198         }
1199     }
1200 }

source navigation ] diff markup ] identifier search ] general search ]

Due to the LXR bug, the updates fail sometimes to remove references to deleted files. The Saturday's full rebuilds fix these problems
This page was automatically generated by the LXR engine. Valid HTML 4.01!