TEST (CorrespondenceEstimation, CorrespondenceEstimationSetSearchMethod)
{
  // Generating 3 random clouds
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1 (new pcl::PointCloud<pcl::PointXYZ> ());
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2 (new pcl::PointCloud<pcl::PointXYZ> ());
  for ( size_t i = 0; i < 50; i++ )
  {
    cloud1->points.emplace_back(float (rand()), float (rand()), float (rand()));
    cloud2->points.emplace_back(float (rand()), float (rand()), float (rand()));
  }
  // Build a KdTree for each
  pcl::search::KdTree<pcl::PointXYZ>::Ptr tree1 (new pcl::search::KdTree<pcl::PointXYZ> ());
  tree1->setInputCloud (cloud1);
  pcl::search::KdTree<pcl::PointXYZ>::Ptr tree2 (new pcl::search::KdTree<pcl::PointXYZ> ());
  tree2->setInputCloud (cloud2);
  // Compute correspondences
  pcl::registration::CorrespondenceEstimation<pcl::PointXYZ, pcl::PointXYZ, double> ce;
  ce.setInputSource (cloud1);
  ce.setInputTarget (cloud2);
  pcl::Correspondences corr_orig;
  ce.determineCorrespondences(corr_orig);
  // Now set the kd trees
  ce.setSearchMethodSource (tree1, true);
  ce.setSearchMethodTarget (tree2, true);
  pcl::Correspondences corr_cached;
  ce.determineCorrespondences (corr_cached);
  // Ensure they're the same
  EXPECT_EQ(corr_orig.size(), corr_cached.size());
  for(size_t i = 0; i < corr_orig.size(); i++)
  {
    EXPECT_EQ(corr_orig[i].index_query, corr_cached[i].index_query);
    EXPECT_EQ(corr_orig[i].index_match, corr_cached[i].index_match);
  }
  
}
示例#2
0
文件: qorwns1991.cpp 项目: 2Choi/cpp
int main()
{
    int n,i;
    char x,y,z;
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("\n%c %c %c", &x,&y,&z);

        if(y!='.')
        {
            a[x-64][0]=y-64;
        }
        else
        {
            a[x-64][0]=0;
        }
        if(z!='.')
        {
            a[x-64][1]=z-64;
        }
        else
        {
            a[x-64][1]=0;
        }
    }

    tree1(1);
    printf("\n");
    tree2(1);
    printf("\n");
    tree3(1);
    printf("\n");
}
示例#3
0
文件: qorwns1991.cpp 项目: 2Choi/cpp
void tree1(int index)
{

    printf("%c", index+64);

    if(a[index][0]!=0)
    {
        tree1( a[index][0] );
    }



    if(a[index][1]!=0)
    {
        tree1( a[index][1] );
    }
}
示例#4
0
文件: BFS2.cpp 项目: gnishida/Morph
/**
 * 指定した頂点配下を比べて、アンバランス度を計算する。
 */
int BFS2::computeUnbalanceness(RoadGraph* roads1,  RoadVertexDesc node1, RoadGraph* roads2,  RoadVertexDesc node2) {
	int score = 0;

	// 木構造を作成する
	BFSTree tree1(roads1, node1);
	BFSTree tree2(roads2, node2);

	std::list<RoadVertexDesc> seeds1;
	seeds1.push_back(node1);
	std::list<RoadVertexDesc> seeds2;
	seeds2.push_back(node2);

	while (!seeds1.empty()) {
		RoadVertexDesc parent1 = seeds1.front();
		seeds1.pop_front();
		RoadVertexDesc parent2 = seeds2.front();
		seeds2.pop_front();

		// 子リストを取得
		std::vector<RoadVertexDesc> children1 = tree1.getChildren(parent1);
		std::vector<RoadVertexDesc> children2 = tree2.getChildren(parent2);

		// どちらのノードにも、子ノードがない場合は、スキップ
		if (children1.size() == 0 && children2.size() == 0) continue;

		QMap<RoadVertexDesc, bool> paired1;
		QMap<RoadVertexDesc, bool> paired2;

		while (true) {
			RoadVertexDesc child1, child2;
			if (!findBestPairByDirection(roads1, parent1, &tree1, paired1, roads2, parent2, &tree2, paired2, true, child1, child2)) break;
			
			paired1[child1] = true;
			paired2[child2] = true;
			seeds1.push_back(child1);
			seeds2.push_back(child2);
		}

		// ペアにならなかったノードについて、ペナルティをカウントする
		for (int i = 0; i < children1.size(); i++) {
			if (paired1.contains(children1[i])) continue;
			if (!roads1->graph[children1[i]]->valid) continue;

			score += tree1.getTreeSize(children1[i]);
		}
		for (int i = 0; i < children2.size(); i++) {
			if (paired2.contains(children2[i])) continue;
			if (!roads2->graph[children2[i]]->valid) continue;

			score += tree2.getTreeSize(children2[i]);
		}
	}

	return score;
}
    boost::shared_ptr<Lattice>
    TwoFactorModel::tree(const TimeGrid& grid,
						 const boost::shared_ptr<AdditionalResultCalculator>& additionalResultCalculator) const {

        boost::shared_ptr<ShortRateDynamics> dyn = dynamics();

        boost::shared_ptr<TrinomialTree> tree1(
                                    new TrinomialTree(dyn->xProcess(), grid));
        boost::shared_ptr<TrinomialTree> tree2(
                                    new TrinomialTree(dyn->yProcess(), grid));

        return boost::shared_ptr<Lattice>(
                        new TwoFactorModel::ShortRateTree(tree1, tree2, dyn));
    }
