<commit_before>/*
 * Copyright (c) 2014-2016, imec
 * All rights reserved.
 */

#include <random>
#include <memory>
#include <cstdio>
#include <iostream>
#include <climits>
#include <stdexcept>

#include "error.h"
#include "bpmf.h"
#include "io.h"

static const bool measure_perf = false;

std::ostream *Sys::os;
int Sys::procid = -1;
int Sys::nprocs = -1;

int Sys::nsims;
int Sys::burnin;
int Sys::update_freq;
double Sys::alpha = 2.0;

std::string Sys::odirname = "";

bool Sys::permute = true;
bool Sys::verbose = false;

// verifies that A has the same non-zero structure as B
void assert_same_struct(SparseMatrixD &A, SparseMatrixD &B)
{
    assert(A.cols() == B.cols());
    assert(A.rows() == B.rows());
    for(int i=0; i<A.cols(); ++i) assert(A.col(i).nonZeros() == B.col(i).nonZeros());
}

//
// Does predictions for prediction matrix T
// Computes RMSE (Root Means Square Error)
//
void Sys::predict(Sys& other, bool all)
{
    int n = (iter < burnin) ? 0 : (iter - burnin);

    double se(0.0); // squared err
    double se_avg(0.0); // squared avg err
    unsigned nump(0); // number of predictions

    int lo = all ? 0 : from();
    int hi = all ? num() : to();
    #pragma omp parallel for reduction(+:se,se_avg,nump)
    for(int k = lo; k<hi; k++) {
        for (Eigen::SparseMatrix<double>::InnerIterator it(T,k); it; ++it)
        {
#ifdef BPMF_REDUCE
            if (it.row() >= other.to() || it.row() < other.from())
                continue;
#endif
            auto m = items().col(it.col());
            auto u = other.items().col(it.row());

            assert(m.norm() > 0.0);
            assert(u.norm() > 0.0);

            const double pred = m.dot(u) + mean_rating;
            se += sqr(it.value() - pred);

            // update average prediction
            double &avg = Pavg.coeffRef(it.row(), it.col());
            double delta = pred - avg;
            avg = (n == 0) ? pred : (avg + delta/n);
            double &m2 = Pm2.coeffRef(it.row(), it.col());
            m2 = (n == 0) ? 0 : m2 + delta * (pred - avg);
            se_avg += sqr(it.value() - avg);

            nump++;
        }
    }

    rmse = sqrt( se / nump );
    rmse_avg = sqrt( se_avg / nump );
}

//
// Prints sampling progress
//
void Sys::print(double items_per_sec, double ratings_per_sec, double norm_u, double norm_m) {
  char buf[1024];
  std::string phase = (iter < Sys::burnin) ? "Burnin" : "Sampling";
  sprintf(buf, "%d: %s iteration %d:\t RMSE: %3.4f\tavg RMSE: %3.4f\tFU(%6.2f)\tFM(%6.2f)\titems/sec: %6.2f\tratings/sec: %6.2fM\n",
                    Sys::procid, phase.c_str(), iter, rmse, rmse_avg, norm_u, norm_m, items_per_sec, ratings_per_sec / 1e6);
  Sys::cout() << buf;
}

//
// Constructor with that reads MTX files
//
Sys::Sys(std::string name, std::string fname, std::string probename)
    : name(name), iter(-1), assigned(false), dom(nprocs+1)
{

    read_matrix(fname, M);
    read_matrix(probename, T);

    auto rows = std::max(M.rows(), T.rows());
    auto cols = std::max(M.cols(), T.cols());
    M.conservativeResize(rows,cols);
    T.conservativeResize(rows,cols);
    Pm2 = Pavg = Torig = T; // reference ratings and predicted ratings
    assert(M.rows() == Pavg.rows());
    assert(M.cols() == Pavg.cols());
    assert(Sys::nprocs <= (int)Sys::max_procs);
}

//
// Constructs Sys as transpose of existing Sys
//
Sys::Sys(std::string name, const SparseMatrixD &Mt, const SparseMatrixD &Pt) : name(name), iter(-1), assigned(false), dom(nprocs+1) {
    M = Mt.transpose();
    Pm2 = Pavg = T = Torig = Pt.transpose(); // reference ratings and predicted ratings
    assert(M.rows() == Pavg.rows());
    assert(M.cols() == Pavg.cols());
}

Sys::~Sys()
{
    if (measure_perf) {
        Sys::cout() << " --------------------\n";
        Sys::cout() << name << ": sampling times on " << procid << "\n";
        for(int i = from(); i<to(); ++i)
        {
            Sys::cout() << "\t" << nnz(i) << "\t" << sample_time.at(i) / nsims  << "\n";
        }
        Sys::cout() << " --------------------\n\n";
    }
}

bool Sys::has_prop_posterior() const
{
    return propMu.nonZeros() > 0;
}

void Sys::add_prop_posterior(std::string fnames)
{
    if (fnames.empty()) return;

    std::size_t pos = fnames.find_first_of(",");
    std::string mu_name = fnames.substr(0, pos);
    std::string lambda_name = fnames.substr(pos+1);

    read_matrix(mu_name, propMu);
    read_matrix(lambda_name, propLambda);

    assert(propMu.cols() == num());
    assert(propLambda.cols() == num());

    assert(propMu.rows() == num_latent);
    assert(propLambda.rows() == num_latent * num_latent);

}

//
// Intializes internal Matrices and Vectors
//
void Sys::init()
{
    //-- M
    assert(M.rows() > 0 && M.cols() > 0);
    mean_rating = M.sum() / M.nonZeros();
    items().setZero();
    sum_map().setZero();
    cov_map().setZero();
    norm_map().setZero();
    col_permutation.setIdentity(num());

#ifdef BPMF_REDUCE
    precMu = MatrixNXd::Zero(num_latent, num());
    precLambda = Eigen::MatrixXd::Zero(num_latent * num_latent, num());
#endif

    if (Sys::odirname.size())
    {
        aggrMu = Eigen::MatrixXd::Zero(num_latent, num());
        aggrLambda = Eigen::MatrixXd::Zero(num_latent * num_latent, num());
    }

    int count_sum = 0;
    for(int k = 0; k<M.cols(); k++) {
        int count = M.col(k).nonZeros();
        count_sum += count;
    }

    Sys::cout() << "mean rating: " << mean_rating << std::endl;
    Sys::cout() << "total number of ratings in train: " << M.nonZeros() << std::endl;
    Sys::cout() << "total number of ratings in test: " << T.nonZeros() << std::endl;
    Sys::cout() << "average ratings per row: " << (double)M.nonZeros() / (double)M.cols() << std::endl;
    Sys::cout() << "num " << name << ": " << num() << std::endl;
    if (has_prop_posterior())
    {
        Sys::cout() << "with propagated posterior" << std::endl;
    }

    if (measure_perf) sample_time.resize(num(), .0);
}

class PrecomputedLLT : public Eigen::LLT<MatrixNNd>
{
  public:
    void operator=(const MatrixNNd &m) { m_matrix = m; m_isInitialized = true; m_info = Eigen::Success; }
};

void Sys::computeMuLambda(long idx, const Sys &other, VectorNd &rr, MatrixNNd &MM) const
{
#pragma omp taskloop grainsize(1000)
    for (SparseMatrixD::InnerIterator it(M, idx); it; ++it)
    {
        auto col = other.items().col(it.row());
        MM.triangularView<Eigen::Upper>() += col * col.transpose();
        rr.noalias() += col * ((it.value() - mean_rating) * alpha);
    }
}

//
// Update ONE movie or one user
//
VectorNd Sys::sample(long idx, Sys &other)
{
    auto start = tick();

    VectorNd hp_mu;
    MatrixNNd hp_LambdaF;
    MatrixNNd hp_LambdaL;
    if (has_prop_posterior())
    {
        hp_mu = propMu.col(idx);
        hp_LambdaF = Eigen::Map<MatrixNNd>(propLambda.col(idx).data());
        hp_LambdaL =  hp_LambdaF.llt().matrixL();
    }
    else
    {
        hp_mu = hp.mu;
        hp_LambdaF = hp.LambdaF;
        hp_LambdaL = hp.LambdaL;
    }

    VectorNd rr = hp_LambdaF * hp.mu;                // vector num_latent x 1, we will use it in formula (14) from the paper
    MatrixNNd MM(MatrixNNd::Zero());
    PrecomputedLLT chol;                             // matrix num_latent x num_latent, chol="lambda_i with *" from formula (14)

#ifdef BPMF_REDUCE
    rr += precMu.col(idx);
    MM += precLambdaMatrix(idx);
#else
    computeMuLambda(idx, other, rr, MM);
#endif

    // copy upper -> lower part, matrix is symmetric.
    MM.triangularView<Eigen::Lower>() = MM.transpose();

    chol.compute(hp_LambdaF + alpha * MM);

    if(chol.info() != Eigen::Success) THROWERROR("Cholesky failed");

    // now we should calculate formula (14) from the paper
    // u_i for k-th iteration = Gaussian distribution N(u_i | mu_i with *, [lambda_i with *]^-1) =
    //                        = mu_i with * + s * [U]^-1,
    //                        where
    //                              s is a random vector with N(0, I),
    //                              mu_i with * is a vector num_latent x 1,
    //                              mu_i with * = [lambda_i with *]^-1 * rr,
    //                              lambda_i with * = L * U

    // Expression u_i = U \ (s + (L \ rr)) in Matlab looks for Eigen library like:

    chol.matrixL().solveInPlace(rr);                    // L*Y=rr => Y=L\rr, we store Y result again in rr vector
    rr += nrandn();                                     // rr=s+(L\rr), we store result again in rr vector
    chol.matrixU().solveInPlace(rr);                    // u_i=U\rr
    items().col(idx) = rr;                              // we save rr vector in items matrix (it is user features matrix)

#ifdef BPMF_REDUCE
    #pragma omp critical
    for (SparseMatrixD::InnerIterator it(M, idx); it; ++it)
    {
        other.precLambdaMatrix(it.row()).triangularView<Eigen::Upper>() += rr * rr.transpose();
        other.precMu.col(it.row()).noalias() += rr * (it.value() - mean_rating) * alpha;
    }
#endif

    auto stop = tick();
    register_time(idx, 1e6 * (stop - start));
    //Sys::cout() << "  " << count << ": " << 1e6*(stop - start) << std::endl;

    assert(rr.norm() > .0);

    return rr;
}

