]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/addMiniHeader.cxx
create a new AliEn directory per day (like for the physical files in CASTOR),
[u/mrichter/AliRoot.git] / RAW / addMiniHeader.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // this program adds a mini header to a binary file                          //
21 //                                                                           //
22 // type "addMiniHeader -?" to get a description of the arguments             //
23 //                                                                           //
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <TError.h>
29 #include <TSystem.h>
30 #include "AliMiniHeader.h"
31
32
33 //_____________________________________________________________________________
34 int main(int argc, char** argv)
35 {
36   if ((argc < 2) || (argc > 4) || (strcmp(argv[1], "-?") == 0) || 
37       (strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0)) {
38     printf("\nUsage: addMiniHeader fileName [detectorID [DDLID]]\n\n");
39     printf("         fileName   : name of the raw data file\n");
40     printf("         detectorID : number of the detector (default 0xFF)\n");
41     printf("         DDLID      : number of the DDL (default 0xFFFF)\n\n");
42     return 0;
43   }
44
45   // prepare the mini header
46   AliMiniHeader header;
47   header.fMagicWord[2] = 0x12;
48   header.fMagicWord[1] = 0x34;
49   header.fMagicWord[0] = 0x56;
50   header.fDetectorID = 0xFF;
51   header.fDDLID = 0xFFFF;
52   header.fCompressionFlag = 0;
53   header.fVersion = 1;
54
55   // open the raw data file
56   FILE* file = fopen(argv[1], "rb");
57   if (!file) {
58     ::Fatal("addMiniHeader", "could not open file %s", argv[1]);
59   }
60
61   // set the correct raw data size in the mini header
62   fseek(file, 0, SEEK_END);
63   header.fSize = ftell(file);
64   fseek(file, 0, SEEK_SET);
65
66   // set the detector and DDL id if they are given
67   if (argc > 2) {
68     header.fDetectorID = atoi(argv[2]);
69   }
70   if (argc > 3) {
71     header.fDDLID = atoi(argv[3]);
72   }
73
74   // open a temporary file and write the mini header to it
75   char tmpFileName[] = "tmpXXXXXX";
76   Int_t tmpID = mkstemp(tmpFileName);
77   if (tmpID < 0) {
78     ::Fatal("addMiniHeader", "could not open temporary file");
79   }
80   FILE* tmp = fdopen(tmpID, "w");
81   fwrite(&header, sizeof(header), 1, tmp);
82
83   // copy the raw data to the temporary file
84   char buffer[256];
85   while (Int_t size = fread(buffer, 1, 256, file)) {
86     fwrite(buffer, size, 1, tmp);
87   }
88   fclose(file);
89   fclose(tmp);
90
91   // replace the original file by the temporary one
92   gSystem->Rename(tmpFileName, argv[1]);
93
94   return 0;
95 }