47 const std::string xmlfilename = this->
getProperty(
"OutputFile");
51 std::map<int, std::vector<detid_t>> groupIDwkspIDMap;
53 g_log.
debug() <<
"Size of map = " << groupIDwkspIDMap.size() <<
'\n';
56 std::map<int, std::vector<detid_t>> groupIDdetectorRangeMap;
60 this->
printToXML(groupIDdetectorRangeMap, xmlfilename);
69 const bool excludeZero = !
getProperty(
"SaveUngroupedDetectors");
72 for (
size_t iws = 0; iws <
mGroupWS->getNumberHistograms(); iws++) {
74 auto groupid =
static_cast<int>(
mGroupWS->y(iws)[0]);
76 if (excludeZero && groupid == 0)
80 auto it = groupwkspmap.find(groupid);
81 if (it == groupwkspmap.end()) {
82 std::vector<detid_t> tempvector;
83 groupwkspmap[groupid] = tempvector;
85 it = groupwkspmap.find(groupid);
86 if (it == groupwkspmap.end()) {
87 throw std::invalid_argument(
"Could not find group ID the after creating it in the map.");
91 const auto &mspec =
mGroupWS->getSpectrum(iws);
92 auto &detids = mspec.getDetectorIDs();
93 if (detids.size() != 1) {
94 throw std::invalid_argument(
"Each spectrum should only have one detector. Spectrum " +
98 it->second.insert(it->second.end(), detids.begin(), detids.end());
106 std::map<
int, std::vector<detid_t>> &groupdetidrangemap) {
108 for (
auto &groupdetids : groupdetidsmap) {
111 const int groupid = groupdetids.first;
112 sort(groupdetids.second.begin(), groupdetids.second.end());
114 g_log.
debug() <<
"Group " << groupid <<
" has " << groupdetids.second.size() <<
" detectors. \n";
117 std::vector<detid_t> detranges;
118 detid_t st = groupdetids.second[0];
120 for (
size_t i = 1; i < groupdetids.second.size(); i++) {
121 detid_t detid = groupdetids.second[i];
122 if (detid == ed + 1) {
127 detranges.emplace_back(st);
128 detranges.emplace_back(ed);
135 detranges.emplace_back(st);
136 detranges.emplace_back(ed);
139 groupdetidrangemap[groupid] = detranges;
145 const std::string &xmlfilename) {
148 const auto &instrument =
mGroupWS->getInstrument();
149 const std::string instrumentName = instrument->getName();
150 g_log.
debug() <<
"Instrument " << instrumentName <<
'\n';
153 AutoPtr<Document> pDoc =
new Document;
154 AutoPtr<Element> pRoot = pDoc->createElement(
"detector-grouping");
155 pDoc->appendChild(pRoot);
156 pRoot->setAttribute(
"instrument", instrumentName);
157 pRoot->setAttribute(
"idf-date",
mGroupWS->instrumentMetadata().validFromDate().toISO8601String());
160 if (
mGroupWS->run().hasProperty(
"Description")) {
161 const std::string description =
mGroupWS->run().getProperty(
"Description")->value();
162 pRoot->setAttribute(
"description", description);
166 for (
const auto &groupdetidrange : groupdetidrangemap) {
169 const int groupid = groupdetidrange.first;
170 std::stringstream sid;
173 AutoPtr<Element> pChildGroup = pDoc->createElement(
"group");
174 pChildGroup->setAttribute(
"ID", sid.str());
176 std::string groupNameProp =
"GroupName_" + sid.str();
177 if (
mGroupWS->run().hasProperty(groupNameProp)) {
178 const std::string groupName =
mGroupWS->run().getProperty(groupNameProp)->value();
179 pChildGroup->setAttribute(
"name", groupName);
182 pRoot->appendChild(pChildGroup);
184 g_log.
debug() <<
"Group ID = " << groupid <<
'\n';
187 std::stringstream ss;
189 for (
size_t i = 0; i < groupdetidrange.second.size() / 2; i++) {
192 detid_t ist = groupdetidrange.second[i * 2];
193 detid_t ied = groupdetidrange.second[i * 2 + 1];
196 ss << ist <<
"-" << ied;
197 }
else if (ist == ied) {
200 throw std::invalid_argument(
"Impossible to have this sitaution!");
203 if (i < groupdetidrange.second.size() / 2 - 1) {
207 g_log.
debug() <<
"Detectors: " << groupdetidrange.second[i * 2] <<
", " << groupdetidrange.second[i * 2 + 1]
211 const std::string textvalue = ss.str();
213 g_log.
debug() <<
"Detector IDs Node: " << textvalue <<
'\n';
216 AutoPtr<Element> pDetid = pDoc->createElement(
"detids");
217 AutoPtr<Text> pText1 = pDoc->createTextNode(textvalue);
218 pDetid->appendChild(pText1);
219 pChildGroup->appendChild(pDetid);
225 writer.setNewLine(
"\n");
226 writer.setOptions(XMLWriter::PRETTY_PRINT);
229 ofs.open(xmlfilename.c_str(), std::fstream::out);
231 ofs <<
"<?xml version=\"1.0\"?>\n";
233 writer.writeNode(ofs, pDoc);