#define BUFSZ 0xFF


void crab1D(const char *iFileName = "qinv.dat",
	    const char *oFileName = "Crab1D.root") {
  const float qMax   = 0.4;
  const float qMin   = 0.;
  const int   nBins  = 40;
  const float qWidth = (qMax - qMin)/nBins;
  FILE *file = fopen(iFileName, "r");
  char buf[BUFSZ] = {0};
  TFile *fOut = new TFile(oFileName, "RECREATE");
  TH1F *hQinv = new TH1F("hQinv", ";Q_{inv} (GeV/c);#", nBins, qMin, qMax);


  //
  // Skip 2 useless lines
  //
  fgets(buf, BUFSZ, file);
  fgets(buf, BUFSZ, file);

  float qInv, cf, dCf;
  int num, den;
  double numD, denD;
  while(!feof(file)) {
    int ret = fscanf(file, "%f %f %f %i %i %f %f", &qInv, &cf, &dCf, &num, &den, &numD, &denD);
    qInv /= 1e3; // MeV to GeV
    if (ret != 7) { // 7 - number of parameters to read
      fgets(buf, BUFSZ, file);
      continue;
    }
    hQinv->Fill(qInv, cf);
    int bin = (int)(qInv/qWidth) + 1;
    hQinv->SetBinError(bin, dCf);
  }
  
  hQinv->Write();
  fclose(file);
  fOut->Close();
}
