Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-19 23:20:32

0001 //  .
0002 // ..: P. Chang, philip@physics.ucsd.edu
0003 
0004 #include "stringutil.h"
0005 
0006 //#############################################################################
0007 // rstrip TString
0008 //
0009 void RooUtil::StringUtil::rstrip(TString &in, TString separator) {
0010   TString save(in);
0011   if (separator.EqualTo(" ")) {
0012     // Remove end-of-line spaces
0013     std::string str = in.Data();
0014     str.erase(str.find_last_not_of(" \n\r\t") + 1);
0015     in = str.c_str();
0016   } else {
0017     // Remove text after separator
0018     TObjArray *list = in.Tokenize(separator);
0019     if (list->GetEntries() != 0) {
0020       in = ((TObjString *)list->At(0))->GetString();
0021     }
0022   }
0023   // Print
0024   return;
0025 }
0026 
0027 //#############################################################################
0028 // Convert TString -> vector<TString>
0029 // like ' '.split()
0030 //
0031 RooUtil::StringUtil::vecTString RooUtil::StringUtil::split(TString in, TString separator) {
0032   RooUtil::StringUtil::vecTString out;
0033   TObjArray *list = in.Tokenize(separator);
0034   for (unsigned i = 0; i < (unsigned)list->GetEntries(); ++i) {
0035     TString token = ((TObjString *)list->At(i))->GetString();
0036     out.push_back(token);
0037   }
0038   if (out.size() == 0) {
0039     out.push_back("");
0040   }
0041   delete list;
0042   return out;
0043 }
0044 
0045 //#############################################################################
0046 // Convert TString -> vector<TString>
0047 // like ' '.rsplit()
0048 //
0049 RooUtil::StringUtil::vecTString RooUtil::StringUtil::rsplit(TString in, TString separator) {
0050   TString left = in;
0051   rstrip(left, separator);
0052   int size = left.Length();
0053   vecTString rtn;
0054   rtn.push_back(left);
0055   rtn.push_back(in(size + 1, in.Length() - size - 1));
0056   return rtn;
0057 }
0058 
0059 //#############################################################################
0060 // Convert vector<TString> -> TString
0061 // like ':'.join()
0062 //
0063 TString RooUtil::StringUtil::join(RooUtil::StringUtil::vecTString in, TString joiner, Int_t rm_blanks) {
0064   std::stringstream ss;
0065   for (unsigned i = 0; i < in.size(); ++i) {
0066     TString token = in[i];
0067     ss << token << ((i < in.size() - 1) ? joiner : "");
0068   }
0069   // Remove blanks
0070   TString out = ss.str();
0071   if (rm_blanks) {
0072     out.ReplaceAll(" ", "");
0073   }
0074   return out;
0075 }
0076 
0077 //#############################################################################
0078 // Convert TString -> vector<TString> -> TString
0079 //
0080 TString RooUtil::StringUtil::sjoin(TString in, TString separator, TString joiner, Int_t rm_blanks) {
0081   RooUtil::StringUtil::vecTString vec = RooUtil::StringUtil::split(in, separator);
0082   TString out = RooUtil::StringUtil::join(vec, joiner, rm_blanks);
0083   return out;
0084 }
0085 
0086 //#############################################################################
0087 RooUtil::StringUtil::vecTString RooUtil::StringUtil::filter(RooUtil::StringUtil::vecTString &vec, TString keyword) {
0088   RooUtil::StringUtil::vecTString newvec;
0089   for (unsigned i = 0; i < vec.size(); ++i) {
0090     if (!vec[i].Contains(keyword)) {
0091       continue;
0092     }
0093     newvec.push_back(vec[i]);
0094   }
0095   return newvec;
0096 }
0097 
0098 //#############################################################################
0099 RooUtil::StringUtil::vecVecTString RooUtil::StringUtil::chunk(RooUtil::StringUtil::vecTString vec, Int_t nchunk) {
0100   int bunch_size = vec.size() / nchunk + (vec.size() % nchunk > 0);
0101   vecVecTString bunches;
0102   for (size_t i = 0; i < vec.size(); i += bunch_size) {
0103     auto last = std::min(vec.size(), i + bunch_size);
0104     bunches.emplace_back(vec.begin() + i, vec.begin() + last);
0105   }
0106   return bunches;
0107 }
0108 
0109 //#############################################################################
0110 // From std::vector<TString> form an expression for TTree::Draw
0111 TString RooUtil::StringUtil::formexpr(vecTString in) {
0112   in.erase(std::remove_if(in.begin(), in.end(), [](TString s) { return s.EqualTo("1"); }), in.end());
0113   if (in.size() == 0)
0114     in.push_back("1");
0115   return Form("(%s)", RooUtil::StringUtil::join(in, ")*(").Data());
0116 }
0117 
0118 //#############################################################################
0119 // Clean unwanted parantheses
0120 TString RooUtil::StringUtil::cleanparantheses(TString input) {
0121   std::string s = input.Data();
0122   remove_parantheses(s);
0123   return s.c_str();
0124 }
0125 
0126 //#############################################################################
0127 // Under the hood for cleaning unwanted parantheses
0128 void RooUtil::StringUtil::remove_parantheses(std::string &S) {
0129   using namespace std;
0130   map<int, bool> pmap;
0131   for (size_t i = 0; i < S.size(); i++) {
0132     map<int, bool>::iterator it;
0133     if (S.at(i) == '(') {
0134       pmap[i] = true;
0135     } else if (S.at(i) == ')') {
0136       it = pmap.end();
0137       it--;
0138       if (!(*it).second) {
0139         pmap.erase(it);
0140       } else {
0141         S.erase(S.begin() + i);
0142         S.erase(S.begin() + (*it).first);
0143         pmap.erase(it);
0144         i = i - 2;
0145       }
0146     } else {
0147       if (!pmap.empty()) {
0148         it = pmap.end();
0149         it--;
0150         (*it).second = false;
0151       }
0152     }
0153   }
0154 }
0155 
0156 //#############################################################################
0157 // Given a template replace tokens by pattern.
0158 // Could be thought of as "".format() from python. (although it's not nearly as good as that...)
0159 TString RooUtil::StringUtil::format(TString tmp, std::vector<TString> tokens) {
0160   for (auto &token : tokens) {
0161     std::vector<TString> v = rsplit(token, "=");
0162     TString key = v[0];
0163     TString val = v[1];
0164     tmp.ReplaceAll(Form("{%s}", key.Data()), val);
0165   }
0166   return tmp;
0167 }
0168 
0169 //std::string RooUtil::StringUtil::parser(std::string _input, int loc_){
0170 //
0171 //    using namespace std;
0172 //
0173 //    string input = _input;
0174 //
0175 //    set<char> support;
0176 //    support.insert('+');
0177 //    support.insert('-');
0178 //    support.insert('*');
0179 //    support.insert('/');
0180 //    support.insert('>');
0181 //    support.insert('<');
0182 //    support.insert('=');
0183 //
0184 //    string expi;
0185 //    set<char> op;
0186 //    int loc = loc_;
0187 //    int size = input.size();
0188 //
0189 //    while(1){
0190 //        if(input[loc] ==  '('){
0191 //            expi += parser(input,loc+1);
0192 //        }else if(input[loc] == ')'){
0193 //          if((input[loc+1] != '*') && (input[loc+1] != '/')){
0194 //              return expi;
0195 //          }else{
0196 //              if ((op.find('+') == op.end()) && (op.find('-') == op.end())){
0197 //                  return expi;
0198 //              }else{
0199 //                  return '('+expi+')';
0200 //              }
0201 //          }
0202 //        }else{
0203 //            char temp = input[loc];
0204 //            expi=expi+temp;
0205 //            if(support.find(temp) != support.end()){
0206 //                op.insert(temp);
0207 //            }
0208 //        }
0209 //        loc++;
0210 //        if(loc >= size){
0211 //            break;
0212 //        }
0213 //    }
0214 //
0215 //    return expi;
0216 //}