]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/oldmacros/AliITSMerge.C
macro to be updated for newIO
[u/mrichter/AliRoot.git] / ITS / oldmacros / AliITSMerge.C
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2
3 #include "iostream.h"
4 #include "TDatetime.h"
5 #include "STEER/AliRun.h"
6 #include "STEER/AliRunDigitizer.h"
7 #include "ITS/AliITSDigitizer.h"
8 #include "ITS/AliITS.h"
9 #include "ITS/AliITSDetType.h"
10 #include "ITS/AliITSresponseSDD.h"
11 #include "TStopwatch.h"
12
13 #endif
14
15 TFile* AccessFile(TString inFile="galice.root", TString acctype="R");
16 void writeAR(TFile * fin, TFile *fou);
17 Int_t ChangeITSDefaults(TFile *hitfile,AliITS *ITS,TString opt="");
18 //#define DEBUG
19 Int_t AliITSMerge(TString digFile="galiceMD.root", 
20             TString sdigFileSig="galiceS_sig.root", 
21             TString sdigFileBg="",TString opt=""){
22     // Standeard ITS SDigits to Digits, with posible merging.
23
24     // Dynamically link some shared libs
25     if (gClassTable->GetID("AliRun") < 0) {
26         gROOT->LoadMacro("loadlibs.C");
27         loadlibs();
28     } // end if
29     // Connect the Root Galice file containing Geometry, Kine and Hits
30     if (gAlice) {delete gAlice; gAlice = 0;}
31
32     TFile *sdigfilesig = 0;   // pointer to signal input file.
33     TFile *sdigfilebg  = 0;   // pointer to background input file.
34     TFile *digfile = 0;  // possible output file for TreeD
35
36     // Setup to copy gAlice and the event tree to the output file.
37
38     if(digFile.CompareTo(sdigFileSig)==0){//write output to same file as input.
39         sdigfilesig = AccessFile(sdigFileSig,"U");//input file open for update.
40     }else{ // different output file then input file.
41         sdigfilesig = AccessFile(sdigFileSig,"R");//input file open read only
42         digfile = new TFile(digFile,"NEW");
43     } // end if digFile == hitFile.
44
45     AliITS *ITS = (AliITS*)gAlice->GetDetector("ITS");      
46     if (!ITS) {
47         cerr<<"AliITSHits2DigitsDefault.C : AliITS object not found on file"
48             << endl;
49         return 3;
50     }  // end if !ITS
51
52     ChangeITSDefaults(sdigfilesig,ITS,opt);
53     // write the AliRun object to the output file if different from input file.
54     if(digfile){
55         writeAR(sdigfilesig,digfile);
56         digfile->Close(); // Manager will open in update mode.
57         digfile = 0;
58     } // end if digfile
59     sdigfilesig->Close();
60     sdigfilesig = 0;
61 //    delete gAlice; gAlice=0;   // there is a problem with deleting gAlice????
62
63     AliRunDigitizer *manager;
64     if(sdigFileBg.CompareTo("")==0) { // do not merge.
65         manager = new AliRunDigitizer(1,1);
66     }else{
67         manager = new AliRunDigitizer(2,1);
68         manager->SetInputStream(0,sdigFileSig.Data());
69         manager->SetInputStream(1,sdigFileBg.Data());
70     } // end if
71     if (digFile.CompareTo(sdigFileSig) !=0) {
72         manager->SetOutputFile(digFile);
73     } // end if
74 //    manager->SetCopyTreesFromInput(0);
75     AliITSDigitizer *dITS = new AliITSDigitizer(manager);
76     if(opt.Contains("ROI")){
77         cout << "Region of Interest selected" << endl;
78         dITS->SetByRegionOfInterestFlag(1);
79     }else{
80         cout << "Digizing everthing" << endl;
81         dITS->SetByRegionOfInterestFlag(0);
82     } // end if
83
84     TStopwatch timer;
85     timer.Start();
86     manager->Exec("all");
87     timer.Stop(); 
88     timer.Print();
89
90     if(digfile!=0){
91         cout << digFile << " size =" << digfile->GetSize() << endl;
92     }else if(sdigfilesig!=0){
93         cout << sdigFileSig << " size =" << sdigfilesig->GetSize() << endl;
94     } // end if sdigfile!=0
95
96
97     if(digfile) digfile->Close();
98     if(sdigfilesig) sdigfilesig->Close();
99     if(sdigfilebg)  sdigfilebg->Close();
100
101     // There is a problem deleting gAlice. It is related to the destructor
102     // and the fTreeE in AliRun. So for now it is not deleted.
103 //    delete gAlice; // digfile is closed by deleting gAlice if != hitfile.
104 //    gAlice = 0;
105     delete manager;
106 }
107 //______________________________________________________________________
108 TFile * AccessFile(TString FileName, TString acctype){
109     // Function used to open the input file and fetch the AliRun object
110
111     TFile *retfil = 0;
112     TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(FileName);
113     if(file) {
114         file->Close();
115         delete file;
116         file = 0;
117     } // end if file
118     if(acctype.Contains("U")){
119         file = new TFile(FileName,"UPDATE");
120     } // end if open for update
121     if(acctype.Contains("N") && !file){
122         file = new TFile(FileName,"RECREATE");
123     } // end if open a new file
124     if(!file) file = new TFile(FileName,"READ");   // default readonly
125     if (!file->IsOpen()) {
126         cerr << "Can't open " << FileName << " !" << endl;
127         return retfil;
128     } // end if error opeing file
129
130     // Get AliRun object from file or return if not on file
131     if (gAlice) {delete gAlice; gAlice = 0;}
132     gAlice = (AliRun*)file->Get("gAlice");
133     if (!gAlice) {
134         cerr << "AliRun object not found on file "<< FileName << "!" << endl;
135         file->Close();  // close file and return error.
136         return retfil;
137     } // end if !gAlice
138     return file;
139 }
140 //______________________________________________________________________
141 void writeAR(TFile * fin, TFile *fou) {
142     TDirectory *current = gDirectory;
143     TTree *TeOld;
144     TTree *TeNew;
145     AliHeader *alhe = new AliHeader();
146     TeOld = (TTree*)fin->Get("TE");
147     TeOld->SetBranchAddress("Header",&alhe);
148     TeOld->SetBranchStatus("*",1);
149     fou->cd();
150     TeNew = TeOld->CloneTree();
151     TeNew->Write(0,TObject::kOverwrite);
152     gAlice->Write(0,TObject::kOverwrite);
153     current->cd();
154     delete alhe;
155 #ifdef DEBUG
156     cout << "AliRun object written to file" << endl;
157 #endif
158 }
159 //______________________________________________________________________
160 Int_t ChangeITSDefaults(TFile *hitfile,AliITS *ITS,TString opt){
161
162     TDatime *ct0 = new TDatime(2002,04,26,00,00,00);
163     TDatime ct = hitfile->GetCreationDate();
164
165     if(ct0->GetDate()>ct.GetDate()){
166         // For old files, must change SDD noise.
167         AliITSresponseSDD *resp1 = (AliITSresponseSDD*)ITS->DetType(1)->
168             GetResponseModel();
169         resp1 = new AliITSresponseSDD();
170         ITS->SetResponseModel(1,resp1);
171         cout << "Changed response class for SDD:" << endl;
172         resp1->Print();
173     } // end if
174
175     if(opt.Contains("Dubna")){
176         AliITSresponseSPDdubna *resp0 = new AliITSresponseSPDdubna();
177         if(ITS->DetType(0)->GetResponseModel() !=0){
178             delete ((AliITSresponse*)ITS->DetType(0)->GetResponseModel());
179             ITS->DetType(0)->ResponseModel(0);
180         } // end if
181         ITS->DetType(0)->ResponseModel(resp0);
182         AliITSsegmentationSPD *seg0 = (AliITSsegmentationSPD*)ITS->
183             DetType(0)->GetSegmentationModel();
184         AliITSsimulationSPDdubna *sim0 = new AliITSsimulationSPDdubna(seg0,
185                                                                       resp0);
186         if(ITS->DetType(0)->GetSimulationModel() !=0){
187             delete ((AliITSsimulation*)ITS->DetType(0)->GetSimulationModel());
188             ITS->DetType(0)->SimulationModel(0);
189         } // end if
190         ITS->DetType(0)->SimulationModel(sim0);
191     } // end if Dubna
192 }