//
// update ALL movies / users in parallel
//
void Sys::sample(Sys &other)
{
    iter++;
    thread_vector<VectorNd>  sums(VectorNd::Zero()); // sum
    thread_vector<double>    norms(0.0); // squared norm
    thread_vector<MatrixNNd> prods(MatrixNNd::Zero()); // outer prod

#ifdef BPMF_REDUCE
    other.precMu.setZero();
    other.precLambda.setZero();
#endif

#pragma omp parallel for schedule(guided)
    for (int i = from(); i < to(); ++i)
    {
#pragma omp task
        {
            auto r = sample(i, other);

            MatrixNNd cov = (r * r.transpose());
            prods.local() += cov;
            sums.local() += r;
            norms.local() += r.squaredNorm();

            if (iter >= burnin && Sys::odirname.size())
            {
                aggrMu.col(i) += r;
                aggrLambda.col(i) += Eigen::Map<Eigen::VectorXd>(cov.data(), num_latent * num_latent);
            }

            send_item(i);
        }
    }
#pragma omp taskwait

    VectorNd sum = sums.combine();
    MatrixNNd prod = prods.combine();
    double norm = norms.combine();

    const int N = num();
    local_sum() = sum;
    local_cov() = (prod - (sum * sum.transpose() / N)) / (N-1);
    local_norm() = norm;
}

void Sys::register_time(int i, double t)
{
    if (measure_perf) sample_time.at(i) += t;
}<commit_msg>Remove illegal OpenMP pragma<commit_after>/*
 * Copyright (c) 2014-2016, imec
 * All rights reserved.
 */

#include <random>
#include <memory>
#include <cstdio>
#include <iostream>
#include <climits>
#include <stdexcept>

#include "error.h"
#include "bpmf.h"
#include "io.h"

static const bool measure_perf = false;

std::ostream *Sys::os;
int Sys::procid = -1;
int Sys::nprocs = -1;

int Sys::nsims;
int Sys::burnin;
int Sys::update_freq;
double Sys::alpha = 2.0;

std::string Sys::odirname = "";

bool Sys::permute = true;
bool Sys::verbose = false;

// verifies that A has the same non-zero structure as B
void assert_same_struct(SparseMatrixD &A, SparseMatrixD &B)
{
    assert(A.cols() == B.cols());
    assert(A.rows() == B.rows());
    for(int i=0; i<A.cols(); ++i) assert(A.col(i).nonZeros() == B.col(i).nonZeros());
}

//
// Does predictions for prediction matrix T
// Computes RMSE (Root Means Square Error)
//
void Sys::predict(Sys& other, bool all)
{
    int n = (iter < burnin) ? 0 : (iter - burnin);

    double se(0.0); // squared err
    double se_avg(0.0); // squared avg err
    unsigned nump(0); // number of predictions

    int lo = all ? 0 : from();
    int hi = all ? num() : to();
    #pragma omp parallel for reduction(+:se,se_avg,nump)
    for(int k = lo; k<hi; k++) {
        for (Eigen::SparseMatrix<double>::InnerIterator it(T,k); it; ++it)
        {
#ifdef BPMF_REDUCE
            if (it.row() >= other.to() || it.row() < other.from())
                continue;
#endif
            auto m = items().col(it.col());
            auto u = other.items().col(it.row());

            assert(m.norm() > 0.0);
            assert(u.norm() > 0.0);

            const double pred = m.dot(u) + mean_rating;
            se += sqr(it.value() - pred);

            // update average prediction
            double &avg = Pavg.coeffRef(it.row(), it.col());
            double delta = pred - avg;
            avg = (n == 0) ? pred : (avg + delta/n);
            double &m2 = Pm2.coeffRef(it.row(), it.col());
            m2 = (n == 0) ? 0 : m2 + delta * (pred - avg);
            se_avg += sqr(it.value() - avg);

            nump++;
        }
    }

    rmse = sqrt( se / nump );
    rmse_avg = sqrt( se_avg / nump );
}

//
// Prints sampling progress
//
void Sys::print(double items_per_sec, double ratings_per_sec, double norm_u, double norm_m) {
  char buf[1024];
  std::string phase = (iter < Sys::burnin) ? "Burnin" : "Sampling";
  sprintf(buf, "%d: %s iteration %d:\t RMSE: %3.4f\tavg RMSE: %3.4f\tFU(%6.2f)\tFM(%6.2f)\titems/sec: %6.2f\tratings/sec: %6.2fM\n",
                    Sys::procid, phase.c_str(), iter, rmse, rmse_avg, norm_u, norm_m, items_per_sec, ratings_per_sec / 1e6);
  Sys::cout() << buf;
}

//
// Constructor with that reads MTX files
//
Sys::Sys(std::string name, std::string fname, std::string probename)
    : name(name), iter(-1), assigned(false), dom(nprocs+1)
{

    read_matrix(fname, M);
    read_matrix(probename, T);

    auto rows = std::max(M.rows(), T.rows());
    auto cols = std::max(M.cols(), T.cols());
    M.conservativeResize(rows,cols);
    T.conservativeResize(rows,cols);
    Pm2 = Pavg = Torig = T; // reference ratings and predicted ratings
    assert(M.rows() == Pavg.rows());
    assert(M.cols() == Pavg.cols());
    assert(Sys::nprocs <= (int)Sys::max_procs);
}

//
// Constructs Sys as transpose of existing Sys
//
Sys::Sys(std::string name, const SparseMatrixD &Mt, const SparseMatrixD &Pt) : name(name), iter(-1), assigned(false), dom(nprocs+1) {
    M = Mt.transpose();
    Pm2 = Pavg = T = Torig = Pt.transpose(); // reference ratings and predicted ratings
    assert(M.rows() == Pavg.rows());
    assert(M.cols() == Pavg.cols());
}

Sys::~Sys()
{
    if (measure_perf) {
        Sys::cout() << " --------------------\n";
        Sys::cout() << name << ": sampling times on " << procid << "\n";
        for(int i = from(); i<to(); ++i)
        {
            Sys::cout() << "\t" << nnz(i) << "\t" << sample_time.at(i) / nsims  << "\n";
        }
        Sys::cout() << " --------------------\n\n";
    }
}

bool Sys::has_prop_posterior() const
{
    return propMu.nonZeros() > 0;
}

void Sys::add_prop_posterior(std::string fnames)
{
    if (fnames.empty()) return;

    std::size_t pos = fnames.find_first_of(",");
    std::string mu_name = fnames.substr(0, pos);
    std::string lambda_name = fnames.substr(pos+1);

    read_matrix(mu_name, propMu);
    read_matrix(lambda_name, propLambda);

    assert(propMu.cols() == num());
    assert(propLambda.cols() == num());

    assert(propMu.rows() == num_latent);
    assert(propLambda.rows() == num_latent * num_latent);

}

//
// Intializes internal Matrices and Vectors
//
void Sys::init()
{
    //-- M
    assert(M.rows() > 0 && M.cols() > 0);
    mean_rating = M.sum() / M.nonZeros();
    items().setZero();
    sum_map().setZero();
    cov_map().setZero();
    norm_map().setZero();
    col_permutation.setIdentity(num());

#ifdef BPMF_REDUCE
    precMu = MatrixNXd::Zero(num_latent, num());
    precLambda = Eigen::MatrixXd::Zero(num_latent * num_latent, num());
#endif

    if (Sys::odirname.size())
    {
        aggrMu = Eigen::MatrixXd::Zero(num_latent, num());
        aggrLambda = Eigen::MatrixXd::Zero(num_latent * num_latent, num());
    }

    int count_sum = 0;
    for(int k = 0; k<M.cols(); k++) {
        int count = M.col(k).nonZeros();
        count_sum += count;
    }

    Sys::cout() << "mean rating: " << mean_rating << std::endl;
    Sys::cout() << "total number of ratings in train: " << M.nonZeros() << std::endl;
    Sys::cout() << "total number of ratings in test: " << T.nonZeros() << std::endl;
    Sys::cout() << "average ratings per row: " << (double)M.nonZeros() / (double)M.cols() << std::endl;
    Sys::cout() << "num " << name << ": " << num() << std::endl;
    if (has_prop_posterior())
    {
        Sys::cout() << "with propagated posterior" << std::endl;
    }

    if (measure_perf) sample_time.resize(num(), .0);
}

class PrecomputedLLT : public Eigen::LLT<MatrixNNd>
{
  public:
    void operator=(const MatrixNNd &m) { m_matrix = m; m_isInitialized = true; m_info = Eigen::Success; }
};

void Sys::computeMuLambda(long idx, const Sys &other, VectorNd &rr, MatrixNNd &MM) const
{
    for (SparseMatrixD::InnerIterator it(M, idx); it; ++it)
    {
        auto col = other.items().col(it.row());
        MM.triangularView<Eigen::Upper>() += col * col.transpose();
        rr.noalias() += col * ((it.value() - mean_rating) * alpha);
    }
}

//
// Update ONE movie or one user
//
VectorNd Sys::sample(long idx, Sys &other)
{
    auto start = tick();

    VectorNd hp_mu;
    MatrixNNd hp_LambdaF;
    MatrixNNd hp_LambdaL;
    if (has_prop_posterior())
    {
        hp_mu = propMu.col(idx);
        hp_LambdaF = Eigen::Map<MatrixNNd>(propLambda.col(idx).data());
        hp_LambdaL =  hp_LambdaF.llt().matrixL();
    }
    else
    {
        hp_mu = hp.mu;
        hp_LambdaF = hp.LambdaF;
        hp_LambdaL = hp.LambdaL;
    }

    VectorNd rr = hp_LambdaF * hp.mu;                // vector num_latent x 1, we will use it in formula (14) from the paper
    MatrixNNd MM(MatrixNNd::Zero());
    PrecomputedLLT chol;                             // matrix num_latent x num_latent, chol="lambda_i with *" from formula (14)

#ifdef BPMF_REDUCE
    rr += precMu.col(idx);
    MM += precLambdaMatrix(idx);
#else
    computeMuLambda(idx, other, rr, MM);
#endif

    // copy upper -> lower part, matrix is symmetric.
    MM.triangularView<Eigen::Lower>() = MM.transpose();

    chol.compute(hp_LambdaF + alpha * MM);

    if(chol.info() != Eigen::Success) THROWERROR("Cholesky failed");

    // now we should calculate formula (14) from the paper
    // u_i for k-th iteration = Gaussian distribution N(u_i | mu_i with *, [lambda_i with *]^-1) =
    //                        = mu_i with * + s * [U]^-1,
    //                        where
    //                              s is a random vector with N(0, I),
    //                              mu_i with * is a vector num_latent x 1,
    //                              mu_i with * = [lambda_i with *]^-1 * rr,
    //                              lambda_i with * = L * U

    // Expression u_i = U \ (s + (L \ rr)) in Matlab looks for Eigen library like:

    chol.matrixL().solveInPlace(rr);                    // L*Y=rr => Y=L\rr, we store Y result again in rr vector
    rr += nrandn();                                     // rr=s+(L\rr), we store result again in rr vector
    chol.matrixU().solveInPlace(rr);                    // u_i=U\rr
    items().col(idx) = rr;                              // we save rr vector in items matrix (it is user features matrix)

#ifdef BPMF_REDUCE
    #pragma omp critical
    for (SparseMatrixD::InnerIterator it(M, idx); it; ++it)
    {
        other.precLambdaMatrix(it.row()).triangularView<Eigen::Upper>() += rr * rr.transpose();
        other.precMu.col(it.row()).noalias() += rr * (it.value() - mean_rating) * alpha;
    }
#endif

    auto stop = tick();
    register_time(idx, 1e6 * (stop - start));
    //Sys::cout() << "  " << count << ": " << 1e6*(stop - start) << std::endl;

    assert(rr.norm() > .0);

    return rr;
}

