/* * Minimal example to open an HDF5 file, list datasets, and plot the /xpos dataset using ROOT. * Compile with: g++ plotXpos.cc -o plotXpos \ -I/usr/include/hdf5/serial \ -L/usr/lib/x86_64-linux-gnu/hdf5/serial \ -lhdf5_serial_cpp -lhdf5_serial \ `root-config --cflags --libs` * Prerequisite sudo apt install libhdf5-dev hdf5-tools */ #include #include #include #include #include #include using namespace H5; // Funzione ricorsiva per listare tutti i dataset in un gruppo void listDatasets(Group& group, const std::string& prefix = "/") { // Numero oggetti nel gruppo hsize_t num_objs = group.getNumObjs(); for (hsize_t i = 0; i < num_objs; i++) { // Nome dell'i-esimo oggetto nel gruppo std::string name = group.getObjnameByIdx(i); // Tipo dell'oggetto H5G_obj_t type = group.getObjTypeByIdx(i); std::string full_path = prefix + name; if (type == H5G_GROUP) { // Se è un gruppo, ricorsivamente esploro Group subgroup = group.openGroup(name); listDatasets(subgroup, full_path + "/"); } else if (type == H5G_DATASET) { // Se è un dataset, stampo il percorso completo std::cout << "Dataset: " << full_path << std::endl; } else { // Altri tipi (simbolic link, tipo non gestito...) std::cout << "Altro tipo: " << full_path << std::endl; } } } int plotXposHistograms(const std::string& filename) { TApplication app("app", nullptr, nullptr); try { // Apri il file in sola lettura H5File file(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ); // Apri dataset /xpos DataSet dataset = file.openDataSet("/xpos"); DataSpace dataspace = dataset.getSpace(); // Controlla dimensioni int rank = dataspace.getSimpleExtentNdims(); if (rank != 2) { std::cerr << "Dataset /xpos non è 2D, rango: " << rank << std::endl; return 1; } hsize_t dims[2]; dataspace.getSimpleExtentDims(dims, nullptr); size_t nRows = dims[0]; size_t nCols = dims[1]; std::cout << "/xpos dimensioni: " << nRows << " x " << nCols << std::endl; // Leggi dati in vettore 1D row-major std::vector data(nRows * nCols); dataset.read(data.data(), PredType::NATIVE_DOUBLE); // Parametri istogrammi const int nBins = 384; const double binWidth = 0.0050; const double xMin = 0.0; const double xMax = binWidth * nBins; // Crea canvas 2x2 TCanvas* canvas = new TCanvas("canvas", "Profilo del fascio", 1200, 600); canvas->Divide(2, 2); // Crea e riempi istogrammi prime 4 colonne TH1F* histos[4]; for (int col = 0; col < 4; ++col) { histos[col] = new TH1F(Form("h%d", col), Form("Coord %d;Pos [cm];Entries", col), nBins, xMin, xMax); histos[col]->SetLineColor(kGreen + 2); histos[col]->SetLineWidth(1); for (size_t row = 0; row < nRows; ++row) { double val = data[row * nCols + col]; histos[col]->Fill(val); } } // Disegna gli istogrammi for (int i = 0; i < 4; ++i) { canvas->cd(i + 1); histos[i]->Draw("HIST"); gPad->SetGrid(); } canvas->Update(); // Avvia loop eventi ROOT per tenere la finestra aperta app.Run(); } catch (FileIException& e) { std::cerr << "Errore apertura file HDF5" << std::endl; e.printErrorStack(); return 1; } catch (DataSetIException& e) { std::cerr << "Errore apertura dataset" << std::endl; e.printErrorStack(); return 1; } catch (DataSpaceIException& e) { std::cerr << "Errore dataspace" << std::endl; e.printErrorStack(); return 1; } return 0; } int main(int argc, char** argv) { const char* filename = "run810063.h5"; // List datasets in the file // Apri il file in sola lettura H5File file(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ); // Apri il gruppo radice Group root = file.openGroup("/"); // Lista tutti i dataset ricorsivamente listDatasets(root); int ret = plotXposHistograms(filename); return ret; }