]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/ConvertPHOSToNewIO.C
Transition to NewIO
[u/mrichter/AliRoot.git] / PHOS / ConvertPHOSToNewIO.C
1 #ifndef __MAKECINT__
2  #ifndef __CINT__
3 #include <TROOT.h>
4 #include <TRint.h>
5 #include <TMath.h>
6 #include <TRandom.h>
7 #include <TVector.h>
8 #include <TMatrix.h>
9 #include <TGeometry.h>
10 #include <TNode.h>
11 #include <TTUBS.h>
12 #include <TObjectTable.h>
13 #include <iostream.h>
14 #include <fstream.h>
15 #include "AliMC.h"
16 #include <TFile.h>
17 #include <TStyle.h>
18 #include <TCanvas.h>
19 #include <TPad.h>
20 #include <TLine.h>
21 #include <TText.h>
22 #include <TTree.h>
23 #include <TBranch.h>
24 #include "AliModule.h"
25 #include "AliRunLoader.h"
26 #include "AliHeader.h"
27 #include "AliDetector.h"
28 #include "AliConfig.h"
29 #include "AliRun.h"
30 #include "AliRunLoader.h"
31 #include <TParticle.h>
32 #include "TBranchClones.h"
33 #include "TBranchElement.h"  
34
35 #include "PHOS/AliPHOSClusterizerv1.h"
36 #include "PHOS/AliPHOSDigitizer.h"
37 #include "PHOS/AliPHOSSDigitizer.h"
38 #include "PHOS/AliPHOSTrackSegmentMakerv1.h"
39 #include "PHOS/AliPHOSPIDv1.h"
40
41  #endif
42 #endif
43
44 void ConvertPHOSToNewIO(const char* name = "galice.root", TString branch ="Event" )
45 {
46 //  AliLoader::SetDebug();
47   AliConfig* conf = AliConfig::Instance();
48   TClass* detclass = AliDetector::Class(); 
49   void * pbuf[100]; 
50   TBranch* branches[100];
51   Int_t nbranches = 0;
52 //  AliLoader::SetDebug(5); 
53   AliRunLoader* rl = AliRunLoader::Open("galiceNewIO.root",branch,"recreate");
54   rl->SetCompressionLevel(2);
55   rl->SetNumberOfEventsPerFile(1000);
56
57   AliRun* outAliRun;
58
59   if (gAlice == 0x0)
60    {
61      outAliRun = new AliRun("OutgAlice","Output gAlice");
62    }
63   else
64    {
65     outAliRun = gAlice;
66    } 
67   gAlice = 0x0;
68    
69   outAliRun->SetRunLoader(rl);
70   TString fSdig = "PHOS.SDigits."+branch+".root";
71   TString fDig = "PHOS.Digits."+branch+".root";
72   TString fRec = "PHOS.RecData."+branch+".root";
73
74   TFile* insdfile  = TFile::Open(fSdig);
75   TFile* indigfile = TFile::Open(fDig);
76   TFile* inrecfile = TFile::Open(fRec);
77   TFile* infile    = TFile::Open(name);
78
79   if (infile == 0x0)
80    {
81      ::Error("ConvertToNewIO.C","Can not open input file %s",name);
82      return;
83    }
84   
85   AliRun* inAliRun = (AliRun*)infile->Get("gAlice");
86   
87   if(inAliRun == 0x0)
88    {
89      ::Error("ConvertToNewIO.C","Can not find gAlice in input file");
90      return;
91    }
92   gAlice = inAliRun;
93   
94   TObjArray* modules = inAliRun->Modules();
95   if (modules == 0x0)
96    {
97      ::Error("ConvertToNewIO.C","Can not get array with modules from AliRun");
98      return;
99    }
100   TIter next(modules);
101   AliModule* module;
102   while ((module = (AliModule*)next()))  
103    {
104      outAliRun->AddModule(module);
105      
106      TClass* modclass = module->IsA();
107      AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
108      if (det) 
109       {
110         ////::Info("ConvertToNewIO.C"," Adding %s to RL.",det->GetName());
111         conf->Add(det,branch);
112         rl->AddLoader(det);
113       }
114    }
115
116   TParticle* particleBuffer = new TParticle();
117   /***************************************************/
118   /****          Event to Event         **************/
119   /***************************************************/
120
121   TTree* treeE = (TTree*)infile->Get("TE");
122
123   if (treeE == 0x0)
124    {
125      ::Error("ConvertToNewIO.C","Can not get TreeE from AliRun");
126      return;
127    }
128   rl->MakeTree("E");
129     
130   AliHeader* inheader = new AliHeader();
131   treeE->SetBranchAddress("Header",&inheader);
132   
133   Int_t nevents = (Int_t)treeE->GetEntries();
134   for (Int_t i = 0; i<nevents; i++)
135    {
136      ::Info("ConvertToNewIO.C","Converting Event %d.",i);
137      rl->SetEventNumber(i);
138      treeE->GetEvent(i);
139      /*****************************************/
140      /*            H E A D E R                */
141      /*****************************************/
142      
143      ////::Info("ConvertToNewIO.C","Copying Header");
144      AliHeader* outheader = rl->GetHeader();
145      
146      outheader->SetEvent(inheader->GetEvent());
147      outheader->SetNvertex(inheader->GetNvertex());
148      outheader->SetNprimary(inheader->GetNprimary());
149      outheader->SetNtrack(inheader->GetNtrack());
150      outheader->SetRun(inheader->GetRun());
151      outheader->SetEventNrInRun(inheader->GetEventNrInRun());
152      outheader->SetStack(inheader->Stack());
153      outheader->SetGenEventHeader(inheader->GenEventHeader());
154      rl->TreeE()->Fill();
155      rl->SetNumberOfEventsPerFile(1000);
156      /*****************************************/
157      /*       K I N E M A T I C S             */
158      /*****************************************/
159      ////::Info("ConvertToNewIO.C","Copying Kinematics.");
160      TString treeKname("TreeK");
161      treeKname+=i;
162      TTree* treeK = (TTree*)infile->Get(treeKname);
163      if (treeK)
164       { 
165        if (treeK->GetEntries() > 0)
166        {   
167         //I do this gimnastics to set directory correctly, without changing NewIO code 
168         //only for this purpose
169         rl->SetNumberOfEventsPerFile(1000);
170         rl->LoadKinematics("update");
171         rl->MakeTree("K");
172         rl->GetEventFolder()->Remove(rl->TreeK());
173         delete rl->TreeK();
174         
175         treeK->SetBranchAddress("Particles",&particleBuffer);
176         ////::Info("ConvertToNewIO.C","Cloning TreeK ...");
177         TTree* tk = treeK->CloneTree();
178         ////::Info("ConvertToNewIO.C","Cloning TreeK ... Done");
179         tk->SetName(AliRunLoader::fgkKineContainerName);
180         rl->GetEventFolder()->Add(tk);
181         rl->WriteKinematics("OVERWRITE");
182         rl->UnloadKinematics();
183        }
184        else
185         {
186           Info("ConvertToNewIO.C","Kinematics Tree is Empty");
187         }
188       }
189      else
190       {
191         Error("ConvertToNewIO.C","Could not find Kinematics tree named %s",treeKname.Data());
192       }
193      delete treeK;
194      /*****************************************/
195      /*   T R A C K   R E F E R E N C E S     */
196      /*****************************************/
197      ////::Info("ConvertToNewIO.C","Copying Track Refs.");
198      TString treeTRname("TreeTR");
199      treeTRname+=i;
200      TTree* treeTR = (TTree*)infile->Get(treeTRname);
201      if (treeTR)
202       { 
203        if (treeTR->GetEntries() > 0)
204        {   
205         next.Reset();
206         while ((module = (AliModule*)next()))  
207          {
208            TClass* modclass = module->IsA();
209            AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
210            if (det) 
211             {
212               TClonesArray* trbuffer = new TClonesArray("AliTrackReference", 100);
213               treeTR->SetBranchAddress(det->GetName(),&trbuffer);
214             }
215          }
216         
217         //I do this gimnastics to set directory correctly, without changing NewIO code 
218         //only for this purpose
219         rl->SetNumberOfEventsPerFile(1000);
220         rl->LoadTrackRefs("update");
221         rl->MakeTrackRefsContainer();
222         rl->GetEventFolder()->Remove(rl->TreeTR());
223         delete rl->TreeTR();
224         
225         ////::Info("ConvertToNewIO.C","Cloning TreeTR ...");
226         TTree* tr = treeTR->CloneTree();
227         ////::Info("ConvertToNewIO.C","Cloning TreeTR ... Done");
228         
229         tr->SetName(AliRunLoader::fgkTrackRefsContainerName);
230         rl->GetEventFolder()->Add(tr);
231         rl->WriteTrackRefs("OVERWRITE");   
232         rl->UnloadTrackRefs();
233        }
234        else
235         {
236           Info("ConvertToNewIO.C","Track References Tree is Empty");
237         }
238       }
239      else
240       {
241         Error("ConvertToNewIO.C","Could not find Track Refs tree named %s",treeTRname.Data());
242       }
243      delete treeTR;
244       
245      /*****************************************/
246      /*          H I T S                      */
247      /*****************************************/
248      ////::Info("ConvertToNewIO.C","Copying Hits.");
249      TString treeHname("TreeH");
250      treeHname+=i;
251      TTree* treeH = (TTree*)infile->Get(treeHname);
252      
253      if (treeH)
254       { 
255        if (treeH->GetEntries() > 0)
256         {   
257          TObjArray* lob = treeH->GetListOfBranches();
258          TObjArray* loaders = new TObjArray();
259          TIter nextnewmodule(outAliRun->Modules());
260         
261          while ((module = (AliModule*)nextnewmodule()))
262           {
263             TClass* modclass = module->IsA();
264             AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
265             //TClonesArray* ca = 0;
266             if (det) 
267              {
268                AliLoader* loader = det->GetLoader();
269                if (loader == 0x0)
270                 {
271                   ::Error("ConvertToNewIO.C","Can not find loader from %s.",det->GetName());
272                   continue;
273                 }
274
275                TString mask(det->GetName());
276                
277                loader->LoadHits("update");
278                loader->MakeTree("H");
279                loaders->Add(loader);
280                for(Int_t b=0; b<lob->GetEntries();b++)
281                 {
282                   TBranch* branch = (TBranch*)lob->At(b);
283                   TString bname(branch->GetName());//
284                   if ( bname.BeginsWith(det->GetName()) )
285                    {
286                      ////::Info("ConvertToNewIO.C","Found branch %s.",branch->GetName());
287                      ////::Info("ConvertToNewIO.C","Buffer Class Name %s.",branch->GetClassName());
288                      TString contname(branch->GetClassName());
289                   
290                      Int_t splitlvl = branch->GetSplitLevel();
291                     // if (splitlvl) splitlvl = 99;
292                  
293                      if( contname.CompareTo("TClonesArray") == 0)
294                       { 
295                         TBranchElement* belem = (TBranchElement*)branch;
296                         //::Info("ConvertToNewIO.C","Clones Class Name %s.",belem->GetClonesName());
297                     
298                         TClonesArray *ca = new TClonesArray(belem->GetClonesName());
299                         pbuf[nbranches] = ca;
300             
301                         branch->SetAddress(&(pbuf[nbranches]));
302                         //::Info("ConvertToNewIO.C","Buffer addrss %#x",pbuf[nbranches]);
303  
304                         //::Info("ConvertToNewIO.C","Creating branch for Clones SpliLvl = %d",splitlvl);
305                         branches[nbranches] = loader->TreeH()->Branch(branch->GetName(),&(pbuf[nbranches]),4000,splitlvl);
306                         nbranches++;
307                       }
308                      else
309                       {
310                         //::Info("ConvertToNewIO.C","Class Nmme is %s",branch->GetClassName());
311                         TClass* bcl = gROOT->GetClass(branch->GetClassName());
312                         if (bcl == 0x0)
313                          {
314                            ::Error("ConvertToNewIO.C","Can not get TClass object of class named %s",branch->GetClassName());
315                            continue;
316                          }
317                         pbuf[nbranches] = bcl->New();
318                         //::Info("ConvertToNewIO.C","Dumping buffer:");
319                         //((TObject*)pbuf[nbranches])->Dump();
320                         //::Info("ConvertToNewIO.C","Setting Adress:");
321                         branch->SetAddress(&(pbuf[nbranches]));
322                         //::Info("ConvertToNewIO.C","Creating branch SpliLvl = %d",splitlvl);
323                         branches[nbranches] =loader->TreeH()->Branch(branch->GetName(),branch->GetClassName(),&(pbuf[nbranches]),4000,splitlvl);
324                         nbranches++;
325                       }
326                    }
327                 }//loop over branches
328              }//if module is detector
329           }//while loop over modules
330          Int_t nentr = (Int_t)treeH->GetEntries();
331          //::Info("ConvertToNewIO.C","Copying Hits . Number of entries %d  ... ",nentr);
332                
333
334          Int_t nl = loaders->GetEntries();
335          for (Int_t e = 0; e < nentr; e++)
336            {
337              //printf("%d\n",e);
338              treeH->GetEntry(e);
339              
340              for (Int_t l = 0; l < nbranches; l++)
341               {
342                 //printf("Branch %d addr %#x\n",l,pbuf[l]);
343                 //printf("%s %#x \n", branches[l]->GetName(),pbuf[l]);
344                 branches[l]->SetAddress(&(pbuf[l]));    
345               }
346      
347              for (Int_t l = 0; l < nl; l++)
348               {
349                 AliLoader* loader = (AliLoader*)loaders->At(l);
350                 //printf("Filling %s\n",loader->GetName());
351                 loader->TreeH()->Fill();
352               }
353              #ifndef __MAKECINT__
354               #ifndef __CINT__
355                fflush(0);
356               #endif
357              #endif
358             }
359          ////printf("\n");
360                
361         ////::Info("ConvertToNewIO.C","Copying Hits ...  Done");
362         for (Int_t l = 0; l < nl; l++)
363          {
364            AliLoader* loader = (AliLoader*)loaders->At(l);
365            loader->WriteHits("OVERWRITE");
366            loader->UnloadHits();
367          }
368         delete loaders;
369         for (Int_t l = 0; l < nbranches; l++)
370          {
371            delete (TObject*)pbuf[l];
372          }
373          nbranches = 0;             
374        }
375        else //tree has any entries
376         {
377           Info("ConvertToNewIO.C","Hits Tree is Empty");
378         }
379       }
380      else //treeH
381       {
382        ::Warning("ConvertToNewIO.C","Could not get TreeH from in AliRun.");
383       }
384      delete treeH; 
385
386      /*****************************************/
387      /*          S  D i g i t s               */
388      /*****************************************/
389      //::Info("ConvertToNewIO.C","Copying S Digits.\n\n\n");
390      TString treeSname("TreeS");
391      treeSname+=i;
392      
393      TTree* treeS = (TTree*)insdfile->Get(treeSname);
394      if (treeS)
395        { 
396          TObjArray* lob = treeS->GetListOfBranches();
397          TObjArray* loaders = new TObjArray();
398          TIter nextnewmodule(outAliRun->Modules());
399          
400          while ((module = (AliModule*)nextnewmodule()))
401            {
402              TClass* modclass = module->IsA();
403              AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
404              //TClonesArray* ca = 0;
405              if (det) 
406                {
407                  AliLoader* loader = det->GetLoader();
408                  if (loader == 0x0)
409                    {
410                      ::Error("ConvertToNewIO.C","Can not find loader from %s.",det->GetName());
411                      continue;
412                    }
413                  
414                  TString mask(det->GetName());
415                  
416                  loader->LoadSDigits("update");
417                  loader->MakeTree("S");
418                  loaders->Add(loader);
419                  for(Int_t b=0; b<lob->GetEntries();b++)
420                    {
421                      TBranch* branch = (TBranch*)lob->At(b);
422                      
423                      TString bname(branch->GetName());//
424                      if ( bname.BeginsWith(det->GetName()) )
425                        {
426                          
427                          ////::Info("ConvertToNewIO.C","Found branch %s.",branch->GetName());
428                          ////::Info("ConvertToNewIO.C","Buffer Class Name %s.",branch->GetClassName());
429                          TString contname(branch->GetClassName());
430                          
431                          Int_t splitlvl = branch->GetSplitLevel();
432                          // if (splitlvl) splitlvl = 99;
433                          
434                          if ( contname.CompareTo("TClonesArray") == 0)
435                            {
436                              TBranchElement* belem = (TBranchElement*)branch;
437                              //::Info("ConvertToNewIO.C","Clones Class Name %s.",belem->GetClonesName());
438                              
439                              TClonesArray * ca = new TClonesArray(belem->GetClonesName());
440                              pbuf[nbranches] = ca;
441                              
442                              branch->SetAddress(&(pbuf[nbranches]));
443                              //::Info("ConvertToNewIO.C","Buffer addrss %#x",pbuf[nbranches]);
444                              
445                              //::Info("ConvertToNewIO.C","Creating branch for Clones SpliLvl = %d",splitlvl);
446                              branches[nbranches] = loader->TreeS()->Branch(branch->GetName(),&(pbuf[nbranches]),4000,splitlvl);
447                              nbranches++;
448                            }
449                          else
450                            {
451                              TClass* bcl = gROOT->GetClass(branch->GetClassName());
452                              pbuf[nbranches] = bcl->New();
453                              //::Info("ConvertToNewIO.C","Dumping buffer:");
454                              //((TObject*)pbuf[nbranches])->Dump();
455                              //::Info("ConvertToNewIO.C","Setting Adress:");
456                              branch->SetAddress(&(pbuf[nbranches]));
457                              //::Info("ConvertToNewIO.C","Creating branch SpliLvl = %d",splitlvl);
458                              branches[nbranches] =loader->TreeS()->Branch(branch->GetName(),branch->GetClassName(),&(pbuf[nbranches]),4000,splitlvl);
459                              nbranches++;
460                            }
461                        }
462                    }//loop over branches
463             }//if module is detector
464          }//while loop over modules
465         TBranch* bbb = treeS->GetBranch("PHOS");
466         Int_t nentr = (Int_t)bbb->GetEntries();
467         ////::Info("ConvertToNewIO.C","Copying SDigits. Number of entries in PHSO branch %d  ... ",nentr);
468              
469         Int_t nl = loaders->GetEntries();
470         for (Int_t e = 0; e < nentr; e++)
471           {
472             ////printf("%d\r",e);
473 //            treeS->GetEntry(e);
474             bbb->GetEntry(e);
475
476             for (Int_t l = 0; l < nbranches; l++)
477              {
478 //             //printf("%s %#x \n", branches[l]->GetName(),pbuf[l]);
479                branches[l]->SetAddress(&(pbuf[l]));    
480              }
481     
482             for (Int_t l = 0; l < nl; l++)
483              {
484                AliLoader* loader = (AliLoader*)loaders->At(l);
485 //               //printf("Filling %s\n",loader->GetName());
486                loader->TreeS()->Fill();
487              }
488             #ifndef __MAKECINT__
489              #ifndef __CINT__
490               fflush(0);
491              #endif
492             #endif
493            }
494         ////printf("\n");
495               
496         ////::Info("ConvertToNewIO.C","Copying SDigits ...  Done");
497         for (Int_t l = 0; l < nl; l++)
498          {
499            AliLoader* loader = (AliLoader*)loaders->At(l);
500            loader->WriteSDigits("OVERWRITE");
501            loader->UnloadSDigits();
502          }
503         delete loaders;
504         for (Int_t l = 0; l < nbranches; l++)
505          {
506            delete (TObject*)pbuf[l];
507          }
508         nbranches = 0;      
509
510        }
511      else //treeS
512       {
513         ::Warning("ConvertToNewIO.C","Could not get TreeS from in AliRun.");
514       }
515      delete treeS; 
516
517      /*****************************************/
518      /*          D i g i t s                  */
519      /*****************************************/
520      //::Info("ConvertToNewIO.C","Copying Digits.\n\n\n");
521      TString treeDname("TreeD");
522      treeDname+=i;
523      
524      TTree* treeD = (TTree*)indigfile->Get(treeDname);
525      if (treeD)
526        { 
527          TObjArray* lob = treeD->GetListOfBranches();
528          TObjArray* loaders = new TObjArray();
529          TIter nextnewmodule(outAliRun->Modules());
530          
531          while ((module = (AliModule*)nextnewmodule()))
532            {
533              TClass* modclass = module->IsA();
534              AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
535              //TClonesArray* ca = 0;
536              if (det) 
537                {
538                  AliLoader* loader = det->GetLoader();
539                  if (loader == 0x0)
540                {
541                  ::Error("ConvertToNewIO.C","Can not find loader from %s.",det->GetName());
542                  continue;
543                }
544                  
545                  TString mask(det->GetName());
546               
547                  loader->LoadDigits("update");
548                  loader->MakeTree("D");
549                  loaders->Add(loader);
550                  for(Int_t b=0; b<lob->GetEntries();b++)
551                    {
552                      TBranch* branch = (TBranch*)lob->At(b);
553                      
554                      TString bname(branch->GetName());//
555                  if ( bname.BeginsWith(det->GetName()) )
556                    {
557                      
558                      ////::Info("ConvertToNewIO.C","Found branch %s.",branch->GetName());
559                      ////::Info("ConvertToNewIO.C","Buffer Class Name %s.",branch->GetClassName());
560                      TString contname(branch->GetClassName());
561                      
562                      Int_t splitlvl = branch->GetSplitLevel();
563                      // if (splitlvl) splitlvl = 99;
564                  
565                      if ( contname.CompareTo("TClonesArray") == 0)
566                        {
567                          TBranchElement* belem = (TBranchElement*)branch;
568                          ////::Info("ConvertToNewIO.C","Clones Class Name %s.",belem->GetClonesName());
569                          
570                          TClonesArray * ca = new TClonesArray(belem->GetClonesName());
571                          pbuf[nbranches] = ca;
572                          
573                          branch->SetAddress(&(pbuf[nbranches]));
574                          ////::Info("ConvertToNewIO.C","Buffer addrss %#x",pbuf[nbranches]);
575                          
576                          ////::Info("ConvertToNewIO.C","Creating branch for Clones SpliLvl = %d",splitlvl);
577                          branches[nbranches] = loader->TreeD()->Branch(branch->GetName(),&(pbuf[nbranches]),4000,splitlvl);
578                          nbranches++;
579                        }
580                      else
581                        {
582                          TClass* bcl = gROOT->GetClass(branch->GetClassName());
583                          pbuf[nbranches] = bcl->New();
584                          ////::Info("ConvertToNewIO.C","Dumping buffer:");
585                          //((TObject*)pbuf[nbranches])->Dump();
586                          ////::Info("ConvertToNewIO.C","Setting Adress:");
587                          branch->SetAddress(&(pbuf[nbranches]));
588                          ////::Info("ConvertToNewIO.C","Creating branch SpliLvl = %d",splitlvl);
589                          branches[nbranches] =loader->TreeD()->Branch(branch->GetName(),branch->GetClassName(),&(pbuf[nbranches]),4000,splitlvl);
590                          nbranches++;
591                        }
592                    }
593                    }//loop over branches
594             }//if module is detector
595          }//while loop over modules
596          TBranch* bbb = treeD->GetBranch("PHOS");
597          Int_t nentr = (Int_t)bbb->GetEntries();
598          ////::Info("ConvertToNewIO.C","Copying Digits. Number of entries %d  ... ",nentr);
599               
600         Int_t nl = loaders->GetEntries();
601         for (Int_t e = 0; e < nentr; e++)
602           {
603             ////printf("%d\r",e);
604             //treeD->GetEntry(e);
605             bbb->GetEntry(e);
606
607             for (Int_t l = 0; l < nbranches; l++)
608              {
609 //             //printf("%s %#x \n", branches[l]->GetName(),pbuf[l]);
610                branches[l]->SetAddress(&(pbuf[l]));    
611              }
612     
613             for (Int_t l = 0; l < nl; l++)
614              {
615                AliLoader* loader = (AliLoader*)loaders->At(l);
616 //               //printf("Filling %s\n",loader->GetName());
617                loader->TreeD()->Fill();
618              }
619             #ifndef __MAKECINT__
620              #ifndef __CINT__
621               fflush(0);
622              #endif
623             #endif
624            }
625         ////printf("\n");
626               
627        ////::Info("ConvertToNewIO.C","Copying Digits ...  Done");
628        for (Int_t l = 0; l < nl; l++)
629         {
630           AliLoader* loader = (AliLoader*)loaders->At(l);
631           loader->WriteDigits("OVERWRITE");
632           loader->UnloadDigits();
633         }
634        delete loaders;
635        for (Int_t l = 0; l < nbranches; l++)
636          {
637            delete (TObject*)pbuf[l];
638          }
639       nbranches = 0;
640        }  
641      else //treeD
642        {
643          ::Warning("ConvertToNewIO.C","Could not get TreeD from in AliRun.");
644        }
645      delete treeD; 
646             
647
648      /*****************************************/
649      /*          R e c  P o i n t s           */
650      /*****************************************/
651      ////::Info("ConvertToNewIO.C","Copying RecPoints.");
652      TString treeRname("TreeR");
653      treeRname+=i;
654
655      TTree* treeR = (TTree*)inrecfile->Get(treeRname);
656      if (treeR)
657        {    
658          TObjArray* lob = treeR->GetListOfBranches();
659          TObjArray* loaders = new TObjArray();
660          TIter nextnewmodule(outAliRun->Modules());
661          
662          while ((module = (AliModule*)nextnewmodule()))
663            {
664            TClass* modclass = module->IsA();
665            AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
666           
667            if (det) 
668              {
669                AliLoader* loader = det->GetLoader();
670                if (loader == 0x0)
671                  {
672                    ::Error("ConvertToNewIO.C","Can not find loader from %s.",det->GetName());
673                    continue;
674                  }
675                
676                TString mask(det->GetName());
677                
678                loader->LoadRecPoints("update");
679                loader->MakeTree("R");
680                loaders->Add(loader);
681                for(Int_t b=0; b<lob->GetEntries();b++)
682                  {
683                    TBranch* branch = (TBranch*)lob->At(b);
684                    
685                    TString bname(branch->GetName());//
686                    if ( bname.Contains(det->GetName()) )
687                      {
688                        if(bname.Contains("Emc")||bname.Contains("Cpv"))
689                          {
690                            ////::Info("ConvertToNewIO.C","Found branch %s.",branch->GetName());
691                            ////::Info("ConvertToNewIO.C","Buffer Class Name %s.",branch->GetClassName());
692                            TString contname(branch->GetClassName());
693                            
694                            Int_t splitlvl = branch->GetSplitLevel();
695                            // if (splitlvl) splitlvl = 99;
696                            
697                            if ( contname.CompareTo("TClonesArray") == 0)
698                              {
699                                TBranchElement* belem = (TBranchElement*)branch;
700                                ////::Info("ConvertToNewIO.C","Clones Class Name %s.",belem->GetClonesName());
701                                
702                                TClonesArray * ca = new TClonesArray(belem->GetClonesName());
703                                pbuf[nbranches] = ca;
704                                
705                                branch->SetAddress(&(pbuf[nbranches]));
706                                ////::Info("ConvertToNewIO.C","Buffer addrss %#x",pbuf[nbranches]);
707                                
708                                ////::Info("ConvertToNewIO.C","Creating branch for Clones SpliLvl = %d",splitlvl);
709                                branches[nbranches] = loader->TreeR()->Branch(branch->GetName(),&(pbuf[nbranches]),4000,splitlvl);
710                                nbranches++;
711                              }
712                            else
713                              {
714                                TClass* bcl = gROOT->GetClass(branch->GetClassName());
715                                pbuf[nbranches] = bcl->New();
716                                ////::Info("ConvertToNewIO.C","Dumping buffer:");
717                                //((TObject*)pbuf[nbranches])->Dump();
718                                ////::Info("ConvertToNewIO.C","Setting Adress:");
719                                branch->SetAddress(&(pbuf[nbranches]));
720                                ////::Info("ConvertToNewIO.C","Creating branch SpliLvl = %d",splitlvl);
721                                branches[nbranches] =loader->TreeR()->Branch(branch->GetName(),branch->GetClassName(),&(pbuf[nbranches]),4000,splitlvl);
722                                nbranches++;
723                              }
724                          }
725                      }
726                  }//loop over branches
727              }//if module is detector
728            }//while loop over modules
729          TBranch* bbb = treeR->GetBranch("PHOSEmcRP");
730          Int_t nentr = (Int_t)bbb->GetEntries();
731          ////::Info("ConvertToNewIO.C","Copying RecPoints. Number of entries %d  ... ",nentr);
732   
733         
734          //Int_t nl = loaders->GetEntries();
735          //       //printf(">>>>>>>>>>>>>>>>>>>>%d\n",nl);
736          bbb->SetAddress(&(pbuf[0]));
737
738          for (Int_t e = 0; e < nentr; e++)
739            {
740              ////printf("%d\r",e);
741             
742              bbb->GetEntry(e);
743              AliLoader* loader = (AliLoader*)loaders->At(0);
744              
745              TBranch* bbb = treeR->GetBranch("PHOSCpvRP");
746              Int_t nentr = (Int_t)bbb->GetEntries();
747              ////::Info("ConvertToNewIO.C","Copying RecPoints. Number of entries %d  ... ",nentr);
748              
749              bbb->SetAddress(&(pbuf[1]));
750             
751              for (Int_t e = 0; e < nentr; e++)
752                {
753                  //              //printf("%d\r",e);
754                  bbb->GetEntry(e);
755                  //              AliLoader* loader = (AliLoader*)loaders->At(0);
756                }
757              ////printf("Filling %s\n",loader->GetName());
758              loader->TreeR()->Fill();
759
760              #ifndef __MAKECINT__
761               #ifndef __CINT__
762               fflush(0);
763              #endif
764             #endif
765            }
766          ////printf("\n");
767
768
769         ////::Info("ConvertToNewIO.C","Copying RecPoints ...  Done");
770          Int_t nl = loaders->GetEntries();
771          for (Int_t l = 0; l < nl; l++)
772          {
773
774            AliLoader* loader = (AliLoader*)loaders->At(l);
775            loader->WriteRecPoints("OVERWRITE");
776            loader->UnloadRecPoints();
777          }
778         delete loaders;
779         for (Int_t l = 0; l < nbranches; l++)
780          {
781            delete (TObject*)pbuf[l];//delete branches buffers 
782          }
783         nbranches = 0;      
784       
785       }
786      else //treeR
787       {
788         ::Warning("ConvertToNewIO.C","Could not get TreeR from in AliRun.");
789       }
790      delete treeR; 
791     
792      /*****************************************/
793      /*          Track Segments               */
794      /*****************************************/
795      ////::Info("ConvertToNewIO.C","Copying Tracks.");
796      TString treeTname("TreeR");
797      treeTname+=i;
798
799      TTree* treeT = (TTree*)inrecfile->Get(treeTname);
800      if (treeT)
801        {    
802          TObjArray* lob = treeT->GetListOfBranches();
803          TObjArray* loaders = new TObjArray();
804          TIter nextnewmodule(outAliRun->Modules());
805          
806          while ((module = (AliModule*)nextnewmodule()))
807            {
808            TClass* modclass = module->IsA();
809            AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
810            //TClonesArray* ca = 0;
811            if (det) 
812              {
813                AliLoader* loader = det->GetLoader();
814                if (loader == 0x0)
815                  {
816                    ::Error("ConvertToNewIO.C","Can not find loader from %s.",det->GetName());
817                    continue;
818                  }
819                
820                TString mask(det->GetName());
821                
822                loader->LoadTracks("update");
823                loader->MakeTree("T");
824                loaders->Add(loader);
825                for(Int_t b=0; b<lob->GetEntries();b++)
826                  {
827                    TBranch* branch = (TBranch*)lob->At(b);
828                    
829                    TString bname(branch->GetName());//
830                    if ( bname.Contains(det->GetName()) )
831                      {
832                        if(bname.Contains("TS"))
833                          {
834                            ////::Info("ConvertToNewIO.C","Found branch %s.",branch->GetName());
835                            ////::Info("ConvertToNewIO.C","Buffer Class Name %s.",branch->GetClassName());
836                            TString contname(branch->GetClassName());
837                            
838                            Int_t splitlvl = branch->GetSplitLevel();
839                            // if (splitlvl) splitlvl = 99;
840                            
841                            if ( contname.CompareTo("TClonesArray") == 0)
842                              {
843                                TBranchElement* belem = (TBranchElement*)branch;
844                                ////::Info("ConvertToNewIO.C","Clones Class Name %s.",belem->GetClonesName());
845                                
846                                TClonesArray * ca = new TClonesArray(belem->GetClonesName());
847                                pbuf[nbranches] = ca;
848                                
849                                branch->SetAddress(&(pbuf[nbranches]));
850                                ////::Info("ConvertToNewIO.C","Buffer addrss %#x",pbuf[nbranches]);
851                                
852                                ////::Info("ConvertToNewIO.C","Creating branch for Clones SpliLvl = %d",splitlvl);
853                                branches[nbranches] = loader->TreeT()->Branch(branch->GetName(),&(pbuf[nbranches]),4000,splitlvl);
854                                nbranches++;
855                              }
856                            else
857                              {
858                                TClass* bcl = gROOT->GetClass(branch->GetClassName());
859                                pbuf[nbranches] = bcl->New();
860                                ////::Info("ConvertToNewIO.C","Dumping buffer:");
861                                //((TObject*)pbuf[nbranches])->Dump();
862                                ////::Info("ConvertToNewIO.C","Setting Adress:");
863                                branch->SetAddress(&(pbuf[nbranches]));
864                                ////::Info("ConvertToNewIO.C","Creating branch SpliLvl = %d",splitlvl);
865                                branches[nbranches] =loader->TreeT()->Branch(branch->GetName(),branch->GetClassName(),&(pbuf[nbranches]),4000,splitlvl);
866                                nbranches++;
867                              }
868                          }
869                      }
870                  }//loop over branches
871              }//if module is detector
872            }//while loop over modules
873          TBranch* bbb = treeT->GetBranch("PHOSTS");
874          Int_t nentr = (Int_t)bbb->GetEntries();
875          ////::Info("ConvertToNewIO.C","Copying Tracks. Number of entries %d  ... ",nentr);
876               
877
878         Int_t nl = loaders->GetEntries();
879         for (Int_t e = 0; e < nentr; e++)
880           {
881             // //printf("%d\r",e);
882             //bbb->GetEntry(e);
883             //treeR->GetEntry(e);
884             
885             for (Int_t l = 0; l < nbranches; l++)
886              {
887                //            //printf("%s %#x \n", branches[l]->GetName(),pbuf[l]);
888                branches[l]->SetAddress(&(pbuf[l]));    
889              }
890             bbb->GetEntry(e);
891             for (Int_t l = 0; l < nl; l++)
892              {
893                AliLoader* loader = (AliLoader*)loaders->At(l);
894                ////printf("Filling %s\n",loader->GetName());
895               loader->TreeT()->Fill();
896              }
897             
898
899             #ifndef __MAKECINT__
900              #ifndef __CINT__
901               fflush(0);
902              #endif
903             #endif
904           }
905         ////printf("\n");
906         
907         ////::Info("ConvertToNewIO.C","Copying Tracks ...  Done");
908         for (Int_t l = 0; l < nl; l++)
909          {
910            AliLoader* loader = (AliLoader*)loaders->At(l);
911            loader->WriteTracks("OVERWRITE");
912            loader->UnloadTracks();
913          }
914         delete loaders;
915         for (Int_t l = 0; l < nbranches; l++)
916          {
917            delete (TObject*)pbuf[l];//delete branches buffers
918          }
919         nbranches = 0;      
920       
921       }
922      else //treeT
923       {
924         ::Warning("ConvertToNewIO.C","Could not get TreeR from in AliRun.");
925       }
926      delete treeT; 
927   
928     
929
930
931
932      /*****************************************/
933      /*          Rec Particles                */
934      /*****************************************/
935      ////::Info("ConvertToNewIO.C","Copying RecParticles.");
936      TString treePname("TreeR");
937      treePname+=i;
938
939      TTree* treeP = (TTree*)inrecfile->Get(treeTname);
940      if (treeP)
941        {    
942          TObjArray* lob = treeP->GetListOfBranches();
943          TObjArray* loaders = new TObjArray();
944          TIter nextnewmodule(outAliRun->Modules());
945          
946          while ((module = (AliModule*)nextnewmodule()))
947            {
948            TClass* modclass = module->IsA();
949            AliDetector *det = (AliDetector*)(modclass->DynamicCast(detclass,module));
950            //TClonesArray* ca = 0;
951            if (det) 
952              {
953                AliLoader* loader = det->GetLoader();
954                if (loader == 0x0)
955                  {
956                    ::Error("ConvertToNewIO.C","Can not find loader from %s.",det->GetName());
957                    continue;
958                  }
959                
960                TString mask(det->GetName());
961                
962                loader->LoadRecParticles("update");
963                loader->MakeTree("P");
964                loaders->Add(loader);
965                for(Int_t b=0; b<lob->GetEntries();b++)
966                  {
967                    TBranch* branch = (TBranch*)lob->At(b);
968                    
969                    TString bname(branch->GetName());//
970                    if ( bname.Contains(det->GetName()) )
971                      {
972                        if(bname.Contains("PHOSRP"))
973                          {
974                            ////::Info("ConvertToNewIO.C","Found branch %s.",branch->GetName());
975                            ////::Info("ConvertToNewIO.C","Buffer Class Name %s.",branch->GetClassName());
976                            TString contname(branch->GetClassName());
977                            
978                            Int_t splitlvl = branch->GetSplitLevel();
979                            // if (splitlvl) splitlvl = 99;
980                            
981                            if ( contname.CompareTo("TClonesArray") == 0)
982                              {
983                                TBranchElement* belem = (TBranchElement*)branch;
984                                ////::Info("ConvertToNewIO.C","Clones Class Name %s.",belem->GetClonesName());
985                                
986                                TClonesArray * ca = new TClonesArray(belem->GetClonesName());
987                                pbuf[nbranches] = ca;
988                                
989                                branch->SetAddress(&(pbuf[nbranches]));
990                                ////::Info("ConvertToNewIO.C","Buffer addrss %#x",pbuf[nbranches]);
991                                
992                                ////::Info("ConvertToNewIO.C","Creating branch for Clones SpliLvl = %d",splitlvl);
993                                branches[nbranches] = loader->TreeP()->Branch(branch->GetName(),&(pbuf[nbranches]),4000,splitlvl);
994                                nbranches++;
995                              }
996                            else
997                              {
998                                TClass* bcl = gROOT->GetClass(branch->GetClassName());
999                                pbuf[nbranches] = bcl->New();
1000                                ////::Info("ConvertToNewIO.C","Dumping buffer:");
1001                                //((TObject*)pbuf[nbranches])->Dump();
1002                                ////::Info("ConvertToNewIO.C","Setting Adress:");
1003                                branch->SetAddress(&(pbuf[nbranches]));
1004                                ////::Info("ConvertToNewIO.C","Creating branch SpliLvl = %d",splitlvl);
1005                                branches[nbranches] =loader->TreeP()->Branch(branch->GetName(),branch->GetClassName(),&(pbuf[nbranches]),4000,splitlvl);
1006                                nbranches++;
1007                              }
1008                          }
1009                      }
1010                  }//loop over branches
1011              }//if module is detector
1012            }//while loop over modules
1013          TBranch* bbb = treeP->GetBranch("PHOSRP");
1014          Int_t nentr = (Int_t)bbb->GetEntries();
1015          ////::Info("ConvertToNewIO.C","Copying RecParticles. Number of entries %d  ... ",nentr);
1016               
1017
1018         Int_t nl = loaders->GetEntries();
1019         for (Int_t e = 0; e < nentr; e++)
1020           {
1021             ////printf("%d\r",e);
1022             bbb->GetEntry(e);
1023             //treeR->GetEntry(e);
1024             
1025             for (Int_t l = 0; l < nbranches; l++)
1026              {
1027                //            //printf("%s %#x \n", branches[l]->GetName(),pbuf[l]);
1028                branches[l]->SetAddress(&(pbuf[l]));    
1029              }
1030     
1031             for (Int_t l = 0; l < nl; l++)
1032              {
1033                AliLoader* loader = (AliLoader*)loaders->At(l);
1034                ////printf("Filling %s\n",loader->GetName());
1035                loader->TreeP()->Fill();
1036              }
1037             
1038
1039             #ifndef __MAKECINT__
1040              #ifndef __CINT__
1041               fflush(0);
1042              #endif
1043             #endif
1044           }
1045         ////printf("\n");
1046         
1047         ////::Info("ConvertToNewIO.C","Copying RecParticles ...  Done");
1048         for (Int_t l = 0; l < nl; l++)
1049          {
1050            AliLoader* loader = (AliLoader*)loaders->At(l);
1051            loader->WriteRecParticles("OVERWRITE");
1052            loader->UnloadRecParticles();
1053          }
1054         delete loaders;
1055         for (Int_t l = 0; l < nbranches; l++)
1056          {
1057            delete (TObject*)pbuf[l];//delete branches buffers 
1058          }
1059         nbranches = 0;      
1060       
1061       }
1062      else //treeP
1063       {
1064         ::Warning("ConvertToNewIO.C","Could not get TreeR from in AliRun.");
1065       }
1066      delete treeP; 
1067
1068
1069
1070     }//end of loop over events
1071   /***************************************************/
1072   /****             Write Tasks          *************/
1073   /***************************************************/
1074   
1075   AliLoader * loader =  rl->GetLoader("PHOSLoader");
1076   /****             S Digits             *************/
1077   TTree * s = (TTree*)insdfile->Get("TreeS0");
1078   TBranch * bsd = s->GetBranch("AliPHOSSDigitizer");
1079   AliPHOSSDigitizer * sdig = 0;
1080   bsd->SetAddress(&sdig);
1081   bsd->GetEntry(0) ; 
1082   sdig->SetEventFolderName(sdig->GetName()) ; 
1083   sdig->SetName("PHOSSDigitizer");
1084   sdig->Print() ;  
1085   TFile fsd(loader->GetSDigitsFileName(), "update") ; 
1086   sdig->Write() ;
1087   fsd.Print() ; 
1088   fsd.Close() ; 
1089   delete s ;
1090   
1091   /****              Digits             *************/
1092   TTree * d = (TTree*)indigfile->Get("TreeD0");
1093   TBranch * bd = d->GetBranch("AliPHOSDigitizer");
1094   AliPHOSDigitizer * dig = 0 ;
1095   bd->SetAddress(&dig) ; 
1096   bd->GetEntry(0) ; 
1097   dig->SetEventFolderName(dig->GetName()) ; 
1098   dig->SetName("PHOSDigitizer");
1099   dig->Print() ;  
1100   //dig->Dump() ;  
1101   ////::Info("Digitizer","Print done");
1102   TFile fd(loader->GetDigitsFileName(), "update") ; 
1103   dig->Write() ;
1104   fd.Print() ; 
1105   fd.Close() ;
1106   delete d; 
1107  /****             Rec Data             *************/
1108   TTree * t = (TTree*)inrecfile->Get("TreeR0");
1109  /****             Clusterizer          *************/
1110   TBranch * bclu = t->GetBranch("AliPHOSClusterizer");
1111   AliPHOSClusterizerv1 * clu = 0;
1112   bclu->SetAddress(&clu);
1113   bclu->GetEntry(0) ;
1114   clu->SetEventFolderName(clu->GetName()) ; 
1115   clu->SetName("PHOSReconstructioner");
1116   clu->Print() ;  
1117   TFile fclu(loader->GetRecPointsFileName(), "update") ; 
1118   clu->Write() ;
1119   fclu.Print() ; 
1120   fclu.Close() ; 
1121   /****             TrackSegmentMaker      *************/
1122   TBranch * btr = t->GetBranch("AliPHOSTrackSegmentMaker");
1123   AliPHOSTrackSegmentMakerv1 * tra = 0 ;
1124   btr->SetAddress(&tra);
1125   btr->GetEntry(0) ;
1126   tra->SetEventFolderName(tra->GetName()) ; 
1127   tra->SetName("PHOSTracker");
1128   tra->Print() ;  
1129   TFile ftr(loader->GetTracksFileName(), "update") ; 
1130   tra->Write() ;
1131   ftr.Print() ; 
1132   ftr.Close() ; 
1133
1134   /****             PID          *************/
1135
1136   TBranch * bpid = t->GetBranch("AliPHOSPID");
1137   AliPHOSPIDv1 * pid = 0 ;
1138   bpid->SetAddress(&pid);
1139   bpid->GetEntry(0) ; 
1140   pid->SetEventFolderName(pid->GetName()) ; 
1141   pid->SetName("PHOSPIDTask");
1142   pid->Print() ;  
1143   TFile fpid(loader->GetRecParticlesFileName(), "update") ; 
1144   pid->Write() ;
1145   fpid.Print() ; 
1146   fpid.Close() ; 
1147   delete t;
1148
1149
1150
1151   /***************************************************/
1152   /****             Run to Run           *************/
1153   /***************************************************/
1154
1155   rl->WriteHeader("OVERWRITE");
1156
1157   infile->cd();
1158   TGeometry* geo = inAliRun->GetGeometry();
1159   rl->CdGAFile();
1160   geo->Write();
1161   
1162   rl->WriteAliRun();
1163   rl->WriteRunLoader();
1164
1165   ////::Info("ConvertToNewIO.C","Done");
1166   
1167 }
1168