//
// update ALL movies / users in parallel
//
void Sys::sample(Sys &other)
{
    iter++;
    thread_vector<VectorNd>  sums(VectorNd::Zero()); // sum
    thread_vector<double>    norms(0.0); // squared norm
    thread_vector<MatrixNNd> prods(MatrixNNd::Zero()); // outer prod

#ifdef BPMF_REDUCE
    other.precMu.setZero();
    other.precLambda.setZero();
#endif

#pragma omp parallel for schedule(guided)
    for (int i = from(); i < to(); ++i)
    {
#pragma omp task
        {
            auto r = sample(i, other);

            MatrixNNd cov = (r * r.transpose());
            prods.local() += cov;
            sums.local() += r;
            norms.local() += r.squaredNorm();

            if (iter >= burnin && Sys::odirname.size())
            {
                aggrMu.col(i) += r;
                aggrLambda.col(i) += Eigen::Map<Eigen::VectorXd>(cov.data(), num_latent * num_latent);
            }

            send_item(i);
        }
    }
#pragma omp taskwait

    VectorNd sum = sums.combine();
    MatrixNNd prod = prods.combine();
    double norm = norms.combine();

    const int N = num();
    local_sum() = sum;
    local_cov() = (prod - (sum * sum.transpose() / N)) / (N-1);
    local_norm() = norm;
}

void Sys::register_time(int i, double t)
{
    if (measure_perf) sample_time.at(i) += t;
}<|endoftext|>


<commit_before>//A ROOT macro that prepares Tree for TMVA training
//----------------------------------------------
//Author: Maxwell Cui
//Date: Aug 19, 2017
//----------------------------------------------

#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>

void create(TFile* inputTree, TString outputName)
{
  TTree *oldTree=new TTree;
  inputTree->GetObject("nominal_Loose",oldTree);

  //Deactive all branches
  oldTree->SetBranchStatus("*",0);

  //Active the interested branches
  oldTree->SetBranchStatus("ht",1);
  oldTree->SetBranchStatus("met_met",1);
  oldTree->SetBranchStatus("met_sumet",1);
  oldTree->SetBranchStatus("mu_pt",1);
  oldTree->SetBranchStatus("el_pt",1);
  oldTree->SetBranchStatus("mu",1);
  oldTree->SetBranchStatus("jet_pt",1);
  oldTree->SetBranchStatus("met_phi",1);
  oldTree->SetBranchStatus("SSee",1);
  oldTree->SetBranchStatus("SSmm",1);
  oldTree->SetBranchStatus("SSem",1);
  oldTree->SetBranchStatus("eee",1);
  oldTree->SetBranchStatus("eem",1);
  oldTree->SetBranchStatus("emm",1);
  oldTree->SetBranchStatus("mmm",1);
  oldTree->SetBranchStatus("lep_pt",1);
  oldTree->SetBranchStatus("jet_mv2c10",1);   //Using MV2c10 for b-tagging
  //oldTree->SetBranchStatus("weight_mc",1);

  //====================Output file==========================
  //
  TFile *outputFile=new TFile(outputName,"recreate");
  //
  //=========================================================

  //Copy to new tree
  TTree *newTree=oldTree->CloneTree();

  //Working with b-jet
  //
  // Declaration of leaf types
  Float_t         mu;
  vector<float>   *el_pt;
  vector<float>   *mu_pt;
  vector<float>   *jet_pt;
  vector<float>   *jet_mv2c10;
  Float_t         met_met;
  Float_t         met_phi;
  Float_t         met_sumet;
  Int_t           SSee;
  Int_t           SSmm;
  Int_t           SSem;
  Int_t           eee;
  Int_t           eem;
  Int_t           emm;
  Int_t           mmm;
  vector<float>   *lep_pt;
  Float_t         ht;

  // List of branches
  TBranch        *b_mu;   //!
  TBranch        *b_el_pt;   //!
  TBranch        *b_mu_pt;   //!
  TBranch        *b_jet_pt;   //!
  TBranch        *b_jet_mv2c10;   //!
  TBranch        *b_met_met;   //!
  TBranch        *b_met_sumet;
  TBranch        *b_met_phi;   //!
  TBranch        *b_SSee;   //!
  TBranch        *b_SSmm;   //!
  TBranch        *b_SSem;   //!
  TBranch        *b_eee;   //!
  TBranch        *b_eem;   //!
  TBranch        *b_emm;   //!
  TBranch        *b_mmm;   //!
  TBranch        *b_lep_pt;   //!
  TBranch        *b_ht;   //!

  //Set object pointer
  el_pt = 0;
  mu_pt = 0;
  jet_pt = 0;
  jet_mv2c10 = 0;
  lep_pt = 0;

  //Set branch addresses and brunch pointers
  oldTree->SetBranchAddress("mu", &mu, &b_mu);
  oldTree->SetBranchAddress("el_pt", &el_pt, &b_el_pt);
  oldTree->SetBranchAddress("mu_pt", &mu_pt, &b_mu_pt);
  oldTree->SetBranchAddress("jet_pt", &jet_pt, &b_jet_pt);
  oldTree->SetBranchAddress("jet_mv2c10", &jet_mv2c10, &b_jet_mv2c10);
  oldTree->SetBranchAddress("met_met", &met_met, &b_met_met);
  oldTree->SetBranchAddress("met_phi", &met_phi, &b_met_phi);
  oldTree->SetBranchAddress("met_sumet", &met_sumet, &b_met_sumet);
  oldTree->SetBranchAddress("SSee", &SSee, &b_SSee);
  oldTree->SetBranchAddress("SSmm", &SSmm, &b_SSmm);
  oldTree->SetBranchAddress("SSem", &SSem, &b_SSem);
  oldTree->SetBranchAddress("eee", &eee, &b_eee);
  oldTree->SetBranchAddress("eem", &eem, &b_eem);
  oldTree->SetBranchAddress("emm", &emm, &b_emm);
  oldTree->SetBranchAddress("mmm", &mmm, &b_mmm);
  oldTree->SetBranchAddress("lep_pt", &lep_pt, &b_lep_pt);
  oldTree->SetBranchAddress("ht", &ht, &b_ht);

  //Declare bjet variable
  Int_t bjet;

  //Add new branch
  TBranch *bjetBranch=newTree->Branch("bjet",&bjet,"bjet/I");

  Int_t nentries=(Int_t)oldTree->GetEntries();

  //Calculate bjet, algorithm is provided by Prof. Varnes
  for(Int_t i=0;i<nentries;i++)
    {
      oldTree->GetEntry(i);
      bj=0;
      if(SSee || SSem || SSmm || eee || eem || emm || mmm)
      	{
      	  for(unsigned int ibjet=0;ibjet<jet_mv2c10->size();ibjet++)
      	    {
      	      ///     if  (jet_mv2c10->at(ibjet) > 0.1758475) {  // 85% WP
	      if  (jet_mv2c10->at(ibjet) > 0.645925) {  // 77% WP
		//if (jet_mv2c10->at(ibjet) > 0.8244273) {  // 70% WP
		bjet_n++;
	      }

      	    }
      	}
      bjetBranch->Fill();
    }

  newTree->Print();
  newTree->Write();

  outputFile->Close();

  delete oldTree;
}

void prepTree()
{

  //Variable 'envName' can be the environmental variable on the system that
  //includes the path to the directory of the data files.
  //Please make sure that the variable is exported.

  const char* envName="MCDATA";
  std::string dataPATH=std::getenv(envName);

  std::ifstream inputFile("datafiles.txt");
  std::string fileName;

  while(std::getline(inputFile,fileName))
    {
      std::cout<<"Readig file: "<<dataPATH+fileName<<std::endl;
      TFile *bg=new TFile(dataPATH+fileName);
      create(bg,fileName);
      delete bg;
    }
  inputFile.close();
}
<commit_msg>update<commit_after>//A ROOT macro that prepares Tree for TMVA training
//----------------------------------------------
//Author: Maxwell Cui
//Date: Aug 19, 2017
//----------------------------------------------

#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>

