46 const std::string xmlfilename = this->
getProperty(
"OutputFile");
50 std::map<int, std::vector<detid_t>> groupIDwkspIDMap;
52 g_log.
debug() <<
"Size of map = " << groupIDwkspIDMap.size() <<
'\n';
55 std::map<int, std::vector<detid_t>> groupIDdetectorRangeMap;
59 this->
printToXML(groupIDdetectorRangeMap, xmlfilename);
68 const bool excludeZero = !
getProperty(
"SaveUngroupedDetectors");
71 for (
size_t iws = 0; iws <
mGroupWS->getNumberHistograms(); iws++) {
73 auto groupid =
static_cast<int>(
mGroupWS->y(iws)[0]);
75 if (excludeZero && groupid == 0)
79 auto it = groupwkspmap.find(groupid);
80 if (it == groupwkspmap.end()) {
81 std::vector<detid_t> tempvector;
82 groupwkspmap[groupid] = tempvector;
84 it = groupwkspmap.find(groupid);
85 if (it == groupwkspmap.end()) {
86 throw std::invalid_argument(
"Could not find group ID the after creating it in the map.");
90 const auto &mspec =
mGroupWS->getSpectrum(iws);
91 auto &detids = mspec.getDetectorIDs();
92 if (detids.size() != 1) {
93 throw std::invalid_argument(
"Each spectrum should only have one detector. Spectrum " +
97 it->second.insert(it->second.end(), detids.begin(), detids.end());
105 std::map<
int, std::vector<detid_t>> &groupdetidrangemap) {
107 for (
auto &groupdetids : groupdetidsmap) {
110 const int groupid = groupdetids.first;
111 sort(groupdetids.second.begin(), groupdetids.second.end());
113 g_log.
debug() <<
"Group " << groupid <<
" has " << groupdetids.second.size() <<
" detectors. \n";
116 std::vector<detid_t> detranges;
117 detid_t st = groupdetids.second[0];
119 for (
size_t i = 1; i < groupdetids.second.size(); i++) {
120 detid_t detid = groupdetids.second[i];
121 if (detid == ed + 1) {
126 detranges.emplace_back(st);
127 detranges.emplace_back(ed);
134 detranges.emplace_back(st);
135 detranges.emplace_back(ed);
138 groupdetidrangemap[groupid] = detranges;
144 const std::string &xmlfilename) {
147 const auto &instrument =
mGroupWS->getInstrument();
148 const std::string instrumentName = instrument->getName();
149 g_log.
debug() <<
"Instrument " << instrumentName <<
'\n';
152 AutoPtr<Document> pDoc =
new Document;
153 AutoPtr<Element> pRoot = pDoc->createElement(
"detector-grouping");
154 pDoc->appendChild(pRoot);
155 pRoot->setAttribute(
"instrument", instrumentName);
156 pRoot->setAttribute(
"idf-date", instrument->getValidFromDate().toISO8601String());
159 if (
mGroupWS->run().hasProperty(
"Description")) {
160 const std::string description =
mGroupWS->run().getProperty(
"Description")->value();
161 pRoot->setAttribute(
"description", description);
165 for (
const auto &groupdetidrange : groupdetidrangemap) {
168 const int groupid = groupdetidrange.first;
169 std::stringstream sid;
172 AutoPtr<Element> pChildGroup = pDoc->createElement(
"group");
173 pChildGroup->setAttribute(
"ID", sid.str());
175 std::string groupNameProp =
"GroupName_" + sid.str();
176 if (
mGroupWS->run().hasProperty(groupNameProp)) {
177 const std::string groupName =
mGroupWS->run().getProperty(groupNameProp)->value();
178 pChildGroup->setAttribute(
"name", groupName);
181 pRoot->appendChild(pChildGroup);
183 g_log.
debug() <<
"Group ID = " << groupid <<
'\n';
186 std::stringstream ss;
188 for (
size_t i = 0; i < groupdetidrange.second.size() / 2; i++) {
191 detid_t ist = groupdetidrange.second[i * 2];
192 detid_t ied = groupdetidrange.second[i * 2 + 1];
195 ss << ist <<
"-" << ied;
196 }
else if (ist == ied) {
199 throw std::invalid_argument(
"Impossible to have this sitaution!");
202 if (i < groupdetidrange.second.size() / 2 - 1) {
206 g_log.
debug() <<
"Detectors: " << groupdetidrange.second[i * 2] <<
", " << groupdetidrange.second[i * 2 + 1]
210 const std::string textvalue = ss.str();
212 g_log.
debug() <<
"Detector IDs Node: " << textvalue <<
'\n';
215 AutoPtr<Element> pDetid = pDoc->createElement(
"detids");
216 AutoPtr<Text> pText1 = pDoc->createTextNode(textvalue);
217 pDetid->appendChild(pText1);
218 pChildGroup->appendChild(pDetid);
224 writer.setNewLine(
"\n");
225 writer.setOptions(XMLWriter::PRETTY_PRINT);
228 ofs.open(xmlfilename.c_str(), std::fstream::out);
230 ofs <<
"<?xml version=\"1.0\"?>\n";
232 writer.writeNode(std::cout, pDoc);
233 writer.writeNode(ofs, pDoc);