MEtoEDM

MEtoEDMObject

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
#ifndef MEtoEDMFormat_h
#define MEtoEDMFormat_h

/** \class MEtoEDM
 *  
 *  DataFormat class to hold the information from a ME tranformed into
 *  ROOT objects as appropriate
 *
 *  \author M. Strang SUNY-Buffalo
 */

#include <TClass.h>
#include <TObject.h>
#include <TH1F.h>
#include <TH1S.h>
#include <TH1D.h>
#include <TH2F.h>
#include <TH2S.h>
#include <TH2D.h>
#include <TH2Poly.h>
#include <TH3F.h>
#include <TProfile.h>
#include <TProfile2D.h>
#include <TObjString.h>
#include <TString.h>
#include <THashList.h>
#include <TList.h>

#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <map>
#include <cstdint>

#define METOEDMFORMAT_DEBUG 0

template <class T>
class MEtoEDM {
public:
  MEtoEDM() {}
  explicit MEtoEDM(size_t reservedSize) { MEtoEdmObject.reserve(reservedSize); }
  virtual ~MEtoEDM() {}

  typedef std::vector<uint32_t> TagList;

  struct MEtoEDMObject {
    std::string name;
    TagList tags;
    T object;
  };

  typedef std::vector<MEtoEDMObject> MEtoEdmObjectVector;

  void putMEtoEdmObject(const std::string &name, const T &object) {
    typename MEtoEdmObjectVector::value_type temp;
    MEtoEdmObject.push_back(temp);
    MEtoEdmObject.back().name = name;
    MEtoEdmObject.back().object = object;
  }

  const MEtoEdmObjectVector &getMEtoEdmObject() const { return MEtoEdmObject; }

  bool CheckBinLabels(const TAxis *a1, const TAxis *a2) {
    // check that axis have same labels
    THashList *l1 = (const_cast<TAxis *>(a1))->GetLabels();
    THashList *l2 = (const_cast<TAxis *>(a2))->GetLabels();

    if (!l1 && !l2)
      return true;
    if (!l1 || !l2) {
      return false;
    }
    // check now labels sizes  are the same
    if (l1->GetSize() != l2->GetSize()) {
      return false;
    }
    for (int i = 1; i <= a1->GetNbins(); ++i) {
      TString label1 = a1->GetBinLabel(i);
      TString label2 = a2->GetBinLabel(i);
      if (label1 != label2) {
        return false;
      }
    }
    return true;
  }