void create(TFile* inputTree, TString outputName)
{
  TTree *oldTree=new TTree;
  inputTree->GetObject("nominal_Loose",oldTree);

  //Deactive all branches
  oldTree->SetBranchStatus("*",0);

  //Active the interested branches
  oldTree->SetBranchStatus("ht",1);
  oldTree->SetBranchStatus("met_met",1);
  oldTree->SetBranchStatus("met_sumet",1);
  oldTree->SetBranchStatus("mu_pt",1);
  oldTree->SetBranchStatus("el_pt",1);
  oldTree->SetBranchStatus("mu",1);
  oldTree->SetBranchStatus("jet_pt",1);
  oldTree->SetBranchStatus("met_phi",1);
  oldTree->SetBranchStatus("SSee",1);
  oldTree->SetBranchStatus("SSmm",1);
  oldTree->SetBranchStatus("SSem",1);
  oldTree->SetBranchStatus("eee",1);
  oldTree->SetBranchStatus("eem",1);
  oldTree->SetBranchStatus("emm",1);
  oldTree->SetBranchStatus("mmm",1);
  oldTree->SetBranchStatus("lep_pt",1);
  oldTree->SetBranchStatus("jet_mv2c10",1);   //Using MV2c10 for b-tagging
  //oldTree->SetBranchStatus("weight_mc",1);

  //====================Output file==========================
  //
  TFile *outputFile=new TFile(outputName,"recreate");
  //
  //=========================================================

  //Copy to new tree
  TTree *newTree=oldTree->CloneTree();

  //Working with b-jet
  //
  // Declaration of leaf types
  Float_t         mu;
  vector<float>   *el_pt;
  vector<float>   *mu_pt;
  vector<float>   *jet_pt;
  vector<float>   *jet_mv2c10;
  Float_t         met_met;
  Float_t         met_phi;
  Float_t         met_sumet;
  Int_t           SSee;
  Int_t           SSmm;
  Int_t           SSem;
  Int_t           eee;
  Int_t           eem;
  Int_t           emm;
  Int_t           mmm;
  vector<float>   *lep_pt;
  Float_t         ht;

  // List of branches
  TBranch        *b_mu;   //!
  TBranch        *b_el_pt;   //!
  TBranch        *b_mu_pt;   //!
  TBranch        *b_jet_pt;   //!
  TBranch        *b_jet_mv2c10;   //!
  TBranch        *b_met_met;   //!
  TBranch        *b_met_sumet;
  TBranch        *b_met_phi;   //!
  TBranch        *b_SSee;   //!
  TBranch        *b_SSmm;   //!
  TBranch        *b_SSem;   //!
  TBranch        *b_eee;   //!
  TBranch        *b_eem;   //!
  TBranch        *b_emm;   //!
  TBranch        *b_mmm;   //!
  TBranch        *b_lep_pt;   //!
  TBranch        *b_ht;   //!

  //Set object pointer
  el_pt = 0;
  mu_pt = 0;
  jet_pt = 0;
  jet_mv2c10 = 0;
  lep_pt = 0;

  //Set branch addresses and brunch pointers
  oldTree->SetBranchAddress("mu", &mu, &b_mu);
  oldTree->SetBranchAddress("el_pt", &el_pt, &b_el_pt);
  oldTree->SetBranchAddress("mu_pt", &mu_pt, &b_mu_pt);
  oldTree->SetBranchAddress("jet_pt", &jet_pt, &b_jet_pt);
  oldTree->SetBranchAddress("jet_mv2c10", &jet_mv2c10, &b_jet_mv2c10);
  oldTree->SetBranchAddress("met_met", &met_met, &b_met_met);
  oldTree->SetBranchAddress("met_phi", &met_phi, &b_met_phi);
  oldTree->SetBranchAddress("met_sumet", &met_sumet, &b_met_sumet);
  oldTree->SetBranchAddress("SSee", &SSee, &b_SSee);
  oldTree->SetBranchAddress("SSmm", &SSmm, &b_SSmm);
  oldTree->SetBranchAddress("SSem", &SSem, &b_SSem);
  oldTree->SetBranchAddress("eee", &eee, &b_eee);
  oldTree->SetBranchAddress("eem", &eem, &b_eem);
  oldTree->SetBranchAddress("emm", &emm, &b_emm);
  oldTree->SetBranchAddress("mmm", &mmm, &b_mmm);
  oldTree->SetBranchAddress("lep_pt", &lep_pt, &b_lep_pt);
  oldTree->SetBranchAddress("ht", &ht, &b_ht);

  //Declare bjet variable
  Int_t bjet;

  //Add new branch
  TBranch *bjetBranch=newTree->Branch("bjet",&bjet,"bjet/I");

  Int_t nentries=(Int_t)oldTree->GetEntries();

  //Calculate bjet, algorithm is provided by Prof. Varnes
  for(Int_t i=0;i<nentries;i++)
    {
      oldTree->GetEntry(i);
      bj=0;
      if(SSee || SSem || SSmm || eee || eem || emm || mmm)
      	{
      	  for(unsigned int ibjet=0;ibjet<jet_mv2c10->size();ibjet++)
      	    {
      	      ///     if  (jet_mv2c10->at(ibjet) > 0.1758475) {  // 85% WP
	      if  (jet_mv2c10->at(ibjet) > 0.645925) {  // 77% WP
		//if (jet_mv2c10->at(ibjet) > 0.8244273) {  // 70% WP
		bjet++;
	      }

      	    }
      	}
      bjetBranch->Fill();
    }

  newTree->Print();
  newTree->Write();

  outputFile->Close();

  delete oldTree;
}

void prepTree()
{

  //Variable 'envName' can be the environmental variable on the system that
  //includes the path to the directory of the data files.
  //Please make sure that the variable is exported.

  const char* envName="MCDATA";
  std::string dataPATH=std::getenv(envName);

  std::ifstream inputFile("datafiles.txt");
  std::string fileName;

  while(std::getline(inputFile,fileName))
    {
      std::cout<<"Readig file: "<<dataPATH+fileName<<std::endl;
      TFile *bg=new TFile(dataPATH+fileName);
      create(bg,fileName);
      delete bg;
    }
  inputFile.close();
}
<|endoftext|>


<commit_before>//A ROOT macro that prepares Tree for TMVA training
//----------------------------------------------
//Author: Maxwell Cui
//Date: Aug 19, 2017
//----------------------------------------------

#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>

void create(TFile* inputTree, TString outputName)
{
  TTree *oldTree=new TTree;
  inputTree->GetObject("nominal_Loose",oldTree);

  //Deactive all branches
  oldTree->SetBranchStatus("*",0);

  //Active the interested branches
  oldTree->SetBranchStatus("ht",1);
  oldTree->SetBranchStatus("met_met",1);
  oldTree->SetBranchStatus("met_sumet",1);
  oldTree->SetBranchStatus("mu_pt",1);
  oldTree->SetBranchStatus("el_pt",1);
  oldTree->SetBranchStatus("mu",1);
  oldTree->SetBranchStatus("jet_pt",1);
  oldTree->SetBranchStatus("met_phi",1);
  oldTree->SetBranchStatus("SSee_2016",1);
  oldTree->SetBranchStatus("SSmm_2016",1);
  oldTree->SetBranchStatus("SSem_2016",1);
  oldTree->SetBranchStatus("eee_2016",1);
  oldTree->SetBranchStatus("eem_2016",1);
  oldTree->SetBranchStatus("emm_2016",1);
  oldTree->SetBranchStatus("mmm_2016",1);
  oldTree->SetBranchStatus("lep_pt",1);
  oldTree->SetBranchStatus("jet_mv2c10",1);   //Using MV2c10 for b-tagging
  //oldTree->SetBranchStatus("weight_mc",1);

  //====================Output file==========================
  //
  TFile *outputFile=new TFile(outputName,"recreate");
  //
  //=========================================================

  //Copy to new tree
  TTree *newTree=oldTree->CloneTree();

  //Working with b-jet
  //
  // Declaration of leaf types
  Float_t         mu;
  vector<float>   *el_pt;
  vector<float>   *mu_pt;
  vector<float>   *jet_pt;
  vector<float>   *jet_mv2c10;
  Float_t         met_met;
  Float_t         met_phi;
  Float_t         met_sumet;
  Int_t           SSee_2016;
  Int_t           SSmm_2016;
  Int_t           SSem_2016;
  Int_t           eee_2016;
  Int_t           eem_2016;
  Int_t           emm_2016;
  Int_t           mmm_2016;
  vector<float>   *lep_pt;
  Float_t         ht;

  // List of branches
  TBranch        *b_mu;   //!
  TBranch        *b_el_pt;   //!
  TBranch        *b_mu_pt;   //!
  TBranch        *b_jet_pt;   //!
  TBranch        *b_jet_mv2c10;   //!
  TBranch        *b_met_met;   //!
  TBranch        *b_met_sumet;
  TBranch        *b_met_phi;   //!
  TBranch        *b_SSee_2016;   //!
  TBranch        *b_SSmm_2016;   //!
  TBranch        *b_SSem_2016;   //!
  TBranch        *b_eee_2016;   //!
  TBranch        *b_eem_2016;   //!
  TBranch        *b_emm_2016;   //!
  TBranch        *b_mmm_2016;   //!
  TBranch        *b_lep_pt;   //!
  TBranch        *b_ht;   //!

  //Set object pointer
  el_pt = 0;
  mu_pt = 0;
  jet_pt = 0;
  jet_mv2c10 = 0;
  lep_pt = 0;

  //Set branch addresses and brunch pointers
  oldTree->SetBranchAddress("mu", &mu, &b_mu);
  oldTree->SetBranchAddress("el_pt", &el_pt, &b_el_pt);
  oldTree->SetBranchAddress("mu_pt", &mu_pt, &b_mu_pt);
  oldTree->SetBranchAddress("jet_pt", &jet_pt, &b_jet_pt);
  oldTree->SetBranchAddress("jet_mv2c10", &jet_mv2c10, &b_jet_mv2c10);
  oldTree->SetBranchAddress("met_met", &met_met, &b_met_met);
  oldTree->SetBranchAddress("met_phi", &met_phi, &b_met_phi);
  oldTree->SetBranchAddress("met_sumet", &met_sumet, &b_met_sumet);
  oldTree->SetBranchAddress("SSee_2016", &SSee_2016, &b_SSee_2016);
  oldTree->SetBranchAddress("SSmm_2016", &SSmm_2016, &b_SSmm_2016);
  oldTree->SetBranchAddress("SSem_2016", &SSem_2016, &b_SSem_2016);
  oldTree->SetBranchAddress("eee_2016", &eee_2016, &b_eee_2016);
  oldTree->SetBranchAddress("eem_2016", &eem_2016, &b_eem_2016);
  oldTree->SetBranchAddress("emm_2016", &emm_2016, &b_emm_2016);
  oldTree->SetBranchAddress("mmm_2016", &mmm_2016, &b_mmm_2016);
  oldTree->SetBranchAddress("lep_pt", &lep_pt, &b_lep_pt);
  oldTree->SetBranchAddress("ht", &ht, &b_ht);

  //Declare bjet variable
  Int_t bjet;

  //Add new branch
  TBranch *bjetBranch=newTree->Branch("bjet",&bjet,"bjet/I");

  Int_t nentries=(Int_t)oldTree->GetEntries();

  //Calculate bjet, algorithm is provided by Prof. Varnes
  for(Int_t i=0;i<nentries;i++)
    {
      oldTree->GetEntry(i);
      bjet=0;
      if(SSee_2016 || SSem_2016 || SSmm_2016 || eee_2016 || eem_2016 || emm_2016 || mmm_2016)
      	{
      	  for(unsigned int ibjet=0;ibjet<jet_mv2c10->size();ibjet++)
      	    {
      	      ///     if  (jet_mv2c10->at(ibjet) > 0.1758475) {  // 85% WP
	      if  (jet_mv2c10->at(ibjet) > 0.645925) {  // 77% WP
		//if (jet_mv2c10->at(ibjet) > 0.8244273) {  // 70% WP
		bjet++;
	      }

      	    }
      	}
      bjetBranch->Fill();
    }

  //Working with normalization
  //
  Float_t lumi=36.1;

  //Declare leaf and branch
  Float_t         weight_mc;
  TBranch        *b_weight_mc;

  //Set branch addresses and brunch pointers
  oldTree->SetBranchAddress("weight_mc", &weight_mc, &b_weight_mc);

  //Get the data...algrithm is from Prof. Varnes
  TH1F *lumInt=new TH1F;
  inputFile->GetObject("hIntLum",lumInt);
  Float_t mcnorm=lumInt->GetBinContent(1);

  //Declare variable for event weight
  Float_t evtWeight;
  TBranch *ewBranch=newTree->Branch("evtWeight",&evtWeight,"evtWeight/F");

  for(Int_t i=0;i<nentries;i++)
    {
      oldTree->GetEntry(i);
      evtWeight=weight_mc*lumi/mcnorm;
      ewBranch->Fill();
    }

  newTree->Print();
  newTree->Write();

  outputFile->Close();

  delete oldTree;
}

