Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:24

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWInterface
0004 // Class  :     FWPSetCellEditor
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:
0010 //         Created:  Mon Feb 28 20:44:59 CET 2011
0011 //
0012 #include <boost/algorithm/string.hpp>
0013 #include <sstream>
0014 #include "KeySymbols.h"
0015 
0016 // user include files
0017 #include "Fireworks/FWInterface/src/FWPSetCellEditor.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "Fireworks/Core/interface/fwLog.h"
0020 
0021 #include "FWCore/Utilities/interface/Parse.h"
0022 
0023 //______________________________________________________________________________
0024 
0025 template <class T>
0026 bool editNumericParameter(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0027   std::stringstream str(value);
0028   T v;
0029   str >> v;
0030   bool fail = str.fail();
0031   if (tracked)
0032     ps.addParameter(label, v);
0033   else
0034     ps.addUntrackedParameter(label, v);
0035 
0036   return fail;
0037 }
0038 //______________________________________________________________________________
0039 
0040 void editStringParameter(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0041   if (tracked)
0042     ps.addParameter(label, value);
0043   else
0044     ps.addUntrackedParameter(label, value);
0045 }
0046 
0047 //______________________________________________________________________________
0048 
0049 void editBoolParameter(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0050   bool x = false;
0051 
0052   if (boost::iequals(value, "true")) {
0053     x = true;
0054   } else if (boost::iequals(value, "false")) {
0055     x = false;
0056   } else {
0057     fwLog(fwlog::kError) << "Invalid value. Possible values are true/false case insensitive." << std::endl;
0058     return;
0059   }
0060   if (tracked)
0061     ps.addParameter<bool>(label, x);
0062   else
0063     ps.addUntrackedParameter<bool>(label, x);
0064 }
0065 
0066 //______________________________________________________________________________
0067 void editFileInPath(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0068   if (tracked)
0069     ps.addParameter(label, edm::FileInPath(value));
0070   else
0071     ps.addUntrackedParameter(label, edm::FileInPath(value));
0072 }
0073 
0074 //______________________________________________________________________________
0075 
0076 bool editVInputTag(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0077   std::vector<edm::InputTag> inputTags;
0078   std::stringstream iss(value);
0079   std::string vitem;
0080   bool fail = false;
0081   size_t fst, lst;
0082 
0083   while (getline(iss, vitem, ',')) {
0084     fst = vitem.find('[');
0085     lst = vitem.find(']');
0086 
0087     if (fst != std::string::npos)
0088       vitem.erase(fst, 1);
0089     if (lst != std::string::npos)
0090       vitem.erase(lst, 1);
0091 
0092     std::vector<std::string> tokens = edm::tokenize(vitem, ":");
0093     size_t nwords = tokens.size();
0094 
0095     if (nwords > 3) {
0096       fail = true;
0097       return fail;
0098     } else {
0099       std::string it_label("");
0100       std::string it_instance("");
0101       std::string it_process("");
0102 
0103       if (nwords > 0)
0104         it_label = tokens[0];
0105       if (nwords > 1)
0106         it_instance = tokens[1];
0107       if (nwords > 2)
0108         it_process = tokens[2];
0109 
0110       inputTags.push_back(edm::InputTag(it_label, it_instance, it_process));
0111     }
0112   }
0113 
0114   if (tracked)
0115     ps.addParameter(label, inputTags);
0116   else
0117     ps.addUntrackedParameter(label, inputTags);
0118 
0119   return fail;
0120 }
0121 
0122 //______________________________________________________________________________
0123 
0124 bool editInputTag(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0125   std::vector<std::string> tokens = edm::tokenize(value, ":");
0126   size_t nwords = tokens.size();
0127 
0128   bool fail;
0129 
0130   if (nwords > 3) {
0131     fail = true;
0132   } else {
0133     std::string it_label("");
0134     std::string it_instance("");
0135     std::string it_process("");
0136 
0137     if (nwords > 0)
0138       it_label = tokens[0];
0139     if (nwords > 1)
0140       it_instance = tokens[1];
0141     if (nwords > 2)
0142       it_process = tokens[2];
0143 
0144     if (tracked)
0145       ps.addParameter(label, edm::InputTag(it_label, it_instance, it_process));
0146     else
0147       ps.addUntrackedParameter(label, edm::InputTag(it_label, it_instance, it_process));
0148 
0149     fail = false;
0150   }
0151 
0152   return fail;
0153 }
0154 
0155 //______________________________________________________________________________
0156 
0157 bool editESInputTag(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0158   std::vector<std::string> tokens = edm::tokenize(value, ":");
0159   size_t nwords = tokens.size();
0160 
0161   bool fail;
0162 
0163   if (nwords > 2) {
0164     fail = true;
0165   } else {
0166     std::string it_module("");
0167     std::string it_data("");
0168 
0169     if (nwords > 0)
0170       it_module = tokens[0];
0171     if (nwords > 1)
0172       it_data = tokens[1];
0173 
0174     if (tracked)
0175       ps.addParameter(label, edm::ESInputTag(it_module, it_data));
0176     else
0177       ps.addUntrackedParameter(label, edm::ESInputTag(it_module, it_data));
0178 
0179     fail = false;
0180   }
0181 
0182   return fail;
0183 }
0184 
0185 //______________________________________________________________________________
0186 template <typename T>
0187 void editVectorParameter(edm::ParameterSet &ps, bool tracked, const std::string &label, const std::string &value) {
0188   std::vector<T> valueVector;
0189 
0190   std::stringstream iss(value);
0191   std::string vitem;
0192 
0193   size_t fst, lst;
0194 
0195   while (getline(iss, vitem, ',')) {
0196     fst = vitem.find('[');
0197     lst = vitem.find(']');
0198 
0199     if (fst != std::string::npos)
0200       vitem.erase(fst, 1);
0201     if (lst != std::string::npos)
0202       vitem.erase(lst, 1);
0203 
0204     std::stringstream oss(vitem);
0205     T on;
0206     oss >> on;
0207 
0208     valueVector.push_back(on);
0209   }
0210 
0211   if (tracked)
0212     ps.addParameter(label, valueVector);
0213   else
0214     ps.addUntrackedParameter(label, valueVector);
0215 }
0216 
0217 //______________________________________________________________________________
0218 
0219 bool FWPSetCellEditor::apply(FWPSetTableManager::PSetData &data, FWPSetTableManager::PSetData &parent) {
0220   switch (data.type) {
0221     case 'I':
0222       editNumericParameter<int32_t>(*parent.pset, data.tracked, data.label, GetText());
0223       break;
0224     case 'B':
0225       editBoolParameter(*parent.pset, data.tracked, data.label, GetText());
0226       break;
0227     case 'U':
0228       editNumericParameter<uint32_t>(*parent.pset, data.tracked, data.label, GetText());
0229       break;
0230     case 'D':
0231       editNumericParameter<double>(*parent.pset, data.tracked, data.label, GetText());
0232       break;
0233     case 'L':
0234       editNumericParameter<long long>(*parent.pset, data.tracked, data.label, GetText());
0235       break;
0236     case 'X':
0237       editNumericParameter<unsigned long long>(*parent.pset, data.tracked, data.label, GetText());
0238       break;
0239     case 'S':
0240       editStringParameter(*parent.pset, data.tracked, data.label, GetText());
0241       break;
0242     case 'i':
0243       editVectorParameter<int32_t>(*parent.pset, data.tracked, data.label, GetText());
0244       break;
0245     case 'u':
0246       editVectorParameter<uint32_t>(*parent.pset, data.tracked, data.label, GetText());
0247       break;
0248     case 'l':
0249       editVectorParameter<long long>(*parent.pset, data.tracked, data.label, GetText());
0250       break;
0251     case 'x':
0252       editVectorParameter<unsigned long long>(*parent.pset, data.tracked, data.label, GetText());
0253       break;
0254     case 'd':
0255       editVectorParameter<double>(*parent.pset, data.tracked, data.label, GetText());
0256       break;
0257     case 's':
0258       editVectorParameter<std::string>(*parent.pset, data.tracked, data.label, GetText());
0259       break;
0260     case 't':
0261       editInputTag(*parent.pset, data.tracked, data.label, GetText());
0262       break;
0263     case 'g':
0264       editESInputTag(*parent.pset, data.tracked, data.label, GetText());
0265       break;
0266     case 'v':
0267       editVInputTag(*parent.pset, data.tracked, data.label, GetText());
0268       break;
0269     case 'F':
0270       editFileInPath(*parent.pset, data.tracked, data.label, GetText());
0271       break;
0272     default:
0273       fwLog(fwlog::kError) << "unsupported parameter" << std::endl;
0274       UnmapWindow();
0275       return false;
0276   }
0277   return true;
0278 }
0279 
0280 //______________________________________________________________________________
0281 
0282 bool FWPSetCellEditor::HandleKey(Event_t *event) {
0283   UInt_t keysym = event->fCode;
0284 
0285   if (keysym == (UInt_t)gVirtualX->KeysymToKeycode(kKey_Escape)) {
0286     TGFrame *p = dynamic_cast<TGFrame *>(const_cast<TGWindow *>(GetParent()));
0287     while (p) {
0288       TGMainFrame *mp = dynamic_cast<TGMainFrame *>(p);
0289       //   printf("editor find parent %p, %s, %p\n", p, p->ClassName(), mp);
0290       if (mp) {
0291         return mp->HandleKey(event);
0292       }
0293       p = dynamic_cast<TGFrame *>(const_cast<TGWindow *>(p->GetParent()));
0294     }
0295   }
0296 
0297   return TGTextEntry::HandleKey(event);
0298 }