1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
|
/****************************************************************************
*
* This is a part of TOTEM offline software.
* Authors:
* Maciej Wróbel (wroblisko@gmail.com)
* Jan Kašpar (jan.kaspar@cern.ch)
* Marcin Borratynski (mborratynski@gmail.com)
* Seyed Mohsen Etesami (setesami@cern.ch)
* Laurent Forthomme
****************************************************************************/
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/Framework/interface/ModuleFactory.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/ESProducts.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DataFormats/CTPPSDetId/interface/TotemRPDetId.h"
#include "DataFormats/CTPPSDetId/interface/CTPPSDiamondDetId.h"
#include "DataFormats/CTPPSDetId/interface/TotemTimingDetId.h"
#include "DataFormats/CTPPSDetId/interface/TotemT2DetId.h"
#include "CondFormats/DataRecord/interface/TotemReadoutRcd.h"
#include "CondFormats/PPSObjects/interface/TotemDAQMapping.h"
#include "CondFormats/PPSObjects/interface/TotemAnalysisMask.h"
#include "CondFormats/PPSObjects/interface/TotemFramePosition.h"
#include "Utilities/Xerces/interface/Xerces.h"
#include "Utilities/Xerces/interface/XercesStrUtils.h"
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <memory>
#include <sstream>
//#define DEBUG 1
//----------------------------------------------------------------------------------------------------
using namespace std;
/**
* \brief Loads TotemDAQMapping and TotemAnalysisMask from two XML files.
**/
class TotemDAQMappingESSourceXML : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
public:
static const std::string tagVFAT;
static const std::string tagChannel;
static const std::string tagAnalysisMask;
/// Common position tags
static const std::string tagArm;
/// RP XML tags
static const std::string tagRPStation;
static const std::string tagRPPot;
static const std::string tagRPPlane;
/// COMMON Chip XML tags
static const std::string tagChip1;
static const std::string tagChip2;
/// diamond specific tags
static const std::string tagDiamondPlane;
static const std::string tagDiamondCh;
/// totem timing specific tags
static const std::string tagSampicBoard;
static const std::string tagSampicCh;
static const std::string tagTotemTimingCh;
static const std::string tagTotemTimingPlane;
/// TOTEM nT2 specific tags
static const std::string tagTotemT2Plane;
static const std::string tagTotemT2Tile;
TotemDAQMappingESSourceXML(const edm::ParameterSet &);
~TotemDAQMappingESSourceXML() override;
static void fillDescriptions(edm::ConfigurationDescriptions &);
edm::ESProducts<std::unique_ptr<TotemDAQMapping>, std::unique_ptr<TotemAnalysisMask>> produce(const TotemReadoutRcd &);
private:
unsigned int verbosity;
/// label of the CTPPS sub-system
string subSystemName;
//subdetector id for sampic
unsigned int sampicSubDetId;
//Unpack multiple channels per payload for T2
bool packedPayload;
/// the mapping files
std::vector<std::string> mappingFileNames;
struct ConfigBlock {
/// validity interval
edm::EventRange validityRange;
/// the mapping files
std::vector<std::string> mappingFileNames;
/// the mask files
std::vector<std::string> maskFileNames;
};
vector<ConfigBlock> configuration;
/// index of the current block in 'configuration' array
unsigned int currentBlock;
/// flag whether the 'currentBlock' index is valid
bool currentBlockValid;
/// enumeration of XML node types
enum NodeType {
nUnknown,
nSkip,
nTop,
nArm,
nRPStation,
nRPPot,
nRPPlane,
nDiamondPlane,
nChip,
nDiamondCh,
nChannel,
nSampicBoard,
nSampicChannel,
nTotemTimingPlane,
nTotemTimingCh,
nTotemT2Plane,
nTotemT2Tile
};
/// whether to parse a mapping of a mask XML
enum ParseType { pMapping, pMask };
/// parses XML file
void ParseXML(ParseType,
const string &file,
const std::unique_ptr<TotemDAQMapping> &,
const std::unique_ptr<TotemAnalysisMask> &);
/// recursive method to extract RP-related information from the DOM tree
void ParseTreeRP(ParseType,
xercesc::DOMNode *,
NodeType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &,
const std::unique_ptr<TotemAnalysisMask> &);
/// recursive method to extract RP-related information from the DOM tree
void ParseTreeDiamond(ParseType,
xercesc::DOMNode *,
NodeType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &,
const std::unique_ptr<TotemAnalysisMask> &);
/// recursive method to extract RP-related information from the DOM tree
void ParseTreeTotemTiming(ParseType,
xercesc::DOMNode *,
NodeType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &,
const std::unique_ptr<TotemAnalysisMask> &);
/// recursive method to extract nT2-related information from the DOM tree
void ParseTreeTotemT2(ParseType,
xercesc::DOMNode *,
NodeType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &,
const std::unique_ptr<TotemAnalysisMask> &);
private:
/// adds the path prefix, if needed
string CompleteFileName(const string &fn);
/// returns true iff the node is of the given name
bool Test(xercesc::DOMNode *node, const std::string &name) {
return !(name.compare(cms::xerces::toString(node->getNodeName())));
}
/// determines node type
NodeType GetNodeType(xercesc::DOMNode *);
/// returns the content of the node
string GetNodeContent(xercesc::DOMNode *parent) { return string(cms::xerces::toString(parent->getTextContent())); }
/// returns the value of the node
string GetNodeValue(xercesc::DOMNode *node) { return cms::xerces::toString(node->getNodeValue()); }
/// extracts VFAT's DAQ channel from XML attributes
TotemFramePosition ChipFramePosition(xercesc::DOMNode *chipnode);
/// extracts VFAT's DAQ channel from XML attributes for packed T2 payload
TotemT2FramePosition ChipT2FramePosition(xercesc::DOMNode *chipnode);
void GetChannels(xercesc::DOMNode *n, std::set<unsigned char> &channels);
bool RPNode(NodeType type) {
return ((type == nArm) || (type == nRPStation) || (type == nRPPot) || (type == nRPPlane) || (type == nChip));
}
bool DiamondNode(NodeType type) {
return ((type == nArm) || (type == nRPStation) || (type == nRPPot) || (type == nDiamondPlane) ||
(type == nDiamondCh));
}
bool TotemTimingNode(NodeType type) {
return ((type == nArm) || (type == nRPStation) || (type == nRPPot) || (type == nSampicBoard) ||
(type == nSampicChannel) || (type == nTotemTimingPlane) || (type == nTotemTimingCh));
}
bool TotemT2Node(NodeType type) { return type == nArm || type == nTotemT2Plane || type == nTotemT2Tile; }
bool CommonNode(NodeType type) { return ((type == nChip) || (type == nArm)); }
protected:
/// sets infinite validity of this data
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
const edm::IOVSyncValue &,
edm::ValidityInterval &) override;
};
//----------------------------------------------------------------------------------------------------
using namespace std;
using namespace edm;
using namespace xercesc;
const string TotemDAQMappingESSourceXML::tagVFAT = "vfat";
const string TotemDAQMappingESSourceXML::tagChannel = "channel";
const string TotemDAQMappingESSourceXML::tagAnalysisMask = "analysisMask";
// common XML position tags
const string TotemDAQMappingESSourceXML::tagArm = "arm";
// common XML Chip tags
const string TotemDAQMappingESSourceXML::tagChip1 = "vfat";
const string TotemDAQMappingESSourceXML::tagChip2 = "test_vfat";
// specific RP XML tags
const string TotemDAQMappingESSourceXML::tagRPStation = "station";
const string TotemDAQMappingESSourceXML::tagRPPot = "rp_detector_set";
const string TotemDAQMappingESSourceXML::tagRPPlane = "rp_plane";
// specific tags for diamond
const string TotemDAQMappingESSourceXML::tagDiamondPlane = "rp_plane_diamond";
const string TotemDAQMappingESSourceXML::tagDiamondCh = "diamond_channel";
// specific tags for totem timing
const string TotemDAQMappingESSourceXML::tagSampicBoard = "rp_sampic_board";
const string TotemDAQMappingESSourceXML::tagSampicCh = "rp_sampic_channel";
const string TotemDAQMappingESSourceXML::tagTotemTimingCh = "timing_channel";
const string TotemDAQMappingESSourceXML::tagTotemTimingPlane = "timing_plane";
// specific tags for TOTEM nT2
const string TotemDAQMappingESSourceXML::tagTotemT2Plane = "nt2_plane";
const string TotemDAQMappingESSourceXML::tagTotemT2Tile = "nt2_tile";
//----------------------------------------------------------------------------------------------------
TotemDAQMappingESSourceXML::TotemDAQMappingESSourceXML(const edm::ParameterSet &conf)
: verbosity(conf.getUntrackedParameter<unsigned int>("verbosity", 0)),
subSystemName(conf.getUntrackedParameter<string>("subSystem")),
sampicSubDetId(conf.getParameter<unsigned int>("sampicSubDetId")),
packedPayload(conf.getParameter<bool>("multipleChannelsPerPayload")),
currentBlock(0),
currentBlockValid(false) {
for (const auto &it : conf.getParameter<vector<ParameterSet>>("configuration")) {
ConfigBlock b;
b.validityRange = it.getParameter<EventRange>("validityRange");
b.mappingFileNames = it.getParameter<vector<string>>("mappingFileNames");
b.maskFileNames = it.getParameter<vector<string>>("maskFileNames");
configuration.push_back(b);
}
setWhatProduced(this, subSystemName);
findingRecord<TotemReadoutRcd>();
}
//----------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::setIntervalFor(const edm::eventsetup::EventSetupRecordKey &key,
const edm::IOVSyncValue &iosv,
edm::ValidityInterval &oValidity) {
LogVerbatim("TotemDAQMappingESSourceXML") << ">> TotemDAQMappingESSourceXML::setIntervalFor(" << key.name() << ")";
LogVerbatim("TotemDAQMappingESSourceXML")
<< " run=" << iosv.eventID().run() << ", event=" << iosv.eventID().event();
currentBlockValid = false;
for (unsigned int idx = 0; idx < configuration.size(); ++idx) {
const auto &bl = configuration[idx];
edm::EventRange range = bl.validityRange;
// If "<run>:min" is specified in python config, it is translated into event <run>:0:1.
// However, the truly minimal event id often found in data is <run>:0:0. Therefore the
// adjustment below is needed.
if (range.startEventID().luminosityBlock() == 0 && range.startEventID().event() == 1)
range = edm::EventRange(edm::EventID(range.startEventID().run(), 0, 0), range.endEventID());
if (edm::contains(range, iosv.eventID())) {
currentBlockValid = true;
currentBlock = idx;
const IOVSyncValue begin(range.startEventID());
const IOVSyncValue end(range.endEventID());
oValidity = edm::ValidityInterval(begin, end);
LogVerbatim("TotemDAQMappingESSourceXML") << " block found: index=" << currentBlock << ", interval=("
<< range.startEventID() << " - " << range.endEventID() << ")";
return;
}
}
if (!currentBlockValid) {
throw cms::Exception("TotemDAQMappingESSourceXML::setIntervalFor")
<< "No configuration for event " << iosv.eventID();
}
}
//----------------------------------------------------------------------------------------------------
TotemDAQMappingESSourceXML::~TotemDAQMappingESSourceXML() {}
//----------------------------------------------------------------------------------------------------
string TotemDAQMappingESSourceXML::CompleteFileName(const string &fn) {
FileInPath fip(fn);
return fip.fullPath();
}
//----------------------------------------------------------------------------------------------------
static inline std::string to_string(const XMLCh *ch) { return XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(ch); }
edm::ESProducts<std::unique_ptr<TotemDAQMapping>, std::unique_ptr<TotemAnalysisMask>>
TotemDAQMappingESSourceXML::produce(const TotemReadoutRcd &) {
assert(currentBlockValid);
auto mapping = std::make_unique<TotemDAQMapping>();
auto mask = std::make_unique<TotemAnalysisMask>();
try {
// initialize Xerces
cms::concurrency::xercesInitialize();
// load mapping files
for (const auto &fn : configuration[currentBlock].mappingFileNames)
ParseXML(pMapping, CompleteFileName(fn), mapping, mask);
// load mask files
for (const auto &fn : configuration[currentBlock].maskFileNames)
ParseXML(pMask, CompleteFileName(fn), mapping, mask);
// release Xerces
cms::concurrency::xercesTerminate();
} catch (const XMLException &e) {
throw cms::Exception("XMLDocument") << "cms::concurrency::xercesInitialize failed because of "
<< to_string(e.getMessage());
} catch (const SAXException &e) {
throw cms::Exception("XMLDocument") << "XML parser (SAX) reported: " << to_string(e.getMessage()) << ".";
} catch (const DOMException &e) {
throw cms::Exception("XMLDocument") << "XML parser (DOM) reported: " << to_string(e.getMessage()) << ".";
}
// commit the products
return edm::es::products(std::move(mapping), std::move(mask));
}
//----------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::ParseXML(ParseType pType,
const string &file,
const std::unique_ptr<TotemDAQMapping> &mapping,
const std::unique_ptr<TotemAnalysisMask> &mask) {
unique_ptr<XercesDOMParser> parser(new XercesDOMParser());
parser->parse(file.c_str());
DOMDocument *domDoc = parser->getDocument();
if (!domDoc)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseXML")
<< "Cannot parse file `" << file << "' (domDoc = NULL).";
DOMElement *elementRoot = domDoc->getDocumentElement();
if (!elementRoot)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseXML") << "File `" << file << "' is empty.";
ParseTreeRP(pType, elementRoot, nTop, 0, mapping, mask);
ParseTreeDiamond(pType, elementRoot, nTop, 0, mapping, mask);
ParseTreeTotemTiming(pType, elementRoot, nTop, 0, mapping, mask);
ParseTreeTotemT2(pType, elementRoot, nTop, 0, mapping, mask);
}
//-----------------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::ParseTreeRP(ParseType pType,
xercesc::DOMNode *parent,
NodeType parentType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &mapping,
const std::unique_ptr<TotemAnalysisMask> &mask) {
#ifdef DEBUG
printf(">> TotemDAQMappingESSourceXML::ParseTreeRP(%s, %u, %u)\n",
cms::xerces::toString(parent->getNodeName()),
parentType,
parentID);
#endif
DOMNodeList *children = parent->getChildNodes();
for (unsigned int i = 0; i < children->getLength(); i++) {
DOMNode *n = children->item(i);
if (n->getNodeType() != DOMNode::ELEMENT_NODE)
continue;
NodeType type = GetNodeType(n);
#ifdef DEBUG
printf("\tname = %s, type = %u\n", cms::xerces::toString(n->getNodeName()), type);
#endif
// structure control
if (!RPNode(type))
continue;
NodeType expectedParentType;
switch (type) {
case nArm:
expectedParentType = nTop;
break;
case nRPStation:
expectedParentType = nArm;
break;
case nRPPot:
expectedParentType = nRPStation;
break;
case nRPPlane:
expectedParentType = nRPPot;
break;
case nChip:
expectedParentType = nRPPlane;
break;
case nChannel:
expectedParentType = nChip;
break;
default:
expectedParentType = nUnknown;
break;
}
if (expectedParentType != parentType) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Node " << cms::xerces::toString(n->getNodeName()) << " not allowed within "
<< cms::xerces::toString(parent->getNodeName()) << " block.\n";
}
// parse tag attributes
unsigned int id = 0, hw_id = 0;
bool id_set = false, hw_id_set = false;
bool fullMask = false;
DOMNamedNodeMap *attr = n->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id")) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &id);
id_set = true;
}
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "hw_id")) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%x", &hw_id);
hw_id_set = true;
}
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "full_mask"))
fullMask = (strcmp(cms::xerces::toString(a->getNodeValue()).c_str(), "no") != 0);
}
// content control
if (!id_set)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeRP")
<< "id not given for element `" << cms::xerces::toString(n->getNodeName()) << "'";
if (!hw_id_set && type == nChip && pType == pMapping)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeRP")
<< "hw_id not given for element `" << cms::xerces::toString(n->getNodeName()) << "'";
if (type == nRPPlane && id > 9)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeRP")
<< "Plane IDs range from 0 to 9. id = " << id << " is invalid.";
#ifdef DEBUG
printf("\tID found: 0x%x\n", id);
#endif
// store mapping data
if (pType == pMapping && type == nChip) {
const TotemFramePosition &framepos = ChipFramePosition(n);
TotemVFATInfo vfatInfo;
vfatInfo.hwID = hw_id;
const unsigned int armIdx = (parentID / 1000) % 10;
const unsigned int stIdx = (parentID / 100) % 10;
const unsigned int rpIdx = (parentID / 10) % 10;
const unsigned int plIdx = parentID % 10;
vfatInfo.symbolicID.symbolicID = TotemRPDetId(armIdx, stIdx, rpIdx, plIdx, id);
mapping->insert(framepos, vfatInfo);
continue;
}
// store mask data
if (pType == pMask && type == nChip) {
const unsigned int armIdx = (parentID / 1000) % 10;
const unsigned int stIdx = (parentID / 100) % 10;
const unsigned int rpIdx = (parentID / 10) % 10;
const unsigned int plIdx = parentID % 10;
TotemSymbID symbId;
symbId.symbolicID = TotemRPDetId(armIdx, stIdx, rpIdx, plIdx, id);
TotemVFATAnalysisMask am;
am.fullMask = fullMask;
GetChannels(n, am.maskedChannels);
mask->insert(symbId, am);
continue;
}
// recursion (deeper in the tree)
ParseTreeRP(pType, n, type, parentID * 10 + id, mapping, mask);
}
}
//----------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::ParseTreeDiamond(ParseType pType,
xercesc::DOMNode *parent,
NodeType parentType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &mapping,
const std::unique_ptr<TotemAnalysisMask> &mask) {
#ifdef DEBUG
printf(">> TotemDAQMappingESSourceXML::ParseTreeDiamond(%s, %u, %u)\n",
cms::xerces::toString(parent->getNodeName()),
parentType,
parentID);
#endif
DOMNodeList *children = parent->getChildNodes();
for (unsigned int i = 0; i < children->getLength(); i++) {
DOMNode *n = children->item(i);
if (n->getNodeType() != DOMNode::ELEMENT_NODE)
continue;
NodeType type = GetNodeType(n);
#ifdef DEBUG
printf("\tname = %s, type = %u\n", cms::xerces::toString(n->getNodeName()), type);
#endif
// structure control
if (!DiamondNode(type))
continue;
NodeType expectedParentType;
switch (type) {
case nArm:
expectedParentType = nTop;
break;
case nRPStation:
expectedParentType = nArm;
break;
case nRPPot:
expectedParentType = nRPStation;
break;
case nDiamondPlane:
expectedParentType = nRPPot;
break;
case nDiamondCh:
expectedParentType = nDiamondPlane;
break;
default:
expectedParentType = nUnknown;
break;
}
if (expectedParentType != parentType) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Node " << cms::xerces::toString(n->getNodeName()) << " not allowed within "
<< cms::xerces::toString(parent->getNodeName()) << " block.\n";
}
// parse tag attributes
unsigned int id = 0, hw_id = 0;
bool id_set = false, hw_id_set = false;
DOMNamedNodeMap *attr = n->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id")) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &id);
id_set = true;
}
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "hw_id")) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%x", &hw_id);
hw_id_set = true;
}
}
// content control
if (!id_set)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeDiamond")
<< "id not given for element `" << cms::xerces::toString(n->getNodeName()) << "'";
if (!hw_id_set && type == nDiamondCh && pType == pMapping)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeDiamond")
<< "hw_id not given for element `" << cms::xerces::toString(n->getNodeName()) << "'";
if (type == nDiamondPlane && id > 3)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeDiamond")
<< "Plane IDs range from 0 to 3. id = " << id << " is invalid.";
#ifdef DEBUG
printf("\tID found: 0x%x\n", id);
#endif
// store mapping data
if (pType == pMapping && type == nDiamondCh) {
const TotemFramePosition &framepos = ChipFramePosition(n);
TotemVFATInfo vfatInfo;
vfatInfo.hwID = hw_id;
if (type == nDiamondCh) {
unsigned int ArmNum = (parentID / 10000) % 10;
unsigned int StationNum = (parentID / 1000) % 10;
unsigned int RpNum = (parentID / 100) % 10;
unsigned int PlaneNum = (parentID % 100);
vfatInfo.symbolicID.symbolicID = CTPPSDiamondDetId(ArmNum, StationNum, RpNum, PlaneNum, id);
}
mapping->insert(framepos, vfatInfo);
continue;
}
unsigned int childId;
if (pType == pMapping && type == nDiamondPlane)
childId = parentID * 100 + id;
else
childId = parentID * 10 + id;
ParseTreeDiamond(pType, n, type, childId, mapping, mask);
}
}
//----------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::ParseTreeTotemTiming(ParseType pType,
xercesc::DOMNode *parent,
NodeType parentType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &mapping,
const std::unique_ptr<TotemAnalysisMask> &mask) {
DOMNodeList *children = parent->getChildNodes();
// Fill map hwId -> TotemTimingPlaneChannelPair
for (unsigned int i = 0; i < children->getLength(); i++) {
DOMNode *child = children->item(i);
if ((child->getNodeType() != DOMNode::ELEMENT_NODE) || (GetNodeType(child) != nTotemTimingCh))
continue;
int plane = -1;
DOMNamedNodeMap *attr = parent->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id"))
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%d", &plane);
}
int channel = -1;
unsigned int hwId = 0;
attr = child->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id"))
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%d", &channel);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "hwId"))
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%x", &hwId);
}
mapping->totemTimingChannelMap[(uint8_t)hwId] = TotemDAQMapping::TotemTimingPlaneChannelPair(plane, channel);
}
for (unsigned int i = 0; i < children->getLength(); i++) {
DOMNode *n = children->item(i);
if (n->getNodeType() != DOMNode::ELEMENT_NODE)
continue;
NodeType type = GetNodeType(n);
// structure control
if (!TotemTimingNode(type))
continue;
NodeType expectedParentType;
switch (type) {
case nArm:
expectedParentType = nTop;
break;
case nRPStation:
expectedParentType = nArm;
break;
case nRPPot:
expectedParentType = nRPStation;
break;
case nSampicBoard:
expectedParentType = nRPPot;
break;
case nSampicChannel:
expectedParentType = nSampicBoard;
break;
case nTotemTimingPlane:
expectedParentType = nRPPot;
break;
case nTotemTimingCh:
expectedParentType = nTotemTimingPlane;
break;
default:
expectedParentType = nUnknown;
break;
}
if (expectedParentType != parentType) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Node " << cms::xerces::toString(n->getNodeName()) << " not allowed within "
<< cms::xerces::toString(parent->getNodeName()) << " block.\n";
}
// parse tag attributes
unsigned int id = 0;
bool id_set = false;
DOMNamedNodeMap *attr = n->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id")) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &id);
id_set = true;
}
}
// content control
if (!id_set)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeTotemTiming")
<< "id not given for element `" << cms::xerces::toString(n->getNodeName()) << "'";
if (type == nSampicBoard && id > 5)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeTotemTiming")
<< "SampicBoard IDs range from 0 to 5. id = " << id << " is invalid.";
// store mapping data
if (pType == pMapping && type == nSampicChannel) {
const TotemFramePosition &framepos = ChipFramePosition(n);
TotemVFATInfo vfatInfo;
unsigned int ArmNum = (parentID / 10000) % 10;
unsigned int StationNum = (parentID / 1000) % 10;
unsigned int RpNum = (parentID / 100) % 10;
vfatInfo.symbolicID.symbolicID = TotemTimingDetId(ArmNum,
StationNum,
RpNum,
0,
TotemTimingDetId::ID_NOT_SET,
sampicSubDetId); //Dynamical: it is encoded in the frame
mapping->insert(framepos, vfatInfo);
continue;
}
unsigned int childId;
if (pType == pMapping && type == nSampicBoard)
childId = parentID * 100 + id;
else
childId = parentID * 10 + id;
ParseTreeTotemTiming(pType, n, type, childId, mapping, mask);
}
}
//----------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::ParseTreeTotemT2(ParseType pType,
xercesc::DOMNode *parent,
NodeType parentType,
unsigned int parentID,
const std::unique_ptr<TotemDAQMapping> &mapping,
const std::unique_ptr<TotemAnalysisMask> &mask) {
DOMNodeList *children = parent->getChildNodes();
for (unsigned int i = 0; i < children->getLength(); i++) {
DOMNode *child = children->item(i);
if (child->getNodeType() != DOMNode::ELEMENT_NODE)
continue;
NodeType type = GetNodeType(child);
// structure control
if (!TotemT2Node(type))
continue;
NodeType expectedParentType;
switch (type) {
case nArm:
expectedParentType = nTop;
break;
case nTotemT2Plane:
expectedParentType = nArm;
break;
case nTotemT2Tile:
expectedParentType = nTotemT2Plane;
break;
default:
expectedParentType = nUnknown;
break;
}
if (expectedParentType != parentType) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Node " << cms::xerces::toString(child->getNodeName()) << " not allowed within "
<< cms::xerces::toString(parent->getNodeName()) << " block.\n";
}
unsigned int id = 0;
bool id_set = false;
DOMNamedNodeMap *attr = child->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id")) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &id);
id_set = true;
}
}
if (pType == pMapping && type == nTotemT2Tile) {
// parse tag attributes
unsigned int hw_id = 0;
bool hw_id_set = false;
unsigned int channel_in_payload = 0;
bool payload_set = false;
DOMNamedNodeMap *attr = child->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "hwId")) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%x", &hw_id);
hw_id_set = true;
}
if (packedPayload && (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "pay"))) {
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &channel_in_payload);
payload_set = true;
}
}
// content control
if (!id_set)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeTotemT2")
<< "id not given for element `" << cms::xerces::toString(child->getNodeName()) << "'";
if (!hw_id_set)
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeTotemT2")
<< "hwId not given for element `" << cms::xerces::toString(child->getNodeName()) << "'";
if (packedPayload && (!payload_set))
throw cms::Exception("TotemDAQMappingESSourceXML::ParseTreeTotemT2")
<< "Payload position in fibre not given for element `" << cms::xerces::toString(child->getNodeName())
<< "'";
// store mapping data
const TotemT2FramePosition &framepos = (packedPayload ? ChipT2FramePosition(child) : TotemT2FramePosition());
const TotemFramePosition &frameposSingle = (packedPayload ? TotemFramePosition() : ChipFramePosition(child));
TotemVFATInfo vfatInfo;
vfatInfo.hwID = hw_id;
unsigned int arm = parentID / 10, plane = parentID % 10;
vfatInfo.symbolicID.symbolicID = TotemT2DetId(arm, plane, id);
if (verbosity > 2) {
edm::LogWarning("Totem") << "Print T2 frame pos (payload):" << framepos << " ("
<< (packedPayload ? "true" : "false") << ") hw_id / T2 DetID" << hw_id << "/"
<< TotemT2DetId(arm, plane, id) << endl;
}
if (packedPayload)
mapping->insert(framepos, vfatInfo);
else
mapping->insert(frameposSingle, vfatInfo);
continue;
}
// follow tree recursively
ParseTreeTotemT2(pType, child, type, parentID * 10 + id, mapping, mask);
}
}
//----------------------------------------------------------------------------------------------------
TotemFramePosition TotemDAQMappingESSourceXML::ChipFramePosition(xercesc::DOMNode *chipnode) {
TotemFramePosition fp;
unsigned char attributeFlag = 0;
DOMNamedNodeMap *attr = chipnode->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (fp.setXMLAttribute(
cms::xerces::toString(a->getNodeName()), cms::xerces::toString(a->getNodeValue()), attributeFlag) > 1) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Unrecognized tag `" << cms::xerces::toString(a->getNodeName()) << "' or incompatible value `"
<< cms::xerces::toString(a->getNodeValue()) << "'.";
}
}
if (!fp.checkXMLAttributeFlag(attributeFlag)) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Wrong/incomplete DAQ channel specification (attributeFlag = " << attributeFlag << ").";
}
return fp;
}
//----------------------------------------------------------------------------------------------------
TotemT2FramePosition TotemDAQMappingESSourceXML::ChipT2FramePosition(xercesc::DOMNode *chipnode) {
TotemT2FramePosition fp;
unsigned char attributeFlag = 0;
DOMNamedNodeMap *attr = chipnode->getAttributes();
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (fp.setXMLAttribute(
cms::xerces::toString(a->getNodeName()), cms::xerces::toString(a->getNodeValue()), attributeFlag) > 1) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Unrecognized T2 tag `" << cms::xerces::toString(a->getNodeName()) << "' or incompatible value `"
<< cms::xerces::toString(a->getNodeValue()) << "'.";
}
}
if (!fp.checkXMLAttributeFlag(attributeFlag)) {
throw cms::Exception("TotemDAQMappingESSourceXML")
<< "Wrong/incomplete T2 DAQ channel specification (attributeFlag = " << attributeFlag << ").";
}
return fp;
}
//----------------------------------------------------------------------------------------------------
TotemDAQMappingESSourceXML::NodeType TotemDAQMappingESSourceXML::GetNodeType(xercesc::DOMNode *n) {
// common node types
if (Test(n, tagArm))
return nArm;
if (Test(n, tagChip1))
return nChip;
if (Test(n, tagChip2))
return nChip;
// RP node types
if (Test(n, tagRPStation))
return nRPStation;
if (Test(n, tagRPPot))
return nRPPot;
if (Test(n, tagRPPlane))
return nRPPlane;
//diamond specifics
if (Test(n, tagDiamondCh))
return nDiamondCh;
if (Test(n, tagDiamondPlane))
return nDiamondPlane;
//totem timing specifics
if (Test(n, tagSampicBoard))
return nSampicBoard;
if (Test(n, tagSampicCh))
return nSampicChannel;
if (Test(n, tagTotemTimingCh))
return nTotemTimingCh;
if (Test(n, tagTotemTimingPlane))
return nTotemTimingPlane;
// TOTEM nT2 specifics
if (Test(n, tagTotemT2Plane))
return nTotemT2Plane;
if (Test(n, tagTotemT2Tile))
return nTotemT2Tile;
// for backward compatibility
if (Test(n, "trigger_vfat"))
return nSkip;
throw cms::Exception("TotemDAQMappingESSourceXML::GetNodeType")
<< "Unknown tag `" << cms::xerces::toString(n->getNodeName()) << "'.\n";
}
//----------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::GetChannels(xercesc::DOMNode *n, set<unsigned char> &channels) {
DOMNodeList *children = n->getChildNodes();
for (unsigned int i = 0; i < children->getLength(); i++) {
DOMNode *n = children->item(i);
if (n->getNodeType() != DOMNode::ELEMENT_NODE || !Test(n, "channel"))
continue;
DOMNamedNodeMap *attr = n->getAttributes();
bool idSet = false;
for (unsigned int j = 0; j < attr->getLength(); j++) {
DOMNode *a = attr->item(j);
if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id")) {
unsigned int id = 0;
sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &id);
channels.insert(id);
idSet = true;
break;
}
}
if (!idSet) {
throw cms::Exception("TotemDAQMappingESSourceXML::GetChannels") << "Channel tags must have an `id' attribute.";
}
}
}
//----------------------------------------------------------------------------------------------------
void TotemDAQMappingESSourceXML::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
// totemDAQMappingESSourceXML
edm::ParameterSetDescription desc;
desc.addUntracked<unsigned int>("verbosity", 0);
desc.addUntracked<std::string>("subSystem", "")->setComment("set it to: TrackingStrip, ...");
desc.add<unsigned int>("sampicSubDetId");
desc.add<bool>("multipleChannelsPerPayload", false);
{
edm::ParameterSetDescription vpsd1;
vpsd1.add<edm::EventRange>("validityRange", edm::EventRange(1, 0, 1, 1, 0, 0));
vpsd1.add<std::vector<std::string>>("mappingFileNames", {});
vpsd1.add<std::vector<std::string>>("maskFileNames", {});
std::vector<edm::ParameterSet> temp1;
temp1.reserve(1);
{
edm::ParameterSet temp2;
temp2.addParameter<edm::EventRange>("validityRange", edm::EventRange(1, 0, 1, 1, 0, 0));
temp2.addParameter<std::vector<std::string>>("mappingFileNames", {});
temp2.addParameter<std::vector<std::string>>("maskFileNames", {});
temp1.push_back(temp2);
}
desc.addVPSet("configuration", vpsd1, temp1)->setComment("validityRange, mappingFileNames and maskFileNames");
}
descriptions.add("totemDAQMappingESSourceXML", desc);
// or use the following to generate the label from the module's C++ type
//descriptions.addWithDefaultLabel(desc);
}
//----------------------------------------------------------------------------------------------------
DEFINE_FWK_EVENTSETUP_SOURCE(TotemDAQMappingESSourceXML);
|