void prepTree()
{

  //Variable 'envName' can be the environmental variable on the system that
  //includes the path to the directory of the data files.
  //Please make sure that the variable is exported.

  const char* envName="MCDATA";
  std::string dataPATH=std::getenv(envName);

  std::ifstream inputFile("datafiles.txt");
  TString fileName;

  while(std::getline(inputFile,fileName))
    {
      std::cout<<"Readig file: "<<dataPATH+fileName<<std::endl;
      TString combine;
      combine=dataPATH+fileName;
      if(!combine)
	{
	  std::cout<<"No file: "<<combine<<std::endl;
	}
      else
	{
	  TFile *bg=new TFile(combine);
	  create(bg,fileName);
	  delete bg;
	}
    }
  inputFile.close();
}
<commit_msg>update<commit_after>//A ROOT macro that prepares Tree for TMVA training
//----------------------------------------------
//Author: Maxwell Cui
//Date: Aug 19, 2017
//----------------------------------------------

#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>

void create(TFile* inputTree, TString outputName)
{
  TTree *oldTree=new TTree;
  inputTree->GetObject("nominal_Loose",oldTree);

  //Deactive all branches
  oldTree->SetBranchStatus("*",0);

  //Active the interested branches
  oldTree->SetBranchStatus("ht",1);
  oldTree->SetBranchStatus("met_met",1);
  oldTree->SetBranchStatus("met_sumet",1);
  oldTree->SetBranchStatus("mu_pt",1);
  oldTree->SetBranchStatus("el_pt",1);
  oldTree->SetBranchStatus("mu",1);
  oldTree->SetBranchStatus("jet_pt",1);
  oldTree->SetBranchStatus("met_phi",1);
  oldTree->SetBranchStatus("SSee_2016",1);
  oldTree->SetBranchStatus("SSmm_2016",1);
  oldTree->SetBranchStatus("SSem_2016",1);
  oldTree->SetBranchStatus("eee_2016",1);
  oldTree->SetBranchStatus("eem_2016",1);
  oldTree->SetBranchStatus("emm_2016",1);
  oldTree->SetBranchStatus("mmm_2016",1);
  oldTree->SetBranchStatus("lep_pt",1);
  oldTree->SetBranchStatus("jet_mv2c10",1);   //Using MV2c10 for b-tagging
  oldTree->SetBranchStatus("weight_mc",1);

  //====================Output file==========================
  //
  TFile *outputFile=new TFile(outputName,"recreate");
  //
  //=========================================================

  //Copy to new tree
  TTree *newTree=oldTree->CloneTree();

  //Working with b-jet
  //
  // Declaration of leaf types
  Float_t         mu;
  vector<float>   *el_pt;
  vector<float>   *mu_pt;
  vector<float>   *jet_pt;
  vector<float>   *jet_mv2c10;
  Float_t         met_met;
  Float_t         met_phi;
  Float_t         met_sumet;
  Int_t           SSee_2016;
  Int_t           SSmm_2016;
  Int_t           SSem_2016;
  Int_t           eee_2016;
  Int_t           eem_2016;
  Int_t           emm_2016;
  Int_t           mmm_2016;
  vector<float>   *lep_pt;
  Float_t         ht;

  // List of branches
  TBranch        *b_mu;   //!
  TBranch        *b_el_pt;   //!
  TBranch        *b_mu_pt;   //!
  TBranch        *b_jet_pt;   //!
  TBranch        *b_jet_mv2c10;   //!
  TBranch        *b_met_met;   //!
  TBranch        *b_met_sumet;
  TBranch        *b_met_phi;   //!
  TBranch        *b_SSee_2016;   //!
  TBranch        *b_SSmm_2016;   //!
  TBranch        *b_SSem_2016;   //!
  TBranch        *b_eee_2016;   //!
  TBranch        *b_eem_2016;   //!
  TBranch        *b_emm_2016;   //!
  TBranch        *b_mmm_2016;   //!
  TBranch        *b_lep_pt;   //!
  TBranch        *b_ht;   //!

  //Set object pointer
  el_pt = 0;
  mu_pt = 0;
  jet_pt = 0;
  jet_mv2c10 = 0;
  lep_pt = 0;

  //Set branch addresses and brunch pointers
  oldTree->SetBranchAddress("mu", &mu, &b_mu);
  oldTree->SetBranchAddress("el_pt", &el_pt, &b_el_pt);
  oldTree->SetBranchAddress("mu_pt", &mu_pt, &b_mu_pt);
  oldTree->SetBranchAddress("jet_pt", &jet_pt, &b_jet_pt);
  oldTree->SetBranchAddress("jet_mv2c10", &jet_mv2c10, &b_jet_mv2c10);
  oldTree->SetBranchAddress("met_met", &met_met, &b_met_met);
  oldTree->SetBranchAddress("met_phi", &met_phi, &b_met_phi);
  oldTree->SetBranchAddress("met_sumet", &met_sumet, &b_met_sumet);
  oldTree->SetBranchAddress("SSee_2016", &SSee_2016, &b_SSee_2016);
  oldTree->SetBranchAddress("SSmm_2016", &SSmm_2016, &b_SSmm_2016);
  oldTree->SetBranchAddress("SSem_2016", &SSem_2016, &b_SSem_2016);
  oldTree->SetBranchAddress("eee_2016", &eee_2016, &b_eee_2016);
  oldTree->SetBranchAddress("eem_2016", &eem_2016, &b_eem_2016);
  oldTree->SetBranchAddress("emm_2016", &emm_2016, &b_emm_2016);
  oldTree->SetBranchAddress("mmm_2016", &mmm_2016, &b_mmm_2016);
  oldTree->SetBranchAddress("lep_pt", &lep_pt, &b_lep_pt);
  oldTree->SetBranchAddress("ht", &ht, &b_ht);

  //Declare bjet variable
  Int_t bjet;

  //Add new branch
  TBranch *bjetBranch=newTree->Branch("bjet",&bjet,"bjet/I");

  Int_t nentries=(Int_t)oldTree->GetEntries();

  //Calculate bjet, algorithm is provided by Prof. Varnes
  for(Int_t i=0;i<nentries;i++)
    {
      oldTree->GetEntry(i);
      bjet=0;
      if(SSee_2016 || SSem_2016 || SSmm_2016 || eee_2016 || eem_2016 || emm_2016 || mmm_2016)
      	{
      	  for(unsigned int ibjet=0;ibjet<jet_mv2c10->size();ibjet++)
      	    {
      	      ///     if  (jet_mv2c10->at(ibjet) > 0.1758475) {  // 85% WP
	      if  (jet_mv2c10->at(ibjet) > 0.645925)
		{  // 77% WP
		  //if (jet_mv2c10->at(ibjet) > 0.8244273) {  // 70% WP
		bjet++;
		}
      	    }
      	}
      bjetBranch->Fill();
    }

  //Working with normalization
  //
  Float_t lumi=36.1;

  //Declare leaf and branch
  Float_t         weight_mc;
  Float_t         weight_jvt;
  Float_t         weight_leptonSF_tightLeps;
  Float_t         weight_indiv_SF_MU_TTVA;
  Float_t         weight_pileup;
  Float_t         weight_bTagSF_77;

  TBranch        *b_weight_mc;
  TBranch        *b_weight_jvt;
  TBranch        *b_weight_leptonSF_tightLeps;
  TBranch        *b_weight_indiv_SF_MU_TTVA;
  TBranch        *b_weight_pileup;
  TBranch        *b_weight_bTagSF_77;

  //Set branch addresses and brunch pointers
  oldTree->SetBranchAddress("weight_mc", &weight_mc, &b_weight_mc);
  oldTree->SetBranchAddress("weight_jvt", &weight_jvt, &b_weight_jvt);
  oldTree->SetBranchAddress("weight_leptonSF_tightLeps", &weight_leptonSF_tightLeps, &b_weight_leptonSF_tightLeps);
  oldTree->SetBranchAddress("weight_indiv_SF_MU_TTVA", &weight_indiv_SF_MU_TTVA, &b_weight_indiv_SF_MU_TTVA);
  oldTree->SetBranchAddress("weight_pileup", &weight_pileup, &b_weight_pileup);
  oldTree->SetBranchAddress("weight_bTagSF_77", &weight_bTagSF_77, &b_weight_bTagSF_77);

  //Get the data...algrithm is from Prof. Varnes
  TH1F *lumInt=new TH1F;
  inputTree->GetObject("hIntLum",lumInt);
  Float_t mcnorm=lumInt->GetBinContent(1);

  //Declare variable for event weight
  Float_t evtWeight;
  TBranch *evtBranch=newTree->Branch("evtWeight",&evtWeight,"evtWeight/F");

  for(Int_t i=0;i<nentries;i++)
    {
      oldTree->GetEntry(i);
      evtWeight=weight_mc*weight_jvt*(weight_leptonSF_tightLeps/weight_indiv_SF_MU_TTVA)*weight_pileup*weight_bTagSF_77*lumi/mcnorm;
      evtBranch->Fill();
    }

  newTree->Print();
  newTree->Write();

  outputFile->Close();

  delete oldTree;
}

void prepTree()
{

  //Variable 'envName' can be the environmental variable on the system that
  //includes the path to the directory of the data files.
  //Please make sure that the variable is exported.

  const char* envName="MCDATA";
  std::string dataPATH=std::getenv(envName);
  if(dataPATH.empty())
    {
      std::cout<<"No such a variable";
    }
  else
    {
      std::cout<<"The variable contains: "<<dataPATH<<std::endl;
    }

  std::ifstream inputFile("datafiles.txt");
  std::string fileName;

  while(std::getline(inputFile,fileName))
    {
      TString fullInput;
      fullInput=dataPATH+"/"+fileName;
      // std::cout<<"Readig file: "<<fullInput<<std::endl;

      TString outputName;
      outputName="normalized_"+fileName;
      // std::cout<<"\tOutput file is: "<<outputName<<std::endl;

      if(TFile *bg=new TFile(fullInput))
	{
	  create(bg,outputName);
	  delete bg;
	}
      else
	{
	  continue;
	}

    }
  inputFile.close();
}
<|endoftext|>


<commit_before>#ifndef BENG_PROXY_HTTP_CACHE_INTERNAL_HXX
#define BENG_PROXY_HTTP_CACHE_INTERNAL_HXX

#include "http_cache.h"

#ifdef CACHE_LOG
#include <daemon/log.h>
#define cache_log(...) daemon_log(__VA_ARGS__)
#else
#define cache_log(...) do {} while (0)
#endif

static const off_t cacheable_size_limit = 256 * 1024;

#endif
<commit_msg>http_cache: add missing include<commit_after>#ifndef BENG_PROXY_HTTP_CACHE_INTERNAL_HXX
#define BENG_PROXY_HTTP_CACHE_INTERNAL_HXX

#include "http_cache.h"

#include <sys/types.h>

#ifdef CACHE_LOG
#include <daemon/log.h>
#define cache_log(...) daemon_log(__VA_ARGS__)
#else
#define cache_log(...) do {} while (0)
#endif

static const off_t cacheable_size_limit = 256 * 1024;

#endif
<|endoftext|>


