#include #include "TFile.h" #include "TH1.h" #include "TString.h" #include "TGraph.h" #include "TRandom3.h" #include "TCanvas.h" #include "TDirectory.h" #include using namespace std; void SetLeafAddress(TTree* ntuple, const char* name, void* address); void Write_histos2(){ const Int_t nbfiles = 10;//100; //240 //number of files const Int_t nLayers = 16; //number of layers const Int_t nMicrons= 300; //number of slices const Int_t nParticles=50000; // nb of particles Double_t edep; Double_t eventID; Double_t Detector; Double_t step; TH1::AddDirectory(kFALSE); TString current_read_file_name = TString("config300_alpha_2MeV_nucl_0_deg.root") ; TString current_write_file_name = TString("Process/config300_alpha_2MeV_nucl_0_deg.root") ; cout<< "File "<< current_read_file_name << " opened" << endl; TFile *myfile= new TFile(current_read_file_name,"READ"); TFile *myfile2= new TFile(current_write_file_name,"RECREATE"); gDirectory->pwd(); //////////////////////////////////////////////// // Get ntuple with structure specific to GEANT// //////////////////////////////////////////////// cout<< "Retrieving Ntuple"; TTree* ntuple = (TTree*)myfile->Get("Landau"); bool rowWise = true; TBranch* eventBranch = ntuple->FindBranch("row_wise_branch"); if ( ! eventBranch ) rowWise = false; if ( ! rowWise ) { ntuple->SetBranchAddress("EventID",&eventID); ntuple->SetBranchAddress("Detector",&Detector); ntuple->SetBranchAddress("Edep",&edep); ntuple->SetBranchAddress("Step",&step); }else { SetLeafAddress(ntuple, "EventID",&eventID); SetLeafAddress(ntuple, "Detector",&Detector); SetLeafAddress(ntuple, "Edep",&edep); SetLeafAddress(ntuple, "Step",&step); } cout<< " ----- done"<< endl; ////////////////////////// // Set up of histograms // ////////////////////////// cout<< "Creating histograms"; //TH1F** hMicrons = new TH1F*[nParticles*nLayers-1]; std::vector hMicrons; //TH1F** hParticle= new TH1F*[nParticles*nLayers-1]; std::vectorhParticle; for (Int_t a = 0; a < nParticles; ++a) { for (Int_t b = 0; b < nLayers; ++b) { TString nameParticle = TString("Particle_")+Form("%d",a+1)+TString("_Layer_") + Form("%d", b+1) ; //hParticlenew[a*nLayers+b]= new TH1F(nameParticle,nameParticle,301,0,300); hParticle.push_back(new TH1F(nameParticle,nameParticle,301,0,300)); //hParticle[a*nLayers+b]->SetDirectory(0); } } for (Int_t i = 0; i < nLayers; ++i) { for (Int_t k = 0; k < nMicrons; ++k) { TString nameMicron= TString("Layer_") + Form("%d", i+1) + "_Micron_"+ Form("%d", k); hMicrons.push_back(new TH1F (nameMicron,nameMicron,1001,0,1000)); //hMicrons[i*nMicrons+k]->SetDirectory(0); } } cout<< " ----- done"<< endl; // Process Ntuple values into the histograms Int_t hmic; Int_t hpart; cout<< "Filling histograms"; for (Int_t j=0;jGetEntries(); j++) { ntuple->GetEntry(j); if (Detector>0) { hmic= (Detector-1)*nMicrons+ floor(step); hMicrons[hmic]->Fill(edep); hpart= eventID*nLayers + (Detector-1); hParticle[hpart]->Fill(step,edep); //cout << h <pwd(); if (!myfile2->Get("Microns")) myfile2->mkdir("Microns"); myfile2->cd("Microns"); //gDirectory->pwd(); cout<< "Writing Microns histograms"; for (Int_t c = 0; c < nMicrons*nLayers ; ++c) // { if (hMicrons[c]->GetEntries()) { hMicrons[c]->Write("",TObject::kOverwrite); //cout << "written--" <cd(); cout<<"Creating Particles folder and accessing it"<Get("Particles")) myfile2->mkdir("Particles"); myfile2->cd("Particles"); cout<< "Writing Particle histograms"<GetEntries()){hParticle[c]->Write("",TObject::kOverwrite);} //cout<cd(); ////////////////////// // Copy original file// /////////////////////// ntuple= (TTree*)myfile->Get("Landau"); ntuple->CloneTree()->Write("",TObject::kOverwrite); TH1D *myh1; for (Int_t r = 0; r < 18; ++r) { myh1 = (TH1D*)myfile->Get(Form("%d", r)); myh1->Write(); } myfile->Close(); delete myfile; myfile2->Close(); delete myfile2; cout<<"Files closed"<Draw(); cout<FindLeaf(name); if ( ! leaf ) { std::cerr << "Error in : unknown leaf --> " << name << std::endl; return; } leaf->SetAddress(address); }