示例#6
0
文件: tree.c 项目: dancrossnyc/harvey
tree*
simplemung(tree *t)
{
	tree *u;
	struct io *s;

	t = tree1(SIMPLE, t);
	s = openstr();
	pfmt(s, "%t", t);
	t->str = strdup((char *)s->strp);
	closeio(s);
	for(u = t->child[0];u->type==ARGLIST;u = u->child[0]){
		if(u->child[1]->type==DUP
		|| u->child[1]->type==REDIR){
			u->child[1]->child[1] = t;
			t = u->child[1];
			u->child[1] = 0;
		}
	}
	return t;
}
示例#7
0
文件: benchmarks.cpp 项目: FMX/CGAL
void Scene::bench_construction()
{
    if(m_pPolyhedron == NULL)
    {
        std::cout << "Load polyhedron first." << std::endl;
        return;
    }

    std::cout << std::endl << "Benchmark construction" << std::endl;
    std::cout << "#Facets    alone (s)   with KD-tree (s)" << std::endl;

    while(m_pPolyhedron->size_of_facets() < 1000000)
    {
        // refines mesh at increasing speed
        Refiner<Kernel,Polyhedron> refiner(m_pPolyhedron);
        std::size_t digits = nb_digits(m_pPolyhedron->size_of_facets());
        unsigned int nb_splits =
          static_cast<unsigned int>(0.2 * std::pow(10.0,(double)digits - 1.0));
        refiner.run_nb_splits(nb_splits);

        // constructs tree
        CGAL::Timer time1;
        time1.start();
        Facet_tree tree1(m_pPolyhedron->facets_begin(),m_pPolyhedron->facets_end());
        double duration_construction_alone = time1.time();

        CGAL::Timer time2;
        time2.start();
        Facet_tree tree2(m_pPolyhedron->facets_begin(),m_pPolyhedron->facets_end());
        tree2.accelerate_distance_queries();
        double duration_construction_and_kdtree = time2.time();

        std::cout << m_pPolyhedron->size_of_facets() << "\t" 
            << duration_construction_alone     << "\t" 
            << duration_construction_and_kdtree << std::endl;
    }
}
示例#8
0
文件: AnnMatcher.cpp 项目: DO-CV/sara
  //! Compute candidate matches using the Euclidean distance.
  vector<Match> AnnMatcher::compute_matches()
  {
    Timer t;

    flann::KDTreeIndexParams params{ 8 };
    flann::Matrix<float> data1, data2;
    data1 = create_flann_matrix(_keys1.descriptors);
    data2 = create_flann_matrix(_keys2.descriptors);

    flann::Index<flann::L2<float> > tree1(data1, params);
    flann::Index<flann::L2<float> > tree2(data2, params);
    tree1.buildIndex();
    tree2.buildIndex();
    cout << "Built trees in " << t.elapsed() << " seconds." << endl;

    vector<Match> matches;
    matches.reserve(1e5);

    t.restart();
    for (auto i1 = 0u; i1 < _keys1.size(); ++i1)
      append_nearest_neighbors(
        i1, _keys1, _keys2, matches, data2, tree2,
        _squared_ratio_thres, Match::Direction::SourceToTarget,
        _self_matching, _is_too_close, _vec_indices, _vec_dists,
        _max_neighbors);

    for (auto i2 = 0u; i2 < _keys2.size(); ++i2)
      append_nearest_neighbors(
        i2, _keys2, _keys1, matches, data1, tree1,
        _squared_ratio_thres, Match::Direction::TargetToSource,
        _self_matching, _is_too_close, _vec_indices, _vec_dists,
        _max_neighbors);

    // Lexicographical comparison between matches.
    auto compare_match = [](const Match& m1, const Match& m2)
    {
      if (m1.x_index() < m2.x_index())
        return true;
      if (m1.x_index() == m2.x_index() && m1.y_index() < m2.y_index())
        return true;
      if (m1.x_index() == m2.x_index() && m1.y_index() == m2.y_index() &&
          m1.score() < m2.score())
        return true;
      return false;
    };
    sort(matches.begin(), matches.end(), compare_match);

    // Remove redundant matches in each consecutive group of identical matches.
    // We keep the one with the best Lowe score.
    matches.resize(unique(matches.begin(), matches.end()) - matches.begin());

    sort(matches.begin(), matches.end(),
      [&](const Match& m1, const Match& m2) {
        return m1.score() < m2.score();
      }
    );

    cout << "Computed " << matches.size() << " matches in " << t.elapsed() << " seconds." << endl;

    return matches;
  }