<commit_before>/**
 * Copyright (c) 2015-2016, The Kovri I2P Router Project
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be
 *    used to endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//
// Boost.Log logging implementation
//
#include "BoostLog.h"

#include <boost/version.hpp>

#if BOOST_VERSION >= 105600
#include <boost/core/null_deleter.hpp>
#else
#include <boost/core/empty_deleter.hpp>
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/channel_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/severity_feature.hpp>

#include <memory>
#include <string>

namespace kovri {
namespace log {

std::shared_ptr<Log> g_Log = nullptr;
sink_ptr g_LogSink;

#if BOOST_VERSION >= 105600
typedef boost::null_deleter boost_deleter_t;
#else
typedef boost::empty_deleter boost_deleter_t;
#endif

LogImpl::LogImpl(
    LogLevel minlev,
    std::ostream* out) {
  m_LogCore = boost::log::core::get();
  m_LogBackend = boost::make_shared<backend_t>();
  m_LogBackend->add_stream(
      boost::shared_ptr<std::ostream>(
        out,
        boost_deleter_t()));
  g_LogSink = boost::shared_ptr<sink_t>(
      new sink_t(
        m_LogBackend));
  g_LogSink->set_filter(
      boost::log::expressions::attr<LogLevel>(
        "Severity") >= minlev);
  g_LogSink->set_formatter(&LogImpl::Format);
  m_LogCore->add_sink(g_LogSink);
  m_LogCore->add_global_attribute(
      "Timestamp",
      boost::log::attributes::local_clock());
}

Log::Log(
    LogLevel minlev,
    std::ostream * out) {
  m_LogImpl = std::make_shared<LogImpl>(minlev, out);
  m_DefaultLogger = std::make_shared<Logger>(new LoggerImpl);
}

LoggerImpl::LoggerImpl(
    const std::string& name,
    const std::string & channel)
    : log(boost::log::keywords::channel = channel),
      m_Debug(new LogStreamImpl(m_DebugMtx, log, eLogDebug)),
      m_Info(new LogStreamImpl(m_InfoMtx, log, eLogInfo)),
      m_Warn(new LogStreamImpl(m_WarnMtx, log, eLogWarning)),
      m_Error(new LogStreamImpl(m_ErrorMtx, log, eLogError)) {
  log.add_attribute(
      "LogName",
      boost::log::attributes::constant< std::string >(name));
}

LoggerImpl::LoggerImpl() : LoggerImpl("default", "default") {}

LogStreamImpl::LogStreamImpl(
    std::mutex& mtx,
    log_t& l,
    LogLevel level)
    : m_Str(new std::stringbuf),
      m_Access(mtx),
      m_Log(l),
      m_Level(level),
      m_Enable(true) {
  // m_Log.add_global_attribute("Logger", m_ParentName);
}

LogStream & Logger::Error() {
  return m_Impl->Error();
}

LogStream & Logger::Warning() {
  return m_Impl->Warning();
}

LogStream & Logger::Info() {
  return m_Impl->Info();
}

LogStream & Logger::Debug() {
  return m_Impl->Debug();
}

void Logger::Flush() {
  g_LogSink->flush();
}

Logger::Logger(LoggerImpl* impl) : m_Impl(impl) {}

Logger::~Logger() {
  delete m_Impl;
}

LogStream::LogStream(
    LogStreamImpl* impl)
    : std::ostream(impl),
      m_Impl(impl) {}

LogStream::~LogStream() { delete m_Impl; }

LogStream & LogStream::Meta(
    const std::string& key,
    std::string value) {
  // this doesn't do anything yet
  // m_Impl->MetaImpl(key, value);
  return *this;
}

LogStream & LoggerImpl::Debug() {
  return GetLogger(m_Debug, m_DebugMtx);
}

LogStream & LoggerImpl::Info() {
  return GetLogger(m_Info, m_InfoMtx);
}

LogStream & LoggerImpl::Warning() {
  return GetLogger(m_Warn, m_WarnMtx);
}

LogStream & LoggerImpl::Error() {
  return GetLogger(m_Error, m_ErrorMtx);
}

LogStream & LoggerImpl::GetLogger(
    LogStream& l,
    std::mutex & mtx) {
  mtx.lock();
  return l;
}

void LogStreamImpl::WaitForReady() {
  {
    std::lock_guard<std::mutex> lock(m_Access);
  }
}

LogStreamImpl::int_type LogStreamImpl::overflow(
    int_type ch) {
  return std::streambuf::overflow(ch);
}

int LogStreamImpl::sync() {
  int ret;
  ret = m_Str->pubsync();
  Flush();
  m_Access.unlock();
  return ret;
}

// not thread safe
std::streamsize LogStreamImpl::xsputn(
    const LogStreamImpl::char_type* s,
    std::streamsize count) {
  return m_Str->sputn(s, count);
}

LogStreamImpl::~LogStreamImpl() {
  delete m_Str;
}

// not thread safe
void LogStreamImpl::Flush() {
  BOOST_LOG_SEV(m_Log, m_Level) << m_Str;
  delete m_Str;
  m_Str = new std::stringbuf;
  g_LogSink->flush();
}

LogStream & LogStream::Flush() {
  g_LogSink->flush();
  return *this;
}

bool LogStream::IsEnabled() {
  return m_Impl->IsEnabled();
}

void LogStream::Disable() {
  m_Impl->Disable();
}

void LogStream::Enable() {
  m_Impl->Enable();
}

std::ostream& operator<<(
    std::ostream& out,
    LogLevel lev) {
  static const char* levels[] = {
    "DBG",  // debug
    "NFO",  // info
    "WRN",  // warning
    "ERR"   // error
  };
  if (static_cast<std::size_t>(lev) < sizeof(levels) / sizeof(*levels)) {
    out << levels[lev];
  } else {
    out << "?" << static_cast<int>(lev) << "?";
  }
  return out;
}

void LogImpl::Format(
    boost::log::record_view const& rec,
    boost::log::formatting_ostream &s) {
  // const boost::log::attribute_value_set& attrs = rec.attribute_values();
  static std::locale loc(
      std::clog.getloc(),
      new boost::posix_time::time_facet("%Y:%m:%d|%T.%f"));
  std::stringstream ss;
  ss.imbue(loc);
  ss << boost::log::extract<boost::posix_time::ptime>("Timestamp", rec) << ' ';
  s << ss.str();
  s << boost::log::extract<std::string>("Channel", rec) << ":";
  s << boost::log::extract<std::string>("LogName", rec) << "\t\t";
  s << boost::log::extract<LogLevel>("Severity", rec) << "\t\t";
  s << rec[boost::log::expressions::smessage];
}

void LogImpl::Flush() {
  g_LogSink->flush();
}

std::shared_ptr<Log> Log::Get() {
  // make default logger if we don't have a logger
  if (g_Log == nullptr)
    g_Log = std::make_shared<Log>(eLogDebug, &std::clog);
  return g_Log;
}

std::shared_ptr<Logger> Log::Default() {
  return m_DefaultLogger;
}

std::unique_ptr<Logger> Log::New(
    const std::string& name,
    const std::string& channel) {
  return std::unique_ptr<Logger>(
      new Logger(
        new LoggerImpl(
          name,
          channel)));
}

}  // namespace log
}  // namespace kovri
<commit_msg>try fixing #55 again with "correct" header<commit_after>/**
 * Copyright (c) 2015-2016, The Kovri I2P Router Project
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be
 *    used to endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//
// Boost.Log logging implementation
//
#include "BoostLog.h"

#include <boost/version.hpp>

#if BOOST_VERSION >= 105600
#include <boost/core/null_deleter.hpp>
#else
#include <boost/utility/empty_deleter.hpp>
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/channel_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/severity_feature.hpp>

#include <memory>
#include <string>

namespace kovri {
namespace log {

std::shared_ptr<Log> g_Log = nullptr;
sink_ptr g_LogSink;

#if BOOST_VERSION >= 105600
typedef boost::null_deleter boost_deleter_t;
#else
typedef boost::empty_deleter boost_deleter_t;
#endif

LogImpl::LogImpl(
    LogLevel minlev,
    std::ostream* out) {
  m_LogCore = boost::log::core::get();
  m_LogBackend = boost::make_shared<backend_t>();
  m_LogBackend->add_stream(
      boost::shared_ptr<std::ostream>(
        out,
        boost_deleter_t()));
  g_LogSink = boost::shared_ptr<sink_t>(
      new sink_t(
        m_LogBackend));
  g_LogSink->set_filter(
      boost::log::expressions::attr<LogLevel>(
        "Severity") >= minlev);
  g_LogSink->set_formatter(&LogImpl::Format);
  m_LogCore->add_sink(g_LogSink);
  m_LogCore->add_global_attribute(
      "Timestamp",
      boost::log::attributes::local_clock());
}

Log::Log(
    LogLevel minlev,
    std::ostream * out) {
  m_LogImpl = std::make_shared<LogImpl>(minlev, out);
  m_DefaultLogger = std::make_shared<Logger>(new LoggerImpl);
}

LoggerImpl::LoggerImpl(
    const std::string& name,
    const std::string & channel)
    : log(boost::log::keywords::channel = channel),
      m_Debug(new LogStreamImpl(m_DebugMtx, log, eLogDebug)),
      m_Info(new LogStreamImpl(m_InfoMtx, log, eLogInfo)),
      m_Warn(new LogStreamImpl(m_WarnMtx, log, eLogWarning)),
      m_Error(new LogStreamImpl(m_ErrorMtx, log, eLogError)) {
  log.add_attribute(
      "LogName",
      boost::log::attributes::constant< std::string >(name));
}

LoggerImpl::LoggerImpl() : LoggerImpl("default", "default") {}

LogStreamImpl::LogStreamImpl(
    std::mutex& mtx,
    log_t& l,
    LogLevel level)
    : m_Str(new std::stringbuf),
      m_Access(mtx),
      m_Log(l),
      m_Level(level),
      m_Enable(true) {
  // m_Log.add_global_attribute("Logger", m_ParentName);
}

LogStream & Logger::Error() {
  return m_Impl->Error();
}

LogStream & Logger::Warning() {
  return m_Impl->Warning();
}

LogStream & Logger::Info() {
  return m_Impl->Info();
}

LogStream & Logger::Debug() {
  return m_Impl->Debug();
}

void Logger::Flush() {
  g_LogSink->flush();
}

Logger::Logger(LoggerImpl* impl) : m_Impl(impl) {}

Logger::~Logger() {
  delete m_Impl;
}

LogStream::LogStream(
    LogStreamImpl* impl)
    : std::ostream(impl),
      m_Impl(impl) {}

LogStream::~LogStream() { delete m_Impl; }

LogStream & LogStream::Meta(
    const std::string& key,
    std::string value) {
  // this doesn't do anything yet
  // m_Impl->MetaImpl(key, value);
  return *this;
}

LogStream & LoggerImpl::Debug() {
  return GetLogger(m_Debug, m_DebugMtx);
}

LogStream & LoggerImpl::Info() {
  return GetLogger(m_Info, m_InfoMtx);
}

LogStream & LoggerImpl::Warning() {
  return GetLogger(m_Warn, m_WarnMtx);
}

LogStream & LoggerImpl::Error() {
  return GetLogger(m_Error, m_ErrorMtx);
}

LogStream & LoggerImpl::GetLogger(
    LogStream& l,
    std::mutex & mtx) {
  mtx.lock();
  return l;
}

void LogStreamImpl::WaitForReady() {
  {
    std::lock_guard<std::mutex> lock(m_Access);
  }
}

LogStreamImpl::int_type LogStreamImpl::overflow(
    int_type ch) {
  return std::streambuf::overflow(ch);
}

int LogStreamImpl::sync() {
  int ret;
  ret = m_Str->pubsync();
  Flush();
  m_Access.unlock();
  return ret;
}

// not thread safe
std::streamsize LogStreamImpl::xsputn(
    const LogStreamImpl::char_type* s,
    std::streamsize count) {
  return m_Str->sputn(s, count);
}

LogStreamImpl::~LogStreamImpl() {
  delete m_Str;
}

// not thread safe
void LogStreamImpl::Flush() {
  BOOST_LOG_SEV(m_Log, m_Level) << m_Str;
  delete m_Str;
  m_Str = new std::stringbuf;
  g_LogSink->flush();
}

LogStream & LogStream::Flush() {
  g_LogSink->flush();
  return *this;
}

bool LogStream::IsEnabled() {
  return m_Impl->IsEnabled();
}

void LogStream::Disable() {
  m_Impl->Disable();
}

void LogStream::Enable() {
  m_Impl->Enable();
}

std::ostream& operator<<(
    std::ostream& out,
    LogLevel lev) {
  static const char* levels[] = {
    "DBG",  // debug
    "NFO",  // info
    "WRN",  // warning
    "ERR"   // error
  };
  if (static_cast<std::size_t>(lev) < sizeof(levels) / sizeof(*levels)) {
    out << levels[lev];
  } else {
    out << "?" << static_cast<int>(lev) << "?";
  }
  return out;
}

void LogImpl::Format(
    boost::log::record_view const& rec,
    boost::log::formatting_ostream &s) {
  // const boost::log::attribute_value_set& attrs = rec.attribute_values();
  static std::locale loc(
      std::clog.getloc(),
      new boost::posix_time::time_facet("%Y:%m:%d|%T.%f"));
  std::stringstream ss;
  ss.imbue(loc);
  ss << boost::log::extract<boost::posix_time::ptime>("Timestamp", rec) << ' ';
  s << ss.str();
  s << boost::log::extract<std::string>("Channel", rec) << ":";
  s << boost::log::extract<std::string>("LogName", rec) << "\t\t";
  s << boost::log::extract<LogLevel>("Severity", rec) << "\t\t";
  s << rec[boost::log::expressions::smessage];
}

void LogImpl::Flush() {
  g_LogSink->flush();
}

std::shared_ptr<Log> Log::Get() {
  // make default logger if we don't have a logger
  if (g_Log == nullptr)
    g_Log = std::make_shared<Log>(eLogDebug, &std::clog);
  return g_Log;
}

std::shared_ptr<Logger> Log::Default() {
  return m_DefaultLogger;
}

std::unique_ptr<Logger> Log::New(
    const std::string& name,
    const std::string& channel) {
  return std::unique_ptr<Logger>(
      new Logger(
        new LoggerImpl(
          name,
          channel)));
}

}  // namespace log
}  // namespace kovri
<|endoftext|>


<commit_before>#ifndef STREAMBUFFERR_HPP
#define STREAMBUFFERR_HPP
#include <net/stream.hpp>
#include <queue>
#include <util/timer.hpp>

namespace net {
  class StreamBuffer : public net::Stream
  {
  public:
    StreamBuffer(Timers::duration_t timeout=std::chrono::microseconds(10))
      : timer({this,&StreamBuffer::congested}),congestion_timeout(timeout) {}
    using buffer_t = os::mem::buf_ptr;
    using Ready_queue  = std::deque<buffer_t>;
    virtual ~StreamBuffer() {
      timer.stop();
    }

    void on_connect(ConnectCallback cb) override {
      m_on_connect = std::move(cb);
    }

    void on_read(size_t, ReadCallback cb) override {
      m_on_read = std::move(cb);
    }
    void on_data(DataCallback cb) override {
      m_on_data = std::move(cb);
    }
    size_t next_size() override;

    buffer_t read_next() override;

    void on_close(CloseCallback cb) override {
      m_on_close = std::move(cb);
    }
    void on_write(WriteCallback cb) override {
      m_on_write = std::move(cb);
    }

    void signal_data();

    bool read_congested() const noexcept
    { return m_read_congested; }

    bool write_congested() const noexcept
    { return m_write_congested; }

    /**
     * @brief Construct a shared read vector used by streams
     * If allocation failed congestion flag is set
     *
     * @param construction parameters
     *
     * @return nullptr on failure, shared_ptr to buffer on success
     */
    template <typename... Args>
    buffer_t construct_read_buffer(Args&&... args)
    {
      return construct_buffer_with_flag(m_read_congested,std::forward<Args> (args)...);
    }

    /**
     * @brief Construct a shared write vector used by streams
     * If allocation failed congestion flag is set
     *
     * @param construction parameters
     *
     * @return nullptr on failure, shared_ptr to buffer on success
     */
    template <typename... Args>
    buffer_t construct_write_buffer(Args&&... args)
    {
      return construct_buffer_with_flag(m_write_congested,std::forward<Args> (args)...);
    }

    virtual void handle_read_congestion() = 0;
    virtual void handle_write_congestion() = 0;
  protected:
    void closed()
    { if (m_on_close) m_on_close(); }
    void connected()
    { if (m_on_connect) m_on_connect(*this); }
    void stream_on_write(int n)
    { if (m_on_write) m_on_write(n); }
    void enqueue_data(buffer_t data)
    { m_send_buffers.push_back(data); }

    void congested();

    CloseCallback getCloseCallback() { return std::move(this->m_on_close); }

    void reset_callbacks() override
    {
      //remove queue and reset congestion flags and busy flag ??
      this->m_on_close = nullptr;
      this->m_on_connect = nullptr;
      this->m_on_read  = nullptr;
      this->m_on_write = nullptr;
      this->m_on_data = nullptr;
    }
    Timer timer;

  private:
    Timer::duration_t congestion_timeout;
    bool  m_write_congested= false;
    bool  m_read_congested = false;

    ConnectCallback  m_on_connect = nullptr;
    ReadCallback     m_on_read    = nullptr;
    DataCallback     m_on_data    = nullptr;
    WriteCallback    m_on_write   = nullptr;
    CloseCallback    m_on_close   = nullptr;
    Ready_queue      m_send_buffers;

    /**
     * @brief Construct a shared vector and set congestion flag if allocation fails
     *
     * @param flag the flag to set true or false on allocation failure
     * @param args arguments to constructing the buffer
     * @return nullptr on failure , shared pointer to buffer on success
     */

    template <typename... Args>
    buffer_t construct_buffer_with_flag(bool &flag,Args&&... args)
    {
      static buffer_t buffer;
      try
      {
        buffer = std::make_shared<os::mem::buffer>(std::forward<Args> (args)...);
        flag = false;
      }
      catch (std::bad_alloc &e)
      {
        flag = true;
        timer.start(congestion_timeout);
        return nullptr;
      }
      return buffer;
    }

  }; // < class StreamBuffer

  inline size_t StreamBuffer::next_size()
  {
    if (not m_send_buffers.empty()) {
      return m_send_buffers.front()->size();
    }
    return 0;
  }

  inline StreamBuffer::buffer_t StreamBuffer::read_next()
  {

    if (not m_send_buffers.empty()) {
      auto buf = m_send_buffers.front();
      m_send_buffers.pop_front();
      return buf;
    }
    return nullptr;
  }

  inline void StreamBuffer::congested()
  {
    if (m_read_congested)
    {
      handle_read_congestion();
    }
    if (m_write_congested)
    {
      handle_write_congestion();
    }
    //if any of the congestion states are still active make sure the timer is running
    if(m_read_congested or m_write_congested)
    {
      if (!timer.is_running())
      {
        timer.start(congestion_timeout);
      }
    }
    else
    {
      if (timer.is_running())
      {
        timer.stop();
      }
    }
  }

  inline void StreamBuffer::signal_data()
  {
    if (not m_send_buffers.empty())
    {
      if (m_on_data != nullptr){
        //on_data_callback();
        m_on_data();
        if (not m_send_buffers.empty()) {
          m_read_congested=true;
          timer.start(congestion_timeout);
        }
      }
      else if (m_on_read != nullptr)
      {
        for (auto buf : m_send_buffers) {
          // Pop each time, in case callback leads to another call here.
          m_send_buffers.pop_front();
          m_on_read(buf);
          if (m_on_read == nullptr) { break; } //if calling m_on_read reset the callbacks exit
        }
      }
    }
  }
} // namespace net
#endif // STREAMBUFFERR_HPP
<commit_msg>net: removed unicode char in stream buffer code<commit_after>#ifndef STREAMBUFFERR_HPP
#define STREAMBUFFERR_HPP
#include <net/stream.hpp>
#include <queue>
#include <util/timer.hpp>