  bool mergeProduct(const MEtoEDM<T> &newMEtoEDM) {
    const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
    const size_t nObjects = newMEtoEDMObject.size();
    //  NOTE: we remember the present size since we will only add content
    //        from newMEtoEDMObject after this point
    const size_t nOldObjects = MEtoEdmObject.size();

    // if the old and new are not the same size, we want to report a problem
    if (nObjects != nOldObjects) {
      std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
                << nObjects << ", old=" << nOldObjects << std::endl;
    }

    for (unsigned int i = 0; i < nObjects; ++i) {
      unsigned int j = 0;
      // see if the name is already in the old container up to the point where
      // we may have added new entries in the container
      const std::string &name = newMEtoEDMObject[i].name;
      if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
        j = i;
      } else {
        j = 0;
        while (j < nOldObjects && (MEtoEdmObject[j].name != name))
          ++j;
      }
      if (j >= nOldObjects) {
        // this value is only in the new container, not the old one
#if METOEDMFORMAT_DEBUG
        std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
#endif
        MEtoEdmObject.push_back(newMEtoEDMObject[i]);
      } else if (MEtoEdmObject[j].object.CanExtendAllAxes() && newMEtoEDMObject[i].object.CanExtendAllAxes()) {
        TList list;
        list.Add((TObject *)&newMEtoEDMObject[i].object);
        if (MEtoEdmObject[j].object.Merge(&list) == -1) {
          std::cout << "ERROR MEtoEDM::mergeProducts(): merge failed for '" << name << "'" << std::endl;
        }
      } else {
        // this value is also in the new container: add the two
        if (MEtoEdmObject[j].object.GetNbinsX() == newMEtoEDMObject[i].object.GetNbinsX() &&
            MEtoEdmObject[j].object.GetXaxis()->GetXmin() == newMEtoEDMObject[i].object.GetXaxis()->GetXmin() &&
            MEtoEdmObject[j].object.GetXaxis()->GetXmax() == newMEtoEDMObject[i].object.GetXaxis()->GetXmax() &&
            MEtoEdmObject[j].object.GetNbinsY() == newMEtoEDMObject[i].object.GetNbinsY() &&
            MEtoEdmObject[j].object.GetYaxis()->GetXmin() == newMEtoEDMObject[i].object.GetYaxis()->GetXmin() &&
            MEtoEdmObject[j].object.GetYaxis()->GetXmax() == newMEtoEDMObject[i].object.GetYaxis()->GetXmax() &&
            MEtoEdmObject[j].object.GetNbinsZ() == newMEtoEDMObject[i].object.GetNbinsZ() &&
            MEtoEdmObject[j].object.GetZaxis()->GetXmin() == newMEtoEDMObject[i].object.GetZaxis()->GetXmin() &&
            MEtoEdmObject[j].object.GetZaxis()->GetXmax() == newMEtoEDMObject[i].object.GetZaxis()->GetXmax() &&
            CheckBinLabels((TAxis *)MEtoEdmObject[j].object.GetXaxis(),
                           (TAxis *)newMEtoEDMObject[i].object.GetXaxis()) &&
            CheckBinLabels((TAxis *)MEtoEdmObject[j].object.GetYaxis(),
                           (TAxis *)newMEtoEDMObject[i].object.GetYaxis()) &&
            CheckBinLabels((TAxis *)MEtoEdmObject[j].object.GetZaxis(),
                           (TAxis *)newMEtoEDMObject[i].object.GetZaxis())) {
          MEtoEdmObject[j].object.Add(&newMEtoEDMObject[i].object, 1.);
        } else {
          std::cout
              << "ERROR MEtoEDM::mergeProducts(): found histograms with different axis limits or different labels, '"
              << name << "' not merged" << std::endl;
#if METOEDMFORMAT_DEBUG
          std::cout << MEtoEdmObject[j].name << " " << newMEtoEDMObject[i].name << std::endl;
          std::cout << MEtoEdmObject[j].object.GetNbinsX() << " " << newMEtoEDMObject[i].object.GetNbinsX()
                    << std::endl;
          std::cout << MEtoEdmObject[j].object.GetXaxis()->GetXmin() << " "
                    << newMEtoEDMObject[i].object.GetXaxis()->GetXmin() << std::endl;
          std::cout << MEtoEdmObject[j].object.GetXaxis()->GetXmax() << " "
                    << newMEtoEDMObject[i].object.GetXaxis()->GetXmax() << std::endl;
          std::cout << MEtoEdmObject[j].object.GetNbinsY() << " " << newMEtoEDMObject[i].object.GetNbinsY()
                    << std::endl;
          std::cout << MEtoEdmObject[j].object.GetYaxis()->GetXmin() << " "
                    << newMEtoEDMObject[i].object.GetYaxis()->GetXmin() << std::endl;
          std::cout << MEtoEdmObject[j].object.GetYaxis()->GetXmax() << " "
                    << newMEtoEDMObject[i].object.GetYaxis()->GetXmax() << std::endl;
          std::cout << MEtoEdmObject[j].object.GetNbinsZ() << " " << newMEtoEDMObject[i].object.GetNbinsZ()
                    << std::endl;
          std::cout << MEtoEdmObject[j].object.GetZaxis()->GetXmin() << " "
                    << newMEtoEDMObject[i].object.GetZaxis()->GetXmin() << std::endl;
          std::cout << MEtoEdmObject[j].object.GetZaxis()->GetXmax() << " "
                    << newMEtoEDMObject[i].object.GetZaxis()->GetXmax() << std::endl;
#endif
        }
      }
    }
    return true;
  }

  void swap(MEtoEDM<T> &iOther) { MEtoEdmObject.swap(iOther.MEtoEdmObject); }

private:
  MEtoEdmObjectVector MEtoEdmObject;

};  // end class declaration

template <>
inline bool MEtoEDM<double>::mergeProduct(const MEtoEDM<double> &newMEtoEDM) {
  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
  const size_t nObjects = newMEtoEDMObject.size();
  //  NOTE: we remember the present size since we will only add content
  //        from newMEtoEDMObject after this point
  const size_t nOldObjects = MEtoEdmObject.size();

  // if the old and new are not the same size, we want to report a problem
  if (nObjects != nOldObjects) {
    std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
              << nObjects << ", old=" << nOldObjects << std::endl;
  }

  for (unsigned int i = 0; i < nObjects; ++i) {
    unsigned int j = 0;
    // see if the name is already in the old container up to the point where
    // we may have added new entries in the container
    const std::string &name = newMEtoEDMObject[i].name;
    if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
      j = i;
    } else {
      j = 0;
      while (j < nOldObjects && (MEtoEdmObject[j].name != name))
        ++j;
    }
    if (j >= nOldObjects) {
      // this value is only in the new container, not the old one
#if METOEDMFORMAT_DEBUG
      std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
#endif
      MEtoEdmObject.push_back(newMEtoEDMObject[i]);
    }
  }
  return true;
}

