예제 #1
0
bool execute(const std::string& skeleton, const std::string& config_file, std::string output_dir/* = ""*/) {

    std::vector<Plot> plots;
    // If an output directory is specified, use it, otherwise use the current directory
    if (output_dir == "")
      output_dir = ".";

    std::map<std::string, std::string> unique_names;

    get_plots(config_file, plots);

    std::cout << "List of requested plots: ";
    for (size_t i = 0; i < plots.size(); i++) {
        std::cout << "'" << plots[i].name << "'";
        if (i != plots.size() - 1)
            std::cout << ", ";
    }
    std::cout << std::endl;

    // Convert plots name to unique name to avoid collision between different runs
    for (Plot& plot: plots) {
        std::string uuid = get_uuid();
        unique_names[uuid] = plot.name;
        plot.name = uuid;
    }

    std::unique_ptr<TChain> t(new TChain("t"));
    t->Add(skeleton.c_str());

    std::vector<Branch> branches;
    std::function<void(TTreeFormula*)> getBranches = [&branches, &getBranches](TTreeFormula* f) {
        if (!f)
            return;

        for (size_t i = 0; i < f->GetNcodes(); i++) {
            TLeaf* leaf = f->GetLeaf(i);
            if (! leaf)
                continue;

            TBranch* p_branch = getTopBranch(leaf->GetBranch());

            Branch branch;
            branch.name = p_branch->GetName();
            if (std::find_if(branches.begin(), branches.end(), [&branch](const Branch& b) {  return b.name == branch.name;  }) == branches.end()) {
                branch.type = p_branch->GetClassName();
                if (branch.type.empty())
                    branch.type = leaf->GetTypeName();

                branches.push_back(branch);
            }

            for (size_t j = 0; j < f->fNdimensions[i]; j++) {
                if (f->fVarIndexes[i][j])
                    getBranches(f->fVarIndexes[i][j]);
            }
        }

        for (size_t i = 0; i < f->fAliases.GetEntriesFast(); i++) {
            getBranches((TTreeFormula*) f->fAliases.UncheckedAt(i));
        }
    };

    std::string hists_declaration;
    std::string text_plots;
    for (auto& p: plots) {
        // Create formulas
        std::shared_ptr<TTreeFormula> selector(new TTreeFormula("selector", p.plot_cut.c_str(), t.get()));

        getBranches(selector.get());

        std::vector<std::string> splitted_variables = split(p.variable, ":");
        for (const std::string& variable: splitted_variables) {
            std::shared_ptr<TTreeFormula> var(new TTreeFormula("var", variable.c_str(), t.get()));
            getBranches(var.get());
        }

        std::string binning = p.binning;
        binning.erase(std::remove_if(binning.begin(), binning.end(), [](char chr) { return chr == '(' || chr == ')'; }), binning.end());

        // If a variable bin size is specified, declare array that will be passed as array to histogram constructor
        if(binning.find("{") != std::string::npos){
          std::string arrayString = buildArrayForVariableBinning(binning, splitted_variables.size(), p.name);
          hists_declaration += arrayString;
        }

        std::string title = p.title + ";" + p.x_axis + ";" + p.y_axis + ";" + p.z_axis;
        std::string histogram_type = getHistogramTypeForDimension(splitted_variables.size());

        hists_declaration += "    std::unique_ptr<" + histogram_type + "> " + p.name + "(new " + histogram_type + "(\"" + p.name + "\", \"" + title + "\", " + binning + ")); " + p.name + "->SetDirectory(nullptr);\n";
 
        std::string variable_string;
        for (size_t i = 0; i < splitted_variables.size(); i++) {
            variable_string += splitted_variables[i];
            if (i != splitted_variables.size() - 1)
                variable_string += ", ";
        }

        ctemplate::TemplateDictionary plot("plot");
        plot.SetValue("CUT", p.plot_cut);
        plot.SetValue("VAR", variable_string);
        plot.SetValue("HIST", p.name);

        ctemplate::ExpandTemplate(getTemplate("Plot"), ctemplate::DO_NOT_STRIP, &plot, &text_plots);
    }

    // Sort alphabetically
    std::sort(branches.begin(), branches.end(), [](const Branch& a, const Branch& b) {
            return a.name < b.name;
            });

    std::string text_branches;
    for (const auto& branch: branches)  {
        text_branches += "const " + branch.type + "& " + branch.name + " = tree[\"" + branch.name + "\"].read<" + branch.type + ">();\n        ";
    }

    ctemplate::TemplateDictionary header("header");
    header.SetValue("BRANCHES", text_branches);

    std::string output;
    ctemplate::ExpandTemplate(getTemplate("Plotter.h"), ctemplate::DO_NOT_STRIP, &header, &output);

    std::ofstream out(output_dir + "/Plotter.h");
    out << output;
    out.close();

    output.clear();

    std::string text_save_plots;
    for (auto& p: plots) {
        ctemplate::TemplateDictionary save_plot("save_plot");
        save_plot.SetValue("UNIQUE_NAME", p.name);
        save_plot.SetValue("PLOT_NAME", unique_names[p.name]);
        ctemplate::ExpandTemplate(getTemplate("SavePlot"), ctemplate::DO_NOT_STRIP, &save_plot, &text_save_plots);
    }

    ctemplate::TemplateDictionary source("source");
    source.SetValue("HISTS_DECLARATION", hists_declaration);
    source.SetValue("PLOTS", text_plots);
    source.SetValue("SAVE_PLOTS", text_save_plots);
    ctemplate::ExpandTemplate(getTemplate("Plotter.cc"), ctemplate::DO_NOT_STRIP, &source, &output);

    out.open(output_dir + "/Plotter.cc");
    out << output;
    out.close();

    return true;
}
예제 #2
0
파일: pictures.c 프로젝트: mortid0/gefestus
void bird_style_pics(int step)
{
	int kx, ky, krp, nc, nm;
	PARTICLE *p;
	double avr_conc[NC], avr_temp[NC], avr_mass[NC], new_avr_vx[NC], new_avr_vy[NC];
	for (nc = 0; nc < NC; nc++)
	{
		new_avr_vx[nc] = 0;
		new_avr_vy[nc] = 0;
		for (kx = 0; kx < KX; kx++)
		{
			for (ky = 0; ky < KY; ky++)
			{
				temp_field[nc][kx][ky] = 0.0;
				conc_field[nc][kx][ky] = 0.0;
				mass_field[nc][kx][ky] = 0.0;
				impulse_field[nc][kx][ky] = 0.0;
			}
		}
		for (kx = 0; kx < MAX_PLOT_VEL; kx++)
		{
			distr_x[nc][kx] = 0;
			distr_y[nc][kx] = 0;
		}
	}
	for (krp = 0; krp < KRP; krp++)
	{
		for (kx = 0; kx < KX; kx++)
		{
			for (ky = 0; ky < KY; ky++)
			{
				for (nc = 0; nc < NC; nc++)
				{
					for (nm = 0; nm < NM; nm++)
					{
						p = &camera[krp][kx][ky][nc][nm];
						conc_field[nc][kx][ky] += p->weight;
						temp_field[nc][kx][ky] += get_energy(p);
						mass_field[nc][kx][ky] += p->weight * p->mass * SIZEX * SIZEY * SIZEZ;
						impulse_field[nc][kx][ky] += p->weight * atan2(p->v.y, p->v.x);
						new_avr_vx[nc] += p->weight * p->v.x;
						new_avr_vy[nc] += p->weight * p->v.y;
						int num_vel_x = (int)(p->v.x + 0.5 * MAX_PLOT_VEL), num_vel_y = (int)(p->v.y + 0.5 * MAX_PLOT_VEL);
						if ((num_vel_x < MAX_PLOT_VEL)&&(num_vel_x>0)) {distr_x[nc][num_vel_x] += p->weight;}
						if ((num_vel_y < MAX_PLOT_VEL)&&(num_vel_y>0)) {distr_y[nc][num_vel_y] += p->weight;}
// 						if (fabs(p->v.x) < 0.5*MAX_PLOT_VEL) {distr_x[nc][(int)(p->v.x + 0.5 * MAX_PLOT_VEL)] += p->weight;}
// 						if (fabs(p->v.y) < 0.5*MAX_PLOT_VEL) {distr_y[nc][(int)(p->v.y + 0.5 * MAX_PLOT_VEL)] += p->weight;}
					}
				}
			}
		}
	}
	for (nc = 0; nc < NC; nc++)
	{
		avr_conc[nc] = 0;
		avr_temp[nc] = 0;
		avr_mass[nc] = 0;
		for (kx = 0; kx < KX; kx++)
		{
			for (ky = 0; ky < KY; ky++)
			{
				temp_field[nc][kx][ky] /= KRP;
				avr_temp[nc] += temp_field[nc][kx][ky];
				conc_field[nc][kx][ky] /= KRP;
				avr_conc[nc] += conc_field[nc][kx][ky];
				mass_field[nc][kx][ky] /= KRP;
				avr_mass[nc] += mass_field[nc][kx][ky];
				impulse_field[nc][kx][ky] = (conc_field[nc][kx][ky]>0)?(impulse_field[nc][kx][ky]/(conc_field[nc][kx][ky]*KRP)):100.0;
			}
		}
		avr_vx[nc] = (avr_conc[nc] > 0) ? new_avr_vx[nc]/(avr_conc[nc] * KRP) : 0;
		avr_vy[nc] = (avr_conc[nc] > 0) ? new_avr_vy[nc]/(avr_conc[nc] * KRP) : 0;
		printf("comp#%i: temp = %5.5e; conc = %5.5e; mass = %5.5e; vx = %e; vy = %e;\n", nc, avr_temp[nc]/(KX*KY), avr_conc[nc]/(KX*KY), avr_mass[nc] /(KX*KY), avr_vx[nc], avr_vy[nc]);
		
	}
	save_plot("temp2/plotx", distr_x, step);
	save_plot("temp2/ploty", distr_y, step);
	save_field("temp2/temp", temp_field, step);
	save_field("temp2/conc", conc_field, step);
	save_field("temp2/full_mass", mass_field, step);
	save_three_column("temp2/impulse", impulse_field, step);
//	print_forces(step);
}