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
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

#include "CondTools/Hcal/interface/HcalDbXml.h"
#include "CondFormats/HcalObjects/interface/HcalRawGains.h"

bool hasKey (const std::string& line, const std::string& key) {
  return line.find ("<"+key+">") != std::string::npos;
}

double getValue (const std::string& line) {
  unsigned pos1 = line.find (">");
  unsigned pos2 = line.find ("</");
  if (pos1 != std::string::npos && pos2 != std::string::npos) {
    std::string valueStr (line, pos1+1, pos2-pos1);
    return atof (valueStr.c_str());
  }
  std::cerr << "Can not decode line: " << line << std::endl;
  return 0;
}

int getIntValue (const std::string& line) {
  unsigned pos1 = line.find (">");
  unsigned pos2 = line.find ("</");
  if (pos1 != std::string::npos && pos2 != std::string::npos) {
    std::string valueStr (line, pos1+1, pos2-pos1);
    return atoi (valueStr.c_str());
  }
  std::cerr << "Can not decode line: " << line << std::endl;
  return 0;
}

int main (int argn, char* argv []) {
  std::ifstream inStream (argv[1]);
  std::ofstream outStream (argv[2]);
  HcalRawGains gains;
  float gain = 0;
  int eta = 0;
  int phi = 0;
  int depth = 0;
  int z = 0;
  char buffer [1024];
  while (inStream.getline (buffer, 1024)) {
    std::string line (buffer);
    if (hasKey (line, "Z")) z = getIntValue (line);
    else if (hasKey (line, "PHI")) phi = getIntValue (line);
    else if (hasKey (line, "ETA")) eta = getIntValue (line);
    else if (hasKey (line, "DEPTH")) depth = getIntValue (line);
    else if (hasKey (line, "COEFFICIENT")) gain = getValue (line);
    else if (hasKey (line, "PHI")) phi = getIntValue (line);
    else if (hasKey (line, "PHI")) phi = getIntValue (line);
    else if (hasKey (line, "/DATA")) { // flush data
      HcalDetId id (HcalEndcap, z*eta, phi, depth);
      HcalRawGain newItem (id.rawId(), gain, 0., 0., HcalRawGain::GOOD);
      gains.addValues (id, newItem);
      
    }
  }
  gains.sort ();
  HcalDbXml::dumpObject (outStream, 1, 1, 0xfffffffful, "he_signal_leveling_from_Petr", gains);
  return 0;
}