template <>
inline bool MEtoEDM<int>::mergeProduct(const MEtoEDM<int> &newMEtoEDM) {
  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
  const size_t nObjects = newMEtoEDMObject.size();
  //  NOTE: we remember the present size since we will only add content
  //        from newMEtoEDMObject after this point
  const size_t nOldObjects = MEtoEdmObject.size();

  // if the old and new are not the same size, we want to report a problem
  if (nObjects != nOldObjects) {
    std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
              << nObjects << ", old=" << nOldObjects << std::endl;
  }

  for (unsigned int i = 0; i < nObjects; ++i) {
    unsigned int j = 0;
    // see if the name is already in the old container up to the point where
    // we may have added new entries in the container
    const std::string &name = newMEtoEDMObject[i].name;
    if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
      j = i;
    } else {
      j = 0;
      while (j < nOldObjects && (MEtoEdmObject[j].name != name))
        ++j;
    }
    if (j >= nOldObjects) {
      // this value is only in the new container, not the old one
#if METOEDMFORMAT_DEBUG
      std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
#endif
      MEtoEdmObject.push_back(newMEtoEDMObject[i]);
    } else {
      // this value is also in the new container: add the two
      if (MEtoEdmObject[j].name.find("EventInfo/processedEvents") != std::string::npos) {
        MEtoEdmObject[j].object += (newMEtoEDMObject[i].object);
      }
      if (MEtoEdmObject[j].name.find("EventInfo/iEvent") != std::string::npos ||
          MEtoEdmObject[j].name.find("EventInfo/iLumiSection") != std::string::npos) {
        if (MEtoEdmObject[j].object < newMEtoEDMObject[i].object) {
          MEtoEdmObject[j].object = (newMEtoEDMObject[i].object);
        }
      }
    }
  }
  return true;
}

template <>
inline bool MEtoEDM<long long>::mergeProduct(const MEtoEDM<long long> &newMEtoEDM) {
  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
  const size_t nObjects = newMEtoEDMObject.size();
  //  NOTE: we remember the present size since we will only add content
  //        from newMEtoEDMObject after this point
  const size_t nOldObjects = MEtoEdmObject.size();

  // if the old and new are not the same size, we want to report a problem
  if (nObjects != nOldObjects) {
    std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
              << nObjects << ", old=" << nOldObjects << std::endl;
  }

  for (unsigned int i = 0; i < nObjects; ++i) {
    unsigned int j = 0;
    // see if the name is already in the old container up to the point where
    // we may have added new entries in the container
    const std::string &name = newMEtoEDMObject[i].name;
    if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
      j = i;
    } else {
      j = 0;
      while (j < nOldObjects && (MEtoEdmObject[j].name != name))
        ++j;
    }
    if (j >= nOldObjects) {
      // this value is only in the new container, not the old one
#if METOEDMFORMAT_DEBUG
      std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
#endif
      MEtoEdmObject.push_back(newMEtoEDMObject[i]);
    } else {
      // this value is also in the new container: add the two
      if (MEtoEdmObject[j].name.find("EventInfo/processedEvents") != std::string::npos) {
        MEtoEdmObject[j].object += (newMEtoEDMObject[i].object);
      }
      if (MEtoEdmObject[j].name.find("EventInfo/iEvent") != std::string::npos ||
          MEtoEdmObject[j].name.find("EventInfo/iLumiSection") != std::string::npos) {
        if (MEtoEdmObject[j].object < newMEtoEDMObject[i].object) {
          MEtoEdmObject[j].object = (newMEtoEDMObject[i].object);
        }
      }
    }
  }
  return true;
}

template <>
inline bool MEtoEDM<TString>::mergeProduct(const MEtoEDM<TString> &newMEtoEDM) {
  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
  const size_t nObjects = newMEtoEDMObject.size();
  //  NOTE: we remember the present size since we will only add content
  //        from newMEtoEDMObject after this point
  const size_t nOldObjects = MEtoEdmObject.size();

  // if the old and new are not the same size, we want to report a problem
  if (nObjects != nOldObjects) {
    std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
              << nObjects << ", old=" << nOldObjects << std::endl;
  }

  for (unsigned int i = 0; i < nObjects; ++i) {
    unsigned int j = 0;
    // see if the name is already in the old container up to the point where
    // we may have added new entries in the container
    const std::string &name = newMEtoEDMObject[i].name;
    if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
      j = i;
    } else {
      j = 0;
      while (j < nOldObjects && (MEtoEdmObject[j].name != name))
        ++j;
    }
    if (j >= nOldObjects) {
      // this value is only in the new container, not the old one
#if METOEDMFORMAT_DEBUG
      std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
#endif
      MEtoEdmObject.push_back(newMEtoEDMObject[i]);
    }
  }
  return true;
}

#endif