Bug corrected.
[u/mrichter/AliRoot.git] / STEER / addObjectAndUpdateAOD.C
CommitLineData
a19c5742 1void addObjectAndUpdateAOD(const char *fileName = "AliAOD.root") {
2 // This little macro takes an already created AOD file and adds
3 // a new branch to it.
a68eeaf1 4 // You need write access to the AOD file, which is generally not
5 // the case for AOD files available on the GRID/on CAF.
6 // Be aware that if something breaks (lost connection, node goes
7 // down, ...) the content of the file will be essentially lost,
8 // meaning:
9 // WITH THIS UPDATE PROCEDURE YOU HAVE THE POWER TO DESTROY DATA!
a19c5742 10 //
11 // To read back this data (including the newly updated branch), have
12 // a look at $ALICE_ROOT/STEER/ReadAOD.C and add one line
13 // TClonesArray *esdTracks =
14 // (TClonesArray*)ev->GetList()->FindObject("esdTracks");
15 // after
16 // ev->ReadFromTree(aodTree);
17 //
18 // esdTracks will be 'filled' correctly after each call of
19 // aodTree->GetEvent(nEv);
20 //
21
22
23 // open input file and get the TTree
24 TFile inFile(fileName, "UPDATE");
25
26 TTree *aodTree = (TTree*)inFile.Get("aodTree");
27 AliAODEvent *ev = new AliAODEvent();
28 ev->ReadFromTree(aodTree);
29
30 // add new information to the list (we use AliESDtracks as an example)
31 TClonesArray *tracks = new TClonesArray("AliESDtrack", 0);
32 ev->AddObject(tracks); // add new object to the list
33
34 // define a unique name
35 const char *name = "esdTracks";
36 tracks->SetName(name); // set this name to be the name of the TClonesArray
37 // create the new branch
38 TBranch *newBranch = aodTree->Branch(name, &tracks); // the branch gets the same name
39
40 // loop over events
41 Int_t nEvent = aodTree->GetEntries();
42
43 for(Int_t iEv = 0; iEv < nEvent; iEv++) {
44
45 /*
46 // read events (only necessary if you want to access the old data)
47 aodTree->GetEvent(nEv);
48 */
49
50 tracks->Delete(); // for each event delete old entries of new TClonesArray
51
52 Int_t nTracks = gRandom->Rndm() * 50; // randomize size of TClonesArray (just for this example)
53 tracks->Expand(nTracks); // expand container (just for speed)
54
55 // fill TClonesArray
56 TClonesArray &rTracks = *tracks;
57 for (Int_t iTr = 0; iTr< nTracks; iTr++) {
58 new(rTracks[iTr]) AliESDtrack();
59 }
60
61 // fill the new branch
62 newBranch->Fill();
63 }
64
65 // (over)write the tree (to the same file!)
66
67 aodTree->Write("", TObject::kOverwrite);
68 inFile.Close();
69
70 return;
71}