Dear @faca87 ,
First, a disclaimer
Your code is full of new without a single delete. This is extremely bad practice, please refrain from it, use std::unique_ptr sparingly and just declare most if not all of your objects on the stack.
Secondly, do not use raw c-style casts like (TH1D*)myobj. If you really need to cast an object to another type, prefer static_cast or dynamic_cast depending on your needs.
Back to your issue
In your code, I see the following
TH1D *dati1 = (TH1D*) dati->Clone();
[...]
dati1->Write();
I would like to bring to your attention the call to Write. Where would you expect that call to write your histogram? I see many TFiles being opened in your application, for example:
TFile *ROOTOut = new TFile(FileOut.c_str(),"RECREATE");
TFile *ROOTData = TFile::Open(FileData.c_str(),"READ");
And then this other for loop which opens more files
TFile *ROOTMC[Nbr];
for (int j=0; j<Nbr; j++) {
ROOTMC[j] = TFile::Open(FileMC[j].c_str(),"READ");
mc[j] = (TH1D*)ROOTMC[j]->Get(Hist_MC.c_str());
}
So, in which file specifically would you expect the Write operation to write the file?
If the answer is ROOTOut, then your expectation is wrong. By default, some ROOT objects that have the Write method will be written in the latest opened file (also aliased by the variable gDirectory). It’s important that you know/remember what this variable is pointing to at any given point of your application.
Or, as a better solution, you can be more explicit about what object you want to write to what file, i.e.
ROOTOut->WriteObject(myhisto, "name_of_the_histo");
Cheers,
Vincenzo