namespace net {
  class StreamBuffer : public net::Stream
  {
  public:
    StreamBuffer(Timers::duration_t timeout=std::chrono::microseconds(10))
      : timer({this,&StreamBuffer::congested}),congestion_timeout(timeout) {}
    using buffer_t = os::mem::buf_ptr;
    using Ready_queue  = std::deque<buffer_t>;
    virtual ~StreamBuffer() {
      timer.stop();
    }

    void on_connect(ConnectCallback cb) override {
      m_on_connect = std::move(cb);
    }

    void on_read(size_t, ReadCallback cb) override {
      m_on_read = std::move(cb);
    }
    void on_data(DataCallback cb) override {
      m_on_data = std::move(cb);
    }
    size_t next_size() override;

    buffer_t read_next() override;

    void on_close(CloseCallback cb) override {
      m_on_close = std::move(cb);
    }
    void on_write(WriteCallback cb) override {
      m_on_write = std::move(cb);
    }

    void signal_data();

    bool read_congested() const noexcept
    { return m_read_congested; }

    bool write_congested() const noexcept
    { return m_write_congested; }

    /**
     * @brief Construct a shared read vector used by streams
     * If allocation failed congestion flag is set
     *
     * @param construction parameters
     *
     * @return nullptr on failure, shared_ptr to buffer on success
     */
    template <typename... Args>
    buffer_t construct_read_buffer(Args&&... args)
    {
      return construct_buffer_with_flag(m_read_congested,std::forward<Args> (args)...);
    }

