2b285f844af9858d63df525bd0d0afff71bfc5af
[u/mrichter/AliRoot.git] / HLT / comp / AliL3Compress.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright &copy ASV
5
6 #include <stdio.h>
7 #include <stream.h>
8 #include <stdlib.h>
9
10 #include "AliL3Compress.h"
11 #include "AliL3TrackArray.h"
12 #include "AliL3ModelTrack.h"
13 #include "bitio.h"
14
15 ClassImp(AliL3Compress)
16
17 AliL3Compress::AliL3Compress()
18 {
19   fTracks=0;
20 }
21
22 AliL3Compress::~AliL3Compress()
23 {
24   if(fTracks)
25     delete fTracks;
26 }
27
28 void AliL3Compress::WriteFile(AliL3TrackArray *tracks,Char_t *filename)
29 {
30   FILE *file = fopen(filename,"w");
31   Short_t ntracks = tracks->GetNTracks();
32   //cout<<"Writing "<<ntracks<<" tracks to file"<<endl;
33     
34   Int_t count=0;
35   AliL3ClusterModel *clusters=0;
36   AliL3TrackModel *model=0;
37   for(Int_t i=0; i<ntracks; i++)
38     {
39       AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
40       if(!track) continue;
41       model = track->GetModel();
42       if(model->fNClusters==0) continue;
43       clusters = track->GetClusters();
44       //cout<<"Writing "<<(int)model->fNClusters<<" clusters"<<endl;
45       if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
46       //cout<<"Writing "<<(int)model->fNClusters<<" clusters to file"<<endl;
47       if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
48       count++;
49     }
50   cout<<"Wrote "<<count<<" tracks "<<endl;
51   fclose(file);
52 }
53
54 void AliL3Compress::ReadFile(Char_t *filename)
55 {
56   FILE *file = fopen(filename,"r");
57   
58   if(fTracks)
59     delete fTracks;
60   fTracks = new AliL3TrackArray("AliL3ModelTrack");
61   
62   while(!feof(file))
63     {
64       AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
65       track->Init(0,0);
66       AliL3TrackModel *model = track->GetModel();
67       AliL3ClusterModel *clusters = track->GetClusters();
68       if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
69       if(fread(clusters,(model->fNClusters)*sizeof(AliL3ClusterModel),1,file)!=1) break;
70     }
71   
72   cout<<"Read "<<fTracks->GetNTracks()<<" tracks from file"<<endl;
73   fclose(file);
74 }
75
76 void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
77 {
78   
79   BIT_FILE *output = OpenOutputBitFile(outfile);
80   FILE *input = fopen(infile,"r");
81
82   AliL3TrackModel track;
83   AliL3ClusterModel cluster;
84   Int_t temp;
85
86   while(!feof(input))
87     {
88       if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
89       
90       if(output->mask != 0x80) //Write the current byte to file.
91         {
92           if(putc(output->rack,output->file )!=output->rack)
93             cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
94           output->mask=0x80;
95         }
96       fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
97       for(Int_t i=0; i<track.fNClusters; i++)
98         {
99           if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
100           Int_t flag = (Int_t)cluster.fPresent;
101           OutputBit(output,flag);
102           if(!flag) continue;
103           temp = (Int_t)cluster.fDTime;
104           if(temp<0)
105             OutputBit(output,0);
106           else
107             OutputBit(output,1);
108           OutputBits(output,abs(temp),8);
109           temp = (Int_t)cluster.fDPad;
110           if(temp<0)
111             OutputBit(output,0);
112           else
113             OutputBit(output,1);
114           OutputBits(output,abs(temp),8);
115           temp = (Int_t)cluster.fDCharge;
116           OutputBits(output,temp,10);
117           
118           //Short_t temp=(Short_t)cluster.fDTime;
119           // cout<<"flag "<<(int)flag<<" dtime "<<(int)cluster.fDTime<<" dpad "<<(int)cluster.fDPad<<" charge "<<cluster.fDCharge<<endl;
120         }
121       
122     }
123   
124   fclose(input);
125   CloseOutputBitFile(output);
126 }
127
128 void AliL3Compress::ExpandFile(Char_t *infile,Char_t *outfile)
129 {
130   BIT_FILE *input = OpenInputBitFile(infile);
131   FILE *output = fopen(outfile,"w");
132
133   AliL3TrackModel trackmodel;
134   AliL3ClusterModel *clusters=0;
135   
136   while(!feof(input->file))
137     {
138       
139       if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
140       fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
141       input->mask=0x80;//make sure we read a new byte from file.
142       clusters = new AliL3ClusterModel[(trackmodel.fNClusters)];
143       for(Int_t i=0; i<trackmodel.fNClusters; i++)
144         {
145           Int_t temp,sign;
146           temp = InputBit(input);
147           if(!temp) 
148             {
149               clusters[i].fPresent=kFALSE;
150               continue;
151             }
152           clusters[i].fPresent=kTRUE;
153           sign=InputBit(input);
154           temp = InputBits(input,8);
155           if(!sign)
156             temp*=-1;
157           clusters[i].fDTime = temp;
158           sign=InputBit(input);
159           temp = InputBits(input,8);
160           if(!sign)
161             temp*=-1;
162           clusters[i].fDPad = temp;
163           temp=InputBits(input,10);
164           clusters[i].fDCharge = temp;
165         }
166       fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
167       delete [] clusters;
168     }
169   fclose(output);
170   CloseInputBitFile(input);
171 }