Bug fix. Removed delete statement
[u/mrichter/AliRoot.git] / STEER / cloneAODTreeAndAddObject.C
CommitLineData
a19c5742 1void cloneAODTreeAndAddObject(const char *newFileName = "AliAOD_new.root", const char *orgFileName = "AliAOD.root") {
2 // This little macro takes an already created AOD file and clones it.
3 // After adding a new brach, the new TTree is written to a new file.
4 //
5 // To read back this data (including the newly updated branch), have
6 // a look at $ALICE_ROOT/STEER/ReadAOD.C and add one line
7 // TClonesArray *esdTracks =
8 // (TClonesArray*)ev->GetList()->FindObject("esdTracks");
9 // after
10 // ev->ReadFromTree(aodTree);
11 //
12 // esdTracks will be 'filled' correctly after each call of
13 // aodTree->GetEvent(nEv);
14 //
15
16
17 // open input file and get the TTree
18 TFile orgFile(orgFileName, "READ");
19 // get original TTree
20 TTree *orgAodTree = (TTree*)orgFile.Get("aodTree");
21
22 // open new output file
23 TFile *newFile = new TFile(newFileName, "RECREATE");
24 // clone old TTree
25 TTree *newAodTree = orgAodTree->CloneTree();
26
27 // do your gymnastics with the new TTree
28 AliAODEvent *evNew = new AliAODEvent();
29 evNew->ReadFromTree(newAodTree);
30
31 // add new information to the list (we use AliESDtracks as an example)
32 TClonesArray *tracks = new TClonesArray("AliESDtrack", 0);
33 evNew->AddObject(tracks); // add new object to the list
34
35 // define a unique name
36 const char *name = "esdTracks";
37 tracks->SetName(name); // set this name to be the name of the TClonesArray
38 // create the new branch
39 TBranch *newBranch = newAodTree->Branch(name, &tracks); // the branch gets the same name
40
41 // loop over events
42 Int_t nEvent = newAodTree->GetEntries();
43
44 for(Int_t iEv = 0; iEv < nEvent; iEv++) {
45
46 /*
47 // read events (only necessary if you want to access the old data)
389c0b8b 48 newAodTree->GetEvent(iEv);
a19c5742 49 */
50
51 tracks->Delete(); // for each event delete old entries of new TClonesArray
52
53 Int_t nTracks = gRandom->Rndm() * 50; // randomize size of TClonesArray (just for this example)
54 tracks->Expand(nTracks); // expand container (just for speed)
55
56 // fill TClonesArray
57 TClonesArray &rTracks = *tracks;
58 for (Int_t iTr = 0; iTr< nTracks; iTr++) {
59 new(rTracks[iTr]) AliESDtrack();
60 }
61
62 // fill the new branch
63 newBranch->Fill();
64 }
65
389c0b8b 66 // delete old and write new UserInfo
67 newAodTree->GetUserInfo()->Clear();
68 newAodTree->GetUserInfo()->Add(evNew);
69
a19c5742 70 // write new TTree to file
71 newAodTree->Write();
72
73 // close files
74 newFile->Close();
75 delete newFile;
76
77 orgFile.Close();
78}