    /**
     * @brief Construct a shared write vector used by streams
     * If allocation failed congestion flag is set
     *
     * @param construction parameters
     *
     * @return nullptr on failure, shared_ptr to buffer on success
     */
    template <typename... Args>
    buffer_t construct_write_buffer(Args&&... args)
    {
      return construct_buffer_with_flag(m_write_congested,std::forward<Args> (args)...);
    }

    virtual void handle_read_congestion() = 0;
    virtual void handle_write_congestion() = 0;
  protected:
    void closed()
    { if (m_on_close) m_on_close(); }
    void connected()
    { if (m_on_connect) m_on_connect(*this); }
    void stream_on_write(int n)
    { if (m_on_write) m_on_write(n); }
    void enqueue_data(buffer_t data)
    { m_send_buffers.push_back(data); }

    void congested();

    CloseCallback getCloseCallback() { return std::move(this->m_on_close); }

    void reset_callbacks() override
    {
      //remove queue and reset congestion flags and busy flag ??
      this->m_on_close = nullptr;
      this->m_on_connect = nullptr;
      this->m_on_read  = nullptr;
      this->m_on_write = nullptr;
      this->m_on_data = nullptr;
    }
    Timer timer;

  private:
    Timer::duration_t congestion_timeout;
    bool  m_write_congested= false;
    bool  m_read_congested = false;

    ConnectCallback  m_on_connect = nullptr;
    ReadCallback     m_on_read    = nullptr;
    DataCallback     m_on_data    = nullptr;
    WriteCallback    m_on_write   = nullptr;
    CloseCallback    m_on_close   = nullptr;
    Ready_queue      m_send_buffers;

    /**
     * @brief Construct a shared vector and set congestion flag if allocation fails
     *
     * @param flag the flag to set true or false on allocation failure
     * @param args arguments to constructing the buffer
     * @return nullptr on failure , shared pointer to buffer on success
     */

    template <typename... Args>
    buffer_t construct_buffer_with_flag(bool &flag,Args&&... args)
    {
      static buffer_t buffer;
      try
      {
        buffer = std::make_shared<os::mem::buffer>(std::forward<Args> (args)...);
        flag = false;
      }
      catch (std::bad_alloc &e)
      {
        flag = true;
        timer.start(congestion_timeout);
        return nullptr;
      }
      return buffer;
    }

  }; // < class StreamBuffer

  inline size_t StreamBuffer::next_size()
  {
    if (not m_send_buffers.empty()) {
      return m_send_buffers.front()->size();
    }
    return 0;
  }

  inline StreamBuffer::buffer_t StreamBuffer::read_next()
  {

    if (not m_send_buffers.empty()) {
      auto buf = m_send_buffers.front();
      m_send_buffers.pop_front();
      return buf;
    }
    return nullptr;
  }

  inline void StreamBuffer::congested()
  {
    if (m_read_congested)
    {
      handle_read_congestion();
    }
    if (m_write_congested)
    {
      handle_write_congestion();
    }
    //if any of the congestion states are still active make sure the timer is running
    if(m_read_congested or m_write_congested)
    {
      if (!timer.is_running())
      {
        timer.start(congestion_timeout);
      }
    }
    else
    {
      if (timer.is_running())
      {
        timer.stop();
      }
    }
  }

  inline void StreamBuffer::signal_data()
  {
    if (not m_send_buffers.empty())
    {
      if (m_on_data != nullptr){
        //on_data_callback();
        m_on_data();
        if (not m_send_buffers.empty()) {
          m_read_congested=true;
          timer.start(congestion_timeout);
        }
      }
      else if (m_on_read != nullptr)
      {
        for (auto buf : m_send_buffers) {
          // Pop each time, in case callback leads to another call here.
          m_send_buffers.pop_front();
          m_on_read(buf);
          if (m_on_read == nullptr) {
            break;
          } //if calling m_on_read reset the callbacks exit
        }
      }
    }
  }
} // namespace net
#endif // STREAMBUFFERR_HPP
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: recently_used_file.cxx,v $
 *
 *  $Revision: 1.6 $
 *
 *  last change: $Author: obo $ $Date: 2006-09-17 01:42:27 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_shell.hxx"

#ifndef INCLUDED_RECENTLY_USED_FILE
#include "recently_used_file.hxx"
#endif

#ifndef _RTL_USTRING_HXX_
#include <rtl/ustring.hxx>
#endif

#ifndef _OSL_PROCESS_H_
#include <osl/process.h>
#endif

#ifndef _OSL_SECURITY_HXX_
#include <osl/security.hxx>
#endif

#ifndef _OSL_THREAD_H_
#include <osl/thread.h>
#endif

#ifndef _OSL_FILE_HXX_
#include <osl/file.hxx>
#endif

#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <unistd.h>

const rtl::OUString RECENTLY_USED_FILE_NAME = rtl::OUString::createFromAscii(".recently-used");
const rtl::OUString SLASH = rtl::OUString::createFromAscii("/");

namespace /* private */ {

inline void ensure_final_slash(/*inout*/ rtl::OUString& path)
{
    if ((path.getLength() > 0) &&
        (SLASH.pData->buffer[0] != path.pData->buffer[path.getLength() - 1]))
        path += SLASH;
}

} // namespace private

//------------------------------------------------
recently_used_file::recently_used_file() :
    file_(NULL)
{
    osl::Security sec;
    rtl::OUString homedir_url;

    if (sec.getHomeDir(homedir_url))
    {
        rtl::OUString homedir;
        osl::FileBase::getSystemPathFromFileURL(homedir_url, homedir);

        rtl::OUString rufn = homedir;
        ensure_final_slash(rufn);
        rufn += RECENTLY_USED_FILE_NAME;

        rtl::OString tmp =
            rtl::OUStringToOString(rufn, osl_getThreadTextEncoding());

        file_ = fopen(tmp.getStr(), "r+");

        /* create if not exist */
        if (NULL == file_) {
            mode_t umask_ = umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
            file_ = fopen(tmp.getStr(), "w+");
            umask(umask_);
        }

        if (NULL == file_)
            throw "I/O error opening ~/.recently-used";

#if defined(MACOSX) && (BUILD_OS_MINOR == 2)
    if (flock(fileno(file_),(LOCK_EX | LOCK_NB)) != 0)
#else
        if (lockf(fileno(file_), F_LOCK, 0) != 0)
#endif
        {
            fclose(file_);
            throw "Cannot lock ~/.recently-used";
        }
    }
    else
        throw "Cannot determine user home directory";
}

//------------------------------------------------
recently_used_file::~recently_used_file()
{
#if defined(MACOSX) && (BUILD_OS_MINOR == 2)
        flock(fileno(file_),LOCK_UN);
#else
    lockf(fileno(file_), F_ULOCK, 0);
#endif
    fclose(file_);
}

//------------------------------------------------
void recently_used_file::reset() const
{
    rewind(file_);
}

//------------------------------------------------
void recently_used_file::flush()
{
    fflush(file_);
}

//------------------------------------------------
void recently_used_file::truncate(off_t length)
{
    ftruncate(fileno(file_), length);
}

//------------------------------------------------
size_t recently_used_file::read(char* buffer, size_t size) const
{
    size_t  r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_);

    if ((r < size) && ferror(file_))
        throw "I/O error: read failed";

    return r;
}

//----------------------------
void recently_used_file::write(const char* buffer, size_t size) const
{
    if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_))
        throw "I/O error: write failed";
}

//----------------------------
bool recently_used_file::eof() const
{
    return feof(file_);
}

<commit_msg>INTEGRATION: CWS changefileheader (1.6.122); FILE MERGED 2008/04/01 15:40:21 thb 1.6.122.2: #i85898# Stripping all external header guards 2008/03/31 13:17:11 rt 1.6.122.1: #i87441# Change license header to LPGL v3.<commit_after>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: recently_used_file.cxx,v $
 * $Revision: 1.7 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_shell.hxx"

#ifndef INCLUDED_RECENTLY_USED_FILE
#include "recently_used_file.hxx"
#endif
#include <rtl/ustring.hxx>
#include <osl/process.h>
#include <osl/security.hxx>
#include <osl/thread.h>
#include <osl/file.hxx>

#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <unistd.h>

const rtl::OUString RECENTLY_USED_FILE_NAME = rtl::OUString::createFromAscii(".recently-used");
const rtl::OUString SLASH = rtl::OUString::createFromAscii("/");

namespace /* private */ {

inline void ensure_final_slash(/*inout*/ rtl::OUString& path)
{
    if ((path.getLength() > 0) &&
        (SLASH.pData->buffer[0] != path.pData->buffer[path.getLength() - 1]))
        path += SLASH;
}

} // namespace private

//------------------------------------------------
recently_used_file::recently_used_file() :
    file_(NULL)
{
    osl::Security sec;
    rtl::OUString homedir_url;

    if (sec.getHomeDir(homedir_url))
    {
        rtl::OUString homedir;
        osl::FileBase::getSystemPathFromFileURL(homedir_url, homedir);

        rtl::OUString rufn = homedir;
        ensure_final_slash(rufn);
        rufn += RECENTLY_USED_FILE_NAME;

        rtl::OString tmp =
            rtl::OUStringToOString(rufn, osl_getThreadTextEncoding());

        file_ = fopen(tmp.getStr(), "r+");

        /* create if not exist */
        if (NULL == file_) {
            mode_t umask_ = umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
            file_ = fopen(tmp.getStr(), "w+");
            umask(umask_);
        }

        if (NULL == file_)
            throw "I/O error opening ~/.recently-used";

#if defined(MACOSX) && (BUILD_OS_MINOR == 2)
    if (flock(fileno(file_),(LOCK_EX | LOCK_NB)) != 0)
#else
        if (lockf(fileno(file_), F_LOCK, 0) != 0)
#endif
        {
            fclose(file_);
            throw "Cannot lock ~/.recently-used";
        }
    }
    else
        throw "Cannot determine user home directory";
}

//------------------------------------------------
recently_used_file::~recently_used_file()
{
#if defined(MACOSX) && (BUILD_OS_MINOR == 2)
        flock(fileno(file_),LOCK_UN);
#else
    lockf(fileno(file_), F_ULOCK, 0);
#endif
    fclose(file_);
}

//------------------------------------------------
void recently_used_file::reset() const
{
    rewind(file_);
}

//------------------------------------------------
void recently_used_file::flush()
{
    fflush(file_);
}

//------------------------------------------------
void recently_used_file::truncate(off_t length)
{
    ftruncate(fileno(file_), length);
}

//------------------------------------------------
size_t recently_used_file::read(char* buffer, size_t size) const
{
    size_t  r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_);

    if ((r < size) && ferror(file_))
        throw "I/O error: read failed";

    return r;
}

//----------------------------
void recently_used_file::write(const char* buffer, size_t size) const
{
    if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_))
        throw "I/O error: write failed";
}

//----------------------------
bool recently_used_file::eof() const
{
    return feof(file_);
}

<|endoftext|>