示例#9
0
int main(int argc, char** argv) {
  MPI_Init(&argc, &argv);
  MPI_Comm comm = MPI_COMM_WORLD;
  int np;
  MPI_Comm_size(comm, &np);
  int myrank;
  MPI_Comm_rank(comm, &myrank);

  parse_command_line_options(argc, argv);

  int test =
      strtoul(commandline_option(
                  argc, argv, "-test", "1", false,
                  "-test <int> = (1)    : 1) Gaussian profile 2) Zalesak disk"),
              NULL, 10);

  {
    tbslas::SimConfig* sim_config = tbslas::SimConfigSingleton::Instance();
    tbslas::new_nodes<Tree_t::Real_t>(sim_config->tree_chebyshev_order, 3);

    pvfmm::Profile::Enable(sim_config->profile);
    // =========================================================================
    // PRINT METADATA
    // =========================================================================
    if (!myrank) {
      MetaData_t::Print();
    }

    // =========================================================================
    // TEST CASE
    // =========================================================================
    double data_dof = 0;
    pvfmm::BoundaryType bc;
    switch (test) {
      case 1:

        // fn_2 = tbslas::get_linear_field_y<double,3>;
        // data_dof = 1;

        // fn_2 = get_gaussian_field_atT<double,3>;
        // data_dof = 1;

        // fn_2 = get_vorticity_field_tv_wrapper<double>;
        // data_dof = 3;

        fn_2 = get_taylor_green_field_tv_ns_wrapper<double>;
        data_dof = 3;

        // fn_2 = tbslas::get_vorticity_field<double,3>;
        // data_dof = 3;

        // fn_2 = get_hopf_field_wrapper<double>;
        // data_dof = 3;

        // fn_2 = get_taylor_green_field_tv_wrapper<double>;
        // data_dof = 3;

        bc = pvfmm::Periodic;
        // bc = pvfmm::FreeSpace;

        break;
    }

    // =========================================================================
    // SIMULATION PARAMETERS
    // =========================================================================
    sim_config->vtk_filename_variable = "conc";
    sim_config->bc = bc;

    double DT = sim_config->dt;
    // double DT = 0.3925;
    // double DT = 1;

    double cheb_deg = sim_config->tree_chebyshev_order;

    std::vector<double> tree_set_times;
    std::vector<Tree_t*> tree_set_elems;

    // =========================================================================
    // INIT THE TREE 1
    // =========================================================================
    tcurr = 0;
    Tree_t tree1(comm);
    tbslas::ConstructTree<Tree_t>(
        sim_config->tree_num_point_sources,
        sim_config->tree_num_points_per_octanct,
        sim_config->tree_chebyshev_order, sim_config->tree_max_depth,
        sim_config->tree_adap, sim_config->tree_tolerance, comm, fn_2, data_dof,
        tree1);
    if (sim_config->vtk_save_rate) {
      tree1.Write2File(tbslas::GetVTKFileName(1, "tree").c_str(),
                       sim_config->vtk_order);
    }
    tree_set_times.push_back(tcurr);
    tree_set_elems.push_back(&tree1);

    // =========================================================================
    // INIT THE TREE 2
    // =========================================================================
    tcurr += DT;
    Tree_t tree2(comm);
    tbslas::ConstructTree<Tree_t>(
        sim_config->tree_num_point_sources,
        sim_config->tree_num_points_per_octanct,
        sim_config->tree_chebyshev_order, sim_config->tree_max_depth,
        sim_config->tree_adap, sim_config->tree_tolerance, comm, fn_2, data_dof,
        tree2);

    if (sim_config->vtk_save_rate) {
      tree2.Write2File(tbslas::GetVTKFileName(2, "tree").c_str(),
                       sim_config->vtk_order);
    }

    tree_set_times.push_back(tcurr);
    tree_set_elems.push_back(&tree2);

    // =========================================================================
    // INIT THE TREE 3
    // =========================================================================
    tcurr += DT;
    Tree_t tree3(comm);
    tbslas::ConstructTree<Tree_t>(
        sim_config->tree_num_point_sources,
        sim_config->tree_num_points_per_octanct,
        sim_config->tree_chebyshev_order, sim_config->tree_max_depth,
        sim_config->tree_adap, sim_config->tree_tolerance, comm, fn_2, data_dof,
        tree3);

    if (sim_config->vtk_save_rate) {
      tree3.Write2File(tbslas::GetVTKFileName(3, "tree").c_str(),
                       sim_config->vtk_order);
    }

    tree_set_times.push_back(tcurr);
    tree_set_elems.push_back(&tree3);

    // =========================================================================
    // INIT THE TREE 4
    // =========================================================================
    tcurr += DT;
    Tree_t tree4(comm);
    tbslas::ConstructTree<Tree_t>(
        sim_config->tree_num_point_sources,
        sim_config->tree_num_points_per_octanct,
        sim_config->tree_chebyshev_order, sim_config->tree_max_depth,
        sim_config->tree_adap, sim_config->tree_tolerance, comm, fn_2, data_dof,
        tree4);
    if (sim_config->vtk_save_rate) {
      tree4.Write2File(tbslas::GetVTKFileName(4, "tree").c_str(),
                       sim_config->vtk_order);
    }

    tree_set_times.push_back(tcurr);
    tree_set_elems.push_back(&tree4);

    // =========================================================================
    // MERGE
    // =========================================================================
    tcurr = 1.5 * DT;
    Tree_t merged_tree(comm);
    tbslas::ConstructTree<Tree_t>(
        sim_config->tree_num_point_sources,
        sim_config->tree_num_points_per_octanct,
        sim_config->tree_chebyshev_order, sim_config->tree_max_depth,
        sim_config->tree_adap, sim_config->tree_tolerance, comm, fn_2, data_dof,
        merged_tree);

    double in_al2, in_rl2, in_ali, in_rli;
    CheckChebOutput<Tree_t>(&merged_tree, fn_2, data_dof, in_al2, in_rl2,
                            in_ali, in_rli, std::string("Input"));

    // =========================================================================
    // COLLECT THE MERGED TREE POINTS
    // =========================================================================
    std::vector<double> merged_tree_points_pos;
    int num_leaf =
        tbslas::CollectChebTreeGridPoints(merged_tree, merged_tree_points_pos);

    // =========================================================================
    // CONSTRUCT THE FUNCTOR
    // =========================================================================
    tbslas::FieldSetFunctor<double, Tree_t> tree_set_functor(tree_set_elems,
                                                             tree_set_times);
    // tbslas::NodeFieldFunctor<double,Tree_t> tree_functor(&merged_tree);

    // int num_points = 1;
    // std::vector<double> xtmp(num_points*3);
    // std::vector<double> vtmp(num_points*data_dof);

    // xtmp[0] = 0.8;
    // xtmp[1] = 1.0;
    // xtmp[2] = 0.3;

    // tree_functor(xtmp.data(),
    //              num_points,
    //              1.5*DT,
    //              vtmp.data());
    // std::cout << "MYRANK: " << myrank << " vals: " << vtmp[0] << " " <<
    // vtmp[1] << " " << vtmp[2] << std::endl;

    // =========================================================================
    // INTERPOLATE IN TIME
    // =========================================================================
    int merged_tree_num_points = merged_tree_points_pos.size() / 3;
    std::vector<double> merged_tree_points_val(merged_tree_num_points *
                                               data_dof);

    pvfmm::Profile::Tic("EvalSet", &sim_config->comm, false, 5);
    tree_set_functor(merged_tree_points_pos.data(), merged_tree_num_points,
                     1.5 * DT, merged_tree_points_val.data());

    // tree_functor(merged_tree_points_pos.data(),
    //              merged_tree_num_points,
    //              merged_tree_points_val.data());
    pvfmm::Profile::Toc();

    // =========================================================================
    // FIX THE VALUES MEMORY LAYOUT
    // =========================================================================
    int d = cheb_deg + 1;
    int num_pnts_per_node = d * d * d;
    std::vector<double> mt_pnts_val_ml(merged_tree_num_points * data_dof);
    for (int nindx = 0; nindx < num_leaf; nindx++) {
      int input_shift = nindx * num_pnts_per_node * data_dof;
      for (int j = 0; j < num_pnts_per_node; j++) {
        for (int i = 0; i < data_dof; i++) {
          mt_pnts_val_ml[input_shift + j + i * num_pnts_per_node] =
              merged_tree_points_val[input_shift + j * data_dof + i];
        }
      }
    }

    // =========================================================================
    // SET INTERPOLATED VALUES
    // =========================================================================
    pvfmm::Profile::Tic("SetValues", &sim_config->comm, false, 5);
    tbslas::SetTreeGridValues(merged_tree, cheb_deg, data_dof, mt_pnts_val_ml);
    pvfmm::Profile::Toc();

    if (sim_config->vtk_save_rate) {
      merged_tree.Write2File(tbslas::GetVTKFileName(0, "tree_interp").c_str(),
                             sim_config->vtk_order);
    }

    double al2, rl2, ali, rli;
    CheckChebOutput<Tree_t>(&merged_tree, fn_2, data_dof, al2, rl2, ali, rli,
                            std::string("Output"));

    // =========================================================================
    // TEST
    // =========================================================================
    // tree_set_functor(xtmp.data(),
    //                  num_points,
    //                  1.5*DT,
    //                  vtmp.data());
    // std::cout << "vals: " << vtmp[0] << " " << vtmp[1] << " " << vtmp[2] <<
    // std::endl;

    // tbslas::NodeFieldFunctor<double,Tree_t> tf(&merged_tree);
    // tf(xtmp.data(),
    //    num_points,
    //    1.5*DT,
    //    vtmp.data());
    // std::cout << "vals: " << vtmp[0] << " " << vtmp[1] << " " << vtmp[2] <<
    // std::endl;

    // =========================================================================
    // REPORT RESULTS
    // =========================================================================
    int tcon_max_depth = 0;
    int tvel_max_depth = 0;
    tbslas::GetTreeMaxDepth(merged_tree, tcon_max_depth);
    // tbslas::GetTreeMaxDepth(tvel, tvel_max_depth);

    typedef tbslas::Reporter<double> Rep;
    if (!myrank) {
      Rep::AddData("NP", np, tbslas::REP_INT);
      Rep::AddData("OMP", sim_config->num_omp_threads, tbslas::REP_INT);

      Rep::AddData("TOL", sim_config->tree_tolerance);
      Rep::AddData("Q", sim_config->tree_chebyshev_order, tbslas::REP_INT);

      Rep::AddData("MaxD", sim_config->tree_max_depth, tbslas::REP_INT);
      Rep::AddData("CMaxD", tcon_max_depth, tbslas::REP_INT);
      // Rep::AddData("VMaxD", tvel_max_depth, tbslas::REP_INT);

      // Rep::AddData("CBC", sim_config->use_cubic?1:0, tbslas::REP_INT);
      // Rep::AddData("CUF", sim_config->cubic_upsampling_factor,
      // tbslas::REP_INT);

      Rep::AddData("DT", sim_config->dt);
      // Rep::AddData("TN", sim_config->total_num_timestep, tbslas::REP_INT);
      // Rep::AddData("TEST", test, tbslas::REP_INT);
      // Rep::AddData("MERGE", merge, tbslas::REP_INT);

      Rep::AddData("InAL2", in_al2);
      Rep::AddData("OutAL2", al2);

      Rep::AddData("InRL2", in_rl2);
      Rep::AddData("OutRL2", rl2);

      Rep::AddData("InALINF", in_ali);
      Rep::AddData("OutALINF", ali);

      Rep::AddData("InRLINF", in_rli);
      Rep::AddData("OutRLINF", rli);

      Rep::Report();
    }

    // Output Profiling results.
    // pvfmm::Profile::print(&comm);
  }

  // Shut down MPI
  MPI_Finalize();
  return 0;
}
示例#10
0
文件: mbfit.C 项目: hengne/d0wmass
void mbfit(){

  gROOT->ProcessLine(".x /home/jholzbau/rootlogon.C");

  TString var = "setfit";

  TH1::SetDefaultSumw2();

  //Plan is to plot SET for CAFE and PMCS, Then with SET-MBSET for PMCS vs CAFE, Then with scaled MBSET (event by event depending on SET or MBSET value or something)


   //run3 files
  // TString rootfile1 = "/prj_root/7055/wmass2/jenny/pythia_zee_run2b3_default_tree_20130703_152033/pythia_zee_run2b3_default_tree_20130703_152033_20130703_152344_23/ZRootTree3.root"; 
   //   TString rootfile2 = "/prj_root/7055/wmass2/jenny/mbtreecafe2/WMASS_MC_RUN2B3_zee_ovl_12p8M_1372882719_default_tree2_20130703151839-15902467.d0cabsrv1.fnal.gov/ZRootTree2.root";
  //TString rootfile1 = "/prj_root/7055/wmass2/jenny/pythia_zee_run2b3_default_tree_20130703_152033/run3.root";
  //TString rootfile1 = "/prj_root/7055/wmass2/jenny/pythia_zee_run2b3_default_tree_vtxcut_redo_20130716_142720/result.root";


  //  TString rootfile1 = "/prj_root/7055/wmass2/jenny/pythia_zee_run2b3_default_tree_vtxcut_redo_20130726_085527/tree.root";
 TString rootfile1 = "/prj_root/7055/wmass2/jenny/pythia_zee_run2b3_mbtest_inclusiveweight_NEWmblib_pow04mbprob_TREE_20130726_085527/tree.root";
  TString rootfile2 = "/prj_root/7055/wmass2/jenny/mbtreecafe2/run3.root";


 Double_t xmin = 0;
 Double_t xmax = 300;
 Int_t nbin = 250;
 
 TH1D *tcafe = new TH1D("topcafe", "topcafe",  nbin, xmin, xmax);
 TH1D *tpmcs = new TH1D("toppmcs", "toppmcs",  nbin, xmin, xmax);
 TH1D *tpmcsd = new TH1D("toppmcsd", "toppmcsd",  nbin, xmin, xmax);
 TH1D *tpmcsmb = new TH1D("toppmcsmb", "toppmcsmb",  nbin, xmin, xmax);
 TH1D *tpmcsother = new TH1D("toppmcsother", "toppmcsother",  nbin, xmin, xmax);

 TH1D *tcafesum = new TH1D("topcafesum", "topcafesum",  nbin, xmin, xmax);
 TH1D *tpmcssum = new TH1D("toppmcssum", "toppmcssum",  nbin, xmin, xmax);
 TH1D *tpmcsmbsum = new TH1D("toppmcsmbsum", "toppmcsmbsum",  nbin, xmin, xmax);
 TH1D *tpmcsothersum = new TH1D("toppmcsothersum", "toppmcsothersum",  nbin, xmin, xmax);

 TH1D *t = new TH1D("top", "top",  nbin, xmin, xmax);
 TH1D *b = new TH1D("bottom", "bottom",  nbin, xmin, xmax);


 TH2D *mbandset = new TH2D("mbandset", "mbandset", nbin, xmin, xmax, nbin, xmin, xmax);
 TH2D *otherandset = new TH2D("otherandset", "otherandset", nbin, xmin, xmax, nbin, xmin, xmax);


 TH1D *cafemb = new TH1D("cafemb", "cafemb",  500, 0, 125);
 TH1D *pmcsmb = new TH1D("pmcsmb", "pmcsmb",  500, 0, 125);
 TH1D *ratio = new TH1D("ratio", "ratio",   500, 0, 125);

  //TH1D *t = new TH1D("top", "top",  100, 0, 150);
  //TH1D *b = new TH1D("bottom", "bottom",  100, 0, 150);
  
  TTree *tree1(0);
  TFile *input1(0);
  TTree *tree2(0);
  TFile *input2(0);
  

 if (!gSystem->AccessPathName( rootfile1 )) {
   cout << "accessing " << rootfile1 << endl;
   input1 = TFile::Open( rootfile1 );
 } 
 else{
   cout<< "problem accessing "<<rootfile1<< endl;
   exit(0);
 }

 tree1 = (TTree*)input1->Get(TreeName1);

 int nentries = tree1->GetEntries();
 cout<<nentries<<" in tree"<<endl;

if (!gSystem->AccessPathName( rootfile2 )) {
   cout << "accessing " << rootfile2 << endl;
   input2 = TFile::Open( rootfile2 );
 } 
 else{
   cout<< "problem accessing "<<rootfile2<< endl;
   exit(0);
 }

 tree2 = (TTree*)input2->Get(TreeName2);

 int nentries2 = tree2->GetEntries();
 cout<<nentries2<<" in tree"<<endl;

 // if(nentries > 1000000) nentries = 1000000;
 // if(nentries > 630000) nentries = 630000;
 // if(nentries2 > 630000) nentries2 = 630000;
 // if(nentries2 > 1000000) nentries2 = 1000000;
 
 float em_e[10];
 float em_pt[10];
float em_eta[10];
float em_phi[10];
float em_deteta[10];

 tree2->SetBranchAddress("em_pt", em_pt);
 tree2->SetBranchAddress("em_e", em_e);
 tree2->SetBranchAddress("em_eta", em_eta);
 tree2->SetBranchAddress("em_phi", em_phi);
 tree2->SetBranchAddress("em_deteta", em_deteta);


   
   // for (int j = 0; j < nentries2; j++){
   //   tree2->GetEntry(j);
   // tree2->GetLeaf("nelec")->GetValue(0) > 0 && 
   //   if((fabs(em_deteta[0]) < 1.1 ) && (fabs(em_deteta[1]) < 1.1 ) && tree2->GetLeaf("Ptz")->GetValue(0) < 3  && tree2->GetLeaf("Luminosity")->GetValue(0)  < 3){
   //     tcafe->Fill(tree2->GetLeaf("ScalarEt")->GetValue(0));
   //     tcafesum->Fill(tree2->GetLeaf("ScalarEt")->GetValue(0), tree2->GetLeaf("ScalarEt")->GetValue(0));
   //   }
   // }

 float em_e1[10];
 float em_pt1[10];
float em_eta1[10];
float em_phi1[10];
float em_deteta1[10];

 tree1->SetBranchAddress("em_pt", em_pt1);
 tree1->SetBranchAddress("em_e", em_e1);
 tree1->SetBranchAddress("em_eta", em_eta1);
 tree1->SetBranchAddress("em_phi", em_phi1);
 tree1->SetBranchAddress("em_deteta", em_deteta1);

 // cout<<"here"<<endl;
 // nentries = 100000;
 double pmcsmbz = 0;
 double pmcsset = 0;
 double test = 0;
 double test2 = 0;
 double test3 = 0;
 double wgt = 0;
 for (int i = 0; i < nentries; i++){

   //Need to do an iterative correction.  Start with scale, adjust mb.  then apply linear correction, refit. etc.  This way approximation that cafe and pmcs set is the same during subtraction is better

   //Should be able to do cafe-pmcs and use weight on mb to make mb account for difference in shape (neg weight issue? scale first?)

   tree1->GetEntry(i);
   if((fabs(em_deteta1[0]) < 1.1 ) && (fabs(em_deteta1[1]) < 1.1 ) && tree1->GetLeaf("Ptz")->GetValue(0) < 3 && tree1->GetLeaf("Luminosity")->GetValue(0) < 20){
   // cout<<i<<" of "<<nentries<<endl;
   //  cout<<tree1->GetLeaf("ScalarEt")->GetValue(0)<<"  "<<ratiotest->FindBin(tree1->GetLeaf("ScalarEt")->GetValue(0))<<endl;
   //  cout<<ratiotest->GetBinContent(ratiotest->FindBin(tree1->GetLeaf("ScalarEt")->GetValue(0)))<<endl;
     // cout<<ratiotest->GetBinContent(20)<<endl;
     pmcsmbz =  tree1->GetLeaf("MBScalarEt")->GetValue(0);
     wgt =1;
     // if(tree1->GetLeaf("MBScalarEt")->GetValue(0) < 5 ){
     //  if (tree1->GetLeaf("MBScalarEt")->GetValue(0) < 2.5 && tree1->GetLeaf("MBScalarEt")->GetValue(0) > 0.5) wgt = 2.5/tree1->GetLeaf("MBS//calarEt")->GetValue(0);
     // else wgt = tree1->GetLeaf("MBScalarEt")->GetValue(0)/5.0;
     // }
if (tree1->GetLeaf("MBScalarEt")->GetValue(0) > 0.5 && tree1->GetLeaf("MBScalarEt")->GetValue(0) < 5 ){
     wgt =  1.3;
 }
     /**
//fixes 30-50 GeV part of curve
if(tree1->GetLeaf("MBScalarEt")->GetValue(0) < 10 ){
wgt =0.5;
}
     **/

     //  wgt = 0;
     //}else if (tree1->GetLeaf("MBScalarEt")->GetValue(0) < 1 ){
     //  wgt = 1 + int(gRandom->Gaus(1,2));
       //  cout<<gRandom->Gaus(2.5,2)<<endl;
     // }


     // }else if  (tree1->GetLeaf("MBScalarEt")->GetValue(0) < 10 ){
     //   wgt = 2.0;
     // }

     // wgt = TMath::Power(pmcsmbz, 0.5/(10/(TMath::Sqrt(pmcsmbz)))) ;
     // wgt = TMath::Power(pmcsmbz, 0.5/(10/(TMath::Sqrt(pmcsmbz)))) + 0.2*pmcsmbz;// + pmcsmbz - 0.09*pmcsmbz*pmcsmbz;
     //  wgt = (TMath::Power(pmcsmbz, 0.5)* (0.5  * (1. + TMath::Erf((pmcsmbz-.5)/(TMath::Sqrt(2)*5))))) / ((1+TMath::Exp(-1*TMath::Power(pmcsmbz/2, 2.0))));
     //wgt = int( (TMath::Power(pmcsmbz, 0.4)) );
     // wgt = (TMath::Power(pmcsmbz, 0.70 - tree1->GetLeaf("Luminosity")->GetValue(0)/50));
   
       // if(tree1->GetLeaf("MBScalarEt")->GetValue(0) < 15){
       //	 wgt = wgt*2.0/tree1->GetLeaf("MBScalarEt")->GetValue(0);
       //}

     // / (0.5  * (1. + TMath::Erf((pmcsmbz-.5)/(TMath::Sqrt(2)*1.0))));
       
       // + (0.5  * (1. + TMath::Erf((pmcsmbz-.5)/(TMath::Sqrt(2)*5)))); //0.5 is good for upper region, 0.75 for middle
     //cout<<TMath::Power(pmcsmbz, 0.5/(10/(TMath::Sqrt(pmcsmbz))))<<"  "<<(0.01  * (1. + TMath::Erf((pmcsmbz-5)/(TMath::Sqrt(2)))))<<endl;
     //wgt = 0.582  * (1. + TMath::Erf((pmcsmbz-1.0935)/(TMath::Sqrt(2)*4.3111)));
     // wgt = wgt*(0.53  * (1. + TMath::Erf((pmcsmbz-0.288)/(TMath::Sqrt(2)*4.0453))));
     //  wgt = wgt*(0.489  * (1. + TMath::Erf((pmcsmbz+0.305)/(TMath::Sqrt(2)*4.89144))));
     /**
	//works ok-ish but doesn't work as weight (obviously...)
     pmcsmbz =  tree1->GetLeaf("MBScalarEt")->GetValue(0)*1.5;
     pmcsmbz = pmcsmbz*(0.544657  * (1. + TMath::Erf((pmcsmbz-0.6808)/(TMath::Sqrt(2)*3.18052))));
     pmcsmbz = pmcsmbz*(0.530694  * (1. + TMath::Erf((pmcsmbz-0.726896)/(TMath::Sqrt(2)*1.0429))));
     pmcsmbz = pmcsmbz*(0.480563  * (1. + TMath::Erf((pmcsmbz-0.321864)/(TMath::Sqrt(2)*0.536511))));
     **/

     //    pmcsmbz = pmcsmbz*(0.342784  * (1. + TMath::Erf((pmcsmbz-0.167683)/(TMath::Sqrt(2)*0.540161))));
     //pmcsmbz =   pmcsmbz*(0.9587 + 0.00365*pmcsmbz);
     //  pmcsmbz =   pmcsmbz*(0.903 + 0.007296*pmcsmbz + 0.00003*pmcsmbz*pmcsmbz - 0.00000119*pmcsmbz*pmcsmbz*pmcsmbz);//*ratiotest2->FindBin(tree1->GetLeaf("MBScalarEt")->GetValue(0));
     //  if (tree1->GetLeaf("ScalarEt")->GetValue(0) > 30) pmcsmbz =  tree1->GetLeaf("MBScalarEt")->GetValue(0)*1.0*(0.42 + 0.0071*tree1->GetLeaf("ScalarEt")->GetValue(0));//*ratiotest->GetBinContent(ratiotest->FindBin(tree1->GetLeaf("MBScalarEt")->GetValue(0)));//1.4;
     //cout<<pmcsmbz<<endl;
     //  pmcsmbz = pmcsmbz/(-0.00249 - (0.0003136*pmcsmbz) + (0.00121*pmcsmbz*pmcsmbz));
     //  if (pmcsmbz < 50) pmcsmbz = pmcsmbz/(-0.00005 - (0.00196*pmcsmbz) + (0.00132*pmcsmbz*pmcsmbz));
     //cout<<pmcsmbz<<" new "<<endl;
     //below way over corrects...  obviously not compensating for "other" correctly...
     // if (pmcsmbz > 0) pmcsmbz = pmcsmbz* ((0.5  * (1. + TMath::Erf((pmcsmbz-8.36)/(TMath::Sqrt(2)*4.9)))));
     // if (pmcsmbz > 0) pmcsmbz = TMath::Sqrt(pmcsmbz);// * (1/(0.5  * (1. + TMath::Erf((pmcsmbz-8.36)/(sqrt(2)*4.9)))));
     //  else pmcsmbz = 0;

     pmcsset = tree1->GetLeaf("ScalarEt")->GetValue(0) - tree1->GetLeaf("MBScalarEt")->GetValue(0) + pmcsmbz;

     tpmcs->Fill(pmcsset, wgt);//minus mb plus mb
     tpmcsd->Fill(pmcsset);
     tpmcsother->Fill(pmcsset -pmcsmbz, wgt);
     tpmcsmb->Fill(pmcsmbz, wgt);
     pmcsmb->Fill(pmcsmbz, wgt);

     tpmcssum->Fill(pmcsset, pmcsset);//minus mb plus mb
     tpmcsothersum->Fill(pmcsset, pmcsset -pmcsmbz);
     tpmcsmbsum->Fill(pmcsset, pmcsmbz);

     test += pmcsset;
     test2 += pmcsset -pmcsmbz;
     test3 += pmcsmbz;
     //cout<<pmcsset<<"  "<<pmcsset -pmcsmbz<<"  "<<pmcsmbz<<endl;
     b->Fill(pmcsset, pmcsmbz);
     t->Fill(pmcsset, pmcsmbz - pmcsset);

     mbandset->Fill(pmcsset, pmcsmbz, wgt);
     otherandset->Fill(pmcsset, pmcsset -pmcsmbz, wgt);
     //   tpmcs->Fill(pmcsmbz);
       //   tpmcs->Fill((pmcsset - pmcsmbz) + (pmcsmbz*(1.25 + 0.005*pmcsmbz- 0.0002*pmcsmbz*pmcsmbz)));

       // tpmcs->Fill((pmcsset - pmcsmbz)  + (pmcsmbz + (0.052 - 0.019*pmcsmbz+ 0.0018*pmcsmbz*pmcsmbz)));

       // tpmcs->Fill((pmcsset - pmcsmbz)  + (pmcsmbz * (0.052 - 0.019*pmcsmbz+ 0.0018*pmcsmbz*pmcsmbz)));

       // tpmcs->Fill((pmcsset - pmcsmbz)  + (1.6*pmcsmbz + (0.2 - 0.065*pmcsmbz+ 0.0023*pmcsmbz*pmcsmbz)));

       //uncomment top before using
       //  tpmcs->Fill((pmcsset - pmcsmbz)  + (pmcsmbz * (1.018 + 0.0319*pmcsmbz - 0.00228*pmcsmbz*pmcsmbz + 0.00008*pmcsmbz*pmcsmbz*pmcsmbz)));

   }
 }

 TH1D *tmp;
 int binz = 0;
 for (int j = 0; j < nentries2; j++){
   tree2->GetEntry(j);
   // tree2->GetLeaf("nelec")->GetValue(0) > 0 && 
   if((fabs(em_deteta[0]) < 1.1 ) && (fabs(em_deteta[1]) < 1.1 ) && tree2->GetLeaf("Ptz")->GetValue(0) < 3  && tree2->GetLeaf("Luminosity")->GetValue(0)  < 20){
     tcafe->Fill(tree2->GetLeaf("ScalarEt")->GetValue(0));
     tcafesum->Fill(tree2->GetLeaf("ScalarEt")->GetValue(0), tree2->GetLeaf("ScalarEt")->GetValue(0));

     binz = tpmcs->FindBin(tree2->GetLeaf("ScalarEt")->GetValue(0));
     tmp = otherandset->ProjectionY("test"+j, binz, binz, "");
     cafemb->Fill(tree2->GetLeaf("ScalarEt")->GetValue(0) - tmp->GetRandom());
   }
 }
 // for(int i = 0; i < nbin+1; i++){
 //   tmp = otherandset->ProjectionY("test"+i, i, i, "");
 //   cafemb->Fill(tcafe->GetXaxis()->GetBinCenter(i) - tmp->GetRandom());
 // }
 TCanvas *c5 = new TCanvas("c5","c5",800,800);
 cafemb->Draw();
 pmcsmb->Scale(cafemb->Integral()/pmcsmb->Integral());
 pmcsmb->SetLineColor(kBlue);
 pmcsmb->Draw("same");
 c5->Update();

 TCanvas *c4 = new TCanvas("c4","c4",800,800);
 tpmcsmbsum->Scale(tcafesum->Integral()/tpmcssum->Integral());//orget enertries?
 tpmcsothersum->Scale(tcafesum->Integral()/tpmcssum->Integral());
 tpmcssum->Scale(tcafesum->Integral()/tpmcssum->Integral());
 tpmcssum->Draw();
 tcafesum->SetMarkerColor(kRed);
 tcafesum->SetLineColor(kRed);
 tcafesum->Draw("same");
 
 tpmcsmbsum->SetLineColor(kBlue);
 tpmcsmbsum->Draw("same");
 tpmcsothersum->SetLineColor(kGreen);
 tpmcsothersum->Draw("same");

 c4->Update();
 c4->Print("PlotSETPTPMCS_RecoVarAfterFITSUM_"+var+".eps");
 c4->Print("PlotSETPTPMCS_RecoVarAfterFITSUM_"+var+".gif");


 TCanvas *c3 = new TCanvas("c3","c3",800,800);
  tpmcsmb->Scale(tcafe->Integral()/tpmcs->Integral());
  tpmcsother->Scale(tcafe->Integral()/tpmcs->Integral());
   tpmcs->Scale(tcafe->Integral()/tpmcs->Integral());
   tpmcsd->Scale(tcafe->Integral()/tpmcsd->Integral());
 tpmcs->Draw();

 tpmcsd->SetMarkerColor(kMagenta);
 tpmcsd->SetLineColor(kMagenta);
 tpmcsd->Draw("same");

 tcafe->SetMarkerColor(kRed);
 tcafe->SetLineColor(kRed);
 tcafe->Draw("same");
 
 tpmcsmb->SetLineColor(kBlue);
 tpmcsmb->Draw("same");
 tpmcsother->SetLineColor(kGreen);
 tpmcsother->Draw("same");

 cout<<test<<"  "<<test2<<"  "<<test3<<endl;
 cout<<tpmcs->Integral()<<"  "<<tpmcsmb->Integral()<<"  "<<tpmcsother->Integral()<<endl;

 c3->Update();
 c3->Print("PlotSETPTPMCS_RecoVarAfterFIT_"+var+".eps");
 c3->Print("PlotSETPTPMCS_RecoVarAfterFIT_"+var+".gif");

  TCanvas *c1 = new TCanvas("c1","c1",800,800);
  t->Scale(tcafe->Integral()/t->Integral());
 //Would have to randomly get a SET value from CAFe distribution (treat it like pdf) and subtract the setother from PMCS to get cafemb, event by event.
 // for(int i = 0; i < nbin+1; i++){
 //  t->SetBinContent(i, tcafe->GetXaxis()->GetBinCenter() - t->GetXaxis()->GetBinCenter()

  t->Add(tcafe);
   b->Scale(t->Integral()/b->Integral());
  b->Draw();
  t->SetMarkerColor(kRed);
  t->SetLineColor(kRed);
  t->Draw("same");

  c1->Update();
  c1->Print("PlotSETPTPMCS_MB_"+var+".eps");
  c1->Print("PlotSETPTPMCS_MB_"+var+".gif");

 TCanvas *c2 = new TCanvas("c2","c2",800,800);
 //for ratio want (cafe(set+mbset) - pmcs(set))/pmcsmb
 // ratio->Divide(t, b,1.0,1.0,"B");
  // ratio->Divide(tpmcs, tcafe,1.0,1.0,"B");
 ratio->Divide(cafemb, pmcsmb,1.0,1.0,"B");
 ratio->Draw();

 TF1 *func = new TF1("func",fitexp,0,125,3);
 // func->SetParameter(0, 0.0);
 // func->SetParameter(1, 30000);
 // func->SetParameter(2, 300000);
 // func->SetParameter(3, 0.1);

  func->SetParameter(0, 10.0);
   func->SetParameter(1, 10.0);
   func->SetParameter(2, 0.6);
   //  func->SetParameter(2, 0.0015);
   //  func->SetParameter(3, 0.0015);

 // func->FixParameter(0, 11);
 //  func->FixParameter(1, 40);
 // func->FixParameter(2, 250000);
 //func->FixParameter(3, 5);
 

// t->Fit("func", "+R");
// t->Draw();

   ratio->Fit("func", "+R");
   ratio->Draw();

 c2->Update();
 c2->Print("PlotSETPTPMCS_Ratio_"+var+".eps");
 c2->Print("PlotSETPTPMCS_Ratio_"+var+".gif");


 TCanvas *c6 = new TCanvas("c6","c6",800,800);
 mbandset->Draw("colz");

  c6->Update();
  c6->Print("PlotSETPTPMCS_MBandSET_"+var+".eps");
  c6->Print("PlotSETPTPMCS_MBandSET_"+var+".gif");



}