#include "TROOT.h" #include "TFile.h" #include "TTree.h" #include "TBrowser.h" #include "TH2.h" #include "TMath.h" #include "TRandom.h" #include "TCanvas.h" const Int_t MAXMEC = 30; class Gctrak : public TObject { public: Float_t vect[7]; Float_t getot; Float_t gekin; Float_t vout[7]; ///lmec = new Int_t[MAXMEC]; gstep->namec = new Int_t[MAXMEC]; gstep->step = 0.1; gstep->destep = 0; gstep->nmec = 0; gstep->pid = 0; // transport particles for (Int_t i=0;i <10000; i++) { // generate a new particle if necessary if (newParticle) { px = gRandom->Gaus(0, .02); py = gRandom->Gaus(0, .02); pz = gRandom->Gaus(0, .02); p = TMath::Sqrt(px * px + py * py + pz * pz); charge = 1; if (gRandom->Rndm() < 0.5) charge = -1; gstep->pid += 1; gstep->vect[0] = 0; gstep->vect[1] = 0; gstep->vect[2] = 0; gstep->vect[3] = px / p; gstep->vect[4] = py / p; gstep->vect[5] = pz / p; gstep->vect[6] = p*charge; gstep->getot = TMath::Sqrt(p * p + mass * mass); gstep->gekin = gstep->getot - mass; newParticle = kFALSE; } // fill the Tree with current step parameters t2.Fill(); // transport particle in magnetic field helixStep(gstep->step, gstep->vect, vout); // make one step //apply energy loss gstep->destep = gstep->step*gRandom->Gaus(0.0002, 0.00001); gstep->gekin -= gstep->destep; gstep->getot = gstep->gekin + mass; gstep->vect[6] = charge * TMath::Sqrt(gstep->getot * gstep->getot - mass * mass); gstep->vect[0] = vout[0]; gstep->vect[1] = vout[1]; gstep->vect[2] = vout[2]; gstep->vect[3] = vout[3]; gstep->vect[4] = vout[4]; gstep->vect[5] = vout[5]; gstep->nmec = (Int_t)(5 * gRandom->Rndm()); for (Int_t l=0; lnmec; l++) { gstep->lmec[l] = l; gstep->namec[l] = l + 100; } if (gstep->gekin < 0.001) newParticle = kTRUE; if (TMath::Abs(gstep->vect[2]) > 30) newParticle = kTRUE; } // save the Tree header. The file will be automatically closed // when going out of the function scope t2.Write(); } void tree106_read() { // read the Tree generated by tree2w and fill one histogram // we are only interested by the destep branch. // note that we create the TFile and TTree objects on the heap // because we want to keep these objects alive when we leave // this function. auto f = TFile::Open("tree106.root"); auto t2 = f->Get("t2"); Gctrak *gstep = nullptr; t2->SetBranchAddress("track", &gstep); auto b_destep = t2->GetBranch("destep"); // create one histogram auto hdestep = new TH1F("hdestep", "destep in Mev", 100, 1e-5, 3e-5); // read only the destep branch for all entries Long64_t nentries = t2->GetEntries(); for (Long64_t i=0; iGetEntry(i); hdestep->Fill(gstep->destep); } // we do not close the file // we want to keep the generated histograms // we fill a 3-d scatter plot with the particle step coordinates auto c1 = new TCanvas("c1", "c1", 600, 800); c1->SetFillColor(42); c1->Divide(1, 2); c1->cd(1); hdestep->SetFillColor(45); hdestep->Fit("gaus"); c1->cd(2); gPad->SetFillColor(37); t2->SetMarkerColor(kRed); t2->Draw("vect[0]:vect[1]:vect[2]"); if (gROOT->IsBatch()) return; // invoke the x3d viewer gPad->GetViewer3D("ogl"); } void tree106_tree() { tree106_write(); tree106_read(); }