ROOT matrix to RADWARE format

Hi, I have a two-dimensional matrix in “.root” format, but I want to convert it to “.m4b” radware format. Could anyone please help me with this?

Welcome to the ROOT forum

A similar question [1] was already asked on the ROOT forum. May be It can help you ?

[1] Radware matrix to ROOT

Hi, I tried to convert a .m4b file to root format using the given program, but it was unable to read the .m4b file.

what is your root 2-dim matrix ? a TH2 ?

Yes it is in TH2D format

I do not know how a .m4b exactly looks like but according to what I see here and there it seems to be some kind of ASCII file. In that case something like that should be done:

void convertToM4b(const char* rootFilename, const char* outputFilename) {
    TFile *file = new TFile(rootFilename);
    TH2F *hist = (TH2F*)file->Get("histogram_name"); 
    std::ofstream outputFile(outputFilename);

    // Write histogram data to output file in RADWARE .m4b format ?
    // This loop shold be adapted to match the .m4b format
    for (int i = 1; i <= hist->GetNbinsX(); ++i) {
        for (int j = 1; j <= hist->GetNbinsY(); ++j) {
            float binContent = hist->GetBinContent(i, j);
            float binError = hist->GetBinError(i, j);
            outputFile << i << " " << j << " " << binContent << " " << binError << std::endl;



root[0]    .x convertToM4b.C("input.root", "output.m4b");

@couet Thank you… After modifying the program according to the radware format (4bytes per channel), it worked…

1 Like

May be you can post here what you wrote ? It might be useful for other users…

The modified portion of the program is given below.

// The initial part of the program will remain the same. 
// The modified loop for writing the histogram data to .m4b format is as follows:

    FILE *f = fopen(output_file_name, "wb");
    for (int j = 1; j <= hist->GetNbinsX(); ++j) 
        for (int i = 1; i <= hist->GetNbinsY(); ++i)
            int value = static_cast<int>(hist->GetBinContent(i, j));
            fwrite(&value, sizeof(int), 1, f);