#include #include #include #include #include "JDetector/JDetector.hh" #include "JDetector/JDetectorToolkit.hh" #include "JGeometry3D/JVector3D.hh" #include "JIO/JByteArrayIO.hh" #include "Jeep/JeepToolkit.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { using namespace JPP; /** * Wild card for string identifier or module identifier. */ static const int WILDCARD = -1; static const std::string setx_t = "setx"; //!< Set x-position static const std::string sety_t = "sety"; //!< Set y-position static const std::string setz_t = "setz"; //!< Set z-position static const std::string addx_t = "addx"; //!< Add x-position static const std::string addy_t = "addy"; //!< Add y-position static const std::string addz_t = "addz"; //!< Add z-position static const std::string subx_t = "subx"; //!< Subtract x-position static const std::string suby_t = "suby"; //!< Subtract y-position static const std::string subz_t = "subz"; //!< Subtract z-position static const std::string mul_t = "mul"; //!< Multiply z-position by (1 + value) static const std::string div_t = "div"; //!< Divide z-position by (1 + value) /** * Auxiliary class for module modifications. */ struct JModifier { /** * Default constructor. */ JModifier() {} /** * Apply action to given module depending on number of values. * * \param module module * \return true if valid action; else false */ bool apply(JModule& module) const { if (action == setx_t) { module.set(JVector3D(value, module.getY(), module.getZ())); } else if (action == addx_t) { module.add(JVector3D(value, 0.0, 0.0)); } else if (action == subx_t) { module.sub(JVector3D(value, 0.0, 0.0)); } else if (action == sety_t) { module.set(JVector3D(module.getX(), value, module.getZ())); } else if (action == addy_t) { module.add(JVector3D(0.0, value, 0.0)); } else if (action == suby_t) { module.sub(JVector3D(0.0, value, 0.0)); } else if (action == setz_t) { module.set(JVector3D(module.getX(), module.getY(), value)); } else if (action == addz_t) { module.add(JVector3D(0.0, 0.0, value)); } else if (action == subz_t) { module.sub(JVector3D(0.0, 0.0, value)); } else if (action == mul_t) { JVector3D center; if (value > 0.0) center = JVector3D(module.getPosition().getX(), module.getPosition().getY(), module.getPosition().getZ() * (1.0 + value)); else center = JVector3D(module.getPosition().getX(), module.getPosition().getY(), module.getPosition().getZ() / (1.0 - value)); module.set(center); } else if (action == div_t) { JVector3D center; if (value > 0.0) center = JVector3D(module.getPosition().getX(), module.getPosition().getY(), module.getPosition().getZ() / (1.0 + value)); else center = JVector3D(module.getPosition().getX(), module.getPosition().getY(), module.getPosition().getZ() * (1.0 - value)); module.set(center); } else { // return false; } return true; } /** * Read modifier from input. * * \param in input stream * \param modifier modifier * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JModifier& modifier) { return in >> modifier.action >> modifier.value; } /** * Write modifier to output. * * \param out output stream * \param modifier modifier * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier) { return out << modifier.action << ' ' << modifier.value; } std::string action; double value; }; /** * Print module modification. * * \param out output stream * \param module module * \param modifier modifier */ inline void print(std::ostream& out, const JModule& module, const JModifier& modifier) { using namespace std; using namespace JPP; out << "Modifier" << ' ' << getLabel(module.getLocation()) << ' ' << setw(10) << module.getID() << ' ' << "action " << modifier << endl; } } /** * \file * * Auxiliary program to modify detector calibration. * * Syntax: *
 *     -M     "     (setx|addx|subx|sety|addy|suby|setz|addz|subz) value"
 *     -(S|s) "         (setx|addx|subx|sety|addy|suby|setz|addz|subz) value"
 * 
* Options -M and -S refer to a module and a string, respectively.\n * The values provided for a string modification coherently apply to the modules of the specified string number.\n * The option -s is equivalent to option -S except that * the action applies only to the optical modules in the string and not the base module. * * If the module identifier or string number is -1, * the action is applied to all modules or strings in the detector, respectively. * * For options (set|add|sub)(x|y|z), the value corresponds to last character of the the quoted action. * * The units of all positions are m. * * Multiple options -M, -S or -s will be processed in order of appearance. * * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; typedef pair pair_type; string detectorFile; vector mod; vector str; vector dos; size_t size; int debug; try { JParser<> zap("Auxiliary program to modify detector."); zap['a'] = make_field(detectorFile, "detector file I/O"); zap['M'] = make_field(mod, "module modification") = JPARSER::initialised(); zap['S'] = make_field(str, "string modification (optical modules and base module") = JPARSER::initialised(); zap['s'] = make_field(dos, "string modification (optical modules only)") = JPARSER::initialised(); zap['N'] = make_field(size, "internal buffer size") = 500000; zap['d'] = make_field(debug, "debug level") = 2; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } if (getFilenameExtension(detectorFile) != BINARY_DETECTOR_FILE_FORMAT[1]) { FATAL("Wrong file name extension - should be " << BINARY_DETECTOR_FILE_FORMAT[1] << endl); } JDetector detector; JByteArrayWriter buffer; buffer.resize(size); fstream io; io.open(detectorFile.c_str(), ios::binary | ios_base::in | ios_base::out); if (!io) { FATAL("Error opening file " << detectorFile << endl); } size_t len = 0; for ( ; ; ) { io.read(buffer.data() + len, buffer.size() - len); len += io.gcount(); if (io.eof()) break; else buffer.resize(2 * buffer.size()); } buffer.resize(len); JByteArrayReader in(buffer.data(), buffer.size()); detector.read(in); detector.comment.clear(); detector.comment.add(argv[0]); for (vector::const_iterator i = mod.begin(); i != mod.end(); ++i) { for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) { if (module->getID() == i->first || i->first == WILDCARD ){ if (debug >= debug_t) { print(cout, *module, i->second); } if (!i->second.apply(*module)) { ERROR("No valid action: " << i->first << ' ' << i->second << endl); } } } } for (vector::const_iterator i = str.begin(); i != str.end(); ++i) { for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) { if (module->getString() == i->first || i->first == WILDCARD) { if (debug >= debug_t) { print(cout, *module, i->second); } if (!i->second.apply(*module)) { ERROR("No valid action: " << i->first << ' ' << i->second << endl); } } } } for (vector::const_iterator i = dos.begin(); i != dos.end(); ++i) { for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) { if (module->getFloor() != 0) { if (module->getString() == i->first || i->first == WILDCARD) { if (debug >= debug_t) { print(cout, *module, i->second); } if (!i->second.apply(*module)) { ERROR("No valid action: " << i->first << ' ' << i->second << endl); } } } } } buffer.clear(); detector.write(buffer); io.clear(); io.seekp(0, ios_base::beg); io.write(buffer.data(), buffer.size()); io.close(); }