ROOT

Descrição

De acordo com a página do ROOT, ROOT é um framework para processamento de dados, nascido no CERN, no coração da pesquisa em física de altas energias. Milhares de físicos usam aplicações ROOT diariamente para analisar dados ou realizar simulações.

Versões Disponíveis

  • root/6.10.02 (default)

Carregando o Módulo

# Carregar ROOT
module load root/6.10.02

# Verificar instalação
root --version
which root

Submissão de Jobs

submit_root.sh
#!/bin/bash
#SBATCH -J root_job
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 24:00:00
#SBATCH --mem=8G

export INPUT="analysis.cpp"
export OUTPUT="analysis.root"

module load root/6.10.02

job-nanny root -b -l -q analysis.cpp

Exemplo de Script ROOT (C++)

analysis.cpp
#include <TFile.h>
#include <TTree.h>
#include <TH1F.h>
#include <TCanvas.h>
#include <iostream>

void analysis() {
    // Abrir arquivo de dados
    TFile *input = TFile::Open("data.root");
    TTree *tree = (TTree*)input->Get("tree");

    // Criar histogramas
    TH1F *h_x = new TH1F("h_x", "Distribuição de x", 100, -5, 5);
    TH1F *h_y = new TH1F("h_y", "Distribuição de y", 100, -5, 5);

    // Variáveis para leitura
    float x, y;
    tree->SetBranchAddress("x", &x);
    tree->SetBranchAddress("y", &y);

    // Loop sobre eventos
    Long64_t nentries = tree->GetEntries();
    for (Long64_t i = 0; i < nentries; i++) {
        tree->GetEntry(i);
        h_x->Fill(x);
        h_y->Fill(y);

        if (i % 100000 == 0) {
            std::cout << "Processados " << i << " eventos" << std::endl;
        }
    }

    // Estatísticas
    std::cout << "Média de x: " << h_x->GetMean() << std::endl;
    std::cout << "RMS de x: " << h_x->GetRMS() << std::endl;
    std::cout << "Média de y: " << h_y->GetMean() << std::endl;
    std::cout << "RMS de y: " << h_y->GetRMS() << std::endl;

    // Salvar resultados
    TFile *output = TFile::Open("results.root", "RECREATE");
    h_x->Write();
    h_y->Write();
    output->Close();

    // Plotar
    TCanvas *c1 = new TCanvas("c1", "Histogramas", 800, 400);
    c1->Divide(2,1);
    c1->cd(1);
    h_x->Draw();
    c1->cd(2);
    h_y->Draw();
    c1->SaveAs("histograms.png");

    input->Close();
}

Script ROOT com Python (PyROOT)

submit_pyroot.sh
#!/bin/bash
#SBATCH -J pyroot
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 12:00:00
#SBATCH --mem=8G

export INPUT="analysis.py"
export OUTPUT="pyroot_results.root"

module load root/6.10.02

job-nanny python analysis.py
analysis.py
import ROOT
import numpy as np

# Gerar dados
data = np.random.normal(0, 1, 1000000)

# Criar histograma ROOT
hist = ROOT.TH1F("hist", "Distribuição Normal", 100, -5, 5)

for value in data:
    hist.Fill(value)

# Estatísticas
print(f"Média: {hist.GetMean()}")
print(f"RMS: {hist.GetMean()}")
print(f"Entradas: {hist.GetEntries()}")

# Ajuste gaussiano
fit = ROOT.TF1("fit", "gaus", -5, 5)
hist.Fit(fit, "Q")

print(f"Parâmetros do fit:")
print(f"  Constante: {fit.GetParameter(0)}")
print(f"  Média: {fit.GetParameter(1)}")
print(f"  Sigma: {fit.GetParameter(2)}")

# Salvar
output = ROOT.TFile("pyroot_results.root", "RECREATE")
hist.Write()
output.Close()

Processamento em Lote

submit_root_batch.sh
#!/bin/bash
#SBATCH -J root_batch
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 48:00:00
#SBATCH --mem=16G

export INPUT="files.txt"
export OUTPUT="merged_results.root"

module load root/6.10.02

# Criar script para processar múltiplos arquivos
cat > batch_process.C << 'EOF'
void batch_process() {
    // Lista de arquivos
    ifstream file_list("files.txt");
    string filename;

    TChain chain("tree");

    while (file_list >> filename) {
        cout << "Adicionando: " << filename << endl;
        chain.Add(filename.c_str());
    }

    // Processar chain
    TH1F *h = new TH1F("h", "Distribuição", 100, -5, 5);
    chain.Draw("x>>h");

    // Salvar
    TFile *output = TFile::Open("merged_results.root", "RECREATE");
    h->Write();
    output->Close();
}
EOF

job-nanny root -b -l -q batch_process.C

Job Array para Simulações

submit_root_array.sh
#!/bin/bash
#SBATCH -J root_array
#SBATCH --array=1-10
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 24:00:00
#SBATCH --mem=8G

SEED=$((12345 + SLURM_ARRAY_TASK_ID))
export INPUT="simulation.C"
export OUTPUT="sim_${SLURM_ARRAY_TASK_ID}.root"

module load root/6.10.02

cat > sim_${SLURM_ARRAY_TASK_ID}.C << EOF
void sim_${SLURM_ARRAY_TASK_ID}() {
    gRandom->SetSeed($SEED);

    // Simulação
    TH1F *h = new TH1F("h", "Simulação", 100, -5, 5);
    for (int i = 0; i < 1000000; i++) {
        h->Fill(gRandom->Gaus(0, 1));
    }

    // Salvar
    TFile *f = TFile::Open("sim_${SLURM_ARRAY_TASK_ID}.root", "RECREATE");
    h->Write();
    f->Close();

    cout << "Simulação ${SLURM_ARRAY_TASK_ID} concluída" << endl;
}
EOF

job-nanny root -b -l -q sim_${SLURM_ARRAY_TASK_ID}.C

Análise RDataFrame

submit_rdf.sh
#!/bin/bash
#SBATCH -J rdf
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 12:00:00
#SBATCH --mem=8G

export INPUT="rdf_analysis.C"
export OUTPUT="rdf_results.root"

module load root/6.10.02

cat > rdf_analysis.C << 'EOF'
#include <ROOT/RDataFrame.hxx>

void rdf_analysis() {
    // Criar dataframe a partir de arquivos
    ROOT::RDataFrame df("tree", "data_*.root");

    // Definir filtros e operações
    auto df_filtered = df.Filter("x > 0 && y < 10");

    // Calcular médias
    auto x_mean = df_filtered.Mean("x");
    auto y_mean = df_filtered.Mean("y");

    // Criar histogramas
    auto hx = df_filtered.Histo1D({"hx", "Distribuição de x", 100, 0, 10}, "x");
    auto hy = df_filtered.Histo1D({"hy", "Distribuição de y", 100, 0, 10}, "y");

    // Resultados
    std::cout << "Média de x: " << *x_mean << std::endl;
    std::cout << "Média de y: " << *y_mean << std::endl;

    // Salvar
    TFile *f = TFile::Open("rdf_results.root", "RECREATE");
    hx->Write();
    hy->Write();
    f->Close();
}
EOF

job-nanny root -b -l -q rdf_analysis.C

Referências

Ver também