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