1 void 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.
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");
10 // ev->ReadFromTree(aodTree);
12 // esdTracks will be 'filled' correctly after each call of
13 // aodTree->GetEvent(nEv);
17 // open input file and get the TTree
18 TFile orgFile(orgFileName, "READ");
20 TTree *orgAodTree = (TTree*)orgFile.Get("aodTree");
22 // open new output file
23 TFile *newFile = new TFile(newFileName, "RECREATE");
25 TTree *newAodTree = orgAodTree->CloneTree();
27 // do your gymnastics with the new TTree
28 AliAODEvent *evNew = new AliAODEvent();
29 evNew->ReadFromTree(newAodTree);
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
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
42 Int_t nEvent = newAodTree->GetEntries();
44 for(Int_t iEv = 0; iEv < nEvent; iEv++) {
47 // read events (only necessary if you want to access the old data)
48 newAodTree->GetEvent(iEv);
51 tracks->Delete(); // for each event delete old entries of new TClonesArray
53 Int_t nTracks = gRandom->Rndm() * 50; // randomize size of TClonesArray (just for this example)
54 tracks->Expand(nTracks); // expand container (just for speed)
57 TClonesArray &rTracks = *tracks;
58 for (Int_t iTr = 0; iTr< nTracks; iTr++) {
59 new(rTracks[iTr]) AliESDtrack();
62 // fill the new branch
66 // delete old and write new UserInfo
67 newAodTree->GetUserInfo()->Clear();
68 newAodTree->GetUserInfo()->Add(evNew);
70 // write new TTree to file